otter-space-sdk/pkg/database/user_test.go

220 lines
4.5 KiB
Go
Raw Normal View History

package database
import (
"context"
"fmt"
"testing"
2024-08-29 13:02:39 +00:00
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)
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)
// Setup Tests
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
// Tests
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,
},
{
name: "Test 02: Duplicate User",
args: args{
ctx: ctx,
user: validUser,
},
want: models.User{},
wantErr: true,
},
}
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)
// Setup Tests
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"}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
logger.Fatal(err)
}
// Tests
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 Tests
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
_, 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
}