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/server_test.go
Dawid Wysokiński efb84b4611
All checks were successful
continuous-integration/drone/push Build is passing
refactor: bundb tests
2022-12-25 12:32:33 +01:00

467 lines
11 KiB
Go

package bundb_test
import (
"context"
"testing"
"time"
"github.com/jackc/pgerrcode"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/stretchr/testify/assert"
"gitea.dwysokinski.me/twhelp/core/internal/bundb"
"gitea.dwysokinski.me/twhelp/core/internal/domain"
)
func TestServer_CreateOrUpdate(t *testing.T) {
t.Parallel()
repo := bundb.NewServer(newDB(t))
t.Run("OK", func(t *testing.T) {
t.Parallel()
createParams := []domain.CreateServerParams{
{
Key: "pl151",
URL: "https://pl151.plemiona.pl",
Open: false,
VersionCode: "pl",
},
{
Key: "pl152",
URL: "https://pl152.plemiona.pl",
Open: true,
VersionCode: "pl",
},
}
updateParams := make([]domain.CreateServerParams, 0, len(createParams))
for _, p := range createParams {
updateParams = append(updateParams, domain.CreateServerParams{
Key: p.Key,
URL: p.URL + "update",
Open: !p.Open,
VersionCode: p.VersionCode,
})
}
createdServers, err := repo.CreateOrUpdate(context.Background(), createParams...)
assert.NoError(t, err)
assertCreatedServers(t, createParams, createdServers)
updatedServers, err := repo.CreateOrUpdate(context.Background(), updateParams...)
assert.NoError(t, err)
assertCreatedServers(t, updateParams, updatedServers)
})
t.Run("OK: len(params) == 0", func(t *testing.T) {
t.Parallel()
servers, err := repo.CreateOrUpdate(context.Background())
assert.NoError(t, err)
assert.Zero(t, servers)
})
t.Run("ERR: version must exist in db", func(t *testing.T) {
t.Parallel()
servers, err := repo.CreateOrUpdate(context.Background(), domain.CreateServerParams{
Key: "pl163",
URL: "https://pl163.plemiona.pl",
Open: false,
VersionCode: "any",
})
var pgErr pgdriver.Error
assert.ErrorAs(t, err, &pgErr)
assert.Equal(t, pgerrcode.ForeignKeyViolation, pgErr.Field('C'))
assert.Equal(t, "servers_version_code_fkey", pgErr.Field('n'))
assert.Zero(t, servers)
})
}
func TestServer_List_ListCount(t *testing.T) {
t.Parallel()
specialServers := []string{
"pls1",
"ens1",
"des1",
"uks1",
"its1",
"frs1",
"uss1",
"nls1",
"ess1",
"ros1",
"rus1",
"grs1",
"trs1",
"css1",
"chs1",
"pts1",
"brs1",
"hus1",
"sks1",
"master",
}
normalOpenServers := []string{
"de188",
"it70",
"pl169",
}
normalClosedServers := []string{
"en113",
}
normalServers := concatStringSlices(normalOpenServers, normalClosedServers)
servers := concatStringSlices(specialServers, normalOpenServers, normalClosedServers)
db := newDB(t)
_ = loadFixtures(t, db)
repo := bundb.NewServer(db)
tests := []struct {
name string
params domain.ListServersParams
expectedServers []string
expectedCount int64
}{
{
name: "Empty struct",
params: domain.ListServersParams{},
expectedServers: servers,
expectedCount: int64(len(servers)),
},
{
name: "Special=true",
params: domain.ListServersParams{
Special: domain.NullBool{
Bool: true,
Valid: true,
},
},
expectedServers: specialServers,
expectedCount: int64(len(specialServers)),
},
{
name: "Special=false",
params: domain.ListServersParams{
Special: domain.NullBool{
Bool: false,
Valid: true,
},
},
expectedServers: normalServers,
expectedCount: int64(len(normalServers)),
},
{
name: "Open=true",
params: domain.ListServersParams{
Open: domain.NullBool{
Bool: true,
Valid: true,
},
},
expectedServers: normalOpenServers,
expectedCount: int64(len(normalOpenServers)),
},
{
name: "Open=false,Special=false",
params: domain.ListServersParams{
Open: domain.NullBool{
Bool: false,
Valid: true,
},
Special: domain.NullBool{
Bool: false,
Valid: true,
},
},
expectedServers: normalClosedServers,
expectedCount: int64(len(normalClosedServers)),
},
{
name: "VersionCodes=[pl,en]",
params: domain.ListServersParams{
VersionCodes: []string{"pl", "en"},
},
expectedServers: []string{"pls1", "pl169", "ens1", "en113"},
expectedCount: 4,
},
{
name: "VersionCodes=[pl,en],Limit=1",
params: domain.ListServersParams{
VersionCodes: []string{"pl", "en"},
Pagination: domain.Pagination{
Limit: 1,
},
},
expectedServers: []string{"en113"},
expectedCount: 4,
},
{
name: "VersionCodes=[pl,en],Offset=2",
params: domain.ListServersParams{
VersionCodes: []string{"pl", "en"},
Pagination: domain.Pagination{
Offset: 2,
},
},
expectedServers: []string{"pls1", "pl169"},
expectedCount: 4,
},
{
name: "VersionCodes=[pl,en],Limit=1,Offset=2",
params: domain.ListServersParams{
VersionCodes: []string{"pl", "en"},
Pagination: domain.Pagination{
Offset: 2,
Limit: 1,
},
},
expectedServers: []string{"pl169"},
expectedCount: 4,
},
{
name: "VersionCodes=[pl,en],Special=false",
params: domain.ListServersParams{
VersionCodes: []string{"pl", "en"},
Special: domain.NullBool{
Bool: false,
Valid: true,
},
},
expectedServers: []string{"pl169", "en113"},
expectedCount: 2,
},
{
name: "Keys=[pl169,en113,ens1],Special=false",
params: domain.ListServersParams{
Keys: []string{"pl169", "en113", "ens1"},
Special: domain.NullBool{
Bool: false,
Valid: true,
},
},
expectedServers: []string{"pl169", "en113"},
expectedCount: 2,
},
{
name: "PlayerSnapshotsCreatedAtLT=2022-03-19T12:00:54.000Z,Special=false",
params: domain.ListServersParams{
PlayerSnapshotsCreatedAtLT: time.Date(
2022,
time.March,
19,
12,
0,
54,
0,
time.UTC,
),
Special: domain.NullBool{
Bool: false,
Valid: true,
},
},
expectedServers: []string{"it70", "en113"},
expectedCount: 2,
},
{
name: "TribeSnapshotsCreatedAtLT=2022-03-19T12:00:54.000Z,Open=true,Special=false",
params: domain.ListServersParams{
TribeSnapshotsCreatedAtLT: time.Date(
2022,
time.March,
19,
12,
0,
54,
0,
time.UTC,
),
Open: domain.NullBool{
Bool: true,
Valid: true,
},
Special: domain.NullBool{
Bool: false,
Valid: true,
},
},
expectedServers: []string{"it70"},
expectedCount: 1,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
resListCount, count, err := repo.ListCount(context.Background(), tt.params)
assert.NoError(t, err)
assert.Equal(t, tt.expectedCount, count)
assert.Len(t, resListCount, len(tt.expectedServers))
for _, key := range tt.expectedServers {
found := false
for _, srv := range resListCount {
if srv.Key == key {
found = true
break
}
}
assert.True(t, found, "server (key=%s) not found", key)
}
resList, err := repo.List(context.Background(), tt.params)
assert.NoError(t, err)
assert.Equal(t, resListCount, resList)
})
}
}
func TestServer_Update(t *testing.T) {
t.Parallel()
db := newDB(t)
repo := bundb.NewServer(db)
fixture := loadFixtures(t, db)
server := fixture.server(t, "de188")
t.Run("OK", func(t *testing.T) {
t.Parallel()
params := domain.UpdateServerParams{
Config: domain.NullServerConfig{
Valid: true,
Config: domain.ServerConfig{
Speed: 0.5,
},
},
BuildingInfo: domain.NullBuildingInfo{
Valid: true,
Info: domain.BuildingInfo{
Garage: domain.Building{
BuildTime: 0.2333,
Pop: 1234,
},
Farm: domain.Building{
MaxLevel: 1,
MinLevel: 30,
Wood: 122,
Stone: 222,
Iron: 322,
Pop: 11,
WoodFactor: 1.07,
StoneFactor: 1.09,
IronFactor: 1.12,
PopFactor: 1.15,
BuildTime: 255,
BuildTimeFactor: 1.90,
},
},
},
UnitInfo: domain.NullUnitInfo{
Valid: true,
Info: domain.UnitInfo{
Catapult: domain.Unit{
BuildTime: 0.233,
},
Knight: domain.Unit{
BuildTime: 28.3333,
},
},
},
NumPlayers: domain.NullInt64{
Int64: 123,
Valid: true,
},
PlayerDataUpdatedAt: time.Now(),
PlayerSnapshotsCreatedAt: time.Now().Add(-15 * time.Minute),
NumTribes: domain.NullInt64{
Int64: 10,
Valid: true,
},
TribeDataUpdatedAt: time.Now().Add(-1 * time.Minute),
TribeSnapshotsCreatedAt: time.Now().Add(-25 * time.Minute),
NumVillages: domain.NullInt64{
Int64: 12345,
Valid: true,
},
VillageDataUpdatedAt: time.Now().Add(-1 * time.Hour),
}
updatedServer, err := repo.Update(context.Background(), server.Key, params)
assert.NoError(t, err)
assert.Equal(t, server.Key, updatedServer.Key)
assert.Equal(t, params.Config.Config, updatedServer.Config)
assert.Equal(t, params.UnitInfo.Info, updatedServer.UnitInfo)
assert.Equal(t, params.BuildingInfo.Info, updatedServer.BuildingInfo)
assert.Equal(t, params.NumPlayers.Int64, updatedServer.NumPlayers)
assert.WithinDuration(t, params.PlayerDataUpdatedAt, updatedServer.PlayerDataUpdatedAt, 1*time.Second)
assert.Equal(t, params.NumTribes.Int64, updatedServer.NumTribes)
assert.WithinDuration(t, params.TribeDataUpdatedAt, updatedServer.TribeDataUpdatedAt, 1*time.Second)
assert.Equal(t, params.NumVillages.Int64, updatedServer.NumVillages)
assert.WithinDuration(t, params.VillageDataUpdatedAt, updatedServer.VillageDataUpdatedAt, 1*time.Second)
assert.WithinDuration(t, params.PlayerSnapshotsCreatedAt, updatedServer.PlayerSnapshotsCreatedAt, 1*time.Second)
assert.WithinDuration(t, params.TribeSnapshotsCreatedAt, updatedServer.TribeSnapshotsCreatedAt, 1*time.Second)
})
t.Run("ERR: nothing to update", func(t *testing.T) {
t.Parallel()
updatedServer, err := repo.Update(context.Background(), server.Key, domain.UpdateServerParams{})
assert.ErrorIs(t, err, domain.ErrNothingToUpdate)
assert.Zero(t, updatedServer)
})
t.Run("ERR: server not found", func(t *testing.T) {
t.Parallel()
key := server.Key + "1"
updatedServer, err := repo.Update(context.Background(), key, domain.UpdateServerParams{
Config: domain.NullServerConfig{
Config: domain.ServerConfig{
Speed: 0.5,
},
Valid: true,
},
})
assert.ErrorIs(t, err, domain.ServerNotFoundError{Key: key})
assert.Zero(t, updatedServer)
})
}
func assertCreatedServers(tb testing.TB, params []domain.CreateServerParams, servers []domain.Server) {
tb.Helper()
assert.Len(tb, servers, len(params))
for _, p := range params {
var srv domain.Server
for _, s := range servers {
if s.Key == p.Key {
srv = s
break
}
}
assert.Equal(tb, p.URL, srv.URL)
assert.Equal(tb, p.Open, srv.Open)
assert.Equal(tb, p.VersionCode, srv.VersionCode)
assert.False(tb, srv.Special)
}
}
func concatStringSlices(slices ...[]string) []string {
var totalLen int
for _, s := range slices {
totalLen += len(s)
}
res := make([]string, totalLen)
var i int
for _, s := range slices {
i += copy(res[i:], s)
}
return res
}