feat(test): Add unit tests for CreateUser, GetUserByID, and DeleteUser functions with setup and teardown

This commit is contained in:
SoXX 2024-08-13 12:55:20 +02:00
parent 89ab1dca02
commit 176fdfb8b1

209
pkg/database/user_test.go Normal file
View File

@ -0,0 +1,209 @@
package database
import (
"context"
"fmt"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)
var (
validUser = models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
invalidUser = models.User{BaseModel: models.BaseModel[models.UserID]{ID: "invalid"}}
)
func TestCreateUser(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
type args struct {
ctx context.Context
user models.User
}
tests := []struct {
name string
args args
want models.User
wantErr bool
}{
{
name: "Test 01: Create Valid User",
args: args{
ctx: ctx,
user: validUser,
},
want: validUser,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateUser(tt.args.ctx, tt.args.user)
if (err != nil) != tt.wantErr {
t.Errorf("CreateUser() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkUserID(got, tt.want) {
t.Errorf("CreateUser() got = %v, want %v", got, tt.want)
}
})
}
}
func TestGetUserByID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatal(err)
}
// Setup Test
// Test
type args struct {
ctx context.Context
id models.UserID
}
tests := []struct {
name string
args args
want models.User
wantErr bool
}{
{
name: "Test 01: Get Valid User",
args: args{
ctx: ctx,
id: validUser.ID,
},
want: validUser,
wantErr: false,
},
{
name: "Test 02: Get not existing User",
args: args{
ctx: ctx,
id: invalidUser.ID,
},
want: models.User{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetUserByID(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserByID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkUserID(got, tt.want) {
t.Errorf("GetUserByID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeleteUser(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// Setup Test
_, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatal(err)
}
// Test
type args struct {
ctx context.Context
id models.UserID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Delete Existing User",
args: args{
ctx: ctx,
id: validUser.ID,
},
wantErr: false,
},
{
name: "Test 02: Delete not existing User",
args: args{
ctx: ctx,
id: validUser.ID,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteUser(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("DeleteUser() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkUserID(got models.User, want models.User) bool {
if got.ID != want.ID {
return false
}
return true
}