feat(testing): first tests
Some checks failed
Gitea Build Check / Build (push) Failing after 44s

added the first set of new tests
This commit is contained in:
SoXX 2024-08-13 14:02:15 +02:00
parent 3fadaac69a
commit 146315f991
2 changed files with 518 additions and 15 deletions

View File

@ -0,0 +1,498 @@
package database
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"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"
"gorm.io/gorm"
)
func TestCreateUserSource(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
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUSerSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
// --
// -- -- Tests
type args struct {
ctx context.Context
userSource models.UserSource
}
tests := []struct {
name string
args args
want models.UserSource
wantErr bool
}{
{
name: "Test 01: Valid User Source",
args: args{
ctx: ctx,
userSource: validUSerSource,
},
want: validUSerSource,
wantErr: false,
},
{
name: "Test 02: Invalid User Source",
args: args{
ctx: ctx,
userSource: models.UserSource{},
},
want: models.UserSource{},
wantErr: true,
},
{
name: "Test 03: Duplicate User Source",
args: args{
ctx: ctx,
userSource: validUSerSource,
},
want: models.UserSource{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateUserSource(tt.args.ctx, tt.args.userSource)
if (err != nil) != tt.wantErr {
t.Errorf("CreateUserSource() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateUserSource() got = %v, want %v", got, tt.want)
}
})
}
}
func TestUpdateUserSource(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
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUserSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
validUserSource, err = CreateUserSource(ctx, validUserSource)
if err != nil {
t.Fatalf("CreateUserSource err: %v", err)
}
// --
// -- Create Updates models for UserSource
validUpdateSourceUser := validUserSource
validUpdateSourceUser.AccountID = "1234"
validUpdateSourceUser.ScrapeTimeInterval = "P2D"
validUpdateSourceUser.AccountUsername = "Update_Username"
validUpdateSourceUser.LastScrapeTime = time.Now()
validUpdateSourceUser.AccountValidate = true
invalidUpdateSourceUser := models.UserSource{}
// --
// -- -- Tests
type args struct {
ctx context.Context
userSource models.UserSource
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Update for UserSource",
args: args{
ctx: ctx,
userSource: validUpdateSourceUser,
},
wantErr: false,
},
{
name: "Test 02: Invalid Update for UserSource",
args: args{
ctx: ctx,
userSource: invalidUpdateSourceUser,
},
wantErr: true,
},
{
name: "Test 03: Empty ID for Update for UserSource",
args: args{
ctx: ctx,
userSource: models.UserSource{BaseModel: models.BaseModel[models.UserSourceID]{ID: ""}},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := UpdateUserSource(tt.args.ctx, tt.args.userSource); (err != nil) != tt.wantErr {
t.Errorf("UpdateUserSource() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetUserSourceByID(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
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUserSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
validUserSource, err = CreateUserSource(ctx, validUserSource)
if err != nil {
t.Fatalf("CreateUserSource err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.UserSourceID
}
tests := []struct {
name string
args args
want models.UserSource
wantErr bool
}{
{
name: "Test 01: Valid UserSource ID",
args: args{
ctx: ctx,
id: validUserSource.ID,
},
want: validUserSource,
wantErr: false,
},
{
name: "Test 03: Empty UserSourceID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short UserSourceID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetUserSourceByID(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserSourceByID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkUserSourceID(got, tt.want) {
t.Errorf("GetUserSourceByID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeleteUserSource(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
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUSerSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
validUSerSource, err = CreateUserSource(ctx, validUSerSource)
if err != nil {
t.Fatalf("CreateUserSource err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.UserSourceID
}
var tests = []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Delete Valid UserSource",
args: args{
ctx: ctx,
id: validUSerSource.ID,
},
wantErr: false,
},
{
name: "Test 02: Delete not existed UserSource",
args: args{
ctx: ctx,
id: validUSerSource.ID,
},
wantErr: false,
},
{
name: "Test 03: Empty UserSourceID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short UserSourceID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteUserSource(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("DeleteUserSource() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkUserSourceID(got models.UserSource, want models.UserSource) bool {
if got.ID != want.ID {
return false
}
return true
}

View File

@ -11,13 +11,7 @@ import (
"go.opentelemetry.io/otel" "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) { func TestCreateUser(t *testing.T) {
// Setup trow away container // Setup trow away container
ctx := context.Background() ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx) container, gormDB, err := test.StartPostgresContainer(ctx)
@ -35,6 +29,10 @@ func TestCreateUser(t *testing.T) {
defer container.Terminate(ctx) 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 { type args struct {
ctx context.Context ctx context.Context
user models.User user models.User
@ -54,6 +52,15 @@ func TestCreateUser(t *testing.T) {
want: validUser, want: validUser,
wantErr: false, wantErr: false,
}, },
{
name: "Test 02: Duplicate User",
args: args{
ctx: ctx,
user: validUser,
},
want: models.User{},
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -70,7 +77,6 @@ func TestCreateUser(t *testing.T) {
} }
func TestGetUserByID(t *testing.T) { func TestGetUserByID(t *testing.T) {
// Setup trow away container // Setup trow away container
ctx := context.Background() ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx) container, gormDB, err := test.StartPostgresContainer(ctx)
@ -88,14 +94,11 @@ func TestGetUserByID(t *testing.T) {
defer container.Terminate(ctx) defer container.Terminate(ctx)
validUser, err = CreateUser(ctx, validUser) // Setup Tests
if err != nil { validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
t.Fatal(err) invalidUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: "invalid"}}
}
// Setup Test // Tests
// Test
type args struct { type args struct {
ctx context.Context ctx context.Context
id models.UserID id models.UserID
@ -157,7 +160,9 @@ func TestDeleteUser(t *testing.T) {
logger.AddHook(hook) logger.AddHook(hook)
defer container.Terminate(ctx) defer container.Terminate(ctx)
// Setup Test
// Setup Tests
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
_, err = CreateUser(ctx, validUser) _, err = CreateUser(ctx, validUser)
if err != nil { if err != nil {