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

762 lines
17 KiB
Go
Raw Normal View History

package database
import (
"context"
"fmt"
"reflect"
"testing"
"time"
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"
"gorm.io/gorm"
)
func TestCreateUserFavorite(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.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 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 User to test with
userID := models.UserID(models.UserID(fmt.Sprintf("%025s", "User1")))
validUser := models.User{
BaseModel: models.BaseModel[models.UserID]{
ID: userID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Favorites: nil,
Sources: []models.UserSource{
{
BaseModel: models.BaseModel[models.UserSourceID]{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
},
},
}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePost err: %v", err)
}
// --
// -- Create UserFavorite to test with
validFavorite := models.UserFavorite{
BaseModel: models.BaseModel[models.UserFavoriteID]{
ID: models.UserFavoriteID(fmt.Sprintf("%025s", "Favorite1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: validUser.ID,
PostID: validPost.ID,
UserSourceID: validUser.Sources[0].ID,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
userFav models.UserFavorite
}
tests := []struct {
name string
args args
want models.UserFavorite
wantErr bool
}{
{
name: "Test 01: Valid UserFavorite",
args: args{
ctx: ctx,
userFav: validFavorite,
},
want: validFavorite,
wantErr: false,
},
{
name: "Test 02: Duplicate UserFavorite",
args: args{
ctx: ctx,
userFav: validFavorite,
},
want: models.UserFavorite{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateUserFavorite(tt.args.ctx, tt.args.userFav)
if (err != nil) != tt.wantErr {
t.Errorf("CreateUserFavorite() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateUserFavorite() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateUserFavoriteInBatch(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.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 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 User to test with
userID := models.UserID(models.UserID(fmt.Sprintf("%025s", "User1")))
validUser := models.User{
BaseModel: models.BaseModel[models.UserID]{
ID: userID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Favorites: nil,
Sources: []models.UserSource{
{
BaseModel: models.BaseModel[models.UserSourceID]{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
},
},
}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePost err: %v", err)
}
// --
// -- Create UserFavorites to test with
validUserFavorite := test.GenerateRandomUserFavorites(validUser.ID, validPost.ID, validUser.Sources[0].ID, 10)
// --
// -- -- Tests
type args struct {
ctx context.Context
userFav []models.UserFavorite
batchSize int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid UserFavorite",
args: args{
ctx: ctx,
userFav: validUserFavorite,
batchSize: len(validUserFavorite),
},
wantErr: false,
},
{
name: "Test 02: Duplicate UserFavorite",
args: args{
ctx: ctx,
userFav: validUserFavorite,
batchSize: len(validUserFavorite),
},
wantErr: true,
},
{
name: "Test 03: Nil UserFavorite",
args: args{
ctx: ctx,
userFav: nil,
batchSize: len(validUserFavorite),
},
wantErr: true,
},
{
name: "Test 04: Empty UserFavorite",
args: args{
ctx: ctx,
userFav: []models.UserFavorite{},
batchSize: len(validUserFavorite),
},
wantErr: true,
},
{
name: "Test 08: Empty Batch Size",
args: args{
ctx: ctx,
userFav: []models.UserFavorite{},
batchSize: 0,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateUserFavoriteInBatch(tt.args.ctx, tt.args.userFav, tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("CreateUserFavoriteInBatch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestUpdateUserFavorite(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.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 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 User to test with
userID := models.UserID(models.UserID(fmt.Sprintf("%025s", "User1")))
validUser := models.User{
BaseModel: models.BaseModel[models.UserID]{
ID: userID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Favorites: nil,
Sources: []models.UserSource{
{
BaseModel: models.BaseModel[models.UserSourceID]{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
},
},
}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePost err: %v", err)
}
// --
// -- Create UserFavorite to test with
validFavorite := models.UserFavorite{
BaseModel: models.BaseModel[models.UserFavoriteID]{
ID: models.UserFavoriteID(fmt.Sprintf("%025s", "Favorite1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: validUser.ID,
PostID: validPost.ID,
UserSourceID: validUser.Sources[0].ID,
}
validFavorite, err = CreateUserFavorite(ctx, validFavorite)
if err != nil {
t.Fatalf("CreateUserFavorite err: %v", err)
}
// --
// -- Update UserFavorite
validUpdateFavorite := validFavorite
validFavorite.DeletedAt = gorm.DeletedAt{
Time: time.Time{},
Valid: false,
}
// --
// -- Delete UserFavorite
err = DeleteUserFavorite(ctx, validFavorite.ID)
if err != nil {
t.Fatalf("CreateUserFavorite err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
userFav models.UserFavorite
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Update for UserFavorite",
args: args{
ctx: ctx,
userFav: validUpdateFavorite,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := UpdateUserFavorite(tt.args.ctx, tt.args.userFav); (err != nil) != tt.wantErr {
t.Errorf("UpdateUserFavorite() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetUserFavoritesByID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.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 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 User to test with
userID := models.UserID(models.UserID(fmt.Sprintf("%025s", "User1")))
validUser := models.User{
BaseModel: models.BaseModel[models.UserID]{
ID: userID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Favorites: nil,
Sources: []models.UserSource{
{
BaseModel: models.BaseModel[models.UserSourceID]{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
},
},
}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePost err: %v", err)
}
// --
// -- Create UserFavorite to test with
validFavorite := models.UserFavorite{
BaseModel: models.BaseModel[models.UserFavoriteID]{
ID: models.UserFavoriteID(fmt.Sprintf("%025s", "Favorite1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: validUser.ID,
PostID: validPost.ID,
UserSourceID: validUser.Sources[0].ID,
}
validFavorite, err = CreateUserFavorite(ctx, validFavorite)
if err != nil {
t.Fatalf("CreateUserFavorite err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.UserFavoriteID
}
tests := []struct {
name string
args args
want models.UserFavorite
wantErr bool
}{
{
name: "Test 01: Valid UserFavoriteID",
args: args{
ctx: ctx,
id: validFavorite.ID,
},
want: validFavorite,
wantErr: false,
},
{
name: "Test 03: Empty UserFavoriteID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short UserFavoriteID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetUserFavoritesByID(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserFavoritesByID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkUserFavoriteID(got, tt.want) {
t.Errorf("GetUserFavoritesByID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeleteUserFavorite(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.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 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 User to test with
userID := models.UserID(models.UserID(fmt.Sprintf("%025s", "User1")))
validUser := models.User{
BaseModel: models.BaseModel[models.UserID]{
ID: userID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Favorites: nil,
Sources: []models.UserSource{
{
BaseModel: models.BaseModel[models.UserSourceID]{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
},
},
}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePost err: %v", err)
}
// --
// -- Create UserFavorite to test with
validFavorite := models.UserFavorite{
BaseModel: models.BaseModel[models.UserFavoriteID]{
ID: models.UserFavoriteID(fmt.Sprintf("%025s", "Favorite1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: validUser.ID,
PostID: validPost.ID,
UserSourceID: validUser.Sources[0].ID,
}
validFavorite, err = CreateUserFavorite(ctx, validFavorite)
if err != nil {
t.Fatalf("CreateUserFavorite err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.UserFavoriteID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Delete Valid UserFavorite",
args: args{
ctx: ctx,
id: validFavorite.ID,
},
wantErr: false,
},
{
name: "Test 02: Delete not existed UserFavorite",
args: args{
ctx: ctx,
id: validFavorite.ID,
},
wantErr: false,
},
{
name: "Test 03: Empty UserFavoriteID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short UserFavoriteID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteUserFavorite(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("DeleteUserFavorite() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkUserFavoriteID(got models.UserFavorite, want models.UserFavorite) bool {
if got.ID != want.ID {
return false
}
return true
}