Skip to content

Commit cd479ac

Browse files
committed
Rename function for isolated database connection
Other ways to clean the database in tests will be added soon, so the tests are being moved to a separate file.
1 parent f4aa95f commit cd479ac

5 files changed

+109
-103
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ total: (statements) 100.0%
7676
- Example of test database connection management
7777
in [testingpg](https://github.com/xorcare/testing-go-code-with-postgres/tree/main/testingpg)
7878
package.
79-
- Example of
80-
integration [tests](https://github.com/xorcare/testing-go-code-with-postgres/blob/main/user_repository_test.go).
79+
- Example of integration testing with isolated database for each testcase
80+
[](https://github.com/xorcare/testing-go-code-with-postgres/blob/main/user_repository_with_isolated_database_test.go).
8181
- And example
8282
of [GitHub Actions](https://github.com/xorcare/testing-go-code-with-postgres/blob/main/.github/workflows/go.yml)
8383
and [Gitlab CI](https://github.com/xorcare/testing-go-code-with-postgres/blob/main/.gitlab-ci.yml).

testingpg/testingpg.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type TestingT interface {
3131
Failed() bool
3232
}
3333

34-
func New(t TestingT) *Postgres {
34+
func NewWithIsolatedDatabase(t TestingT) *Postgres {
3535
return newPostgres(t).cloneFromReference()
3636
}
3737

testingpg/testingpg_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestNewPostgres(t *testing.T) {
2525
t.Parallel()
2626

2727
// Arrange
28-
postgres := testingpg.New(t)
28+
postgres := testingpg.NewWithIsolatedDatabase(t)
2929

3030
ctx := context.Background()
3131
dbPool, err := pgxpool.New(ctx, postgres.URL())
@@ -45,7 +45,7 @@ func TestNewPostgres(t *testing.T) {
4545
t.Parallel()
4646

4747
// Arrange
48-
postgres := testingpg.New(t)
48+
postgres := testingpg.NewWithIsolatedDatabase(t)
4949
ctx := context.Background()
5050

5151
// Act
@@ -63,8 +63,8 @@ func TestNewPostgres(t *testing.T) {
6363
t.Parallel()
6464

6565
// Arrange
66-
postgres1 := testingpg.New(t)
67-
postgres2 := testingpg.New(t)
66+
postgres1 := testingpg.NewWithIsolatedDatabase(t)
67+
postgres2 := testingpg.NewWithIsolatedDatabase(t)
6868

6969
ctx := context.Background()
7070

@@ -82,8 +82,8 @@ func TestNewPostgres(t *testing.T) {
8282
t.Parallel()
8383

8484
// Arrange
85-
postgres1 := testingpg.New(t)
86-
postgres2 := testingpg.New(t)
85+
postgres1 := testingpg.NewWithIsolatedDatabase(t)
86+
postgres2 := testingpg.NewWithIsolatedDatabase(t)
8787

8888
// Act
8989
url1 := postgres1.URL()

user_repository_test.go

+4-94
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,6 @@
1-
// Copyright (c) 2023 Vasiliy Vasilyuk. All rights reserved.
2-
// Use of this source code is governed by a BSD-style
3-
// license that can be found in the LICENSE file.
4-
51
package testing_go_code_with_postgres_test
62

7-
import (
8-
"context"
9-
"testing"
10-
"time"
11-
12-
"github.com/google/uuid"
13-
"github.com/stretchr/testify/require"
14-
15-
rootpkg "github.com/xorcare/testing-go-code-with-postgres"
16-
"github.com/xorcare/testing-go-code-with-postgres/testingpg"
17-
)
18-
19-
func TestUserRepository_CreateUser(t *testing.T) {
20-
if testing.Short() {
21-
t.Skip("skipping test in short mode")
22-
}
23-
24-
t.Parallel()
25-
26-
newFullyFiledUser := func() rootpkg.User {
27-
return rootpkg.User{
28-
ID: uuid.New(),
29-
Username: "gopher",
30-
CreatedAt: time.Now().Truncate(time.Microsecond),
31-
}
32-
}
33-
34-
t.Run("Successfully created a User", func(t *testing.T) {
35-
t.Parallel()
36-
37-
// Arrange
38-
postgres := testingpg.New(t)
39-
repo := rootpkg.NewUserRepository(postgres.DB())
40-
41-
user := newFullyFiledUser()
42-
43-
// Act
44-
err := repo.CreateUser(context.Background(), user)
45-
46-
// Assert
47-
require.NoError(t, err)
48-
49-
gotUser, err := repo.ReadUser(context.Background(), user.ID)
50-
require.NoError(t, err)
51-
52-
require.Equal(t, user, gotUser)
53-
})
54-
55-
t.Run("Cannot create a user with the same ID", func(t *testing.T) {
56-
t.Parallel()
57-
58-
// Arrange
59-
postgres := testingpg.New(t)
60-
repo := rootpkg.NewUserRepository(postgres.DB())
61-
62-
user := newFullyFiledUser()
63-
64-
err := repo.CreateUser(context.Background(), user)
65-
require.NoError(t, err)
66-
67-
// Act
68-
err = repo.CreateUser(context.Background(), user)
69-
70-
// Assert
71-
require.Error(t, err)
72-
require.Contains(t, err.Error(), "duplicate key value violates unique constraint")
73-
})
74-
}
75-
76-
func TestUserRepository_ReadUser(t *testing.T) {
77-
if testing.Short() {
78-
t.Skip("skipping test in short mode")
79-
}
80-
81-
t.Parallel()
82-
83-
t.Run("Get an error if the user does not exist", func(t *testing.T) {
84-
t.Parallel()
85-
86-
// Arrange
87-
postgres := testingpg.New(t)
88-
repo := rootpkg.NewUserRepository(postgres.DB())
89-
90-
// Act
91-
_, err := repo.ReadUser(context.Background(), uuid.New())
92-
93-
// Assert
94-
require.Error(t, err)
95-
})
96-
}
3+
// Since the repository contains examples of different approaches to
4+
// database cleanup, this file is empty. The specific tests can be found
5+
// in the following files:
6+
// - user_repository_with_isolated_database_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) 2023 Vasiliy Vasilyuk. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package testing_go_code_with_postgres_test
6+
7+
import (
8+
"context"
9+
"testing"
10+
"time"
11+
12+
"github.com/google/uuid"
13+
"github.com/stretchr/testify/require"
14+
15+
rootpkg "github.com/xorcare/testing-go-code-with-postgres"
16+
"github.com/xorcare/testing-go-code-with-postgres/testingpg"
17+
)
18+
19+
func TestUserRepository_CreateUser(t *testing.T) {
20+
if testing.Short() {
21+
t.Skip("skipping test in short mode")
22+
}
23+
24+
t.Parallel()
25+
26+
newFullyFiledUser := func() rootpkg.User {
27+
return rootpkg.User{
28+
ID: uuid.New(),
29+
Username: "gopher",
30+
CreatedAt: time.Now().Truncate(time.Microsecond),
31+
}
32+
}
33+
34+
t.Run("Successfully created a User", func(t *testing.T) {
35+
t.Parallel()
36+
37+
// Arrange
38+
postgres := testingpg.NewWithIsolatedDatabase(t)
39+
repo := rootpkg.NewUserRepository(postgres.DB())
40+
41+
user := newFullyFiledUser()
42+
43+
// Act
44+
err := repo.CreateUser(context.Background(), user)
45+
46+
// Assert
47+
require.NoError(t, err)
48+
49+
gotUser, err := repo.ReadUser(context.Background(), user.ID)
50+
require.NoError(t, err)
51+
52+
require.Equal(t, user, gotUser)
53+
})
54+
55+
t.Run("Cannot create a user with the same ID", func(t *testing.T) {
56+
t.Parallel()
57+
58+
// Arrange
59+
postgres := testingpg.NewWithIsolatedDatabase(t)
60+
repo := rootpkg.NewUserRepository(postgres.DB())
61+
62+
user := newFullyFiledUser()
63+
64+
err := repo.CreateUser(context.Background(), user)
65+
require.NoError(t, err)
66+
67+
// Act
68+
err = repo.CreateUser(context.Background(), user)
69+
70+
// Assert
71+
require.Error(t, err)
72+
require.Contains(t, err.Error(), "duplicate key value violates unique constraint")
73+
})
74+
}
75+
76+
func TestUserRepository_ReadUser(t *testing.T) {
77+
if testing.Short() {
78+
t.Skip("skipping test in short mode")
79+
}
80+
81+
t.Parallel()
82+
83+
t.Run("Get an error if the user does not exist", func(t *testing.T) {
84+
t.Parallel()
85+
86+
// Arrange
87+
postgres := testingpg.NewWithIsolatedDatabase(t)
88+
repo := rootpkg.NewUserRepository(postgres.DB())
89+
90+
// Act
91+
_, err := repo.ReadUser(context.Background(), uuid.New())
92+
93+
// Assert
94+
require.Error(t, err)
95+
})
96+
}

0 commit comments

Comments
 (0)