All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: twhelp/core#186
476 lines
13 KiB
Go
476 lines
13 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/google/go-cmp/cmp"
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
"github.com/jackc/pgerrcode"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/uptrace/bun/driver/pgdriver"
|
|
)
|
|
|
|
func TestTribeSnapshot_Create(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if testing.Short() {
|
|
t.Skip("skipping long-running test")
|
|
}
|
|
|
|
db := bundbtest.NewDB(t)
|
|
fixture := loadFixtures(t, db)
|
|
repo := bundb.NewTribeSnapshot(db)
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tribeTT := fixture.Tribe(t, "pl169-tt")
|
|
tribeCSA := fixture.Tribe(t, "pl169-csa")
|
|
|
|
params := []domain.CreateTribeSnapshotParams{
|
|
{
|
|
OpponentsDefeated: tribeTT.OpponentsDefeated,
|
|
TribeID: tribeTT.ID,
|
|
ServerKey: tribeTT.ServerKey,
|
|
NumMembers: tribeTT.NumMembers,
|
|
NumVillages: tribeTT.NumVillages,
|
|
Points: tribeTT.Points,
|
|
AllPoints: tribeTT.AllPoints,
|
|
Rank: tribeTT.Rank,
|
|
Dominance: tribeTT.Dominance,
|
|
Date: time.Now().Add(-15 * time.Hour),
|
|
},
|
|
{
|
|
OpponentsDefeated: tribeTT.OpponentsDefeated,
|
|
TribeID: tribeTT.ID,
|
|
ServerKey: tribeTT.ServerKey,
|
|
NumMembers: tribeTT.NumMembers,
|
|
NumVillages: tribeTT.NumVillages,
|
|
Points: tribeTT.Points,
|
|
AllPoints: tribeTT.AllPoints,
|
|
Rank: tribeTT.Rank,
|
|
Dominance: tribeTT.Dominance,
|
|
Date: time.Now().Add(-48 * time.Hour),
|
|
},
|
|
{
|
|
OpponentsDefeated: tribeCSA.OpponentsDefeated,
|
|
TribeID: tribeCSA.ID,
|
|
ServerKey: tribeCSA.ServerKey,
|
|
NumMembers: tribeCSA.NumMembers,
|
|
NumVillages: tribeCSA.NumVillages,
|
|
Points: tribeCSA.Points,
|
|
AllPoints: tribeCSA.AllPoints,
|
|
Rank: tribeCSA.Rank,
|
|
Dominance: tribeCSA.Dominance,
|
|
Date: time.Now().Add(-15 * time.Hour),
|
|
},
|
|
}
|
|
|
|
assert.NoError(t, repo.Create(context.Background(), params...))
|
|
|
|
snapshots, err := repo.List(context.Background(), domain.ListTribeSnapshotsParams{
|
|
ServerKeys: []string{fixture.Server(t, "pl169").Key},
|
|
})
|
|
assert.NoError(t, err)
|
|
for _, p := range params {
|
|
var found bool
|
|
for _, s := range snapshots {
|
|
if cmp.Equal(p, domain.CreateTribeSnapshotParams{
|
|
OpponentsDefeated: s.OpponentsDefeated,
|
|
TribeID: s.TribeID,
|
|
ServerKey: s.ServerKey,
|
|
NumMembers: s.NumMembers,
|
|
NumVillages: s.NumVillages,
|
|
Points: s.Points,
|
|
AllPoints: s.AllPoints,
|
|
Rank: s.Rank,
|
|
Dominance: s.Dominance,
|
|
Date: s.Date,
|
|
}, cmpopts.EquateApproxTime(24*time.Hour)) {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
assert.True(t, found)
|
|
}
|
|
})
|
|
|
|
t.Run("OK: len(params) == 0", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
assert.NoError(t, repo.Create(context.Background()))
|
|
})
|
|
|
|
t.Run("ERR: snapshot already exists", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tribeTT := fixture.Tribe(t, "pl169-tt")
|
|
|
|
params := []domain.CreateTribeSnapshotParams{
|
|
{
|
|
OpponentsDefeated: tribeTT.OpponentsDefeated,
|
|
TribeID: tribeTT.ID,
|
|
ServerKey: tribeTT.ServerKey,
|
|
NumMembers: tribeTT.NumMembers,
|
|
NumVillages: tribeTT.NumVillages,
|
|
Points: tribeTT.Points,
|
|
AllPoints: tribeTT.AllPoints,
|
|
Rank: tribeTT.Rank,
|
|
Dominance: tribeTT.Dominance,
|
|
Date: time.Now().Add(-120 * time.Hour),
|
|
},
|
|
{
|
|
OpponentsDefeated: tribeTT.OpponentsDefeated,
|
|
TribeID: tribeTT.ID,
|
|
ServerKey: tribeTT.ServerKey,
|
|
NumMembers: tribeTT.NumMembers,
|
|
NumVillages: tribeTT.NumVillages,
|
|
Points: tribeTT.Points,
|
|
AllPoints: tribeTT.AllPoints,
|
|
Rank: tribeTT.Rank,
|
|
Dominance: tribeTT.Dominance,
|
|
Date: time.Now().Add(-120 * time.Hour),
|
|
},
|
|
}
|
|
|
|
err := repo.Create(context.Background(), params...)
|
|
var pgErr pgdriver.Error
|
|
assert.ErrorAs(t, err, &pgErr)
|
|
assert.Equal(t, pgerrcode.UniqueViolation, pgErr.Field('C'))
|
|
assert.Equal(t, "tribe_snapshots_tribe_id_server_key_date_key", pgErr.Field('n'))
|
|
})
|
|
|
|
t.Run("ERR: server must exist", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
serverKey := "random"
|
|
tribeTT := fixture.Tribe(t, "pl169-tt")
|
|
|
|
params := []domain.CreateTribeSnapshotParams{
|
|
{
|
|
OpponentsDefeated: tribeTT.OpponentsDefeated,
|
|
TribeID: tribeTT.ID,
|
|
ServerKey: serverKey,
|
|
NumMembers: tribeTT.NumMembers,
|
|
NumVillages: tribeTT.NumVillages,
|
|
Points: tribeTT.Points,
|
|
AllPoints: tribeTT.AllPoints,
|
|
Rank: tribeTT.Rank,
|
|
Dominance: tribeTT.Dominance,
|
|
Date: time.Now().Add(-168 * time.Hour),
|
|
},
|
|
}
|
|
|
|
err := repo.Create(context.Background(), params...)
|
|
var pgErr pgdriver.Error
|
|
assert.ErrorAs(t, err, &pgErr)
|
|
assert.Equal(t, pgerrcode.ForeignKeyViolation, pgErr.Field('C'))
|
|
assert.Equal(t, "tribe_snapshots_server_key_fkey", pgErr.Field('n'))
|
|
})
|
|
|
|
t.Run("ERR: tribe must exist", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tribeTT := fixture.Tribe(t, "pl169-tt")
|
|
|
|
params := []domain.CreateTribeSnapshotParams{
|
|
{
|
|
OpponentsDefeated: tribeTT.OpponentsDefeated,
|
|
TribeID: tribeTT.ID + 123123123131,
|
|
ServerKey: tribeTT.ServerKey,
|
|
NumMembers: tribeTT.NumMembers,
|
|
NumVillages: tribeTT.NumVillages,
|
|
Points: tribeTT.Points,
|
|
AllPoints: tribeTT.AllPoints,
|
|
Rank: tribeTT.Rank,
|
|
Dominance: tribeTT.Dominance,
|
|
Date: time.Now().Add(-168 * time.Hour),
|
|
},
|
|
}
|
|
|
|
err := repo.Create(context.Background(), params...)
|
|
var pgErr pgdriver.Error
|
|
assert.ErrorAs(t, err, &pgErr)
|
|
assert.Equal(t, pgerrcode.ForeignKeyViolation, pgErr.Field('C'))
|
|
assert.Equal(t, "tribe_snapshots_tribe_id_server_key_fkey", pgErr.Field('n'))
|
|
})
|
|
}
|
|
|
|
func TestTribeSnapshot_List_ListCount(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if testing.Short() {
|
|
t.Skip("skipping long-running test")
|
|
}
|
|
|
|
db := bundbtest.NewDB(t)
|
|
fixture := loadFixtures(t, db)
|
|
repo := bundb.NewTribeSnapshot(db)
|
|
snapshots := fixture.TribeSnapshots(t)
|
|
|
|
type expectedSnapshots struct {
|
|
id int64
|
|
serverKey string
|
|
}
|
|
|
|
allSnapshots := make([]expectedSnapshots, 0, len(snapshots))
|
|
for _, p := range snapshots {
|
|
allSnapshots = append(allSnapshots, expectedSnapshots{
|
|
id: p.ID,
|
|
serverKey: p.ServerKey,
|
|
})
|
|
}
|
|
|
|
//nolint:prealloc
|
|
var snapshotsEN113 []expectedSnapshots
|
|
for _, p := range snapshots {
|
|
if p.ServerKey != "en113" {
|
|
continue
|
|
}
|
|
|
|
snapshotsEN113 = append(snapshotsEN113, expectedSnapshots{
|
|
id: p.ID,
|
|
serverKey: p.ServerKey,
|
|
})
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
params domain.ListTribeSnapshotsParams
|
|
expectedSnapshots []expectedSnapshots
|
|
expectedCount int64
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "Sort=[{By=Date,Direction=ASC},{By=ID,Direction=ASC}]",
|
|
params: domain.ListTribeSnapshotsParams{
|
|
Sort: []domain.TribeSnapshotSort{
|
|
{
|
|
By: domain.TribeSnapshotSortByDate,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.TribeSnapshotSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
},
|
|
},
|
|
expectedSnapshots: allSnapshots,
|
|
expectedCount: int64(len(allSnapshots)),
|
|
},
|
|
{
|
|
name: "ServerKey=[en113],Sort=[{By=Date,Direction=ASC},{By=ID,Direction=ASC}]",
|
|
params: domain.ListTribeSnapshotsParams{
|
|
ServerKeys: []string{"en113"},
|
|
Sort: []domain.TribeSnapshotSort{
|
|
{
|
|
By: domain.TribeSnapshotSortByDate,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.TribeSnapshotSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
},
|
|
},
|
|
expectedSnapshots: snapshotsEN113,
|
|
expectedCount: int64(len(snapshotsEN113)),
|
|
},
|
|
{
|
|
name: "ServerKey=[pl169],Sort=[{By=Date,Direction=ASC},{By=ID,Direction=ASC}],Limit=2",
|
|
params: domain.ListTribeSnapshotsParams{
|
|
ServerKeys: []string{"pl169"},
|
|
Pagination: domain.Pagination{
|
|
Limit: 2,
|
|
},
|
|
Sort: []domain.TribeSnapshotSort{
|
|
{
|
|
By: domain.TribeSnapshotSortByDate,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.TribeSnapshotSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
},
|
|
},
|
|
expectedSnapshots: []expectedSnapshots{
|
|
{
|
|
id: 10000,
|
|
serverKey: "pl169",
|
|
},
|
|
{
|
|
id: 10050,
|
|
serverKey: "pl169",
|
|
},
|
|
},
|
|
expectedCount: 4,
|
|
},
|
|
{
|
|
name: "ServerKey=[pl169],Sort=[{By=Date,Direction=ASC},{By=ID,Direction=ASC}],Offset=1",
|
|
params: domain.ListTribeSnapshotsParams{
|
|
ServerKeys: []string{"pl169"},
|
|
Pagination: domain.Pagination{
|
|
Offset: 1,
|
|
},
|
|
Sort: []domain.TribeSnapshotSort{
|
|
{
|
|
By: domain.TribeSnapshotSortByDate,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.TribeSnapshotSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
},
|
|
},
|
|
expectedSnapshots: []expectedSnapshots{
|
|
{
|
|
id: 10050,
|
|
serverKey: "pl169",
|
|
},
|
|
{
|
|
id: 10001,
|
|
serverKey: "pl169",
|
|
},
|
|
{
|
|
id: 10051,
|
|
serverKey: "pl169",
|
|
},
|
|
},
|
|
expectedCount: 4,
|
|
},
|
|
{
|
|
name: "ServerKey=[pl169],Sort=[{By=Date,Direction=DESC},{By=ID,Direction=ASC}],Offset=1,Limit=1",
|
|
params: domain.ListTribeSnapshotsParams{
|
|
ServerKeys: []string{"pl169"},
|
|
Pagination: domain.Pagination{
|
|
Limit: 1,
|
|
Offset: 1,
|
|
},
|
|
Sort: []domain.TribeSnapshotSort{
|
|
{
|
|
By: domain.TribeSnapshotSortByDate,
|
|
Direction: domain.SortDirectionDESC,
|
|
},
|
|
{
|
|
By: domain.TribeSnapshotSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
},
|
|
},
|
|
expectedSnapshots: []expectedSnapshots{
|
|
{
|
|
id: 10051,
|
|
serverKey: "pl169",
|
|
},
|
|
},
|
|
expectedCount: 4,
|
|
},
|
|
{
|
|
name: "ServerKey=[pl169],TribeIDs=[28],Sort=[{By=Date,Direction=ASC},{By=ID,Direction=ASC}]",
|
|
params: domain.ListTribeSnapshotsParams{
|
|
ServerKeys: []string{"pl169"},
|
|
TribeIDs: []int64{28},
|
|
Sort: []domain.TribeSnapshotSort{
|
|
{
|
|
By: domain.TribeSnapshotSortByDate,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.TribeSnapshotSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
},
|
|
},
|
|
expectedSnapshots: []expectedSnapshots{
|
|
{
|
|
id: 10000,
|
|
serverKey: "pl169",
|
|
},
|
|
{
|
|
id: 10001,
|
|
serverKey: "pl169",
|
|
},
|
|
},
|
|
expectedCount: 2,
|
|
},
|
|
{
|
|
name: "ERR: unsupported sort by",
|
|
params: domain.ListTribeSnapshotsParams{
|
|
Sort: []domain.TribeSnapshotSort{
|
|
{By: 100, Direction: domain.SortDirectionDESC},
|
|
},
|
|
},
|
|
expectedSnapshots: nil,
|
|
expectedCount: 0,
|
|
expectedErr: domain.ErrUnsupportedSortBy,
|
|
},
|
|
{
|
|
name: "ERR: unsupported sort direction",
|
|
params: domain.ListTribeSnapshotsParams{
|
|
Sort: []domain.TribeSnapshotSort{
|
|
{By: domain.TribeSnapshotSortByDate, Direction: 100},
|
|
},
|
|
},
|
|
expectedSnapshots: nil,
|
|
expectedCount: 0,
|
|
expectedErr: domain.ErrUnsupportedSortDirection,
|
|
},
|
|
}
|
|
|
|
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.ErrorIs(t, err, tt.expectedErr)
|
|
assert.Equal(t, tt.expectedCount, count)
|
|
assert.Len(t, resListCount, len(tt.expectedSnapshots))
|
|
for _, expSnapshot := range tt.expectedSnapshots {
|
|
found := false
|
|
for _, snapshot := range resListCount {
|
|
if snapshot.ID == expSnapshot.id && snapshot.ServerKey == expSnapshot.serverKey {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
assert.True(t, found, "snapshot (id=%d,serverkey=%s) not found", expSnapshot.id, expSnapshot.serverKey)
|
|
}
|
|
|
|
resList, err := repo.List(context.Background(), tt.params)
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
assert.Equal(t, resListCount, resList)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTribeSnapshot_Delete(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if testing.Short() {
|
|
t.Skip("skipping long-running test")
|
|
}
|
|
|
|
db := bundbtest.NewDB(t)
|
|
fixture := loadFixtures(t, db)
|
|
repo := bundb.NewTribeSnapshot(db)
|
|
|
|
snapshotsBeforeDelete, err := repo.List(context.Background(), domain.ListTribeSnapshotsParams{})
|
|
assert.NoError(t, err)
|
|
assert.Greater(t, len(snapshotsBeforeDelete), 0)
|
|
|
|
assert.NoError(t, repo.Delete(context.Background(), fixture.Server(t, "pl169").Key, time.Date(2021, time.September, 3, 22, 30, 00, 0, time.UTC)))
|
|
|
|
snapshotsAfterDelete, err := repo.List(context.Background(), domain.ListTribeSnapshotsParams{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, len(snapshotsBeforeDelete)-3, len(snapshotsAfterDelete))
|
|
}
|