-
Notifications
You must be signed in to change notification settings - Fork 3
/
user_repository_with_isolated_schema_test.go
134 lines (96 loc) · 2.82 KB
/
user_repository_with_isolated_schema_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2023-2024 Vasiliy Vasilyuk. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testing_go_code_with_postgres_test
import (
"context"
"database/sql"
"errors"
"testing"
"time"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/xorcare/testing-go-code-with-postgres/migrations"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
rootpkg "github.com/xorcare/testing-go-code-with-postgres"
"github.com/xorcare/testing-go-code-with-postgres/testingpg"
)
func migrateDatabaseSchema(t *testing.T, pg *testingpg.Postgres) {
source, err := iofs.New(migrations.FS, ".")
require.NoError(t, err)
mi, err := migrate.NewWithSourceInstance(
"iofs",
source,
pg.URL(),
)
require.NoError(t, err)
err = mi.Up()
if !errors.Is(err, migrate.ErrNoChange) {
require.NoError(t, err)
}
}
func Test_Schema_UserRepository_CreateUser(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
t.Parallel()
newFullyFiledUser := func() rootpkg.User {
return rootpkg.User{
ID: uuid.New(),
Username: "gopher",
CreatedAt: time.Now().Truncate(time.Microsecond),
}
}
t.Run("Successfully created a User", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
t.Parallel()
// Arrange
pg := testingpg.NewWithIsolatedSchema(t)
migrateDatabaseSchema(t, pg)
repo := rootpkg.NewUserRepository(pg.DB())
user := newFullyFiledUser()
// Act
err := repo.CreateUser(context.Background(), user)
// Assert
require.NoError(t, err)
gotUser, err := repo.ReadUser(context.Background(), user.ID)
require.NoError(t, err)
require.Equal(t, user, gotUser)
})
t.Run("Cannot create a user with the same ID", func(t *testing.T) {
t.Parallel()
// Arrange
pg := testingpg.NewWithIsolatedSchema(t)
migrateDatabaseSchema(t, pg)
repo := rootpkg.NewUserRepository(pg.DB())
user := newFullyFiledUser()
err := repo.CreateUser(context.Background(), user)
require.NoError(t, err)
// Act
err = repo.CreateUser(context.Background(), user)
// Assert
require.Error(t, err)
require.Contains(t, err.Error(), "duplicate key value violates unique constraint")
})
}
func Test_Schema_UserRepository_ReadUser(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
t.Parallel()
t.Run("Get an error if the user does not exist", func(t *testing.T) {
t.Parallel()
// Arrange
pg := testingpg.NewWithIsolatedSchema(t)
migrateDatabaseSchema(t, pg)
repo := rootpkg.NewUserRepository(pg.DB())
// Act
_, err := repo.ReadUser(context.Background(), uuid.New())
// Assert
require.ErrorIs(t, err, sql.ErrNoRows)
})
}