feat(test): added post, tags & fixed issues
All checks were successful
Gitea Build Check / Build (push) Successful in 2m0s

fully tested post & tags. Also found bugs with the tests. Those are now fixed.
This commit is contained in:
SoXX 2024-08-14 13:43:11 +02:00
parent 7632daee84
commit 8f6ca691bf
6 changed files with 810 additions and 20 deletions

View File

@ -82,6 +82,8 @@ func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorit
return nil return nil
} }
// UpdateUserFavorite updates the user post information in the database.
// Only supports the undulation of userFavorites, for this set the DeletedAt.Valid to false
func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error { func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserFavorite") ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserFavorite")
defer span.End() defer span.End()
@ -104,11 +106,11 @@ func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty}) return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty})
} }
if !userFav.DeletedAt.Valid { if userFav.DeletedAt.Valid == true {
return nil return nil
} }
result := client.WithContext(ctx).Model(&userFav).Update("deleted_at", gorm.DeletedAt{}) result := client.WithContext(ctx).Unscoped().Model(&models.UserFavorite{}).Where("id", userFav.ID).Update("deleted_at", nil)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
@ -184,7 +186,7 @@ func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDToShort}) return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDToShort})
} }
result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id) result := client.WithContext(ctx).Delete(&userFavorite, id)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})

View File

@ -0,0 +1,761 @@
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 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
}

View File

@ -2,7 +2,8 @@
CREATE TYPE Rating AS ENUM ( CREATE TYPE Rating AS ENUM (
'safe', 'safe',
'questionable', 'questionable',
'explicit' 'explicit',
'unknown'
); );
CREATE TYPE TagType AS ENUM ( CREATE TYPE TagType AS ENUM (

View File

@ -3,14 +3,15 @@ package database
import ( import (
"context" "context"
"fmt" "fmt"
"reflect"
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/test" "git.anthrove.art/Anthrove/otter-space-sdk/v2/test"
"go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"gorm.io/gorm" "gorm.io/gorm"
"reflect"
"testing"
"time"
) )
func TestCreatePost(t *testing.T) { func TestCreatePost(t *testing.T) {
@ -110,7 +111,7 @@ func TestCreatePostInBatch(t *testing.T) {
// -- -- Setup Tests // -- -- Setup Tests
// -- Create Posts to test with // -- Create Posts to test with
validPosts := test.GenerateRandomPosts(5) validPosts := test.GenerateRandomPosts(20)
// -- // --
// -- -- Tests // -- -- Tests

View File

@ -2,8 +2,8 @@ package models
type UserFavorite struct { type UserFavorite struct {
BaseModel[UserFavoriteID] BaseModel[UserFavoriteID]
UserID string `json:"user_id"` UserID UserID `json:"user_id"`
PostID string `json:"post_id"` PostID PostID `json:"post_id"`
UserSourceID UserSourceID `json:"user_source_id"` UserSourceID UserSourceID `json:"user_source_id"`
UserSource UserSource `json:"-" gorm:"foreignKey:ID;references:UserSourceID"` UserSource UserSource `json:"-" gorm:"foreignKey:ID;references:UserSourceID"`
} }

View File

@ -11,11 +11,11 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func GenerateRandomTags(numTags int) []models.Tag { func GenerateRandomTags(num int) []models.Tag {
var tags []models.Tag var tags []models.Tag
tagTypes := []models.TagType{"general", "species", "character", "artist", "lore", "meta", "invalid", "copyright"} tagTypes := []models.TagType{"general", "species", "character", "artist", "lore", "meta", "invalid", "copyright"}
for i := 0; i < numTags; i++ { for i := 0; i < num; i++ {
id, _ := gonanoid.New(10) id, _ := gonanoid.New(10)
tagName := spew.Sprintf("tag_name_%s", id) tagName := spew.Sprintf("tag_name_%s", id)
@ -32,10 +32,10 @@ func GenerateRandomTags(numTags int) []models.Tag {
return tags return tags
} }
func GenerateRandomTagGroups(tags []models.Tag, numGroups int) []models.TagGroup { func GenerateRandomTagGroups(tags []models.Tag, num int) []models.TagGroup {
var tagGroups []models.TagGroup var tagGroups []models.TagGroup
for i := 0; i < numGroups; i++ { for i := 0; i < num; i++ {
id, _ := gonanoid.New(10) id, _ := gonanoid.New(10)
groupName := fmt.Sprintf("tag_group_%s", id) groupName := fmt.Sprintf("tag_group_%s", id)
randomTag := tags[rand.Intn(len(tags))] randomTag := tags[rand.Intn(len(tags))]
@ -51,10 +51,10 @@ func GenerateRandomTagGroups(tags []models.Tag, numGroups int) []models.TagGroup
return tagGroups return tagGroups
} }
func GenerateRandomTagAlias(tags []models.Tag, numGroups int) []models.TagAlias { func GenerateRandomTagAlias(tags []models.Tag, num int) []models.TagAlias {
var tagAliases []models.TagAlias var tagAliases []models.TagAlias
for i := 0; i < numGroups; i++ { for i := 0; i < num; i++ {
id, _ := gonanoid.New(10) id, _ := gonanoid.New(10)
groupName := fmt.Sprintf("tag_alias_%s", id) groupName := fmt.Sprintf("tag_alias_%s", id)
randomTag := tags[rand.Intn(len(tags))] randomTag := tags[rand.Intn(len(tags))]
@ -70,10 +70,10 @@ func GenerateRandomTagAlias(tags []models.Tag, numGroups int) []models.TagAlias
return tagAliases return tagAliases
} }
func GenerateRandomSources(numTags int) []models.Source { func GenerateRandomSources(num int) []models.Source {
var sources []models.Source var sources []models.Source
for i := 0; i < numTags; i++ { for i := 0; i < num; i++ {
id, _ := gonanoid.New(10) id, _ := gonanoid.New(10)
displayName, _ := gonanoid.New(10) displayName, _ := gonanoid.New(10)
domain, _ := gonanoid.New(10) domain, _ := gonanoid.New(10)
@ -100,11 +100,11 @@ func GenerateRandomSources(numTags int) []models.Source {
return sources return sources
} }
func GenerateRandomPosts(numTags int) []models.Post { func GenerateRandomPosts(num int) []models.Post {
var sources []models.Post var sources []models.Post
ratings := []models.Rating{"safe", "explicit", "questionable", "unknown"} ratings := []models.Rating{"safe", "explicit", "questionable", "unknown"}
for i := 0; i < numTags; i++ { for i := 0; i < num; i++ {
id, _ := gonanoid.New(10) id, _ := gonanoid.New(10)
id = spew.Sprintf("source_name_%s", id) id = spew.Sprintf("source_name_%s", id)
rating := ratings[rand.Intn(len(ratings))] rating := ratings[rand.Intn(len(ratings))]
@ -124,3 +124,28 @@ func GenerateRandomPosts(numTags int) []models.Post {
return sources return sources
} }
func GenerateRandomUserFavorites(userID models.UserID, postID models.PostID, userSourceID models.UserSourceID, num int) []models.UserFavorite {
var userFavorites []models.UserFavorite
for i := 0; i < num; i++ {
id, _ := gonanoid.New(6)
id = spew.Sprintf("user_favorite_name_%s", id)
source := models.UserFavorite{
BaseModel: models.BaseModel[models.UserFavoriteID]{
ID: models.UserFavoriteID(id),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
PostID: postID,
UserSourceID: userSourceID,
}
userFavorites = append(userFavorites, source)
}
return userFavorites
}