Dawid Wysokiński
aec3a3736d
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #13
203 lines
4.9 KiB
Go
203 lines
4.9 KiB
Go
package bundb_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/bundb"
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/bundb/internal/model"
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/uptrace/bun/dbfixture"
|
|
)
|
|
|
|
func TestGroup_Create(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := bundb.NewGroup(newDB(t))
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
params, err := domain.NewCreateGroupParams(
|
|
"592292203234328587",
|
|
"en",
|
|
"en113",
|
|
"1234",
|
|
"1235",
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
group, err := repo.Create(context.Background(), params)
|
|
assert.NoError(t, err)
|
|
_, err = uuid.Parse(group.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, params.ServerID(), group.ServerID)
|
|
assert.Equal(t, params.ServerKey(), group.ServerKey)
|
|
assert.Equal(t, params.VersionCode(), group.VersionCode)
|
|
assert.Equal(t, params.ChannelGains(), group.ChannelGains)
|
|
assert.Equal(t, params.ChannelLosses(), group.ChannelLosses)
|
|
assert.WithinDuration(t, time.Now(), group.CreatedAt, 1*time.Second)
|
|
})
|
|
}
|
|
|
|
func TestGroup_UpdateByID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db := newDB(t)
|
|
fixture := loadFixtures(t, db)
|
|
repo := bundb.NewGroup(db)
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
group := getGroupFromFixture(t, fixture, "group-server-1-1")
|
|
params := domain.UpdateGroupParams{
|
|
ChannelGains: domain.NullString{
|
|
String: group.ChannelGains + "update",
|
|
Valid: true,
|
|
},
|
|
ChannelLosses: domain.NullString{
|
|
String: group.ChannelLosses + "update",
|
|
Valid: true,
|
|
},
|
|
ServerKey: domain.NullString{
|
|
String: group.ServerKey + "update",
|
|
Valid: true,
|
|
},
|
|
VersionCode: domain.NullString{
|
|
String: "update",
|
|
Valid: true,
|
|
},
|
|
}
|
|
|
|
updatedGroup, err := repo.UpdateByID(context.Background(), group.ID.String(), params)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, params.ChannelGains.String, updatedGroup.ChannelGains)
|
|
assert.Equal(t, params.ChannelLosses.String, updatedGroup.ChannelLosses)
|
|
assert.Equal(t, params.ServerKey.String, updatedGroup.ServerKey)
|
|
assert.Equal(t, params.VersionCode.String, updatedGroup.VersionCode)
|
|
})
|
|
|
|
t.Run("ERR: nothing to update", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
updatedGroup, err := repo.UpdateByID(context.Background(), "", domain.UpdateGroupParams{})
|
|
assert.ErrorIs(t, err, domain.ErrNothingToUpdate)
|
|
assert.Zero(t, updatedGroup)
|
|
})
|
|
|
|
t.Run("ERR: invalid UUID", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
id := "12345"
|
|
updatedGroup, err := repo.UpdateByID(context.Background(), id, domain.UpdateGroupParams{
|
|
ChannelGains: domain.NullString{
|
|
String: "update",
|
|
Valid: true,
|
|
},
|
|
})
|
|
assert.ErrorIs(t, err, domain.GroupNotFoundError{ID: id})
|
|
assert.Zero(t, updatedGroup)
|
|
})
|
|
|
|
t.Run("ERR: group not found", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
id := uuid.NewString()
|
|
updatedGroup, err := repo.UpdateByID(context.Background(), id, domain.UpdateGroupParams{
|
|
ChannelGains: domain.NullString{
|
|
String: "update",
|
|
Valid: true,
|
|
},
|
|
})
|
|
assert.ErrorIs(t, err, domain.GroupNotFoundError{ID: id})
|
|
assert.Zero(t, updatedGroup)
|
|
})
|
|
}
|
|
|
|
func TestGroup_List(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db := newDB(t)
|
|
fixture := loadFixtures(t, db)
|
|
repo := bundb.NewGroup(db)
|
|
groups := getAllGroupsFromFixture(t, fixture)
|
|
|
|
allGroups := make([]string, 0, len(groups))
|
|
for _, g := range groups {
|
|
allGroups = append(allGroups, g.ID.String())
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
params domain.ListGroupsParams
|
|
expectedGroups []string
|
|
}{
|
|
{
|
|
name: "no params",
|
|
params: domain.ListGroupsParams{},
|
|
expectedGroups: allGroups,
|
|
},
|
|
{
|
|
name: "ServerIDs=[server-1]",
|
|
params: domain.ListGroupsParams{
|
|
ServerIDs: []string{"server-1"},
|
|
},
|
|
expectedGroups: []string{
|
|
"d56ad37f-2637-48ea-98f8-79627f3fcc96",
|
|
"429b790e-7186-4106-b531-4cc4931ce2ba",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
res, err := repo.List(context.Background(), tt.params)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, res, len(tt.expectedGroups))
|
|
for _, id := range tt.expectedGroups {
|
|
found := false
|
|
for _, grp := range res {
|
|
if grp.ID == id {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
assert.True(t, found, "group (id=%s) not found", id)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func getAllGroupsFromFixture(tb testing.TB, fixture *dbfixture.Fixture) []model.Group {
|
|
tb.Helper()
|
|
|
|
//nolint:lll
|
|
ids := []string{"group-server-1-1", "group-server-1-2", "group-server-2-1", "group-server-2-2"}
|
|
|
|
groups := make([]model.Group, 0, len(ids))
|
|
for _, id := range ids {
|
|
groups = append(groups, getGroupFromFixture(tb, fixture, id))
|
|
}
|
|
|
|
return groups
|
|
}
|
|
|
|
func getGroupFromFixture(tb testing.TB, fixture *dbfixture.Fixture, id string) model.Group {
|
|
tb.Helper()
|
|
|
|
row, err := fixture.Row("Group." + id)
|
|
require.NoError(tb, err)
|
|
g, ok := row.(*model.Group)
|
|
require.True(tb, ok)
|
|
return *g
|
|
}
|