1
0
mirror of https://github.com/xorcare/testing-go-code-with-postgres.git synced 2025-06-30 23:23:40 +02:00

Publish an example testing go code with Postgres

This commit is contained in:
Vasiliy Vasilyuk
2023-07-05 01:31:59 +03:00
parent 7e6770223e
commit b346272f7f
15 changed files with 559 additions and 0 deletions

77
user_repository_test.go Normal file
View File

@ -0,0 +1,77 @@
// Copyright (c) 2023 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
import (
"context"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/xorcare/testing-go-code-with-postgres/testingpg"
)
func TestUserRepository_CreateUser(t *testing.T) {
newFullyFiledUser := func() User {
return User{
ID: uuid.New(),
Username: "gopher",
CreatedAt: time.Now().Truncate(time.Microsecond),
}
}
t.Run("Successfully created a User", func(t *testing.T) {
// Arrange
postgres := testingpg.New(t)
repo := NewUserRepository(postgres.PgxPool())
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) {
// Arrange
postgres := testingpg.New(t)
repo := NewUserRepository(postgres.PgxPool())
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 TestUserRepository_ReadUser(t *testing.T) {
t.Run("Get an error if the user does not exist", func(t *testing.T) {
// Arrange
postgres := testingpg.New(t)
repo := NewUserRepository(postgres.PgxPool())
// Act
_, err := repo.ReadUser(context.Background(), uuid.New())
// Assert
require.Error(t, err)
})
}