|
| 1 | +package clients |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "rebrainme/gotest/internal/entities" |
| 11 | + "rebrainme/gotest/test/fixtures" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRepositoryDB_InsertOrUpdateClient(t *testing.T) { |
| 15 | + if testing.Short() { |
| 16 | + t.Skip(skipTestMessage) |
| 17 | + } |
| 18 | + |
| 19 | + var ( |
| 20 | + req = require.New(t) |
| 21 | + ctx = context.Background() |
| 22 | + repo, psqlClient = getTestRepoAndClient(req) |
| 23 | + ) |
| 24 | + defer func() { |
| 25 | + req.NoError(psqlClient.Close()) |
| 26 | + }() |
| 27 | + |
| 28 | + fixtures.ExecuteFixture(psqlClient, fixtures.CleanupFixture{}) |
| 29 | + |
| 30 | + test := func(client entities.Client) func(t *testing.T) { |
| 31 | + return func(t *testing.T) { |
| 32 | + err := repo.InsertOrUpdateClient(ctx, client) |
| 33 | + req.NoError(err) |
| 34 | + |
| 35 | + var model clientModel |
| 36 | + row := psqlClient.QueryRowx(fmt.Sprintf("SELECT * FROM %s WHERE email = $1", tableClients), client.Email) |
| 37 | + err = row.StructScan(&model) |
| 38 | + req.NoError(err) |
| 39 | + req.Equal(client, buildClientEntity(model)) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + clientEmail := "[email protected]" |
| 44 | + |
| 45 | + t.Run("insert new client", test(entities.Client{Email: clientEmail, FullName: "John Doe", City: "Moscow"})) |
| 46 | + t.Run("update exist client", test(entities.Client{Email: clientEmail, FullName: "John Braun", City: "NY"})) |
| 47 | +} |
| 48 | + |
| 49 | +func TestRepositoryDB_FindClientsByEmails(t *testing.T) { |
| 50 | + if testing.Short() { |
| 51 | + t.Skip(skipTestMessage) |
| 52 | + } |
| 53 | + |
| 54 | + var ( |
| 55 | + req = require.New(t) |
| 56 | + ctx = context.Background() |
| 57 | + repo, psqlClient = getTestRepoAndClient(req) |
| 58 | + ) |
| 59 | + defer func() { |
| 60 | + req.NoError(psqlClient.Close()) |
| 61 | + }() |
| 62 | + |
| 63 | + fixtures.ExecuteFixture(psqlClient, fixtures.CleanupFixture{}) |
| 64 | + |
| 65 | + clients := []entities.Client{ |
| 66 | + { Email: "[email protected]", FullName: "John Doe", City: "Moscow"}, |
| 67 | + { Email: "[email protected]", FullName: "Alan Walker", City: "NY"}, |
| 68 | + { Email: "[email protected]", FullName: "Bob Marley", City: "London"}, |
| 69 | + } |
| 70 | + for _, client := range clients { |
| 71 | + err := repo.InsertOrUpdateClient(ctx, client) |
| 72 | + req.NoError(err) |
| 73 | + } |
| 74 | + |
| 75 | + _, err := psqlClient.Exec(fmt.Sprintf("UPDATE %s SET deleted_at = NOW() WHERE email = $1", tableClients), clients[2].Email) |
| 76 | + req.NoError(err) |
| 77 | + |
| 78 | + test := func(emails []string, want []entities.Client) func(t *testing.T) { |
| 79 | + return func(t *testing.T) { |
| 80 | + actual, err := repo.FindClientsByEmails(ctx, emails) |
| 81 | + req.NoError(err) |
| 82 | + req.Len(actual, len(want)) |
| 83 | + req.Equal(want, actual) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + t.Run("get exists clients", test([]string{clients[0].Email, clients[1].Email}, []entities.Client{clients[0], clients[1]})) |
| 88 | + t.Run("get deleted client", test([]string{clients[2].Email}, []entities.Client(nil))) |
| 89 | + t.Run("get all clients", test([]string{clients[0].Email, clients[1].Email, clients[2].Email}, []entities.Client{clients[0], clients[1]})) |
| 90 | +} |
0 commit comments