refactor: adapters - rename structs/files
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Dawid Wysokiński 2023-07-22 08:30:31 +02:00
parent 037d557eef
commit 1c8694f68e
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
8 changed files with 53 additions and 58 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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
}

View File

@ -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}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)