BREAKING CHANGE: V2 of thr SDK #12

Merged
fenpaws merged 124 commits from develop/postgresql into main 2024-07-01 20:46:28 +00:00
10 changed files with 75 additions and 67 deletions
Showing only changes of commit 5aac1f2916 - Show all commits

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -15,8 +15,8 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove
}
user := models.User{
BaseModel: models.BaseModel{
ID: string(anthroveUserID),
BaseModel: models.BaseModel[models.AnthroveUserID]{
ID: anthroveUserID,
},
}
@ -51,7 +51,7 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs
}
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 {
@ -62,8 +62,8 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs
}
userSource := models.UserSource{
User: models.User{BaseModel: models.BaseModel{ID: string(anthroveUserID)}},
SourceID: source.ID,
User: models.User{BaseModel: models.BaseModel[models.AnthroveUserID]{ID: anthroveUserID}},
SourceID: string(source.ID),
AccountUsername: accountUsername,
AccountID: accountId,
UserID: string(anthroveUserID),
@ -248,7 +248,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse
favoritePosts = append(favoritePosts,
models.Post{
BaseModel: models.BaseModel{ID: post.ID},
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: post.ID},
Rating: post.Rating,
})
}

View File

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

View File

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

View File

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

View File

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