This repository has been archived on 2024-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
core-old/internal/bundb/village_test.go
Dawid Wysokiński 3b4f42e344
All checks were successful
continuous-integration/drone/push Build is passing
feat: update data - integration tests (#186)
Reviewed-on: twhelp/core#186
2023-02-16 06:37:48 +00:00

328 lines
8.0 KiB
Go

package bundb_test
import (
"context"
"testing"
"time"
"gitea.dwysokinski.me/twhelp/core/internal/bundb"
"gitea.dwysokinski.me/twhelp/core/internal/bundb/bundbtest"
"gitea.dwysokinski.me/twhelp/core/internal/domain"
"github.com/jackc/pgerrcode"
"github.com/stretchr/testify/assert"
"github.com/uptrace/bun/driver/pgdriver"
)
func TestVillage_CreateOrUpdate(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping long-running test")
}
db := bundbtest.NewDB(t)
fixture := loadFixtures(t, db)
repo := bundb.NewVillage(db)
t.Run("OK", func(t *testing.T) {
t.Parallel()
playerGSus := fixture.Player(t, "de188-g-sus")
playerRIP := fixture.Player(t, "de188-rest-in-peace")
serverKey := playerGSus.ServerKey
createParams := []domain.CreateVillageParams{
{
ID: 999,
Name: "name 999",
Points: 4,
X: 500,
Y: 500,
Continent: "K55",
Bonus: 0,
PlayerID: playerGSus.ID,
ProfileURL: "profile-999",
ServerKey: serverKey,
},
{
ID: 1000,
Name: "name 1000",
Points: 5,
X: 501,
Y: 500,
Continent: "K55",
Bonus: 2,
PlayerID: 0,
ProfileURL: "profile-1000",
ServerKey: serverKey,
},
}
updateParams := make([]domain.CreateVillageParams, 0, len(createParams))
ids := make([]int64, 0, len(createParams))
for i, p := range createParams {
var playerID int64
if i%2 == 0 {
playerID = playerRIP.ID
}
updateParams = append(updateParams, domain.CreateVillageParams{
ID: p.ID,
Name: p.Name + "Update",
Points: p.Points + 1,
X: p.X + 1,
Y: p.Y + 1,
Continent: p.Continent + "1",
Bonus: p.Bonus + 1,
PlayerID: playerID,
ProfileURL: p.ProfileURL + "Update",
ServerKey: p.ServerKey,
})
ids = append(ids, p.ID)
}
listParams := domain.ListVillagesParams{
IDs: ids,
ServerKeys: []string{serverKey},
}
ctx := context.Background()
assert.NoError(t, repo.CreateOrUpdate(ctx, createParams...))
createdVillages, err := repo.List(ctx, listParams)
assert.NoError(t, err)
assertCreatedVillages(t, createParams, createdVillages)
assert.NoError(t, repo.CreateOrUpdate(ctx, updateParams...))
updatedVillages, err := repo.List(ctx, listParams)
assert.NoError(t, err)
assertCreatedVillages(t, updateParams, updatedVillages)
})
t.Run("OK: len(params) == 0", func(t *testing.T) {
t.Parallel()
assert.NoError(t, repo.CreateOrUpdate(context.Background()))
})
t.Run("ERR: server must exist", func(t *testing.T) {
t.Parallel()
err := repo.CreateOrUpdate(context.Background(), domain.CreateVillageParams{
ID: 999,
Name: "name 999",
Points: 4,
X: 500,
Y: 500,
Continent: "K55",
Bonus: 0,
PlayerID: 0,
ServerKey: "pl999",
})
var pgErr pgdriver.Error
assert.ErrorAs(t, err, &pgErr)
assert.Equal(t, pgerrcode.ForeignKeyViolation, pgErr.Field('C'))
assert.Equal(t, "villages_server_key_fkey", pgErr.Field('n'))
})
}
func TestVillage_List_ListCountWithRelations(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping long-running test")
}
db := bundbtest.NewDB(t)
fixture := loadFixtures(t, db)
repo := bundb.NewVillage(db)
villages := fixture.Villages(t)
type expectedVillage struct {
id int64
serverKey string
}
allVillages := make([]expectedVillage, 0, len(villages))
for _, p := range villages {
allVillages = append(allVillages, expectedVillage{
id: p.ID,
serverKey: p.ServerKey,
})
}
//nolint:prealloc
var villagesIT70 []expectedVillage
for _, p := range villages {
if p.ServerKey != "it70" {
continue
}
villagesIT70 = append(villagesIT70, expectedVillage{
id: p.ID,
serverKey: p.ServerKey,
})
}
tests := []struct {
name string
params domain.ListVillagesParams
expectedVillages []expectedVillage
expectedCount int64
}{
{
name: "Empty struct",
params: domain.ListVillagesParams{},
expectedVillages: allVillages,
expectedCount: int64(len(allVillages)),
},
{
name: "ServerKeys=[it70]",
params: domain.ListVillagesParams{ServerKeys: []string{"it70"}},
expectedVillages: villagesIT70,
expectedCount: int64(len(villagesIT70)),
},
{
name: "IDs=[10022, 1114]",
params: domain.ListVillagesParams{IDs: []int64{10022, 1114}},
expectedVillages: []expectedVillage{
{
id: 10022,
serverKey: "it70",
},
{
id: 1114,
serverKey: "pl169",
},
},
expectedCount: 2,
},
{
name: "IDs=[1113, 1114],ServerKeys=[pl169]",
params: domain.ListVillagesParams{IDs: []int64{1113, 1114}, ServerKeys: []string{"pl169"}},
expectedVillages: []expectedVillage{
{
id: 1113,
serverKey: "pl169",
},
{
id: 1114,
serverKey: "pl169",
},
},
expectedCount: 2,
},
{
name: "IDs=[10022, 10023, 10024],ServerKeys=[it70],Limit=1",
params: domain.ListVillagesParams{
IDs: []int64{10022, 10023, 10024},
ServerKeys: []string{"it70"},
Pagination: domain.Pagination{
Limit: 1,
},
},
expectedVillages: []expectedVillage{
{
id: 10022,
serverKey: "it70",
},
},
expectedCount: 3,
},
{
name: "IDs=[10022, 10023, 10024],ServerKeys=[it70],Limit=100,Offset=1",
params: domain.ListVillagesParams{
IDs: []int64{10022, 10023, 10024},
ServerKeys: []string{"it70"},
Pagination: domain.Pagination{
Limit: 100,
Offset: 1,
},
},
expectedVillages: []expectedVillage{
{
id: 10023,
serverKey: "it70",
},
{
id: 10024,
serverKey: "it70",
},
},
expectedCount: 3,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
resListCountWithRelations, count, err := repo.ListCountWithRelations(context.Background(), tt.params)
assert.NoError(t, err)
assert.Equal(t, tt.expectedCount, count)
assert.Len(t, resListCountWithRelations, len(tt.expectedVillages))
for _, expVillage := range tt.expectedVillages {
found := false
for _, village := range resListCountWithRelations {
if village.ID != expVillage.id || village.ServerKey != expVillage.serverKey {
continue
}
if village.PlayerID != 0 && (!village.Player.Valid || village.Player.Player.ID != village.PlayerID) {
continue
}
if village.PlayerID == 0 && village.Player.Valid {
continue
}
if village.Player.Player.Tribe.Valid && village.Player.Player.Tribe.Tribe.ID == 0 {
continue
}
if !village.Player.Player.Tribe.Valid && village.Player.Player.Tribe.Tribe.ID != 0 {
continue
}
found = true
break
}
assert.True(t, found, "village (id=%d,serverkey=%s) not found", expVillage.id, expVillage.serverKey)
}
resList, err := repo.List(context.Background(), tt.params)
assert.NoError(t, err)
assert.Len(t, resList, len(resListCountWithRelations))
for i, village := range resList {
assert.Equal(t, resListCountWithRelations[i].Village, village)
}
})
}
}
func assertCreatedVillages(tb testing.TB, params []domain.CreateVillageParams, villages []domain.Village) {
tb.Helper()
assert.Len(tb, villages, len(params))
for _, p := range params {
var village domain.Village
for _, v := range villages {
if v.ID == p.ID && v.ServerKey == p.ServerKey {
village = v
break
}
}
assert.Equal(tb, p.ID, village.ID)
assert.Equal(tb, p.Name, village.Name)
assert.Equal(tb, p.Points, village.Points)
assert.Equal(tb, p.X, village.X)
assert.Equal(tb, p.Y, village.Y)
assert.Equal(tb, p.Continent, village.Continent)
assert.Equal(tb, p.Bonus, village.Bonus)
assert.Equal(tb, p.PlayerID, village.PlayerID)
assert.Equal(tb, p.ProfileURL, village.ProfileURL)
assert.Equal(tb, p.ServerKey, village.ServerKey)
assert.WithinDuration(tb, village.CreatedAt, time.Now(), 1*time.Second)
}
}