You've already forked testing-go-code-with-postgres
mirror of
https://github.com/xorcare/testing-go-code-with-postgres.git
synced 2025-06-30 23:23:40 +02:00
Add example of tests with transactional cleanup
This commit is contained in:
96
user_repository_with_transactional_cleanup_test.go
Normal file
96
user_repository_with_transactional_cleanup_test.go
Normal file
@ -0,0 +1,96 @@
|
||||
// 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"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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 Test_Transactional_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) {
|
||||
t.Parallel()
|
||||
|
||||
// Arrange
|
||||
db := testingpg.NewWithTransactionalCleanup(t)
|
||||
repo := rootpkg.NewUserRepository(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
|
||||
db := testingpg.NewWithTransactionalCleanup(t)
|
||||
repo := rootpkg.NewUserRepository(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_Transactional_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
|
||||
db := testingpg.NewWithTransactionalCleanup(t)
|
||||
repo := rootpkg.NewUserRepository(db)
|
||||
|
||||
// Act
|
||||
_, err := repo.ReadUser(context.Background(), uuid.New())
|
||||
|
||||
// Assert
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user