From 8a0229eb23766f93abefc6f82ade95268b822ba1 Mon Sep 17 00:00:00 2001 From: soxx Date: Sun, 23 Jun 2024 21:23:38 +0200 Subject: [PATCH] test(postgres): renamed functions to be more clear Signed-off-by: soxx --- internal/postgres/post_test.go | 8 +- internal/postgres/relationships.go | 6 +- internal/postgres/relationships_test.go | 18 ++-- internal/postgres/source.go | 12 +-- internal/postgres/source_test.go | 20 ++--- internal/postgres/tag.go | 4 +- internal/postgres/tag_test.go | 6 +- internal/postgres/user.go | 26 +++--- internal/postgres/user_test.go | 112 ++++++++++++------------ pkg/database/database.go | 46 +++++----- pkg/database/postgres.go | 44 +++++----- 11 files changed, 153 insertions(+), 149 deletions(-) diff --git a/internal/postgres/post_test.go b/internal/postgres/post_test.go index 87d2690..098fe2b 100644 --- a/internal/postgres/post_test.go +++ b/internal/postgres/post_test.go @@ -181,12 +181,12 @@ func TestGetPostBySourceURL(t *testing.T) { Icon: "https://e621.net/icon.ico", } - err = CreateSourceNode(ctx, gormDB, &source) + err = CreateSource(ctx, gormDB, &source) if err != nil { t.Fatal("Could not create source", err) } - err = EstablishAnthrovePostToSourceLink(ctx, gormDB, post.ID, source.Domain) + err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, source.Domain) if err != nil { t.Fatal("Could not create source reference", err) } @@ -278,12 +278,12 @@ func TestGetPostBySourceID(t *testing.T) { Icon: "https://e621.net/icon.ico", } - err = CreateSourceNode(ctx, gormDB, &source) + err = CreateSource(ctx, gormDB, &source) if err != nil { t.Fatal("Could not create source", err) } - err = EstablishAnthrovePostToSourceLink(ctx, gormDB, post.ID, source.Domain) + err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, source.Domain) if err != nil { t.Fatal("Could not create source reference", err) } diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go index 7f319e0..cade964 100644 --- a/internal/postgres/relationships.go +++ b/internal/postgres/relationships.go @@ -7,7 +7,7 @@ import ( "gorm.io/gorm" ) -func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrovePostID string, sourceDomain string) error { +func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthrovePostID string, sourceDomain string) error { var source models.Source var err error @@ -36,7 +36,7 @@ func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrov return nil } -func EstablishUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID string, anthrovePostID string) error { +func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID string, anthrovePostID string) error { userFavorite := models.UserFavorite{ UserID: string(anthroveUserID), PostID: string(anthrovePostID), @@ -55,7 +55,7 @@ func EstablishUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID st return nil } -func CheckUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID string, anthrovePostID string) (bool, error) { +func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID string, anthrovePostID string) (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 if err != nil { diff --git a/internal/postgres/relationships_test.go b/internal/postgres/relationships_test.go index 7d2bc56..4d49f65 100644 --- a/internal/postgres/relationships_test.go +++ b/internal/postgres/relationships_test.go @@ -37,7 +37,7 @@ func TestCheckUserToPostLink(t *testing.T) { t.Fatal(err) } - err = EstablishUserToPostLink(ctx, gormDB, "1", post.ID) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", post.ID) if err != nil { t.Fatal(err) } @@ -102,13 +102,13 @@ func TestCheckUserToPostLink(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := CheckUserToPostLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID) + got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID) if (err != nil) != tt.wantErr { - t.Errorf("CheckUserToPostLink() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("CheckReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr) return } if got != tt.want { - t.Errorf("CheckUserToPostLink() got = %v, want %v", got, tt.want) + t.Errorf("CheckReferenceBetweenUserAndPost() got = %v, want %v", got, tt.want) } }) } @@ -141,7 +141,7 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) { Domain: "e621.net", Icon: "icon.e621.net", } - err = CreateSourceNode(ctx, gormDB, source) + err = CreateSource(ctx, gormDB, source) if err != nil { t.Fatal(err) } @@ -202,8 +202,8 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := EstablishAnthrovePostToSourceLink(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.sourceDomain); (err != nil) != tt.wantErr { - t.Errorf("EstablishAnthrovePostToSourceLink() error = %v, wantErr %v", err, tt.wantErr) + if err := CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.sourceDomain); (err != nil) != tt.wantErr { + t.Errorf("CreateReferenceBetweenPostAndSource() error = %v, wantErr %v", err, tt.wantErr) } }) } @@ -291,8 +291,8 @@ func TestEstablishUserToPostLink(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := EstablishUserToPostLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID); (err != nil) != tt.wantErr { - t.Errorf("EstablishUserToPostLink() error = %v, wantErr %v", err, tt.wantErr) + if err := CreateReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID); (err != nil) != tt.wantErr { + t.Errorf("CreateReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr) } }) } diff --git a/internal/postgres/source.go b/internal/postgres/source.go index 5e4ecd5..ec889fd 100644 --- a/internal/postgres/source.go +++ b/internal/postgres/source.go @@ -9,8 +9,8 @@ import ( "gorm.io/gorm" ) -// CreateSourceNode creates a pgModels.Source -func CreateSourceNode(ctx context.Context, db *gorm.DB, anthroveSource *models.Source) error { +// CreateSource creates a pgModels.Source +func CreateSource(ctx context.Context, db *gorm.DB, anthroveSource *models.Source) error { if anthroveSource.Domain == "" { return fmt.Errorf("anthroveSource domain is required") @@ -31,8 +31,8 @@ func CreateSourceNode(ctx context.Context, db *gorm.DB, anthroveSource *models.S return nil } -// GetAllSourceNodes returns a list of all pgModels.Source -func GetAllSourceNodes(ctx context.Context, db *gorm.DB) ([]models.Source, error) { +// GetAllSource returns a list of all pgModels.Source +func GetAllSource(ctx context.Context, db *gorm.DB) ([]models.Source, error) { var sources []models.Source result := db.WithContext(ctx).Find(&sources) @@ -48,8 +48,8 @@ func GetAllSourceNodes(ctx context.Context, db *gorm.DB) ([]models.Source, error return sources, nil } -// GetSourceNodesByURL returns the first source it finds based on the domain -func GetSourceNodesByURL(ctx context.Context, db *gorm.DB, domain string) (*models.Source, error) { +// GetSourceByURL returns the first source it finds based on the domain +func GetSourceByURL(ctx context.Context, db *gorm.DB, domain string) (*models.Source, error) { var sources models.Source if domain == "" { diff --git a/internal/postgres/source_test.go b/internal/postgres/source_test.go index f0a3103..a6d7863 100644 --- a/internal/postgres/source_test.go +++ b/internal/postgres/source_test.go @@ -70,8 +70,8 @@ func TestCreateSourceNode(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := CreateSourceNode(tt.args.ctx, tt.args.db, tt.args.anthroveSource); (err != nil) != tt.wantErr { - t.Errorf("CreateSourceNode() error = %v, wantErr %v", err, tt.wantErr) + if err := CreateSource(tt.args.ctx, tt.args.db, tt.args.anthroveSource); (err != nil) != tt.wantErr { + t.Errorf("CreateSource() error = %v, wantErr %v", err, tt.wantErr) } }) } @@ -107,7 +107,7 @@ func TestGetAllSourceNodes(t *testing.T) { } for _, source := range sources { - err = CreateSourceNode(ctx, gormDB, &source) + err = CreateSource(ctx, gormDB, &source) if err != nil { t.Fatal(err) } @@ -136,13 +136,13 @@ func TestGetAllSourceNodes(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetAllSourceNodes(tt.args.ctx, tt.args.db) + got, err := GetAllSource(tt.args.ctx, tt.args.db) if (err != nil) != tt.wantErr { - t.Errorf("GetAllSourceNodes() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("GetAllSource() error = %v, wantErr %v", err, tt.wantErr) return } if !checkSourcesNode(got, tt.want) { - t.Errorf("GetAllSourceNodes() got = %v, want %v", got, tt.want) + t.Errorf("GetAllSource() got = %v, want %v", got, tt.want) } }) } @@ -165,7 +165,7 @@ func TestGetSourceNodesByURL(t *testing.T) { Icon: "icon.e621.net", } - err = CreateSourceNode(ctx, gormDB, source) + err = CreateSource(ctx, gormDB, source) if err != nil { t.Fatal(err) } @@ -215,13 +215,13 @@ func TestGetSourceNodesByURL(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetSourceNodesByURL(tt.args.ctx, tt.args.db, tt.args.domain) + got, err := GetSourceByURL(tt.args.ctx, tt.args.db, tt.args.domain) if (err != nil) != tt.wantErr { - t.Errorf("GetSourceNodesByURL() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("GetSourceByURL() error = %v, wantErr %v", err, tt.wantErr) return } if !checkSourceNode(got, tt.want) { - t.Errorf("GetSourceNodesByURL() got = %v, want %v", got, tt.want) + t.Errorf("GetSourceByURL() got = %v, want %v", got, tt.want) } }) } diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go index 885ffa3..4213b28 100644 --- a/internal/postgres/tag.go +++ b/internal/postgres/tag.go @@ -8,7 +8,7 @@ import ( "gorm.io/gorm" ) -func createTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error { +func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error { resultTag := db.WithContext(ctx).Where(tag).Create(tag) @@ -24,7 +24,7 @@ func createTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error { return nil } -func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID string, tag *models.Tag) error { +func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, PostID string, tag *models.Tag) error { if PostID == "" { return fmt.Errorf("PostID is empty") diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go index 00b0b46..d83d760 100644 --- a/internal/postgres/tag_test.go +++ b/internal/postgres/tag_test.go @@ -92,8 +92,8 @@ func TestCreateTagNodeWitRelation(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := CreateTagNodeWitRelation(tt.args.ctx, tt.args.db, tt.args.PostID, tt.args.tag); (err != nil) != tt.wantErr { - t.Errorf("CreateTagNodeWitRelation() error = %v, wantErr %v", err, tt.wantErr) + if err := CreateTagAndReferenceToPost(tt.args.ctx, tt.args.db, tt.args.PostID, tt.args.tag); (err != nil) != tt.wantErr { + t.Errorf("CreateTagAndReferenceToPost() error = %v, wantErr %v", err, tt.wantErr) } }) } @@ -126,7 +126,7 @@ func TestGetTags(t *testing.T) { } for _, tag := range tags { - err = createTag(ctx, gormDB, &tag) + err = CreateTag(ctx, gormDB, &tag) if err != nil { t.Fatal(err) } diff --git a/internal/postgres/user.go b/internal/postgres/user.go index fed56f9..91282d2 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -31,7 +31,7 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID string) error { return nil } -func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthroveUserID string, sourceDomain string, userID string, username string) error { +func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUserID string, sourceDomain string, userID string, username string) error { if anthroveUserID == "" || username == "" || userID == "" { return fmt.Errorf("anthroveUserID cannot be empty") @@ -143,19 +143,23 @@ func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models. return userSourceMap, nil } -func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) { - if anthroveUserID == "" || sourceDisplayName == "" { - return nil, fmt.Errorf("anthroveUserID or sourceDisplayName is empty") +func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID string) (map[string]models.UserSource, error) { + if anthroveUserID == "" { + return nil, fmt.Errorf("anthroveUserID cannot be empty") + } + + if sourceID == "" { + return nil, fmt.Errorf("sourceID cannot be empty") } var userSources []models.UserSource userSourceMap := make(map[string]models.UserSource) - err := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("display_name = ?", sourceDisplayName)).Where("user_id = ?", string(anthroveUserID)).First(&userSources).Error + err := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(&userSources).Error if err != nil { log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "source_display_name": sourceDisplayName, + "anthrove_user_id": anthroveUserID, + "source_id": sourceID, }).Error("database: failed to get specified user source link") return nil, err } @@ -182,8 +186,8 @@ func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID } log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "source_display_name": sourceDisplayName, + "anthrove_user_id": anthroveUserID, + "source_id": sourceID, }).Trace("database: got specified user source link") return userSourceMap, nil @@ -210,7 +214,7 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]string, error) { return userIDs, nil } -func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID string, skip int, limit int) (*models.FavoriteList, error) { +func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID string, skip int, limit int) (*models.FavoriteList, error) { var userFavorites []models.UserFavorite var favoritePosts []models.Post @@ -249,7 +253,7 @@ func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthrov return &models.FavoriteList{Posts: favoritePosts}, nil } -func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { +func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { var userFavorites []models.UserFavorite err := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites).Error if err != nil { diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go index cae2b0c..3adee0f 100644 --- a/internal/postgres/user_test.go +++ b/internal/postgres/user_test.go @@ -76,7 +76,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { Domain: "e621.net", Icon: "icon.e621.net", } - err = CreateSourceNode(ctx, gormDB, source) + err = CreateSource(ctx, gormDB, source) if err != nil { t.Fatal(err) } @@ -170,8 +170,8 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := CreateUserNodeWithSourceRelation(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceDomain, tt.args.userID, tt.args.username); (err != nil) != tt.wantErr { - t.Errorf("CreateUserNodeWithSourceRelation() error = %v, wantErr %v", err, tt.wantErr) + if err := CreateUserWithRelationToSource(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceDomain, tt.args.userID, tt.args.username); (err != nil) != tt.wantErr { + t.Errorf("CreateUserWithRelationToSource() error = %v, wantErr %v", err, tt.wantErr) } }) } @@ -232,7 +232,7 @@ func TestGetAllAnthroveUserIDs(t *testing.T) { } } -func TestGetSpecifiedUserSourceLink(t *testing.T) { +func TestGetUserSourceBySourceID(t *testing.T) { // Setup trow away container ctx := context.Background() container, gormDB, err := test.StartPostgresContainer(ctx) @@ -254,26 +254,27 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) { } source := &models.Source{ + BaseModel: models.BaseModel{ID: expectedResult["e621"].Source.ID}, DisplayName: expectedResult["e621"].Source.DisplayName, Domain: expectedResult["e621"].Source.Domain, } - err = CreateSourceNode(ctx, gormDB, source) + err = CreateSource(ctx, gormDB, source) if err != nil { t.Fatal(err) } - err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", source.Domain, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) + err = CreateUserWithRelationToSource(ctx, gormDB, "1", source.Domain, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) if err != nil { t.Fatal(err) } // Test type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - sourceDisplayName string + ctx context.Context + db *gorm.DB + anthroveUserID models.AnthroveUserID + sourceID string } tests := []struct { name string @@ -282,45 +283,45 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) { wantErr bool }{ { - name: "Test 1: Valid AnthroveUserID and SourceDisplayName", + name: "Test 1: Valid AnthroveUserID and sourceID", args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "1", - sourceDisplayName: "e621", + ctx: ctx, + db: gormDB, + anthroveUserID: "1", + sourceID: source.ID, }, want: expectedResult, wantErr: false, }, { - name: "Test 2: Invalid AnthroveUserID and valid SourceDisplayName", + name: "Test 2: Invalid AnthroveUserID and valid sourceID", args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "22", - sourceDisplayName: "e621", + ctx: ctx, + db: gormDB, + anthroveUserID: "22", + sourceID: source.ID, }, want: nil, wantErr: true, }, { - name: "Test 3: Valid AnthroveUserID and invalid SourceDisplayName", + name: "Test 3: Valid AnthroveUserID and invalid sourceID", args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "1", - sourceDisplayName: "fa", + ctx: ctx, + db: gormDB, + anthroveUserID: "1", + sourceID: "fa", }, want: nil, wantErr: true, }, { - name: "Test 4: No AnthroveUserID and Valid SourceDisplayName", + name: "Test 4: No AnthroveUserID and Valid sourceID", args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceDisplayName: "e621", + ctx: ctx, + db: gormDB, + anthroveUserID: "", + sourceID: source.ID, }, want: nil, wantErr: true, @@ -328,10 +329,10 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) { { name: "Test 5: Valid AnthroveUserID and No SourceDisplayName", args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "1", - sourceDisplayName: "", + ctx: ctx, + db: gormDB, + anthroveUserID: "1", + sourceID: "", }, want: nil, wantErr: true, @@ -339,10 +340,10 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) { { name: "Test 6: No AnthroveUserID and No SourceDisplayName", args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceDisplayName: "", + ctx: ctx, + db: gormDB, + anthroveUserID: "", + sourceID: "", }, want: nil, wantErr: true, @@ -350,18 +351,17 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetSpecifiedUserSourceLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceDisplayName) + got, err := GetUserSourceBySourceID(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID) if (err != nil) != tt.wantErr { - t.Errorf("GetSpecifiedUserSourceLink() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetSpecifiedUserSourceLink() got = %v, want %v", got, tt.want) + t.Errorf("GetUserSourceBySourceID() got = %v, want %v", got, tt.want) } }) } } - func TestGetUserFavoriteNodeWithPagination(t *testing.T) { // Setup trow away containert ctx := context.Background() @@ -420,7 +420,7 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { if err != nil { t.Fatal(err) } - err = EstablishUserToPostLink(ctx, gormDB, "1", expectedResultPost.ID) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", expectedResultPost.ID) if err != nil { t.Fatal(err) } @@ -479,13 +479,13 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetUserFavoriteNodeWithPagination(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.skip, tt.args.limit) + got, err := GetUserFavoriteWithPagination(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.skip, tt.args.limit) if (err != nil) != tt.wantErr { - t.Errorf("GetUserFavoriteNodeWithPagination() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("GetUserFavoriteWithPagination() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetUserFavoriteNodeWithPagination() got = %v, want %v", got, tt.want) + t.Errorf("GetUserFavoriteWithPagination() got = %v, want %v", got, tt.want) } }) } @@ -539,7 +539,7 @@ func TestGetUserFavoritesCount(t *testing.T) { if err != nil { t.Fatal(err) } - err = EstablishUserToPostLink(ctx, gormDB, "1", post.ID) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", post.ID) if err != nil { t.Fatal(err) } @@ -616,7 +616,7 @@ func TestGetUserSourceLinks(t *testing.T) { DisplayName: "e621", Domain: "e621.net", } - err = CreateSourceNode(ctx, gormDB, eSource) + err = CreateSource(ctx, gormDB, eSource) if err != nil { t.Fatal(err) } @@ -625,7 +625,7 @@ func TestGetUserSourceLinks(t *testing.T) { DisplayName: "fa", Domain: "fa.net", } - err = CreateSourceNode(ctx, gormDB, faSource) + err = CreateSource(ctx, gormDB, faSource) if err != nil { t.Fatal(err) } @@ -648,11 +648,11 @@ func TestGetUserSourceLinks(t *testing.T) { }, } - err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", eSource.Domain, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) + err = CreateUserWithRelationToSource(ctx, gormDB, "1", eSource.Domain, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) if err != nil { t.Fatal(err) } - err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", faSource.Domain, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) + err = CreateUserWithRelationToSource(ctx, gormDB, "1", faSource.Domain, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) if err != nil { t.Fatal(err) } @@ -720,7 +720,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { if err != nil { t.Fatal(err) } - err = EstablishUserToPostLink(ctx, gormDB, "1", post.ID) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", post.ID) if err != nil { t.Fatal(err) } @@ -733,7 +733,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { } for i, tag := range tags { - err = CreateTagNodeWitRelation(ctx, gormDB, posts[i].ID, &tag) + err = CreateTagAndReferenceToPost(ctx, gormDB, posts[i].ID, &tag) if err != nil { t.Fatal(err) } @@ -788,13 +788,13 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetUserTagNodeWitRelationToFavedPosts(tt.args.ctx, tt.args.db, tt.args.anthroveUserID) + got, err := GetUserTagWitRelationToFavedPosts(tt.args.ctx, tt.args.db, tt.args.anthroveUserID) if (err != nil) != tt.wantErr { - t.Errorf("GetUserTagNodeWitRelationToFavedPosts() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("GetUserTagWitRelationToFavedPosts() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetUserTagNodeWitRelationToFavedPosts() got = %v, want %v", got, tt.want) + t.Errorf("GetUserTagWitRelationToFavedPosts() got = %v, want %v", got, tt.want) } }) } diff --git a/pkg/database/database.go b/pkg/database/database.go index 76269bf..f0c74f0 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -9,26 +9,26 @@ type OtterSpace interface { // Connect establishes a connection to the database. Connect(ctx context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error - // AddUserWithRelationToSource adds a user with a relation to a source. - AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error + // CreateUserWithRelationToSource adds a user with a relation to a source. + CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error - // AddSource adds a new source to the database. - AddSource(ctx context.Context, anthroveSource *models.Source) error + // CreateSource adds a new source to the database. + CreateSource(ctx context.Context, anthroveSource *models.Source) error - // AddPost adds a new post to the database. - AddPost(ctx context.Context, anthrovePost *models.Post) error + // CreatePost adds a new post to the database. + CreatePost(ctx context.Context, anthrovePost *models.Post) error - // AddTagWithRelationToPost adds a tag with a relation to a post. - AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error + // CreateTagAndReferenceToPost adds a tag with a relation to a post. + CreateTagAndReferenceToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error - // LinkPostWithSource links a post with a source. - LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.PostReference) error + // CreateReferenceBetweenPostAndSource links a post with a source. + CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.PostReference) error - // LinkUserWithPost links a user with a post. - LinkUserWithPost(ctx context.Context, anthroveUser *models.User, anthrovePost *models.Post) error + // CreateReferenceBetweenUserAndPost links a user with a post. + CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUser *models.User, anthrovePost *models.Post) error - // CheckUserPostLink checks if a user-post link exists. - CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) + // CheckReferenceBetweenUserAndPost checks if a user-post link exists. + CheckReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) // GetPostByAnthroveID retrieves a post by its Anthrove ID. GetPostByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, error) @@ -39,26 +39,26 @@ type OtterSpace interface { // GetPostBySourceID retrieves a post by its source ID. GetPostBySourceID(ctx context.Context, sourcePostID string) (*models.Post, error) - // GetUserFavoriteCount retrieves the count of a user's favorites. - GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) + // GetUserFavoritesCount retrieves the count of a user's favorites. + GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) // GetUserSourceLinks retrieves the source links of a user. GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) - // GetSpecifiedUserSourceLink retrieves a specified source link of a user. - GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) + // GetUserSourceBySourceID retrieves a specified source link of a user. + GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID string) (map[string]models.UserSource, error) // GetAllAnthroveUserIDs retrieves all Anthrove user IDs. GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) - // GetUserFavoritePostsWithPagination retrieves a user's favorite posts with pagination. - GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) + // GetUserFavoriteWithPagination retrieves a user's favorite posts with pagination. + GetUserFavoriteWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) - // GetUserTagsTroughFavedPosts retrieves a user's tags through their favorited posts. - GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) + // GetUserTagWitRelationToFavedPosts retrieves a user's tags through their favorited posts. + GetUserTagWitRelationToFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) // GetAllTags retrieves all tags. - GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) + GetAllTags(ctx context.Context) ([]models.Tag, error) // GetAllSources retrieves all sources. GetAllSources(ctx context.Context) ([]models.Source, error) diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 0cfc1bd..3503deb 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -64,32 +64,32 @@ func (p *postgresqlConnection) Connect(_ context.Context, endpoint string, usern return nil } -func (p *postgresqlConnection) AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error { - return postgres.CreateUserNodeWithSourceRelation(ctx, p.db, anthroveUserID, sourceDomain, userID, userID) +func (p *postgresqlConnection) CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error { + return postgres.CreateUserWithRelationToSource(ctx, p.db, anthroveUserID, sourceDomain, userID, userID) } -func (p *postgresqlConnection) AddSource(ctx context.Context, anthroveSource *models.Source) error { - return postgres.CreateSourceNode(ctx, p.db, anthroveSource) +func (p *postgresqlConnection) CreateSource(ctx context.Context, anthroveSource *models.Source) error { + return postgres.CreateSource(ctx, p.db, anthroveSource) } -func (p *postgresqlConnection) AddPost(ctx context.Context, anthrovePost *models.Post) error { +func (p *postgresqlConnection) CreatePost(ctx context.Context, anthrovePost *models.Post) error { return postgres.CreatePost(ctx, p.db, anthrovePost) } -func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error { - return postgres.CreateTagNodeWitRelation(ctx, p.db, anthrovePostID, anthroveTag) +func (p *postgresqlConnection) CreateTagAndReferenceToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error { + return postgres.CreateTagAndReferenceToPost(ctx, p.db, anthrovePostID, anthroveTag) } -func (p *postgresqlConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain string, anthrovePostRelationship *models.PostReference) error { - return postgres.EstablishAnthrovePostToSourceLink(ctx, p.db, anthrovePostID, sourceDomain, anthrovePostRelationship) +func (p *postgresqlConnection) CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain string, anthrovePostRelationship *models.PostReference) error { + return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain, anthrovePostRelationship) } -func (p *postgresqlConnection) LinkUserWithPost(ctx context.Context, anthroveUser *models.User, anthrovePost *models.Post) error { - return postgres.EstablishUserToPostLink(ctx, p.db, anthroveUser.ID, anthrovePost.ID) +func (p *postgresqlConnection) CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUser *models.User, anthrovePost *models.Post) error { + return postgres.CreateReferenceBetweenUserAndPost(ctx, p.db, anthroveUser.ID, anthrovePost.ID) } -func (p *postgresqlConnection) CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) { - return postgres.CheckUserToPostLink(ctx, p.db, anthroveUserID, models.AnthrovePostID(sourcePostID)) +func (p *postgresqlConnection) CheckReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) { + return postgres.CheckReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, models.AnthrovePostID(sourcePostID)) } func (p *postgresqlConnection) GetPostByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, error) { @@ -104,7 +104,7 @@ func (p *postgresqlConnection) GetPostBySourceID(ctx context.Context, sourcePost return postgres.GetPostBySourceID(ctx, p.db, sourcePostID) } -func (p *postgresqlConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) { +func (p *postgresqlConnection) GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) { return postgres.GetUserFavoritesCount(ctx, p.db, anthroveUserID) } @@ -112,7 +112,7 @@ func (p *postgresqlConnection) GetUserSourceLinks(ctx context.Context, anthroveU return postgres.GetUserSourceLinks(ctx, p.db, anthroveUserID) } -func (p *postgresqlConnection) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) { +func (p *postgresqlConnection) GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) { return postgres.GetSpecifiedUserSourceLink(ctx, p.db, anthroveUserID, sourceDisplayName) } @@ -120,24 +120,24 @@ func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]mod return postgres.GetAllAnthroveUserIDs(ctx, p.db) } -func (p *postgresqlConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) { - return postgres.GetUserFavoriteNodeWithPagination(ctx, p.db, anthroveUserID, skip, limit) +func (p *postgresqlConnection) GetUserFavoriteWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) { + return postgres.GetUserFavoriteWithPagination(ctx, p.db, anthroveUserID, skip, limit) } -func (p *postgresqlConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { - return postgres.GetUserTagNodeWitRelationToFavedPosts(ctx, p.db, anthroveUserID) +func (p *postgresqlConnection) GetUserTagWitRelationToFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { + return postgres.GetUserTagWitRelationToFavedPosts(ctx, p.db, anthroveUserID) } -func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) { +func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]models.Tag, error) { return postgres.GetTags(ctx, p.db) } func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]models.Source, error) { - return postgres.GetAllSourceNodes(ctx, p.db) + return postgres.GetAllSource(ctx, p.db) } func (p *postgresqlConnection) GetSourceByURL(ctx context.Context, sourceUrl string) (*models.Source, error) { - return postgres.GetSourceNodesByURL(ctx, p.db, sourceUrl) + return postgres.GetSourceByURL(ctx, p.db, sourceUrl) } func (p *postgresqlConnection) migrateDatabase(connectionString string) error {