otter-space-sdk/pkg/database/userSource_test.go
SoXX 2391bad68e
All checks were successful
Gitea Build Check / Build (push) Successful in 2m33s
feat(version): updated to version 4
2024-08-29 15:02:39 +02:00

499 lines
11 KiB
Go

package database
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"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"
"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
}