feat(postgres): start implementing new generic orm model
Some checks failed
Gitea Build Check / Build (push) Failing after 5m46s

Signed-off-by: soxx <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-25 09:09:18 +02:00
parent 31c4249129
commit 5aac1f2916
10 changed files with 75 additions and 67 deletions

View File

@ -23,8 +23,8 @@ func TestCreateAnthrovePostNode(t *testing.T) {
// Setup Tests // Setup Tests
validPost := &models.Post{ validPost := &models.Post{
BaseModel: models.BaseModel{ BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: fmt.Sprintf("%025s", "1"), ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
}, },
Rating: "safe", Rating: "safe",
} }
@ -84,8 +84,8 @@ func TestGetPostByAnthroveID(t *testing.T) {
// Setup Tests // Setup Tests
post := &models.Post{ post := &models.Post{
BaseModel: models.BaseModel{ BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: fmt.Sprintf("%025s", "1"), ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
}, },
Rating: "safe", Rating: "safe",
} }
@ -112,7 +112,7 @@ func TestGetPostByAnthroveID(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: models.AnthrovePostID(post.ID), anthrovePostID: post.ID,
}, },
want: post, want: post,
wantErr: false, wantErr: false,
@ -163,8 +163,8 @@ func TestGetPostBySourceURL(t *testing.T) {
// Setup Tests // Setup Tests
post := &models.Post{ post := &models.Post{
BaseModel: models.BaseModel{ BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: fmt.Sprintf("%025s", "1"), ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
}, },
Rating: "safe", Rating: "safe",
} }
@ -175,7 +175,9 @@ func TestGetPostBySourceURL(t *testing.T) {
} }
source := models.Source{ source := models.Source{
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")}, BaseModel: models.BaseModel[models.AnthroveSourceID]{
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
},
DisplayName: "e621", DisplayName: "e621",
Domain: "e621.net", Domain: "e621.net",
Icon: "https://e621.net/icon.ico", Icon: "https://e621.net/icon.ico",
@ -260,8 +262,8 @@ func TestGetPostBySourceID(t *testing.T) {
// Setup Tests // Setup Tests
post := &models.Post{ post := &models.Post{
BaseModel: models.BaseModel{ BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: fmt.Sprintf("%025s", "1"), ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
}, },
Rating: "safe", Rating: "safe",
} }
@ -272,7 +274,9 @@ func TestGetPostBySourceID(t *testing.T) {
} }
source := models.Source{ source := models.Source{
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")}, BaseModel: models.BaseModel[models.AnthroveSourceID]{
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
},
DisplayName: "e621", DisplayName: "e621",
Domain: "e621.net", Domain: "e621.net",
Icon: "https://e621.net/icon.ico", Icon: "https://e621.net/icon.ico",
@ -283,7 +287,7 @@ func TestGetPostBySourceID(t *testing.T) {
t.Fatal("Could not create source", err) t.Fatal("Could not create source", err)
} }
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, models.AnthrovePostID(post.ID), models.AnthroveSourceDomain(source.Domain)) err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain))
if err != nil { if err != nil {
t.Fatal("Could not create source reference", err) t.Fatal("Could not create source reference", err)
} }
@ -306,7 +310,7 @@ func TestGetPostBySourceID(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
sourceID: models.AnthroveSourceID(source.ID), sourceID: source.ID,
}, },
want: post, want: post,
wantErr: false, wantErr: false,

View File

@ -20,7 +20,7 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr
// Establish the relationship // Establish the relationship
err = db.WithContext(ctx).Create(models.PostReference{ err = db.WithContext(ctx).Create(models.PostReference{
PostID: string(anthrovePostID), PostID: string(anthrovePostID),
SourceID: source.ID, SourceID: string(source.ID),
URL: string(sourceDomain), URL: string(sourceDomain),
}).Error }).Error

View File

@ -26,8 +26,8 @@ func TestCheckUserToPostLink(t *testing.T) {
} }
post := &models.Post{ post := &models.Post{
BaseModel: models.BaseModel{ BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: fmt.Sprintf("%025s", "1"), ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
}, },
Rating: "safe", Rating: "safe",
} }
@ -61,7 +61,7 @@ func TestCheckUserToPostLink(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
anthrovePostID: models.AnthrovePostID(post.ID), anthrovePostID: post.ID,
}, },
want: true, want: true,
wantErr: false, wantErr: false,
@ -83,7 +83,7 @@ func TestCheckUserToPostLink(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "123", anthroveUserID: "123",
anthrovePostID: models.AnthrovePostID(post.ID), anthrovePostID: post.ID,
}, },
want: false, want: false,
wantErr: false, wantErr: false,
@ -125,8 +125,8 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
// Setup Test // Setup Test
post := &models.Post{ post := &models.Post{
BaseModel: models.BaseModel{ BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: fmt.Sprintf("%025s", "1"), ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
}, },
Rating: "safe", Rating: "safe",
} }
@ -164,7 +164,7 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: models.AnthrovePostID(post.ID), anthrovePostID: post.ID,
sourceDomain: "e621.net", sourceDomain: "e621.net",
}, },
wantErr: false, wantErr: false,
@ -225,8 +225,8 @@ func TestEstablishUserToPostLink(t *testing.T) {
} }
post := &models.Post{ post := &models.Post{
BaseModel: models.BaseModel{ BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: fmt.Sprintf("%025s", "1"), ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
}, },
Rating: "safe", Rating: "safe",
} }
@ -254,7 +254,7 @@ func TestEstablishUserToPostLink(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
anthrovePostID: models.AnthrovePostID(post.ID), anthrovePostID: post.ID,
}, },
wantErr: false, wantErr: false,
}, },

View File

@ -35,8 +35,8 @@ func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, PostID models
} }
pgPost := models.Post{ pgPost := models.Post{
BaseModel: models.BaseModel{ BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: string(PostID), ID: PostID,
}, },
} }

View File

@ -21,8 +21,8 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
// Setup Test // Setup Test
post := &models.Post{ post := &models.Post{
BaseModel: models.BaseModel{ BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: fmt.Sprintf("%025s", "1"), ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
}, },
Rating: "safe", Rating: "safe",
} }

View File

@ -15,8 +15,8 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove
} }
user := models.User{ user := models.User{
BaseModel: models.BaseModel{ BaseModel: models.BaseModel[models.AnthroveUserID]{
ID: string(anthroveUserID), ID: anthroveUserID,
}, },
} }
@ -51,7 +51,7 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs
} }
source := models.Source{ source := models.Source{
BaseModel: models.BaseModel{ID: string(sourceID)}, BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: sourceID},
} }
if err := db.WithContext(ctx).Where(&source).First(&source).Error; err != nil { if err := db.WithContext(ctx).Where(&source).First(&source).Error; err != nil {
@ -62,8 +62,8 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs
} }
userSource := models.UserSource{ userSource := models.UserSource{
User: models.User{BaseModel: models.BaseModel{ID: string(anthroveUserID)}}, User: models.User{BaseModel: models.BaseModel[models.AnthroveUserID]{ID: anthroveUserID}},
SourceID: source.ID, SourceID: string(source.ID),
AccountUsername: accountUsername, AccountUsername: accountUsername,
AccountID: accountId, AccountID: accountId,
UserID: string(anthroveUserID), UserID: string(anthroveUserID),
@ -248,7 +248,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse
favoritePosts = append(favoritePosts, favoritePosts = append(favoritePosts,
models.Post{ models.Post{
BaseModel: models.BaseModel{ID: post.ID}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: post.ID},
Rating: post.Rating, Rating: post.Rating,
}) })
} }

View File

@ -72,7 +72,9 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
// Setup Test // Setup Test
source := &models.Source{ source := &models.Source{
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")}, BaseModel: models.BaseModel[models.AnthroveSourceID]{
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
},
DisplayName: "e621", DisplayName: "e621",
Domain: "e621.net", Domain: "e621.net",
Icon: "icon.e621.net", Icon: "icon.e621.net",
@ -102,7 +104,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceID: models.AnthroveSourceID(source.ID), sourceID: source.ID,
userID: "e1", userID: "e1",
username: "marius", username: "marius",
}, },
@ -114,7 +116,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "2", anthroveUserID: "2",
sourceID: models.AnthroveSourceID(source.ID), sourceID: source.ID,
userID: "e1", userID: "e1",
username: "marius", username: "marius",
}, },
@ -126,7 +128,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "", anthroveUserID: "",
sourceID: models.AnthroveSourceID(source.ID), sourceID: source.ID,
userID: "e1", userID: "e1",
username: "marius", username: "marius",
}, },
@ -150,7 +152,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceID: models.AnthroveSourceID(source.ID), sourceID: source.ID,
userID: "", userID: "",
username: "marius", username: "marius",
}, },
@ -162,7 +164,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceID: models.AnthroveSourceID(source.ID), sourceID: source.ID,
userID: "aa", userID: "aa",
username: "", username: "",
}, },
@ -255,7 +257,9 @@ func TestGetUserSourceBySourceID(t *testing.T) {
} }
source := &models.Source{ source := &models.Source{
BaseModel: models.BaseModel{ID: expectedResult["e621"].Source.ID}, BaseModel: models.BaseModel[models.AnthroveSourceID]{
ID: expectedResult["e621"].Source.ID,
},
DisplayName: expectedResult["e621"].Source.DisplayName, DisplayName: expectedResult["e621"].Source.DisplayName,
Domain: expectedResult["e621"].Source.Domain, Domain: expectedResult["e621"].Source.Domain,
} }
@ -289,7 +293,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceID: models.AnthroveSourceID(source.ID), sourceID: source.ID,
}, },
want: expectedResult, want: expectedResult,
wantErr: false, wantErr: false,
@ -300,7 +304,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "22", anthroveUserID: "22",
sourceID: models.AnthroveSourceID(source.ID), sourceID: source.ID,
}, },
want: nil, want: nil,
wantErr: true, wantErr: true,
@ -322,7 +326,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "", anthroveUserID: "",
sourceID: models.AnthroveSourceID(source.ID), sourceID: source.ID,
}, },
want: nil, want: nil,
wantErr: true, wantErr: true,
@ -377,28 +381,28 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
expectedResultPosts := []models.Post{ expectedResultPosts := []models.Post{
{ {
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post1")}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post1"))},
Rating: "safe", Rating: "safe",
}, },
{ {
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post2")}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post2"))},
Rating: "safe", Rating: "safe",
}, },
{ {
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post3")}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post3"))},
Rating: "explicit", Rating: "explicit",
}, },
{ {
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post4")}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post4"))},
Rating: "explicit", Rating: "explicit",
}, },
{ {
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post5")}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post5"))},
Rating: "questionable", Rating: "questionable",
}, },
{ {
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post6")}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post6"))},
Rating: "safe", Rating: "safe",
}, },
} }
@ -511,27 +515,27 @@ func TestGetUserFavoritesCount(t *testing.T) {
expectedResultPosts := []models.Post{ expectedResultPosts := []models.Post{
{ {
BaseModel: models.BaseModel{ID: "Post1"}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post1"},
Rating: "safe", Rating: "safe",
}, },
{ {
BaseModel: models.BaseModel{ID: "Post2"}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post2"},
Rating: "safe", Rating: "safe",
}, },
{ {
BaseModel: models.BaseModel{ID: "Post3"}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post3"},
Rating: "explicit", Rating: "explicit",
}, },
{ {
BaseModel: models.BaseModel{ID: "Post4"}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post4"},
Rating: "explicit", Rating: "explicit",
}, },
{ {
BaseModel: models.BaseModel{ID: "Post5"}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post5"},
Rating: "questionable", Rating: "questionable",
}, },
{ {
BaseModel: models.BaseModel{ID: "Post6"}, BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post6"},
Rating: "safe", Rating: "safe",
}, },
} }
@ -615,7 +619,7 @@ func TestGetUserSourceLinks(t *testing.T) {
// Setup Test // Setup Test
eSource := &models.Source{ eSource := &models.Source{
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")}, BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1"))},
DisplayName: "e621", DisplayName: "e621",
Domain: "e621.net", Domain: "e621.net",
} }
@ -625,7 +629,7 @@ func TestGetUserSourceLinks(t *testing.T) {
} }
faSource := &models.Source{ faSource := &models.Source{
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "2")}, BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "2"))},
DisplayName: "fa", DisplayName: "fa",
Domain: "fa.net", Domain: "fa.net",
} }
@ -652,11 +656,11 @@ func TestGetUserSourceLinks(t *testing.T) {
}, },
} }
err = CreateUserWithRelationToSource(ctx, gormDB, "1", models.AnthroveSourceID(eSource.ID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) err = CreateUserWithRelationToSource(ctx, gormDB, "1", eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = CreateUserWithRelationToSource(ctx, gormDB, "1", models.AnthroveSourceID(faSource.ID), expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) err = CreateUserWithRelationToSource(ctx, gormDB, "1", faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -714,9 +718,9 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
} }
posts := []models.Post{ posts := []models.Post{
{BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post1")}, Rating: "safe"}, {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post1"))}, Rating: "safe"},
{BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post2")}, Rating: "safe"}, {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post2"))}, Rating: "safe"},
{BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post3")}, Rating: "explicit"}, {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post3"))}, Rating: "explicit"},
} }
for _, post := range posts { for _, post := range posts {
@ -737,7 +741,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
} }
for i, tag := range tags { for i, tag := range tags {
err = CreateTagAndReferenceToPost(ctx, gormDB, models.AnthrovePostID(posts[i].ID), &tag) err = CreateTagAndReferenceToPost(ctx, gormDB, posts[i].ID, &tag)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -2,7 +2,7 @@ package models
// Post model // Post model
type Post struct { type Post struct {
BaseModel BaseModel[AnthrovePostID]
Rating Rating `gorm:"type:enum('safe','questionable','explicit')"` Rating Rating `gorm:"type:enum('safe','questionable','explicit')"`
Tags []Tag `gorm:"many2many:post_tags;"` Tags []Tag `gorm:"many2many:post_tags;"`
Favorites []UserFavorites `gorm:"foreignKey:PostID"` Favorites []UserFavorites `gorm:"foreignKey:PostID"`

View File

@ -2,7 +2,7 @@ package models
// Source model // Source model
type Source struct { type Source struct {
BaseModel BaseModel[AnthroveSourceID]
DisplayName string DisplayName string
Domain string `gorm:"not null;unique"` Domain string `gorm:"not null;unique"`
Icon string `gorm:"not null"` Icon string `gorm:"not null"`

View File

@ -2,7 +2,7 @@ package models
// User model // User model
type User struct { type User struct {
BaseModel BaseModel[AnthroveUserID]
Favorites []UserFavorites `gorm:"foreignKey:UserID"` Favorites []UserFavorites `gorm:"foreignKey:UserID"`
Sources []UserSource `gorm:"foreignKey:UserID"` Sources []UserSource `gorm:"foreignKey:UserID"`
} }