diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go index 5c268f8..f719704 100644 --- a/internal/postgres/relationships.go +++ b/internal/postgres/relationships.go @@ -37,7 +37,7 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr } func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error { - userFavorite := models.UserFavorite{ + userFavorite := models.UserFavorites{ UserID: string(anthroveUserID), PostID: string(anthrovePostID), } @@ -57,7 +57,7 @@ func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrov func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) { var count int64 - err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error + err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error if err != nil { return false, err } diff --git a/internal/postgres/user.go b/internal/postgres/user.go index 1b4e38f..9cdc2da 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -95,7 +95,7 @@ func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID mode } var count int64 - err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Count(&count).Error + err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Count(&count).Error if err != nil { log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID, @@ -223,10 +223,10 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveU } func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) { - var userFavorites []models.UserFavorite + var userFavorites []models.UserFavorites var favoritePosts []models.Post - err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error + err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error if err != nil { log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID, @@ -262,7 +262,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse } func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { - var userFavorites []models.UserFavorite + var userFavorites []models.UserFavorites err := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites).Error if err != nil { log.WithFields(log.Fields{ diff --git a/pkg/models/orm_test.go b/pkg/models/orm_test.go new file mode 100644 index 0000000..d18fd2b --- /dev/null +++ b/pkg/models/orm_test.go @@ -0,0 +1,55 @@ +package models + +import ( + "gorm.io/gorm" + "testing" + "time" +) + +func TestBaseModel_BeforeCreate(t *testing.T) { + type fields struct { + ID string + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt gorm.DeletedAt + } + type args struct { + db *gorm.DB + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "Test 1: Prefilled ID", + fields: fields{ + ID: "1", + }, + args: args{db: nil}, + wantErr: false, + }, + { + name: "Test 1: Autogenerate ID", + fields: fields{ + ID: "", + }, + args: args{db: nil}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + base := &BaseModel{ + ID: tt.fields.ID, + CreatedAt: tt.fields.CreatedAt, + UpdatedAt: tt.fields.UpdatedAt, + DeletedAt: tt.fields.DeletedAt, + } + if err := base.BeforeCreate(tt.args.db); (err != nil) != tt.wantErr { + t.Errorf("BeforeCreate() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/models/post.go b/pkg/models/post.go index bac8a50..9aef5e7 100644 --- a/pkg/models/post.go +++ b/pkg/models/post.go @@ -5,7 +5,7 @@ type Post struct { BaseModel Rating Rating `gorm:"type:enum('safe','questionable','explicit')"` Tags []Tag `gorm:"many2many:post_tags;"` - Favorites []UserFavorite `gorm:"foreignKey:PostID"` + Favorites []UserFavorites `gorm:"foreignKey:PostID"` References []PostReference `gorm:"foreignKey:PostID"` } diff --git a/pkg/models/postReference_test.go b/pkg/models/postReference_test.go new file mode 100644 index 0000000..232a695 --- /dev/null +++ b/pkg/models/postReference_test.go @@ -0,0 +1,42 @@ +package models + +import "testing" + +func TestPostReference_TableName(t *testing.T) { + type fields struct { + PostID string + SourceID string + URL string + SourcePostID string + FullFileURL string + PreviewFileURL string + SampleFileURL string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Test 1: PostReference", + fields: fields{}, + want: "PostReference", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + po := PostReference{ + PostID: tt.fields.PostID, + SourceID: tt.fields.SourceID, + URL: tt.fields.URL, + SourcePostID: tt.fields.SourcePostID, + FullFileURL: tt.fields.FullFileURL, + PreviewFileURL: tt.fields.PreviewFileURL, + SampleFileURL: tt.fields.SampleFileURL, + } + if got := po.TableName(); got != tt.want { + t.Errorf("TableName() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/models/post_test.go b/pkg/models/post_test.go new file mode 100644 index 0000000..c90852e --- /dev/null +++ b/pkg/models/post_test.go @@ -0,0 +1,38 @@ +package models + +import "testing" + +func TestPost_TableName(t *testing.T) { + type fields struct { + BaseModel BaseModel + Rating Rating + Tags []Tag + Favorites []UserFavorites + References []PostReference + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Test 1: Is name Post", + fields: fields{}, + want: "Post", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + po := Post{ + BaseModel: tt.fields.BaseModel, + Rating: tt.fields.Rating, + Tags: tt.fields.Tags, + Favorites: tt.fields.Favorites, + References: tt.fields.References, + } + if got := po.TableName(); got != tt.want { + t.Errorf("TableName() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/models/source_test.go b/pkg/models/source_test.go new file mode 100644 index 0000000..1a5ff8c --- /dev/null +++ b/pkg/models/source_test.go @@ -0,0 +1,40 @@ +package models + +import "testing" + +func TestSource_TableName(t *testing.T) { + type fields struct { + BaseModel BaseModel + DisplayName string + Domain string + Icon string + UserSources []UserSource + References []PostReference + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Test 1: Is name Source", + fields: fields{}, + want: "Source", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + so := Source{ + BaseModel: tt.fields.BaseModel, + DisplayName: tt.fields.DisplayName, + Domain: tt.fields.Domain, + Icon: tt.fields.Icon, + UserSources: tt.fields.UserSources, + References: tt.fields.References, + } + if got := so.TableName(); got != tt.want { + t.Errorf("TableName() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/models/tag_test.go b/pkg/models/tag_test.go new file mode 100644 index 0000000..6f66b18 --- /dev/null +++ b/pkg/models/tag_test.go @@ -0,0 +1,96 @@ +package models + +import "testing" + +func TestTagAlias_TableName(t *testing.T) { + type fields struct { + Name string + TagID string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Test 1: Is Name TagAlias", + fields: fields{}, + want: "TagAlias", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ta := TagAlias{ + Name: tt.fields.Name, + TagID: tt.fields.TagID, + } + if got := ta.TableName(); got != tt.want { + t.Errorf("TableName() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestTagGroup_TableName(t *testing.T) { + type fields struct { + Name string + TagID string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Test 1: Is name TagGroup", + fields: fields{}, + want: "TagGroup", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ta := TagGroup{ + Name: tt.fields.Name, + TagID: tt.fields.TagID, + } + if got := ta.TableName(); got != tt.want { + t.Errorf("TableName() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestTag_TableName(t *testing.T) { + type fields struct { + Name string + Type TagType + Aliases []TagAlias + Groups []TagGroup + Posts []Post + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Test 1: Is name Tag", + fields: fields{}, + want: "Tag", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ta := Tag{ + Name: tt.fields.Name, + Type: tt.fields.Type, + Aliases: tt.fields.Aliases, + Groups: tt.fields.Groups, + Posts: tt.fields.Posts, + } + if got := ta.TableName(); got != tt.want { + t.Errorf("TableName() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/models/user.go b/pkg/models/user.go index 96dd70e..7f723e6 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -3,8 +3,8 @@ package models // User model type User struct { BaseModel - Favorites []UserFavorite `gorm:"foreignKey:UserID"` - Sources []UserSource `gorm:"foreignKey:UserID"` + Favorites []UserFavorites `gorm:"foreignKey:UserID"` + Sources []UserSource `gorm:"foreignKey:UserID"` } func (User) TableName() string { diff --git a/pkg/models/userFavorite.go b/pkg/models/userFavorite.go index 8ccd106..3bebe91 100644 --- a/pkg/models/userFavorite.go +++ b/pkg/models/userFavorite.go @@ -2,13 +2,13 @@ package models import "time" -type UserFavorite struct { +type UserFavorites struct { UserID string `gorm:"primaryKey"` PostID string `gorm:"primaryKey"` CreatedAt time.Time } -func (UserFavorite) TableName() string { +func (UserFavorites) TableName() string { return "UserFavorites" } diff --git a/pkg/models/userFavorite_test.go b/pkg/models/userFavorite_test.go new file mode 100644 index 0000000..3b69143 --- /dev/null +++ b/pkg/models/userFavorite_test.go @@ -0,0 +1,37 @@ +package models + +import ( + "testing" + "time" +) + +func TestUserFavorite_TableName(t *testing.T) { + type fields struct { + UserID string + PostID string + CreatedAt time.Time + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Test 1: Is name UserFavorites", + fields: fields{}, + want: "UserFavorites", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + us := UserFavorites{ + UserID: tt.fields.UserID, + PostID: tt.fields.PostID, + CreatedAt: tt.fields.CreatedAt, + } + if got := us.TableName(); got != tt.want { + t.Errorf("TableName() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/models/userSource_test.go b/pkg/models/userSource_test.go new file mode 100644 index 0000000..ff8cd95 --- /dev/null +++ b/pkg/models/userSource_test.go @@ -0,0 +1,42 @@ +package models + +import "testing" + +func TestUserSource_TableName(t *testing.T) { + type fields struct { + User User + UserID string + Source Source + SourceID string + ScrapeTimeInterval string + AccountUsername string + AccountID string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Test 1: Is name UserSource", + fields: fields{}, + want: "UserSource", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + us := UserSource{ + User: tt.fields.User, + UserID: tt.fields.UserID, + Source: tt.fields.Source, + SourceID: tt.fields.SourceID, + ScrapeTimeInterval: tt.fields.ScrapeTimeInterval, + AccountUsername: tt.fields.AccountUsername, + AccountID: tt.fields.AccountID, + } + if got := us.TableName(); got != tt.want { + t.Errorf("TableName() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/models/user_test.go b/pkg/models/user_test.go new file mode 100644 index 0000000..fa2c511 --- /dev/null +++ b/pkg/models/user_test.go @@ -0,0 +1,34 @@ +package models + +import "testing" + +func TestUser_TableName(t *testing.T) { + type fields struct { + BaseModel BaseModel + Favorites []UserFavorites + Sources []UserSource + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Test 1: Is name User", + fields: fields{}, + want: "User", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + us := User{ + BaseModel: tt.fields.BaseModel, + Favorites: tt.fields.Favorites, + Sources: tt.fields.Sources, + } + if got := us.TableName(); got != tt.want { + t.Errorf("TableName() = %v, want %v", got, tt.want) + } + }) + } +}