From 1c8694f68eb48ed4213449a7334db9cc68b0d870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Sat, 22 Jul 2023 08:30:31 +0200 Subject: [PATCH] refactor: adapters - rename structs/files --- cmd/dcbot/internal/run/run.go | 4 +-- internal/adapter/bun_test.go | 23 +++---------- .../{bun_group.go => group_bun_repository.go} | 24 +++++++------- ...p_test.go => group_bun_repository_test.go} | 32 +++++++++---------- internal/adapter/redis_test.go | 10 ++++-- .../{http_twhelp.go => twhelp_http.go} | 2 ++ ...village.go => village_redis_repository.go} | 12 +++---- ...st.go => village_redis_repository_test.go} | 4 +-- 8 files changed, 53 insertions(+), 58 deletions(-) rename internal/adapter/{bun_group.go => group_bun_repository.go} (86%) rename internal/adapter/{bun_group_test.go => group_bun_repository_test.go} (94%) rename internal/adapter/{http_twhelp.go => twhelp_http.go} (99%) rename internal/adapter/{redis_village.go => village_redis_repository.go} (77%) rename internal/adapter/{redis_village_test.go => village_redis_repository_test.go} (85%) diff --git a/cmd/dcbot/internal/run/run.go b/cmd/dcbot/internal/run/run.go index 2d202ea..d977cb0 100644 --- a/cmd/dcbot/internal/run/run.go +++ b/cmd/dcbot/internal/run/run.go @@ -51,8 +51,8 @@ func New() *cli.Command { return fmt.Errorf("internal.NewTWHelpService: %w", err) } - groupRepo := adapter.NewGroupBun(db) - villageRepo := adapter.NewVillageRedis(redisClient) + groupRepo := adapter.NewGroupBunRepository(db) + villageRepo := adapter.NewVillageRedisRepository(redisClient) choiceSvc := service.NewChoice(twhelpService) groupSvc := service.NewGroup(groupRepo, twhelpService, logger, cfg.MaxGroupsPerServer, cfg.MaxMonitorsPerGroup) diff --git a/internal/adapter/bun_test.go b/internal/adapter/bun_test.go index df23f96..9891ffe 100644 --- a/internal/adapter/bun_test.go +++ b/internal/adapter/bun_test.go @@ -3,7 +3,6 @@ package adapter_test import ( "context" "database/sql" - "errors" "os" "strings" "testing" @@ -39,7 +38,11 @@ func newBunDB(tb testing.TB) *bun.DB { tb.Cleanup(func() { _ = bunDB.Close() }) - require.NoError(tb, retry(bunDB.Ping), "couldn't ping DB") + + bo := backoff.NewExponentialBackOff() + bo.MaxInterval = time.Second * 5 + bo.MaxElapsedTime = 30 * time.Second + require.NoError(tb, backoff.Retry(bunDB.Ping, bo), "couldn't ping DB") _, err := bunDB.Exec("CREATE SCHEMA ?", bun.Safe(schema)) require.NoError(tb, err, "couldn't create schema") @@ -108,19 +111,3 @@ func (f *bunfixture) monitor(tb testing.TB, id string) domain.Monitor { func generateSchema() string { return strings.TrimFunc(strings.ReplaceAll(uuid.NewString(), "-", "_"), unicode.IsNumber) } - -func retry(op func() error) error { - bo := backoff.NewExponentialBackOff() - bo.MaxInterval = time.Second * 5 - bo.MaxElapsedTime = 30 * time.Second - - if err := backoff.Retry(op, bo); err != nil { - if bo.NextBackOff() == backoff.Stop { - return errors.New("reached retry deadline") - } - - return err - } - - return nil -} diff --git a/internal/adapter/bun_group.go b/internal/adapter/group_bun_repository.go similarity index 86% rename from internal/adapter/bun_group.go rename to internal/adapter/group_bun_repository.go index 66c62f9..f721f44 100644 --- a/internal/adapter/bun_group.go +++ b/internal/adapter/group_bun_repository.go @@ -14,15 +14,15 @@ import ( "github.com/uptrace/bun/driver/pgdriver" ) -type GroupBun struct { +type GroupBunRepository struct { db *bun.DB } -func NewGroupBun(db *bun.DB) *GroupBun { - return &GroupBun{db: db} +func NewGroupBunRepository(db *bun.DB) *GroupBunRepository { + return &GroupBunRepository{db: db} } -func (g *GroupBun) Create(ctx context.Context, params domain.CreateGroupParams) (domain.GroupWithMonitors, error) { +func (g *GroupBunRepository) Create(ctx context.Context, params domain.CreateGroupParams) (domain.GroupWithMonitors, error) { group := bunmodel.Group{ ServerID: params.ServerID(), VersionCode: params.VersionCode(), @@ -44,7 +44,7 @@ func (g *GroupBun) Create(ctx context.Context, params domain.CreateGroupParams) return group.ToDomain(), nil } -func (g *GroupBun) Update(ctx context.Context, id string, params domain.UpdateGroupParams) (domain.GroupWithMonitors, error) { +func (g *GroupBunRepository) Update(ctx context.Context, id string, params domain.UpdateGroupParams) (domain.GroupWithMonitors, error) { if params.IsZero() { return domain.GroupWithMonitors{}, domain.ErrNothingToUpdate } @@ -71,7 +71,7 @@ func (g *GroupBun) Update(ctx context.Context, id string, params domain.UpdateGr return g.Get(ctx, id) } -func (g *GroupBun) AddMonitor(ctx context.Context, id string, tribeID int64) (domain.GroupWithMonitors, error) { +func (g *GroupBunRepository) AddMonitor(ctx context.Context, id string, tribeID int64) (domain.GroupWithMonitors, error) { parsedID, err := uuid.Parse(id) if err != nil { return domain.GroupWithMonitors{}, domain.GroupDoesNotExistError{ID: id} @@ -96,7 +96,7 @@ func (g *GroupBun) AddMonitor(ctx context.Context, id string, tribeID int64) (do return g.Get(ctx, id) } -func (g *GroupBun) DeleteMonitors(ctx context.Context, id string, monitorIDs ...string) (domain.GroupWithMonitors, error) { +func (g *GroupBunRepository) DeleteMonitors(ctx context.Context, id string, monitorIDs ...string) (domain.GroupWithMonitors, error) { if _, err := uuid.Parse(id); err != nil { return domain.GroupWithMonitors{}, domain.GroupNotFoundError{ID: id} } @@ -120,7 +120,7 @@ func (g *GroupBun) DeleteMonitors(ctx context.Context, id string, monitorIDs ... return g.Get(ctx, id) } -func (g *GroupBun) List(ctx context.Context, params domain.ListGroupsParams) ([]domain.GroupWithMonitors, error) { +func (g *GroupBunRepository) List(ctx context.Context, params domain.ListGroupsParams) ([]domain.GroupWithMonitors, error) { var groups []bunmodel.Group if err := g.db.NewSelect(). @@ -142,7 +142,7 @@ func (g *GroupBun) List(ctx context.Context, params domain.ListGroupsParams) ([] return result, nil } -func (g *GroupBun) Get(ctx context.Context, id string) (domain.GroupWithMonitors, error) { +func (g *GroupBunRepository) Get(ctx context.Context, id string) (domain.GroupWithMonitors, error) { group, err := g.get(ctx, id, true) if err != nil { return domain.GroupWithMonitors{}, err @@ -150,7 +150,7 @@ func (g *GroupBun) Get(ctx context.Context, id string) (domain.GroupWithMonitors return group.ToDomain(), nil } -func (g *GroupBun) get(ctx context.Context, id string, withMonitors bool) (bunmodel.Group, error) { +func (g *GroupBunRepository) get(ctx context.Context, id string, withMonitors bool) (bunmodel.Group, error) { if _, err := uuid.Parse(id); err != nil { return bunmodel.Group{}, domain.GroupNotFoundError{ID: id} } @@ -176,7 +176,7 @@ func (g *GroupBun) get(ctx context.Context, id string, withMonitors bool) (bunmo return group, nil } -func (g *GroupBun) Delete(ctx context.Context, id string) error { +func (g *GroupBunRepository) Delete(ctx context.Context, id string) error { if _, err := uuid.Parse(id); err != nil { return domain.GroupNotFoundError{ID: id} } @@ -196,7 +196,7 @@ func (g *GroupBun) Delete(ctx context.Context, id string) error { return nil } -func (g *GroupBun) DeleteMany(ctx context.Context, ids ...string) error { +func (g *GroupBunRepository) DeleteMany(ctx context.Context, ids ...string) error { if len(ids) == 0 { return nil } diff --git a/internal/adapter/bun_group_test.go b/internal/adapter/group_bun_repository_test.go similarity index 94% rename from internal/adapter/bun_group_test.go rename to internal/adapter/group_bun_repository_test.go index c88d8b6..70953ef 100644 --- a/internal/adapter/bun_group_test.go +++ b/internal/adapter/group_bun_repository_test.go @@ -13,14 +13,14 @@ import ( "golang.org/x/text/language" ) -func TestGroup_Create(t *testing.T) { +func TestGroupBunRepository_Create(t *testing.T) { t.Parallel() if testing.Short() { t.Skip("skipping long-running test") } - repo := adapter.NewGroupBun(newBunDB(t)) + repo := adapter.NewGroupBunRepository(newBunDB(t)) t.Run("OK", func(t *testing.T) { t.Parallel() @@ -52,7 +52,7 @@ func TestGroup_Create(t *testing.T) { }) } -func TestGroup_Update(t *testing.T) { +func TestGroupBunRepository_Update(t *testing.T) { t.Parallel() if testing.Short() { @@ -61,7 +61,7 @@ func TestGroup_Update(t *testing.T) { db := newBunDB(t) fixture := loadFixtures(t, db) - repo := adapter.NewGroupBun(db) + repo := adapter.NewGroupBunRepository(db) group := fixture.group(t, "group-1-server-1") t.Run("OK", func(t *testing.T) { @@ -143,7 +143,7 @@ func TestGroup_Update(t *testing.T) { }) } -func TestGroup_AddMonitor(t *testing.T) { +func TestGroupBunRepository_AddMonitor(t *testing.T) { t.Parallel() if testing.Short() { @@ -152,7 +152,7 @@ func TestGroup_AddMonitor(t *testing.T) { db := newBunDB(t) fixture := loadFixtures(t, db) - repo := adapter.NewGroupBun(db) + repo := adapter.NewGroupBunRepository(db) group := fixture.group(t, "group-1-server-1") t.Run("OK", func(t *testing.T) { @@ -216,7 +216,7 @@ func TestGroup_AddMonitor(t *testing.T) { }) } -func TestGroup_DeleteMonitors(t *testing.T) { +func TestGroupBunRepository_DeleteMonitors(t *testing.T) { t.Parallel() if testing.Short() { @@ -225,7 +225,7 @@ func TestGroup_DeleteMonitors(t *testing.T) { db := newBunDB(t) fixture := loadFixtures(t, db) - repo := adapter.NewGroupBun(db) + repo := adapter.NewGroupBunRepository(db) t.Run("OK", func(t *testing.T) { t.Parallel() @@ -305,7 +305,7 @@ func TestGroup_DeleteMonitors(t *testing.T) { }) } -func TestGroup_List(t *testing.T) { +func TestGroupBunRepository_List(t *testing.T) { t.Parallel() if testing.Short() { @@ -314,7 +314,7 @@ func TestGroup_List(t *testing.T) { db := newBunDB(t) fixture := loadFixtures(t, db) - repo := adapter.NewGroupBun(db) + repo := adapter.NewGroupBunRepository(db) groups := fixture.groups(t) allGroups := make([]string, 0, len(groups)) @@ -445,7 +445,7 @@ func TestGroup_List(t *testing.T) { } } -func TestGroup_Get(t *testing.T) { +func TestGroupBunRepository_Get(t *testing.T) { t.Parallel() if testing.Short() { @@ -454,7 +454,7 @@ func TestGroup_Get(t *testing.T) { db := newBunDB(t) fixture := loadFixtures(t, db) - repo := adapter.NewGroupBun(db) + repo := adapter.NewGroupBunRepository(db) group := fixture.group(t, "group-1-server-1") t.Run("OK", func(t *testing.T) { @@ -496,7 +496,7 @@ func TestGroup_Get(t *testing.T) { }) } -func TestGroup_Delete(t *testing.T) { +func TestGroupBunRepository_Delete(t *testing.T) { t.Parallel() if testing.Short() { @@ -505,7 +505,7 @@ func TestGroup_Delete(t *testing.T) { db := newBunDB(t) fixture := loadFixtures(t, db) - groupRepo := adapter.NewGroupBun(db) + groupRepo := adapter.NewGroupBunRepository(db) group := fixture.group(t, "group-1-server-1") t.Run("OK", func(t *testing.T) { @@ -554,7 +554,7 @@ func TestGroup_Delete(t *testing.T) { }) } -func TestGroup_DeleteMany(t *testing.T) { +func TestGroupBunRepository_DeleteMany(t *testing.T) { t.Parallel() if testing.Short() { @@ -563,7 +563,7 @@ func TestGroup_DeleteMany(t *testing.T) { db := newBunDB(t) fixture := loadFixtures(t, db) - groupRepo := adapter.NewGroupBun(db) + groupRepo := adapter.NewGroupBunRepository(db) group1 := fixture.group(t, "group-1-server-1") group2 := fixture.group(t, "group-2-server-1") ids := []string{group1.ID, group2.ID} diff --git a/internal/adapter/redis_test.go b/internal/adapter/redis_test.go index a89dd91..0e0f838 100644 --- a/internal/adapter/redis_test.go +++ b/internal/adapter/redis_test.go @@ -3,7 +3,9 @@ package adapter_test import ( "context" "testing" + "time" + "github.com/cenkalti/backoff/v4" "github.com/redis/go-redis/v9" "github.com/stretchr/testify/require" ) @@ -18,9 +20,13 @@ func newRedisClient(tb testing.TB) *redis.Client { tb.Cleanup(func() { _ = client.Close() }) - require.NoError(tb, retry(func() error { + + bo := backoff.NewExponentialBackOff() + bo.MaxInterval = time.Second * 5 + bo.MaxElapsedTime = 30 * time.Second + require.NoError(tb, backoff.Retry(func() error { return client.Ping(context.Background()).Err() - }), "couldn't ping DB") + }, bo), "couldn't ping DB") return client } diff --git a/internal/adapter/http_twhelp.go b/internal/adapter/twhelp_http.go similarity index 99% rename from internal/adapter/http_twhelp.go rename to internal/adapter/twhelp_http.go index 56d7e83..52314a2 100644 --- a/internal/adapter/http_twhelp.go +++ b/internal/adapter/twhelp_http.go @@ -124,6 +124,7 @@ func (t *TWHelpHTTP) GetExistingTribeByTag(ctx context.Context, versionCode, ser if err != nil { return domain.Tribe{}, err } + if len(tribes) == 0 { return domain.Tribe{}, domain.TribeTagNotFoundError{ VersionCode: versionCode, @@ -131,6 +132,7 @@ func (t *TWHelpHTTP) GetExistingTribeByTag(ctx context.Context, versionCode, ser Tag: tribeTag, } } + return t.convertTribeToDomain(tribes[0]), nil } diff --git a/internal/adapter/redis_village.go b/internal/adapter/village_redis_repository.go similarity index 77% rename from internal/adapter/redis_village.go rename to internal/adapter/village_redis_repository.go index d0a3bda..38bf52a 100644 --- a/internal/adapter/redis_village.go +++ b/internal/adapter/village_redis_repository.go @@ -12,12 +12,12 @@ import ( "github.com/redis/go-redis/v9" ) -type VillageRedis struct { +type VillageRedisRepository struct { client redis.UniversalClient } -func NewVillageRedis(client redis.UniversalClient) *VillageRedis { - return &VillageRedis{client: client} +func NewVillageRedisRepository(client redis.UniversalClient) *VillageRedisRepository { + return &VillageRedisRepository{client: client} } const translateVillageCoordsParamsExp = 72 * time.Hour @@ -31,7 +31,7 @@ type translateVillageCoordsParamsRedis struct { SHA256 string } -func (v *VillageRedis) SaveTranslateCoordsParams(ctx context.Context, params domain.TranslateVillageCoordsParams) error { +func (v *VillageRedisRepository) SaveTranslateCoordsParams(ctx context.Context, params domain.TranslateVillageCoordsParams) error { buf := bytes.NewBuffer(nil) if err := gob.NewEncoder(buf).Encode(translateVillageCoordsParamsRedis{ @@ -57,7 +57,7 @@ func (v *VillageRedis) SaveTranslateCoordsParams(ctx context.Context, params dom return nil } -func (v *VillageRedis) GetTranslateCoordsParams(ctx context.Context, sha256Hash string) (domain.TranslateVillageCoordsParams, error) { +func (v *VillageRedisRepository) GetTranslateCoordsParams(ctx context.Context, sha256Hash string) (domain.TranslateVillageCoordsParams, error) { b, err := v.client.Get(ctx, v.buildTranslateCoordsParamsKey(sha256Hash)).Bytes() if errors.Is(err, redis.Nil) { return domain.TranslateVillageCoordsParams{}, domain.TranslateVillageCoordsParamsNotFoundError{ @@ -89,6 +89,6 @@ func (v *VillageRedis) GetTranslateCoordsParams(ctx context.Context, sha256Hash return res, nil } -func (v *VillageRedis) buildTranslateCoordsParamsKey(sha256Hash string) string { +func (v *VillageRedisRepository) buildTranslateCoordsParamsKey(sha256Hash string) string { return "translate_village_coords_params_" + sha256Hash } diff --git a/internal/adapter/redis_village_test.go b/internal/adapter/village_redis_repository_test.go similarity index 85% rename from internal/adapter/redis_village_test.go rename to internal/adapter/village_redis_repository_test.go index d5fe2f2..1ead744 100644 --- a/internal/adapter/redis_village_test.go +++ b/internal/adapter/village_redis_repository_test.go @@ -10,14 +10,14 @@ import ( "github.com/stretchr/testify/require" ) -func TestVillageRedis_SaveTranslateCoordsParams_GetTranslateCoordsParams(t *testing.T) { +func TestVillageRedisRepository_SaveTranslateCoordsParams_GetTranslateCoordsParams(t *testing.T) { t.Parallel() if testing.Short() { t.Skip("skipping long-running test") } - repo := adapter.NewVillageRedis(newRedisClient(t)) + repo := adapter.NewVillageRedisRepository(newRedisClient(t)) params, err := domain.NewTranslateVillageCoordsParams("pl", "pl181", "123|123 898|123", 20) require.NoError(t, err)