From e62280a4164af0b731e3c91ff489adc1b7c9199a Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 21:37:27 +0200 Subject: [PATCH 01/77] feat(database): added additional columns added more columns BREAKING-CHANGE: breaks database compatibility to older SDK versions --- .../migrations/001_inital_database.sql | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/pkg/database/migrations/001_inital_database.sql b/pkg/database/migrations/001_inital_database.sql index 03932c9..1d4fe22 100644 --- a/pkg/database/migrations/001_inital_database.sql +++ b/pkg/database/migrations/001_inital_database.sql @@ -18,7 +18,7 @@ CREATE TYPE TagType AS ENUM ( CREATE TABLE "Post" ( - id CHAR(25) PRIMARY KEY, + id CHAR(25) PRIMARY KEY, rating Rating, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, @@ -28,9 +28,9 @@ CREATE TABLE "Post" CREATE TABLE "Source" ( id CHAR(25) PRIMARY KEY, - display_name TEXT NULL, - icon TEXT NULL, - domain TEXT NOT NULL UNIQUE, + display_name TEXT NULL, + icon TEXT NULL, + domain TEXT NOT NULL UNIQUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, deleted_at TIMESTAMP NULL @@ -47,7 +47,7 @@ CREATE TABLE "Tag" CREATE TABLE "User" ( - id TEXT PRIMARY KEY, + id TEXT PRIMARY KEY, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, deleted_at TIMESTAMP NULL @@ -57,7 +57,7 @@ CREATE TABLE "PostReference" ( post_id TEXT REFERENCES "Post" (id), source_id TEXT REFERENCES "Source" (id), - url TEXT NOT NULL, + url TEXT NOT NULL, full_file_url TEXT, preview_file_url TEXT, sample_file_url TEXT, @@ -79,16 +79,12 @@ CREATE TABLE "TagGroup" created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -CREATE TABLE "UserFavorites" -( - user_id TEXT REFERENCES "User" (id), - post_id TEXT REFERENCES "Post" (id), - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (user_id, post_id) -); - CREATE TABLE "UserSource" ( + id CHAR(25) PRIMARY KEY, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + deleted_at TIMESTAMP NULL NULL, user_id TEXT REFERENCES "User" (id), source_id TEXT REFERENCES "Source" (id), scrape_time_interval INT, @@ -97,10 +93,20 @@ CREATE TABLE "UserSource" last_scrape_time TIMESTAMP, account_validate BOOL DEFAULT FALSE, account_validation_key CHAR(25), - PRIMARY KEY (user_id, source_id), UNIQUE (source_id, account_username, account_id) ); +CREATE TABLE "UserFavorites" +( + id CHAR(25) PRIMARY KEY, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + deleted_at TIMESTAMP NULL NULL, + user_id TEXT REFERENCES "User" (id), + post_id TEXT REFERENCES "Post" (id), + user_source_id CHAR(25) REFERENCES "UserSource" (id) +); + CREATE TABLE "post_tags" ( post_id TEXT REFERENCES "Post" (id), -- 2.45.2 From 0e6a8fd61ef773da3977978d0c2aa5f72cd1bc34 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 21:37:54 +0200 Subject: [PATCH 02/77] feat(database): added additional columns added more columns BREAKING-CHANGE: breaks database compatibility to older SDK versions --- pkg/database/source.go | 27 ++++++++++++++++----------- pkg/models/const.go | 31 ++++++++++++++++++------------- pkg/models/orm.go | 2 +- pkg/models/orm_test.go | 4 ++-- pkg/models/post.go | 2 +- pkg/models/post_test.go | 2 +- pkg/models/source.go | 2 +- pkg/models/source_test.go | 2 +- pkg/models/user.go | 2 +- pkg/models/userFavorite.go | 9 ++++++--- pkg/models/userSource.go | 5 +++-- pkg/models/user_test.go | 2 +- 12 files changed, 52 insertions(+), 38 deletions(-) diff --git a/pkg/database/source.go b/pkg/database/source.go index 544bfcc..3967a3f 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -2,18 +2,23 @@ package database import ( "context" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" ) -type Source interface { - - // CreateSource adds a new source to the database. - CreateSource(ctx context.Context, anthroveSource *models.Source) error - - // GetAllSources retrieves all sources. - GetAllSources(ctx context.Context) ([]models.Source, error) - - // GetSourceByDomain retrieves a source by its URL. - GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) +func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) { + var user models.UserSource + return user, nil +} + +func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { + return nil +} + +func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) { + var user models.UserSource + return user, nil +} + +func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { + return nil } diff --git a/pkg/models/const.go b/pkg/models/const.go index 7e84b0c..4f690a4 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -2,20 +2,25 @@ package models import "time" -type AnthroveUserID string -type AnthrovePostID string -type AnthroveSourceID string -type AnthroveSourceDomain string -type AnthrovePostURL string -type AnthroveTagGroupName string -type AnthroveTagAliasName string -type AnthroveTagID string -type AnthroveScrapeTimeInterval int -type AnthroveUserLastScrapeTime time.Time -type AnthroveTagName string +type ( + UserID string + PostID string + SourceID string + SourceDomain string + PostURL string + TagGroupName string + TagAliasName string + TagID string + ScrapeTimeInterval int + UserLastScrapeTime time.Time + TagName string -type Rating string -type TagType string + Rating string + TagType string + + UserSourceID string + UserFavoriteID string +) const ( SFW Rating = "safe" diff --git a/pkg/models/orm.go b/pkg/models/orm.go index f9209d9..a4ec52f 100644 --- a/pkg/models/orm.go +++ b/pkg/models/orm.go @@ -8,7 +8,7 @@ import ( ) type ID interface { - AnthroveUserID | AnthroveSourceID | AnthrovePostID + UserID | SourceID | PostID | UserSourceID | UserFavoriteID } type BaseModel[T ID] struct { diff --git a/pkg/models/orm_test.go b/pkg/models/orm_test.go index cf9e1a4..780b67d 100644 --- a/pkg/models/orm_test.go +++ b/pkg/models/orm_test.go @@ -42,8 +42,8 @@ func TestBaseModel_BeforeCreate(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - base := &BaseModel[AnthrovePostID]{ - ID: AnthrovePostID(tt.fields.ID), + base := &BaseModel[PostID]{ + ID: PostID(tt.fields.ID), CreatedAt: tt.fields.CreatedAt, UpdatedAt: tt.fields.UpdatedAt, DeletedAt: tt.fields.DeletedAt, diff --git a/pkg/models/post.go b/pkg/models/post.go index d272387..8b24e60 100644 --- a/pkg/models/post.go +++ b/pkg/models/post.go @@ -2,7 +2,7 @@ package models // Post model type Post struct { - BaseModel[AnthrovePostID] + BaseModel[PostID] Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"` Tags []Tag `json:"-" gorm:"many2many:post_tags;"` Favorites []UserFavorites `json:"-" gorm:"foreignKey:PostID"` diff --git a/pkg/models/post_test.go b/pkg/models/post_test.go index 2e7228d..7c6856f 100644 --- a/pkg/models/post_test.go +++ b/pkg/models/post_test.go @@ -4,7 +4,7 @@ import "testing" func TestPost_TableName(t *testing.T) { type fields struct { - BaseModel BaseModel[AnthrovePostID] + BaseModel BaseModel[PostID] Rating Rating Tags []Tag Favorites []UserFavorites diff --git a/pkg/models/source.go b/pkg/models/source.go index 9027b55..aa90bbb 100644 --- a/pkg/models/source.go +++ b/pkg/models/source.go @@ -2,7 +2,7 @@ package models // Source model type Source struct { - BaseModel[AnthroveSourceID] + BaseModel[SourceID] DisplayName string `json:"display_name" ` Domain string `json:"domain" gorm:"not null;unique"` Icon string `json:"icon" gorm:"not null"` diff --git a/pkg/models/source_test.go b/pkg/models/source_test.go index e65b2c8..876db3a 100644 --- a/pkg/models/source_test.go +++ b/pkg/models/source_test.go @@ -4,7 +4,7 @@ import "testing" func TestSource_TableName(t *testing.T) { type fields struct { - BaseModel BaseModel[AnthroveSourceID] + BaseModel BaseModel[SourceID] DisplayName string Domain string Icon string diff --git a/pkg/models/user.go b/pkg/models/user.go index bc2d575..e2a88c1 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -2,7 +2,7 @@ package models // User model type User struct { - BaseModel[AnthroveUserID] + BaseModel[UserID] Favorites []UserFavorites `json:"-" gorm:"foreignKey:UserID"` Sources []UserSource `json:"-" gorm:"foreignKey:UserID"` } diff --git a/pkg/models/userFavorite.go b/pkg/models/userFavorite.go index 182af8f..2572ce8 100644 --- a/pkg/models/userFavorite.go +++ b/pkg/models/userFavorite.go @@ -3,9 +3,12 @@ package models import "time" type UserFavorites struct { - UserID string `json:"user_id" gorm:"primaryKey"` - PostID string `json:"post_id" gorm:"primaryKey"` - CreatedAt time.Time `json:"-"` + BaseModel[UserFavoriteID] + UserID string `json:"user_id"` + PostID string `json:"post_id"` + UserSourceID UserSourceID `json:"user_source_id"` + UserSource UserSource `json:"-" gorm:"foreignKey:ID;references:UserSourceID"` + CreatedAt time.Time `json:"created_at"` } func (UserFavorites) TableName() string { diff --git a/pkg/models/userSource.go b/pkg/models/userSource.go index 9ee0f87..187c2a9 100644 --- a/pkg/models/userSource.go +++ b/pkg/models/userSource.go @@ -3,10 +3,11 @@ package models import "time" type UserSource struct { + BaseModel[UserSourceID] User User `json:"user" gorm:"foreignKey:ID;references:UserID"` - UserID string `json:"user_id" gorm:"primaryKey"` + UserID UserID `json:"user_id"` Source Source `json:"source" gorm:"foreignKey:ID;references:SourceID"` - SourceID string `json:"source_id" gorm:"primaryKey"` + SourceID SourceID `json:"source_id"` ScrapeTimeInterval string `json:"scrape_time_interval"` AccountUsername string `json:"account_username"` AccountID string `json:"account_id"` diff --git a/pkg/models/user_test.go b/pkg/models/user_test.go index c31745b..34db57f 100644 --- a/pkg/models/user_test.go +++ b/pkg/models/user_test.go @@ -4,7 +4,7 @@ import "testing" func TestUser_TableName(t *testing.T) { type fields struct { - BaseModel BaseModel[AnthroveUserID] + BaseModel BaseModel[UserID] Favorites []UserFavorites Sources []UserSource } -- 2.45.2 From f940f22d67a2ea3f308d5c2b1ec318e59b54cc86 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 21:38:35 +0200 Subject: [PATCH 03/77] chore(impl): removed old functions removed all old implementation of the SDK BREAKING-CHANGE: breaks database compatibility to older SDK versions --- pkg/database/database.go | 36 - pkg/database/post.go | 31 - pkg/database/postgres.go | 268 --- pkg/database/postgres_test.go | 3791 --------------------------------- pkg/database/tag.go | 20 - pkg/database/tagalias.go | 19 - pkg/database/taggroup.go | 19 - pkg/database/user.go | 42 - 8 files changed, 4226 deletions(-) delete mode 100644 pkg/database/database.go delete mode 100644 pkg/database/post.go delete mode 100644 pkg/database/postgres.go delete mode 100644 pkg/database/postgres_test.go delete mode 100644 pkg/database/tag.go delete mode 100644 pkg/database/tagalias.go delete mode 100644 pkg/database/taggroup.go delete mode 100644 pkg/database/user.go diff --git a/pkg/database/database.go b/pkg/database/database.go deleted file mode 100644 index 3a6beeb..0000000 --- a/pkg/database/database.go +++ /dev/null @@ -1,36 +0,0 @@ -package database - -import ( - "context" - "database/sql" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" -) - -type OtterSpace interface { - // Connect establishes a connection to the database. - Connect(ctx context.Context, config models.DatabaseConfig) error - - // Post contains all function that are needed to manage Posts - Post - - // User contains all function that are needed to manage the AnthroveUser - User - - // Source contains all function that are needed to manage the Source - Source - - // Tag contains all functions that are used to manage Tag - Tag - - // TagAlias contains all function that are needed to manage the TagAlias - TagAlias - - // TagGroup contains all function that are needed to manage the TagGroup - TagGroup - - // ExecuteRawStatement run a custom query. - ExecuteRawStatement(ctx context.Context, query string, args ...any) error - - // QueryRawStatement runs a custom query and returns the table - QueryRawStatement(ctx context.Context, query string, args ...any) (*sql.Rows, error) -} diff --git a/pkg/database/post.go b/pkg/database/post.go deleted file mode 100644 index f19e70d..0000000 --- a/pkg/database/post.go +++ /dev/null @@ -1,31 +0,0 @@ -package database - -import ( - "context" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" -) - -type Post interface { - - // CreatePost adds a new post to the database. - CreatePost(ctx context.Context, anthrovePost *models.Post) error - - // TODO: Everything - CreatePostInBatch(ctx context.Context, anthrovePost []models.Post, batchSize int) error - - // GetPostByAnthroveID retrieves a post by its Anthrove ID. - GetPostByAnthroveID(ctx context.Context, anthrovePostID models.AnthrovePostID) (*models.Post, error) - - // GetPostByURL retrieves a post by its source URL. - GetPostByURL(ctx context.Context, postURL string) (*models.Post, error) - - // GetPostBySourceID retrieves a post by its source ID. - GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error) - - // CreatePostWithReferenceToTagAnd adds a tag with a relation to a post. - CreatePostWithReferenceToTagAnd(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error - - // CreatePostReference links a post with a source. - CreatePostReference(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error -} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go deleted file mode 100644 index 001d7b0..0000000 --- a/pkg/database/postgres.go +++ /dev/null @@ -1,268 +0,0 @@ -package database - -import ( - "context" - "database/sql" - "embed" - "fmt" - log2 "log" - "os" - "time" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/postgres" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - _ "github.com/lib/pq" - migrate "github.com/rubenv/sql-migrate" - log "github.com/sirupsen/logrus" - gormPostgres "gorm.io/driver/postgres" - "gorm.io/gorm" - gormLogger "gorm.io/gorm/logger" -) - -//go:embed migrations/*.sql -var embedMigrations embed.FS - -type postgresqlConnection struct { - db *gorm.DB - debug bool -} - -func NewPostgresqlConnection() OtterSpace { - return &postgresqlConnection{ - db: nil, - } -} - -func (p *postgresqlConnection) Connect(_ context.Context, config models.DatabaseConfig) error { - var localSSL string - var logLevel gormLogger.LogLevel - - if config.SSL { - localSSL = "require" - } else { - localSSL = "disable" - } - - p.debug = config.Debug - - dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.Timezone) - var err error - - if p.debug { - logLevel = gormLogger.Info - } else { - logLevel = gormLogger.Silent - } - - dbLogger := gormLogger.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), gormLogger.Config{ - SlowThreshold: 200 * time.Millisecond, - LogLevel: logLevel, - IgnoreRecordNotFoundError: true, - Colorful: true, - }) - - db, err := gorm.Open(gormPostgres.Open(dsn), &gorm.Config{ - Logger: dbLogger, - }) - p.db = db - if err != nil { - return err - } - - log.Infof("OtterSpace: database connection established") - - err = p.migrateDatabase(db) - if err != nil { - return err - } - - log.Infof("OtterSpace: migration compleate") - - return nil -} - -func (p *postgresqlConnection) CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error { - return postgres.CreateUserWithRelationToSource(ctx, p.db, anthroveUserID, sourceID, accountId, accountUsername) -} - -func (p *postgresqlConnection) CreateSource(ctx context.Context, anthroveSource *models.Source) error { - return postgres.CreateSource(ctx, p.db, anthroveSource) -} - -func (p *postgresqlConnection) CreatePost(ctx context.Context, anthrovePost *models.Post) error { - return postgres.CreatePost(ctx, p.db, anthrovePost) -} - -func (p *postgresqlConnection) CreatePostInBatch(ctx context.Context, anthrovePost []models.Post, batchSize int) error { - return postgres.CreatePostInBatch(ctx, p.db, anthrovePost, batchSize) -} - -func (p *postgresqlConnection) CreatePostWithReferenceToTagAnd(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error { - return postgres.CreateTagAndReferenceToPost(ctx, p.db, anthrovePostID, anthroveTag) -} - -func (p *postgresqlConnection) CreatePostReference(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error { - return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain, postURL, config) -} - -func (p *postgresqlConnection) CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error { - return postgres.CreateReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID) -} - -func (p *postgresqlConnection) CheckIfUserHasPostAsFavorite(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) { - return postgres.CheckReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID) -} - -func (p *postgresqlConnection) GetPostByAnthroveID(ctx context.Context, anthrovePostID models.AnthrovePostID) (*models.Post, error) { - return postgres.GetPostByAnthroveID(ctx, p.db, anthrovePostID) -} - -func (p *postgresqlConnection) GetPostByURL(ctx context.Context, sourceUrl string) (*models.Post, error) { - return postgres.GetPostBySourceURL(ctx, p.db, sourceUrl) -} - -func (p *postgresqlConnection) GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error) { - return postgres.GetPostBySourceID(ctx, p.db, sourceID) -} - -func (p *postgresqlConnection) GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) { - return postgres.GetUserFavoritesCount(ctx, p.db, anthroveUserID) -} - -func (p *postgresqlConnection) GetAllUserSources(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) { - return postgres.GetUserSourceLinks(ctx, p.db, anthroveUserID) -} - -func (p *postgresqlConnection) GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (*models.UserSource, error) { - return postgres.GetUserSourceBySourceID(ctx, p.db, anthroveUserID, sourceID) -} - -func (p *postgresqlConnection) GetAllUsers(ctx context.Context) ([]models.User, error) { - return postgres.GetAllUsers(ctx, p.db) -} - -func (p *postgresqlConnection) GetAllUserFavoritesWithPagination(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) GetAllTagsFromUser(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { - return postgres.GetUserTagWitRelationToFavedPosts(ctx, p.db, anthroveUserID) -} - -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.GetAllSource(ctx, p.db) -} - -func (p *postgresqlConnection) GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) { - return postgres.GetSourceByDomain(ctx, p.db, sourceDomain) -} - -func (p *postgresqlConnection) UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error { - return postgres.UpdateUserSourceScrapeTimeInterval(ctx, p.db, anthroveUserID, sourceID, scrapeTime) -} - -func (p *postgresqlConnection) UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error { - return postgres.UpdateUserSourceLastScrapeTime(ctx, p.db, anthroveUserID, sourceID, lastScrapeTime) -} - -func (p *postgresqlConnection) UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error { - return postgres.UpdateUserSourceValidation(ctx, p.db, anthroveUserID, sourceID, valid) -} - -func (p *postgresqlConnection) CreateTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName, tagID models.AnthroveTagID) error { - return postgres.CreateTagAlias(ctx, p.db, tagAliasName, tagID) -} - -func (p *postgresqlConnection) GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error) { - return postgres.GetAllTagAlias(ctx, p.db) -} - -func (p *postgresqlConnection) GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) { - return postgres.GetAllTagAliasByTag(ctx, p.db, tagID) -} - -func (p *postgresqlConnection) DeleteTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName) error { - return postgres.DeleteTagAlias(ctx, p.db, tagAliasName) -} - -func (p *postgresqlConnection) CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) error { - return postgres.CreateTagGroup(ctx, p.db, tagGroupName, tagID) -} - -func (p *postgresqlConnection) GetAllTagGroup(ctx context.Context) ([]models.TagGroup, error) { - return postgres.GetAllTagGroup(ctx, p.db) -} - -func (p *postgresqlConnection) GetAllTagGroupByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagGroup, error) { - return postgres.GetAllTagGroupByTag(ctx, p.db, tagID) -} - -func (p *postgresqlConnection) DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error { - return postgres.DeleteTagGroup(ctx, p.db, tagGroupName) -} - -func (p *postgresqlConnection) CreateTag(ctx context.Context, tagName models.AnthroveTagName, tagType models.TagType) error { - return postgres.CreateTag(ctx, p.db, tagName, tagType) -} - -func (p *postgresqlConnection) GetAllTagsByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) { - return postgres.GetAllTagByTagsType(ctx, p.db, tagType) -} - -func (p *postgresqlConnection) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error { - return postgres.DeleteTag(ctx, p.db, tagName) -} - -func (p *postgresqlConnection) CreateTagInBatchAndUpdate(ctx context.Context, tags []models.Tag, batchSize int) error { - return postgres.CreateTagInBatchAndUpdate(ctx, p.db, tags, batchSize) -} - -func (p *postgresqlConnection) CreateTagAliasInBatch(ctx context.Context, tagAliases []models.TagAlias, batchSize int) error { - return postgres.CreateTagAliasInBatch(ctx, p.db, tagAliases, batchSize) -} - -func (p *postgresqlConnection) CreateTagGroupInBatch(ctx context.Context, tagGroups []models.TagGroup, batchSize int) error { - return postgres.CreateTagGroupInBatch(ctx, p.db, tagGroups, batchSize) - -} - -func (p *postgresqlConnection) ExecuteRawStatement(ctx context.Context, query string, args ...any) error { - return postgres.ExecuteRawStatement(ctx, p.db, query, args...) -} - -func (p *postgresqlConnection) QueryRawStatement(ctx context.Context, query string, args ...any) (*sql.Rows, error) { - return postgres.QueryRawStatement(ctx, p.db, query, args...) -} - -// HELPER - -func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error { - dialect := "postgres" - migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"} - - db, err := dbPool.DB() - if err != nil { - return fmt.Errorf("postgres migration: %v", err) - } - - n, err := migrate.Exec(db, dialect, migrations, migrate.Up) - if err != nil { - return fmt.Errorf("postgres migration: %v", err) - } - - if p.debug { - if n != 0 { - log.Infof("postgres migration: applied %d migrations!", n) - - } else { - log.Info("postgres migration: nothing to migrate") - - } - } - - return nil -} diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go deleted file mode 100644 index 221b882..0000000 --- a/pkg/database/postgres_test.go +++ /dev/null @@ -1,3791 +0,0 @@ -package database - -import ( - "context" - "database/sql" - "fmt" - "reflect" - "testing" - "time" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/postgres" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" - "gorm.io/gorm" -) - -func TestNewPostgresqlConnection(t *testing.T) { - // Test - tests := []struct { - name string - want OtterSpace - }{ - { - name: "Test 1: Create new postgresql connection", - want: nil, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if tt.want != NewPostgresqlConnection() { - } else { - t.Errorf("NewPostgresqlConnection() = %s", tt.want) - } - }) - } -} - -func Test_postgresqlConnection_Connect(t *testing.T) { - - // Setup trow away container - ctx := context.Background() - container, _, err := test.StartPostgresContainer(ctx) - if err != nil { - t.Fatalf("Could not start PostgreSQL container: %v", err) - } - defer container.Terminate(ctx) - - // Setup Tests - - dbConfig, err := test.DatabaseModesFromConnectionString(ctx, container) - if err != nil { - t.Fatalf("Could not parse database config: %v", err) - } - - // Test - type args struct { - in0 context.Context - config models.DatabaseConfig - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Connect to postgresql connection", - args: args{ - in0: ctx, - config: *dbConfig, - }, - wantErr: false, - }, - { - name: "Test 1: Empty connection config", - args: args{ - in0: ctx, - config: models.DatabaseConfig{}, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{} - if err := p.Connect(tt.args.in0, tt.args.config); (err != nil) != tt.wantErr { - t.Errorf("Connect() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateUserWithRelationToSource(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) - } - defer container.Terminate(ctx) - - // Setup Test - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - } - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - accountId string - accountUsername string - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid anthroveUserID, sourceID, accountId, accountUsername", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: source.ID, - accountId: "e1", - accountUsername: "marius", - }, - wantErr: false, - }, - { - name: "Test 2: Invalid anthroveUserID, valid sourceID, accountId, accountUsername", - args: args{ - ctx: ctx, - anthroveUserID: "2", - sourceID: source.ID, - accountId: "e1", - accountUsername: "marius", - }, - wantErr: true, - }, - { - name: "Test 3: Empty anthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: source.ID, - accountId: "e1", - accountUsername: "marius", - }, - wantErr: true, - }, - { - name: "Test 4: invalid sourceID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "fa.net", - accountId: "e1", - accountUsername: "marius", - }, - wantErr: true, - }, - { - name: "Test 5: no accountId", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: source.ID, - accountId: "", - accountUsername: "marius", - }, - wantErr: true, - }, - { - name: "Test 6: no accountUsername", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: source.ID, - accountId: "aa", - accountUsername: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateUserWithRelationToSource(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.accountId, tt.args.accountUsername); (err != nil) != tt.wantErr { - t.Errorf("CreateUserWithRelationToSource() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateSource(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validSource := &models.Source{ - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - } - - invalidSource := &models.Source{ - Domain: "", - } - - // Test - type args struct { - ctx context.Context - anthroveSource *models.Source - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid anthroveSource", - args: args{ - ctx: ctx, - anthroveSource: validSource, - }, - wantErr: false, - }, - { - name: "Test 2: inValid anthroveSource", - args: args{ - ctx: ctx, - anthroveSource: invalidSource, - }, - wantErr: true, - }, - { - name: "Test 3: unique anthroveSource", - args: args{ - ctx: ctx, - anthroveSource: validSource, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateSource(tt.args.ctx, tt.args.anthroveSource); (err != nil) != tt.wantErr { - t.Errorf("CreateSource() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreatePost(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) - } - defer container.Terminate(ctx) - - // Setup Tests - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - validPost := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - invalidPost := &models.Post{ - Rating: "error", - } - - // Test - type args struct { - ctx context.Context - anthrovePost *models.Post - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthrovePostID and Rating", - args: args{ - ctx: context.Background(), - anthrovePost: validPost, - }, - wantErr: false, - }, - { - name: "Test 2: Invalid Rating", - args: args{ - ctx: context.Background(), - anthrovePost: invalidPost, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreatePost(tt.args.ctx, tt.args.anthrovePost); (err != nil) != tt.wantErr { - t.Errorf("CreatePost() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateTagAndReferenceToPost(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) - } - defer container.Terminate(ctx) - - // Setup Test - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - tag := &models.Tag{ - Name: "JayTheFerret", - Type: "artist", - } - - // Test - type args struct { - ctx context.Context - anthrovePostID models.AnthrovePostID - anthroveTag *models.Tag - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid PostID and Tag", - args: args{ - ctx: ctx, - anthrovePostID: post.ID, - anthroveTag: tag, - }, - wantErr: false, - }, - { - name: "Test 2: Valid PostID and no Tag", - args: args{ - ctx: ctx, - anthrovePostID: post.ID, - anthroveTag: nil, - }, - wantErr: true, - }, - { - name: "Test 3: Invalid PostID and valid Tag", - args: args{ - ctx: ctx, - anthrovePostID: "123456", - anthroveTag: tag, - }, - wantErr: true, - }, - { - name: "Test 4: No PostID and valid Tag", - args: args{ - ctx: ctx, - anthrovePostID: "", - anthroveTag: tag, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreatePostWithReferenceToTagAnd(tt.args.ctx, tt.args.anthrovePostID, tt.args.anthroveTag); (err != nil) != tt.wantErr { - t.Errorf("CreatePostWithReferenceToTagAnd() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(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) - } - defer container.Terminate(ctx) - - // Setup Test - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthrovePostID models.AnthrovePostID - sourceDomain models.AnthroveSourceDomain - postURl models.AnthrovePostURL - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthrovePostID and anthroveSourceDomain", - args: args{ - ctx: ctx, - anthrovePostID: post.ID, - sourceDomain: "e621.net", - postURl: "http://e621.net/post/eeasd", - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthrovePostID and Valid anthroveSourceDomain", - args: args{ - ctx: ctx, - anthrovePostID: "123456", - sourceDomain: "e621.net", - postURl: "", - }, - wantErr: true, - }, - { - name: "Test 3: Invalid anthroveSourceDomain and Valid AnthrovePostID", - args: args{ - ctx: ctx, - anthrovePostID: "1234", - sourceDomain: "fa.banana", - postURl: "", - }, - wantErr: true, - }, - { - name: "Test 4: Invalid anthroveSourceDomain and Invalid AnthrovePostID", - args: args{ - ctx: ctx, - anthrovePostID: "696969", - postURl: "", - }, - wantErr: true, - }} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreatePostReference(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain, tt.args.postURl, models.PostReferenceConfig{}); (err != nil) != tt.wantErr { - t.Errorf("CreatePostReference() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateReferenceBetweenUserAndPost(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) - } - defer container.Terminate(ctx) - - // Setup Test - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - err = postgres.CreateUser(ctx, gormDB, validUserID) - if err != nil { - t.Fatal(err) - } - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - anthrovePostID models.AnthrovePostID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID and AnthrovePostID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - anthrovePostID: post.ID, - }, - wantErr: false, - }, - { - name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - anthrovePostID: "123456", - }, - wantErr: true, - }, - { - name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: "123", - anthrovePostID: "1234", - }, - wantErr: true, - }, - { - name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: "123", - anthrovePostID: "123456", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateReferenceBetweenUserAndPost(tt.args.ctx, tt.args.anthroveUserID, tt.args.anthrovePostID); (err != nil) != tt.wantErr { - t.Errorf("CreateReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CheckReferenceBetweenUserAndPost(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) - } - defer container.Terminate(ctx) - - // Setup Test - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - err = postgres.CreateUser(ctx, gormDB, validUserID) - if err != nil { - t.Fatal(err) - } - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - anthrovePostID models.AnthrovePostID - } - tests := []struct { - name string - args args - want bool - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID and AnthrovePostID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - anthrovePostID: post.ID, - }, - want: true, - wantErr: false, - }, - { - name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - anthrovePostID: "qadw", - }, - want: false, - wantErr: true, - }, - { - name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: "123", - anthrovePostID: post.ID, - }, - want: false, - wantErr: true, - }, - { - name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: "123", - anthrovePostID: "123456", - }, - want: false, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.CheckIfUserHasPostAsFavorite(tt.args.ctx, tt.args.anthroveUserID, tt.args.anthrovePostID) - if (err != nil) != tt.wantErr { - t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("CheckIfUserHasPostAsFavorite() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetPostByAnthroveID(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) - } - defer container.Terminate(ctx) - - // Setup Tests - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal("Could not create post", err) - } - - // Test - type args struct { - ctx context.Context - anthrovePost models.AnthrovePostID - } - tests := []struct { - name string - args args - want *models.Post - wantErr bool - }{ - { - name: "Test 1: Valid anthrovePostID", - args: args{ - ctx: ctx, - anthrovePost: post.ID, - }, - want: post, - wantErr: false, - }, - { - name: "Test 2: No anthrovePostID", - args: args{ - ctx: ctx, - anthrovePost: "nil", - }, - want: nil, - wantErr: true, - }} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetPostByAnthroveID(tt.args.ctx, tt.args.anthrovePost) - if (err != nil) != tt.wantErr { - t.Errorf("GetPostByAnthroveID() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkPost(got, tt.want) { - t.Errorf("GetPostByAnthroveID() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetPostByURL(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) - } - defer container.Terminate(ctx) - - // Setup Tests - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal("Could not create post", err) - } - - source := models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")), - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.net/icon.ico", - } - - err = postgres.CreateSource(ctx, gormDB, &source) - if err != nil { - t.Fatal("Could not create source", err) - } - - err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://e62asdwad.com/asdas", models.PostReferenceConfig{}) - if err != nil { - t.Fatal("Could not create source reference", err) - } - - // Test - type args struct { - ctx context.Context - sourceUrl string - } - tests := []struct { - name string - args args - want *models.Post - wantErr bool - }{ - { - name: "Test 1: Valid sourceUrl", - args: args{ - ctx: ctx, - sourceUrl: "https://e62asdwad.com/asdas", - }, - want: post, - wantErr: false, - }, - { - name: "Test 2: Invalid sourceUrl", - args: args{ - ctx: ctx, - sourceUrl: "1234", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: No sourceUrl", - args: args{ - ctx: ctx, - sourceUrl: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetPostByURL(tt.args.ctx, tt.args.sourceUrl) - if (err != nil) != tt.wantErr { - t.Errorf("GetPostBySourceURL() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkPost(got, tt.want) { - t.Errorf("GetPostBySourceURL() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetPostBySourceID(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) - } - defer container.Terminate(ctx) - - // Setup Tests - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - source := models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID1, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.net/icon.ico", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal("Could not create post", err) - } - - err = postgres.CreateSource(ctx, gormDB, &source) - if err != nil { - t.Fatal("Could not create source", err) - } - - err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://easd15aed.de/asd", models.PostReferenceConfig{}) - if err != nil { - t.Fatal("Could not create source reference", err) - } - - // Test - type args struct { - ctx context.Context - sourceID models.AnthroveSourceID - } - tests := []struct { - name string - args args - want *models.Post - wantErr bool - }{ - { - name: "Test 1: Valid sourceID", - args: args{ - ctx: ctx, - sourceID: source.ID, - }, - want: post, - wantErr: false, - }, - { - name: "Test 2: Invalid sourceID", - args: args{ - ctx: ctx, - sourceID: "1234", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: No sourceID", - args: args{ - ctx: ctx, - sourceID: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetPostBySourceID(tt.args.ctx, tt.args.sourceID) - if (err != nil) != tt.wantErr { - t.Errorf("GetPostBySourceID() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkPost(got, tt.want) { - t.Errorf("GetPostBySourceID() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetUserFavoritesCount(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) - validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) - validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) - - expectedResultPosts := []models.Post{ - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, - Rating: "safe", - }, - { - - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, - Rating: "safe", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, - Rating: "questionable", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, - Rating: "safe", - }, - } - - err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - for _, post := range expectedResultPosts { - err = postgres.CreatePost(ctx, gormDB, &post) - if err != nil { - t.Fatal(err) - } - err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, post.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want int64 - wantErr bool - }{ - { - name: "Test 1: Valid anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - }, - want: 6, - wantErr: false, - }, - { - name: "Test 2: Invalid anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - anthroveUserID: "2", - }, - want: 0, - wantErr: true, - }, - { - name: "Test 3: no anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - anthroveUserID: "", - }, - want: 0, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetUserFavoritesCount(tt.args.ctx, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetUserFavoritesCount() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("GetUserFavoritesCount() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetUserSourceLinks(t *testing.T) { - // Setup trow away containert - ctx := context.Background() - container, gormDB, err := test.StartPostgresContainer(ctx) - if err != nil { - t.Fatalf("Could not start PostgreSQL container: %v", err) - } - defer container.Terminate(ctx) - - // Setup Test - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - validSourceID2 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source2")) - - eSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, - DisplayName: "e621", - Domain: "e621.net", - } - err = postgres.CreateSource(ctx, gormDB, eSource) - if err != nil { - t.Fatal(err) - } - - faSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID2}, - DisplayName: "fa", - Domain: "fa.net", - } - err = postgres.CreateSource(ctx, gormDB, faSource) - if err != nil { - t.Fatal(err) - } - - expectedResult := make(map[string]models.UserSource) - expectedResult["e621"] = models.UserSource{ - UserID: "e1", - AccountUsername: "e621-user", - Source: models.Source{ - DisplayName: eSource.DisplayName, - Domain: eSource.Domain, - }, - } - expectedResult["fa"] = models.UserSource{ - UserID: "fa1", - AccountUsername: "fa-user", - Source: models.Source{ - DisplayName: faSource.DisplayName, - Domain: faSource.Domain, - }, - } - - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) - if err != nil { - t.Fatal(err) - } - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want map[string]models.UserSource - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - }, - want: expectedResult, - wantErr: false, - }} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllUserSources(tt.args.ctx, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllUserSources() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllUserSources() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetUserSourceBySourceID(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - expectedResult := &models.UserSource{ - UserID: string(validUserID), - AccountUsername: "euser", - Source: models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: source.ID}, - DisplayName: source.DisplayName, - Domain: source.Domain, - Icon: source.Icon, - }, - } - - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult.UserID, expectedResult.AccountUsername) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - } - - tests := []struct { - name string - args args - want *models.UserSource - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID and sourceID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: source.ID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveUserID and valid sourceID", - args: args{ - ctx: ctx, - anthroveUserID: invalidUserID, - sourceID: source.ID, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Valid AnthroveUserID and invalid sourceID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "fa", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 4: No AnthroveUserID and Valid sourceID", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: source.ID, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 5: Valid AnthroveUserID and No SourceDisplayName", - args: args{ - ctx: ctx, - anthroveUserID: "1", - sourceID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 6: No AnthroveUserID and No SourceDisplayName", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetUserSourceBySourceID(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID) - if (err != nil) != tt.wantErr { - t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkUserSource(got, tt.want) { - t.Errorf("GetUserSourceBySourceID() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetAllUsers(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) - } - defer container.Terminate(ctx) - - // Setup Test - validUserID01 := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - validUserID02 := models.AnthroveUserID(fmt.Sprintf("%025s", "User2")) - validUserID03 := models.AnthroveUserID(fmt.Sprintf("%025s", "User3")) - - users := []models.User{ - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID01}, - }, - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID02}, - }, - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID03}, - }, - } - - for _, user := range users { - err = postgres.CreateUser(ctx, gormDB, user.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - } - tests := []struct { - name string - args args - want []models.User - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - }, - want: users, - wantErr: false, - }} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllUsers(tt.args.ctx) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllUsers() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkUser(got, tt.want) { - t.Errorf("GetAllUsers() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetUserFavoriteWithPagination(t *testing.T) { - // Setup trow away containert - ctx := context.Background() - container, gormDB, err := test.StartPostgresContainer(ctx) - if err != nil { - t.Fatalf("Could not start PostgreSQL container: %v", err) - } - defer container.Terminate(ctx) - - // Setup Test - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) - validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) - validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) - - expectedResultPosts := []models.Post{ - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, - Rating: "safe", - }, - { - - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, - Rating: "safe", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, - Rating: "questionable", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, - Rating: "safe", - }, - } - expectedResult := &models.FavoriteList{ - Posts: expectedResultPosts, - } - expectedResult2 := &models.FavoriteList{ - Posts: expectedResultPosts[2:], - } - expectedResult3 := &models.FavoriteList{ - Posts: expectedResultPosts[:3], - } - - err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - for _, expectedResultPost := range expectedResultPosts { - err = postgres.CreatePost(ctx, gormDB, &expectedResultPost) - if err != nil { - t.Fatal(err) - } - err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, expectedResultPost.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - skip int - limit int - } - tests := []struct { - name string - args args - want *models.FavoriteList - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - skip: 0, - limit: 2000, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: Skip first two", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - skip: 2, - limit: 2000, - }, - want: expectedResult2, - wantErr: false, - }, - { - name: "Test 3: Limit of 3", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - skip: 0, - limit: 3, - }, - want: expectedResult3, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllUserFavoritesWithPagination(tt.args.ctx, tt.args.anthroveUserID, tt.args.skip, tt.args.limit) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllUserFavoritesWithPagination() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkFavoritePosts(got, tt.want) { - t.Errorf("GetAllUserFavoritesWithPagination() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetUserTagWitRelationToFavedPosts(t *testing.T) { - // Setup trow away containert - ctx := context.Background() - container, gormDB, err := test.StartPostgresContainer(ctx) - if err != nil { - t.Fatalf("Could not start PostgreSQL container: %v", err) - } - defer container.Terminate(ctx) - - // Setup Test - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - - err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - posts := []models.Post{ - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, Rating: "safe"}, - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, Rating: "safe"}, - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, Rating: "explicit"}, - } - - for _, post := range posts { - err = postgres.CreatePost(ctx, gormDB, &post) - if err != nil { - t.Fatal(err) - } - err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, models.AnthrovePostID(post.ID)) - if err != nil { - t.Fatal(err) - } - } - - tags := []models.Tag{ - {Name: "JayTheFerret", Type: "artist"}, - {Name: "Ferret", Type: "species"}, - {Name: "Jay", Type: "character"}, - } - - for i, tag := range tags { - err = postgres.CreateTagAndReferenceToPost(ctx, gormDB, posts[i].ID, &tag) - if err != nil { - t.Fatal(err) - } - } - - expectedResult := []models.TagsWithFrequency{ - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[0].Name, - Type: tags[0].Type, - }, - }, - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[2].Name, - Type: tags[2].Type, - }, - }, - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[1].Name, - Type: tags[1].Type, - }, - }, - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want []models.TagsWithFrequency - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - }, - want: expectedResult, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagsFromUser(tt.args.ctx, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagsFromUser() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagsFromUser() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTags(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) - } - defer container.Terminate(ctx) - - // Setup Test - - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: "artist", - }, - { - Name: "anthro", - Type: "general", - }, - { - Name: "soxx", - Type: "character", - }, - } - - for _, tag := range tags { - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - } - tests := []struct { - name string - args args - want []models.Tag - wantErr bool - }{ - { - name: "Test 1: Get Tags", - args: args{ - ctx: ctx, - }, - want: tags, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTags(tt.args.ctx) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTags() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTags() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetAllSources(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) - } - defer container.Terminate(ctx) - - // Setup Test - - sources := []models.Source{ - { - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - }, - { - DisplayName: "furaffinity", - Domain: "furaffinity.net", - Icon: "icon.furaffinity.net", - }, - { - DisplayName: "fenpaws", - Domain: "fenpa.ws", - Icon: "icon.fenpa.ws", - }, - } - - for _, source := range sources { - err = postgres.CreateSource(ctx, gormDB, &source) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - } - tests := []struct { - name string - args args - want []models.Source - wantErr bool - }{ - { - name: "Test 1: Get all entries", - args: args{ - ctx: ctx, - }, - want: sources, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllSources(tt.args.ctx) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllSources() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkSources(got, tt.want) { - t.Errorf("GetAllSources() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetSourceByDomain(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) - } - defer container.Terminate(ctx) - - // Setup Test - - source := &models.Source{ - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - } - - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - sourceDomain models.AnthroveSourceDomain - } - tests := []struct { - name string - args args - want *models.Source - wantErr bool - }{ - { - name: "Test 1: Valid URL", - args: args{ - ctx: ctx, - sourceDomain: "e621.net", - }, - want: source, - wantErr: false, - }, - { - name: "Test 2: Invalid URL", - args: args{ - ctx: ctx, - sourceDomain: "eeeee.net", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 2: No URL", - args: args{ - ctx: ctx, - sourceDomain: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetSourceByDomain(tt.args.ctx, tt.args.sourceDomain) - if (err != nil) != tt.wantErr { - t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkSource(got, tt.want) { - t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_migrateDatabase(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) - } - defer container.Terminate(ctx) - - // Setup Test - - // Test - type args struct { - dbPool *gorm.DB - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Migrate Databases", - args: args{dbPool: gormDB}, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.migrateDatabase(tt.args.dbPool); (err != nil) != tt.wantErr { - t.Errorf("migrateDatabase() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -// Helper functions for validation purposes -func checkSource(got *models.Source, want *models.Source) bool { - - if want == nil && got == nil { - return true - } - - if got.Domain != want.Domain { - return false - } - - return true - -} -func checkPost(got *models.Post, want *models.Post) bool { - - if got == nil && want == nil { - return true - } else if got == nil || want == nil { - return false - } - - if got.ID != want.ID { - return false - } - - if got.Rating != want.Rating { - return false - } - - return true -} -func checkSources(got []models.Source, want []models.Source) bool { - for i, source := range want { - if source.DisplayName != got[i].DisplayName { - return false - } - if source.Domain != got[i].Domain { - return false - } - if source.Icon != got[i].Icon { - return false - } - } - - return true -} -func checkUser(got []models.User, want []models.User) bool { - for i, user := range want { - if user.ID != got[i].ID { - return false - } - } - return true -} -func checkUserSource(got *models.UserSource, want *models.UserSource) bool { - - if got == nil && want == nil { - return true - } else if got == nil || want == nil { - return false - } - - if got.UserID != want.UserID { - return false - } - if got.AccountUsername != want.AccountUsername { - return false - } - if got.Source.DisplayName != want.Source.DisplayName { - return false - } - if got.Source.Domain != want.Source.Domain { - return false - } - if got.Source.Icon != want.Source.Icon { - return false - } - - return true -} - -//------------------------- - -func Test_postgresqlConnection_CreateTagAlias(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validTagAliasName01 := models.AnthroveTagAliasName("httyd") - validTagAliasName02 := models.AnthroveTagAliasName("dragon") - - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - tagAliasName models.AnthroveTagAliasName - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - tagAliasName: validTagAliasName01, - tagID: validTagID, - }, - wantErr: false, - }, - { - name: "Test 2: No TagAliasName", - args: args{ - ctx: ctx, - tagAliasName: "", - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 4: No tagID", - args: args{ - ctx: ctx, - tagAliasName: validTagAliasName01, - tagID: "", - }, - wantErr: true, - }, - { - name: "Test 6: Invalide tagID", - args: args{ - ctx: ctx, - tagAliasName: validTagAliasName02, - tagID: "aaa", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateTagAlias(tt.args.ctx, tt.args.tagAliasName, tt.args.tagID); (err != nil) != tt.wantErr { - t.Errorf("CreateTagAlias() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateTagAliasInBatch(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) - } - defer container.Terminate(ctx) - - // Setup Test - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - err = postgres.CreateTagInBatchAndUpdate(ctx, gormDB, tags, len(tags)) - if err != nil { - t.Fatal(err) - } - - tagAlias := []models.TagAlias{ - { - Name: "test1", - TagID: tags[0].Name, - }, - { - Name: "test2", - TagID: tags[1].Name, - }, - { - Name: "test3", - TagID: tags[2].Name, - }, - { - Name: "test4", - TagID: tags[3].Name, - }, - } - emptyTagAlias := []models.TagAlias{} - - // Test - type args struct { - ctx context.Context - tagAliases []models.TagAlias - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - tagAliases: tagAlias, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - tagAliases: emptyTagAlias, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - tagAliases: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - tagAliases: tagAlias, - batchSize: 0, - }, - wantErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateTagAliasInBatch(tt.args.ctx, tt.args.tagAliases, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagAliasInBatch() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTagAlias(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagAlias{ - { - Name: string(validTagAliases[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[2]), - TagID: string(validTagID), - }, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = postgres.CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - } - tests := []struct { - name string - args args - want []models.TagAlias - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ctx: ctx}, - want: expectedResult, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagAlias(tt.args.ctx) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagAlias() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagAlias() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTagAliasByTag(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagAlias{ - { - Name: string(validTagAliases[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[2]), - TagID: string(validTagID), - }, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = postgres.CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - want []models.TagAlias - wantErr bool - }{ - { - name: "Test 1: Valid TagID", - args: args{ - ctx: ctx, - tagID: validTagID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: No TagID", - args: args{ - ctx: ctx, - tagID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Invalid TagID", - args: args{ - ctx: ctx, - tagID: "adads", - }, - want: []models.TagAlias{}, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagAliasByTag(tt.args.ctx, tt.args.tagID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagAliasByTag() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagAliasByTag() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_DeleteTagAlias(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = postgres.CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - tagAliasName models.AnthroveTagAliasName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveTagAliasName", - args: args{ - ctx: ctx, - tagAliasName: validTagAliases[0], - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveTagAliasName", - args: args{ - ctx: ctx, - tagAliasName: "asdad", - }, - wantErr: false, - }, - { - name: "Test 3: No AnthroveTagAliasName", - args: args{ - ctx: ctx, - tagAliasName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.DeleteTagAlias(tt.args.ctx, tt.args.tagAliasName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -//-------------------------- - -func Test_postgresqlConnection_CreateTagGroup(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validTagGroupName01 := models.AnthroveTagGroupName("httyd") - validTagGroupName02 := models.AnthroveTagGroupName("dragon") - - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - tagGroupName models.AnthroveTagGroupName - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - tagGroupName: validTagGroupName01, - tagID: validTagID, - }, - wantErr: false, - }, - { - name: "Test 2: No TagGroupName", - args: args{ - ctx: ctx, - tagGroupName: "", - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 4: No tagID", - args: args{ - ctx: ctx, - tagGroupName: validTagGroupName01, - tagID: "", - }, - wantErr: true, - }, - { - name: "Test 5: Duplicate tagID", - args: args{ - ctx: ctx, - tagGroupName: validTagGroupName01, - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 6: Invalide tagID", - args: args{ - ctx: ctx, - tagGroupName: validTagGroupName02, - tagID: "aaa", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateTagGroup(tt.args.ctx, tt.args.tagGroupName, tt.args.tagID); (err != nil) != tt.wantErr { - t.Errorf("CreateTagGroup() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateTagGroupInBatch(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) - } - defer container.Terminate(ctx) - - // Setup Test - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - err = postgres.CreateTagInBatchAndUpdate(ctx, gormDB, tags, len(tags)) - if err != nil { - t.Fatal(err) - } - - tagGroup := []models.TagGroup{ - { - Name: "test1", - TagID: tags[0].Name, - }, - { - Name: "test2", - TagID: tags[1].Name, - }, - { - Name: "test3", - TagID: tags[2].Name, - }, - { - Name: "test4", - TagID: tags[3].Name, - }, - } - emptyTagGroup := []models.TagGroup{} - - // Test - type args struct { - ctx context.Context - tagGroups []models.TagGroup - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - tagGroups: tagGroup, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - tagGroups: emptyTagGroup, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - tagGroups: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - tagGroups: tagGroup, - batchSize: 0, - }, - wantErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateTagGroupInBatch(tt.args.ctx, tt.args.tagGroups, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagGroupInBatch() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTagGroup(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagGroup{ - { - Name: string(validTagGroupes[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[2]), - TagID: string(validTagID), - }, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = postgres.CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - } - tests := []struct { - name string - args args - want []models.TagGroup - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ctx: ctx}, - want: expectedResult, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagGroup(tt.args.ctx) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagGroup() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagGroup() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTagGroupByTag(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagGroup{ - { - Name: string(validTagGroupes[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[2]), - TagID: string(validTagID), - }, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = postgres.CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - want []models.TagGroup - wantErr bool - }{ - { - name: "Test 1: Valid TagID", - args: args{ - ctx: ctx, - tagID: validTagID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: No TagID", - args: args{ - ctx: ctx, - tagID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Invalid TagID", - args: args{ - ctx: ctx, - tagID: "adads", - }, - want: []models.TagGroup{}, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagGroupByTag(tt.args.ctx, tt.args.tagID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagGroupByTag() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagGroupByTag() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_DeleteTagGroup(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = postgres.CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - tagGroupName models.AnthroveTagGroupName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveTagGroupName", - args: args{ - ctx: ctx, - tagGroupName: validTagGroupes[0], - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveTagGroupName", - args: args{ - ctx: ctx, - tagGroupName: "asdad", - }, - wantErr: false, - }, - { - name: "Test 3: No AnthroveTagGroupName", - args: args{ - ctx: ctx, - tagGroupName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.DeleteTagGroup(tt.args.ctx, tt.args.tagGroupName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -//-------------------------- - -func Test_postgresqlConnection_UpdateUserSourceScrapeTimeInterval(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - scrapeTime models.AnthroveScrapeTimeInterval - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "111", - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "", - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 5: scrapeTime is empty", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: validSourceID, - scrapeTime: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.UpdateUserSourceScrapeTimeInterval(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.scrapeTime); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceScrapeTimeInterval() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_UpdateUserSourceLastScrapeTime(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - validScrapeTime := models.AnthroveUserLastScrapeTime(time.Now()) - inValidScrapeTime := models.AnthroveUserLastScrapeTime{} - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - lastScrapeTime models.AnthroveUserLastScrapeTime - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "111", - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "", - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 5: scrapeTime is empty", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: validSourceID, - lastScrapeTime: inValidScrapeTime, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.UpdateUserSourceLastScrapeTime(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.lastScrapeTime); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceLastScrapeTime() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_UpdateUserSourceValidation(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - valid bool - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: validSourceID, - valid: true, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - valid: true, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: validSourceID, - valid: true, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "111", - valid: true, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "", - valid: true, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.UpdateUserSourceValidation(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.valid); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceValidation() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -//-------------------------- - -func Test_postgresqlConnection_DeleteTag(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - tagName models.AnthroveTagName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid TagName", - args: args{ - ctx: ctx, - tagName: models.AnthroveTagName(validTagID), - }, - wantErr: false, - }, - { - name: "Test 2: Invalid TagName", - args: args{ - ctx: ctx, - tagName: models.AnthroveTagName("aaa"), - }, - wantErr: false, - }, - { - name: "Test 3: No TagName", - args: args{ - ctx: ctx, - tagName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.DeleteTag(tt.args.ctx, tt.args.tagName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTagsByTagType(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validTags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Character, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Alphyron", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - } - - expectetResult := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Character, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Alphyron", - Type: models.Character, - }, - } - - for _, tag := range validTags { - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - tagType models.TagType - } - tests := []struct { - name string - args args - want []models.Tag - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - tagType: models.Character, - }, - want: expectetResult, - wantErr: false, - }, - { - name: "Test 2: invalid Tag Type", - args: args{ - ctx: ctx, - tagType: "aa", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: No Tag Type", - args: args{ - ctx: ctx, - tagType: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagsByTagType(tt.args.ctx, tt.args.tagType) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagsByTagType() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagsByTagType() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_CreateTagInBatchAndUpdate(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) - } - defer container.Terminate(ctx) - - // Setup Test - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - emptyTags := []models.Tag{} - - // Test - type args struct { - ctx context.Context - tags []models.Tag - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - tags: tags, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - tags: emptyTags, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - tags: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - tags: nil, - batchSize: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateTagInBatchAndUpdate(tt.args.ctx, tt.args.tags, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagInBatchAndUpdate() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreatePostInBatch(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) - } - defer container.Terminate(ctx) - - // Setup Tests - - validPosts := []models.Post{ - { - Rating: models.SFW, - }, - { - Rating: models.NSFW, - }, - { - Rating: models.Questionable, - }, - } - - emptyPost := []models.Post{} - - // Test - type args struct { - ctx context.Context - anthrovePost []models.Post - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - anthrovePost: validPosts, - batchSize: len(validPosts), - }, - wantErr: false, - }, - { - name: "Test 2: Emtpy Data", - args: args{ - ctx: ctx, - anthrovePost: emptyPost, - batchSize: 0, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Data", - args: args{ - ctx: ctx, - anthrovePost: nil, - batchSize: 0, - }, - wantErr: true, - }, - { - name: "Test 4: batchSize 0", - args: args{ - ctx: ctx, - anthrovePost: validPosts, - batchSize: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreatePostInBatch(tt.args.ctx, tt.args.anthrovePost, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreatePostInBatch() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func checkFavoritePosts(got *models.FavoriteList, want *models.FavoriteList) bool { - if got == nil && want == nil { - return true - } else if got == nil || want == nil { - return false - } - - for i, post := range got.Posts { - if post.ID == want.Posts[i].ID { - } else { - return false - } - } - - return true -} - -func Test_postgresqlConnection_ExecuteRawStatement(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) - } - defer container.Terminate(ctx) - - // Test - type args struct { - ctx context.Context - query string - args []any - } - tests := []struct { - name string - args args - want *sql.Rows - wantErr bool - }{ - { - name: "Test 01: Empty Query", - args: args{ - ctx: ctx, - query: "", - args: nil, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 02: Nil Query", - args: args{ - ctx: ctx, - query: "aasd", - args: nil, - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - err := p.ExecuteRawStatement(tt.args.ctx, tt.args.query, tt.args.args...) - if (err != nil) != tt.wantErr { - t.Errorf("ExecuteRawStatement() error = %v, wantErr %v", err, tt.wantErr) - return - } - }) - } -} - -func Test_postgresqlConnection_QueryRawStatement(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) - } - defer container.Terminate(ctx) - - // Test - type args struct { - ctx context.Context - query string - args []any - } - tests := []struct { - name string - args args - want *sql.Rows - wantErr bool - }{ - { - name: "Test 01: Empty Query", - args: args{ - ctx: ctx, - query: "", - args: nil, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 02: Nil Query", - args: args{ - ctx: ctx, - query: "aasd", - args: nil, - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.QueryRawStatement(tt.args.ctx, tt.args.query, tt.args.args...) - if (err != nil) != tt.wantErr { - t.Errorf("QueryRawStatement() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("QueryRawStatement() got = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/pkg/database/tag.go b/pkg/database/tag.go deleted file mode 100644 index c7fe822..0000000 --- a/pkg/database/tag.go +++ /dev/null @@ -1,20 +0,0 @@ -package database - -import ( - "context" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" -) - -type Tag interface { - CreateTag(ctx context.Context, tagName models.AnthroveTagName, tagType models.TagType) error - - CreateTagInBatchAndUpdate(ctx context.Context, tags []models.Tag, batchSize int) error - - // GetAllTags retrieves all tags. - GetAllTags(ctx context.Context) ([]models.Tag, error) - - GetAllTagsByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) - - DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error -} diff --git a/pkg/database/tagalias.go b/pkg/database/tagalias.go deleted file mode 100644 index 0f26efb..0000000 --- a/pkg/database/tagalias.go +++ /dev/null @@ -1,19 +0,0 @@ -package database - -import ( - "context" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" -) - -type TagAlias interface { - CreateTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName, tagID models.AnthroveTagID) error - - CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error - - GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error) - - GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) - - DeleteTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName) error -} diff --git a/pkg/database/taggroup.go b/pkg/database/taggroup.go deleted file mode 100644 index e293c68..0000000 --- a/pkg/database/taggroup.go +++ /dev/null @@ -1,19 +0,0 @@ -package database - -import ( - "context" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" -) - -type TagGroup interface { - CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) error - - CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error - - GetAllTagGroup(ctx context.Context) ([]models.TagGroup, error) - - GetAllTagGroupByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagGroup, error) - - DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error -} diff --git a/pkg/database/user.go b/pkg/database/user.go deleted file mode 100644 index ecf5900..0000000 --- a/pkg/database/user.go +++ /dev/null @@ -1,42 +0,0 @@ -package database - -import ( - "context" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" -) - -type User interface { - - // CreateUserWithRelationToSource adds a user with a relation to a source. - CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error - - // CreateReferenceBetweenUserAndPost links a user with a post. - CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error - - UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error - - UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error - - UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error - - GetAllUsers(ctx context.Context) ([]models.User, error) - - // GetUserFavoritesCount retrieves the count of a user's favorites. - GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) - - // GetAllUserSources retrieves the source links of a user. - GetAllUserSources(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) - - // GetUserSourceBySourceID retrieves a specified source link of a user. - GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (*models.UserSource, error) - - // GetAllUserFavoritesWithPagination retrieves a user's favorite posts with pagination. - GetAllUserFavoritesWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) - - // GetAllTagsFromUser retrieves a user's tags through their favorite posts. - GetAllTagsFromUser(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) - - // CheckIfUserHasPostAsFavorite checks if a user-post link exists. - CheckIfUserHasPostAsFavorite(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID models.AnthrovePostID) (bool, error) -} -- 2.45.2 From fefc777888f98fe6949048ba64c520edb2af5c5e Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 21:39:22 +0200 Subject: [PATCH 04/77] feat(impl): Add database client and migration functionality Implement connection to PostgreSQL database using GORM. Embed SQL migrations and handle migration execution with logging capabilities. Define configuration structure for database settings. --- pkg/database/client.go | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 pkg/database/client.go diff --git a/pkg/database/client.go b/pkg/database/client.go new file mode 100644 index 0000000..89ff2ac --- /dev/null +++ b/pkg/database/client.go @@ -0,0 +1,68 @@ +package database + +import ( + "context" + "embed" + "fmt" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + migrate "github.com/rubenv/sql-migrate" + log "github.com/sirupsen/logrus" + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +//go:embed migrations/*.sql +var embedMigrations embed.FS +var client *gorm.DB + +func Connect(_ context.Context, config models.DatabaseConfig) error { + var localSSL string + + if config.SSL { + localSSL = "require" + } else { + localSSL = "disable" + } + + dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.Timezone) + sqlDB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + + if err != nil { + return err + } + + client = sqlDB + err = migrateDatabase(sqlDB, config) + if err != nil { + return err + } + + return nil +} + +func migrateDatabase(dbPool *gorm.DB, config models.DatabaseConfig) error { + dialect := "postgres" + migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"} + + db, err := dbPool.DB() + if err != nil { + return fmt.Errorf("postgres migration: %v", err) + } + + n, err := migrate.Exec(db, dialect, migrations, migrate.Up) + if err != nil { + return fmt.Errorf("postgres migration: %v", err) + } + + if config.Debug { + if n != 0 { + log.Infof("postgres migration: applied %d migrations!", n) + + } else { + log.Info("postgres migration: nothing to migrate") + + } + } + + return nil +} -- 2.45.2 From b8e29de4fc25152f3c14a3184a85e0d75e9fa110 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 22:14:13 +0200 Subject: [PATCH 05/77] refactor(error): error handling in database package - Replace custom error types with string constants for common database errors. - Simplify the Database struct to include a Reason field for error messages. - Update Error method to format the error output using the Reason. - Modify validation error constants for consistency and clarity. --- pkg/error/database.go | 32 +++++++++++++------------------- pkg/error/validation.go | 12 +++++++----- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/pkg/error/database.go b/pkg/error/database.go index 278a043..c372d14 100644 --- a/pkg/error/database.go +++ b/pkg/error/database.go @@ -1,25 +1,19 @@ package error -type EntityAlreadyExists struct{} +import "fmt" -func (e *EntityAlreadyExists) Error() string { - return "EntityAlreadyExists error" +const ( + EntityAlreadyExists = "EntityAlreadyExists" + NoDataWritten = "NoDataWritten" + NoDataFound = "NoDataFound" + DatabaseIsNotConnected = "database is not connected" + DuplicateKey = "DuplicateKey" +) + +type Database struct { + Reason string } -type NoDataWritten struct{} - -func (e *NoDataWritten) Error() string { - return "NoDataWritten error" -} - -type NoDataFound struct{} - -func (e *NoDataFound) Error() string { - return "NoDataFound error" -} - -type NoRelationCreated struct{} - -func (e *NoRelationCreated) Error() string { - return "relationship creation error" +func (e Database) Error() string { + return fmt.Sprintf("Database error: %s", e.Reason) } diff --git a/pkg/error/validation.go b/pkg/error/validation.go index 27ec3c4..6140443 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -3,11 +3,13 @@ package error import "fmt" const ( - AnthroveUserIDIsEmpty = "anthrovePostID cannot be empty" - AnthroveUserIDToShort = "anthrovePostID needs to be 25 characters long" - AnthroveSourceIDEmpty = "anthroveSourceID cannot be empty" - AnthroveSourceIDToShort = "anthroveSourceID needs to be 25 characters long" - AnthroveTagIDEmpty = "tagID cannot be empty" + UserIDIsEmpty = "anthrovePostID cannot be empty" + UserIDToShort = "anthrovePostID needs to be 25 characters long" + SourceIDEmpty = "anthroveSourceID cannot be empty" + SourceIDToShort = "anthroveSourceID needs to be 25 characters long" + UserSourceIDEmpty = "anthroveUserSourceID cannot be empty" + UserSourceIDToShort = "anthroveUserSourceID needs to be 25 characters long" + TagIDEmpty = "tagID cannot be empty" ) type EntityValidationFailed struct { -- 2.45.2 From 598ca747eb24391400bc5bba4485ebceae8b2b64 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 22:14:56 +0200 Subject: [PATCH 06/77] refactor(types): using correct types --- pkg/models/userSource_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/models/userSource_test.go b/pkg/models/userSource_test.go index ff8cd95..30b8efa 100644 --- a/pkg/models/userSource_test.go +++ b/pkg/models/userSource_test.go @@ -5,9 +5,9 @@ import "testing" func TestUserSource_TableName(t *testing.T) { type fields struct { User User - UserID string + UserID UserID Source Source - SourceID string + SourceID SourceID ScrapeTimeInterval string AccountUsername string AccountID string -- 2.45.2 From f798869ef5fcb9722d5b210b4c09a437e7a47c06 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 22:16:03 +0200 Subject: [PATCH 07/77] feat(database): implementation of functions - Added functionality - Add connection checks to `CreateUserSource`, `UpdateUserSource`, `GetUserSourceByID`, and `DeleteUserSource` functions. - Introduce validation for empty and improperly formatted IDs in `UpdateUserSource`, `GetUserSourceByID`, and `DeleteUserSource`. - Handle duplicate key errors and record not found errors with appropriate custom error messages. --- pkg/database/source.go | 88 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/pkg/database/source.go b/pkg/database/source.go index 3967a3f..d9d1db5 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -2,23 +2,107 @@ package database import ( "context" + "errors" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "gorm.io/gorm" ) func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) { - var user models.UserSource - return user, nil + if client == nil { + return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + result := client.WithContext(ctx).Create(&userSource) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return models.UserSource{}, &otterError.Database{Reason: otterError.DuplicateKey} + } + return models.UserSource{}, result.Error + } + + return userSource, nil } func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(userSource.ID) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty} + } + + updatedUserSource := models.UserSource{ + BaseModel: models.BaseModel[models.UserSourceID]{ + ID: userSource.ID, + }, + ScrapeTimeInterval: userSource.ScrapeTimeInterval, + AccountUsername: userSource.AccountUsername, + AccountID: userSource.AccountID, + LastScrapeTime: userSource.LastScrapeTime, + AccountValidate: userSource.AccountValidate, + } + + result := client.WithContext(ctx).Updates(&updatedUserSource) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + return nil } func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) { var user models.UserSource + + if client == nil { + return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty} + } + + if len(id) != 25 { + return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} + } + + result := client.WithContext(ctx).First(&user, id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound} + } + return models.UserSource{}, result.Error + } + return user, nil } func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { + var user models.UserSource + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty} + } + + if len(id) != 25 { + return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} + } + + result := client.WithContext(ctx).Delete(&user, id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + return nil } -- 2.45.2 From 85d7905e8aa5a4e39bdcbdfd07c42e096b396ad6 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 22:43:58 +0200 Subject: [PATCH 08/77] refactor: renamed variables - Update model references from `UserFavorites` to `UserFavorite` in multiple files. - Adjust database queries and struct definitions accordingly to maintain consistency. --- internal/postgres/relationships.go | 4 ++-- internal/postgres/user.go | 6 +++--- pkg/models/post.go | 2 +- pkg/models/post_test.go | 2 +- pkg/models/user.go | 4 ++-- pkg/models/userFavorite_test.go | 6 +++--- pkg/models/user_test.go | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go index 5e03a12..e0d6e3e 100644 --- a/internal/postgres/relationships.go +++ b/internal/postgres/relationships.go @@ -62,7 +62,7 @@ func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrov return &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} } - userFavorite := models.UserFavorites{ + userFavorite := models.UserFavorite{ UserID: string(anthroveUserID), PostID: string(anthrovePostID), } @@ -106,7 +106,7 @@ func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrove return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} } - result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count) + result := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return false, &otterError.NoDataFound{} diff --git a/internal/postgres/user.go b/internal/postgres/user.go index 45a847a..625e285 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -113,7 +113,7 @@ func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID mode return 0, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} } - result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Count(&count) + result := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Count(&count) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return 0, &otterError.NoDataFound{} @@ -242,7 +242,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} } - db.WithContext(ctx).Joins("RIGHT JOIN \"UserFavorites\" AS of ON \"Post\".id = of.post_id AND of.user_id = ?", anthroveUserID).Preload("References").Offset(skip).Limit(limit).Find(&favoritePosts) + db.WithContext(ctx).Joins("RIGHT JOIN \"UserFavorite\" AS of ON \"Post\".id = of.post_id AND of.user_id = ?", anthroveUserID).Preload("References").Offset(skip).Limit(limit).Find(&favoritePosts) log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID, @@ -265,7 +265,7 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov rows, err := db.WithContext(ctx).Raw( `WITH user_posts AS ( - SELECT post_id FROM "UserFavorites" WHERE user_id = $1 + SELECT post_id FROM "UserFavorite" WHERE user_id = $1 ) SELECT post_tags.tag_name AS tag_name, count(*) AS count, (SELECT tag_type FROM "Tag" WHERE "Tag".name = post_tags.tag_name LIMIT 1) AS tag_type FROM post_tags, user_posts WHERE post_tags.post_id IN (user_posts.post_id) GROUP BY post_tags.tag_name ORDER BY tag_type DESC, tag_name DESC`, anthroveUserID).Rows() if err != nil { diff --git a/pkg/models/post.go b/pkg/models/post.go index 8b24e60..0d0dda3 100644 --- a/pkg/models/post.go +++ b/pkg/models/post.go @@ -5,7 +5,7 @@ type Post struct { BaseModel[PostID] Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"` Tags []Tag `json:"-" gorm:"many2many:post_tags;"` - Favorites []UserFavorites `json:"-" gorm:"foreignKey:PostID"` + Favorites []UserFavorite `json:"-" gorm:"foreignKey:PostID"` References []PostReference `json:"references" gorm:"foreignKey:PostID"` } diff --git a/pkg/models/post_test.go b/pkg/models/post_test.go index 7c6856f..b4c856e 100644 --- a/pkg/models/post_test.go +++ b/pkg/models/post_test.go @@ -7,7 +7,7 @@ func TestPost_TableName(t *testing.T) { BaseModel BaseModel[PostID] Rating Rating Tags []Tag - Favorites []UserFavorites + Favorites []UserFavorite References []PostReference } tests := []struct { diff --git a/pkg/models/user.go b/pkg/models/user.go index e2a88c1..bf544ce 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -3,8 +3,8 @@ package models // User model type User struct { BaseModel[UserID] - Favorites []UserFavorites `json:"-" gorm:"foreignKey:UserID"` - Sources []UserSource `json:"-" gorm:"foreignKey:UserID"` + Favorites []UserFavorite `json:"-" gorm:"foreignKey:UserID"` + Sources []UserSource `json:"-" gorm:"foreignKey:UserID"` } func (User) TableName() string { diff --git a/pkg/models/userFavorite_test.go b/pkg/models/userFavorite_test.go index 3b69143..5a09532 100644 --- a/pkg/models/userFavorite_test.go +++ b/pkg/models/userFavorite_test.go @@ -17,14 +17,14 @@ func TestUserFavorite_TableName(t *testing.T) { want string }{ { - name: "Test 1: Is name UserFavorites", + name: "Test 1: Is name UserFavorite", fields: fields{}, - want: "UserFavorites", + want: "UserFavorite", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - us := UserFavorites{ + us := UserFavorite{ UserID: tt.fields.UserID, PostID: tt.fields.PostID, CreatedAt: tt.fields.CreatedAt, diff --git a/pkg/models/user_test.go b/pkg/models/user_test.go index 34db57f..09d3f2f 100644 --- a/pkg/models/user_test.go +++ b/pkg/models/user_test.go @@ -5,7 +5,7 @@ import "testing" func TestUser_TableName(t *testing.T) { type fields struct { BaseModel BaseModel[UserID] - Favorites []UserFavorites + Favorites []UserFavorite Sources []UserSource } tests := []struct { -- 2.45.2 From 3e13046706725cd7d9470010b9cfc057fca7e4a5 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 22:44:55 +0200 Subject: [PATCH 09/77] fix(query): added conditions added the condition for the id --- pkg/database/source.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/database/source.go b/pkg/database/source.go index d9d1db5..d808b74 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -70,7 +70,7 @@ func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.User return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} } - result := client.WithContext(ctx).First(&user, id) + result := client.WithContext(ctx).First(&user, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound} -- 2.45.2 From b6c037cb22e7440f06690cb40f981f4a14271c20 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 22:45:12 +0200 Subject: [PATCH 10/77] refactor: Refactor UserFavorites model to UserFavorite for clarity and consistency - Rename `UserFavorites` struct to `UserFavorite` - Update the `TableName` method to reflect the new struct name --- pkg/models/userFavorite.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pkg/models/userFavorite.go b/pkg/models/userFavorite.go index 2572ce8..920d22a 100644 --- a/pkg/models/userFavorite.go +++ b/pkg/models/userFavorite.go @@ -1,16 +1,13 @@ package models -import "time" - -type UserFavorites struct { +type UserFavorite struct { BaseModel[UserFavoriteID] UserID string `json:"user_id"` PostID string `json:"post_id"` UserSourceID UserSourceID `json:"user_source_id"` UserSource UserSource `json:"-" gorm:"foreignKey:ID;references:UserSourceID"` - CreatedAt time.Time `json:"created_at"` } -func (UserFavorites) TableName() string { +func (UserFavorite) TableName() string { return "UserFavorites" } -- 2.45.2 From 7720110a0a7e2fab5c72b6bbc7bc51ad56a4b187 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 22:45:25 +0200 Subject: [PATCH 11/77] refactor: Update validation error messages for clarity and consistency --- pkg/error/validation.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/error/validation.go b/pkg/error/validation.go index 6140443..360e40b 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -3,13 +3,18 @@ package error import "fmt" const ( - UserIDIsEmpty = "anthrovePostID cannot be empty" - UserIDToShort = "anthrovePostID needs to be 25 characters long" - SourceIDEmpty = "anthroveSourceID cannot be empty" - SourceIDToShort = "anthroveSourceID needs to be 25 characters long" - UserSourceIDEmpty = "anthroveUserSourceID cannot be empty" - UserSourceIDToShort = "anthroveUserSourceID needs to be 25 characters long" - TagIDEmpty = "tagID cannot be empty" + UserIDIsEmpty = "PostID cannot be empty" + UserIDToShort = "PostID needs to be 25 characters long" + SourceIDEmpty = "SourceID cannot be empty" + SourceIDToShort = "SourceID needs to be 25 characters long" + UserSourceIDEmpty = "UserSourceID cannot be empty" + UserSourceIDToShort = "UserSourceID needs to be 25 characters long" + TagIDEmpty = "tagID cannot be empty" + UserFavoriteListIsEmpty = "userFavoriteList cannot be empty" + UserFavoriteIDIsEmpty = "userFavoriteID cannot be empty" + UserFavoriteIDToShort = "UserFavoriteID needs to be 25 characters long" + + BatchSizeIsEmpty = "batchSize cannot be empty" ) type EntityValidationFailed struct { -- 2.45.2 From 1dc9b1fb7c8e49eaaacfcbab239116ac8df4b139 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 22:47:02 +0200 Subject: [PATCH 12/77] feat(database): favorites - Added functionality --- pkg/database/favorite.go | 125 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 pkg/database/favorite.go diff --git a/pkg/database/favorite.go b/pkg/database/favorite.go new file mode 100644 index 0000000..b2194b5 --- /dev/null +++ b/pkg/database/favorite.go @@ -0,0 +1,125 @@ +package database + +import ( + "context" + "errors" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "gorm.io/gorm" +) + +func CreateUserFavorite(ctx context.Context, userFav models.UserFavorite) (models.UserFavorite, error) { + if client == nil { + return models.UserFavorite{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + result := client.WithContext(ctx).Create(&userFav) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return models.UserFavorite{}, &otterError.Database{Reason: otterError.DuplicateKey} + } + return models.UserFavorite{}, result.Error + } + + return userFav, nil +} + +func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorite, batchSize int) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if userFav == nil { + return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty} + } + + if len(userFav) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty} + } + + if batchSize == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + } + + result := client.WithContext(ctx).CreateInBatches(&userFav, batchSize) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + + return nil +} + +func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(userFav.ID) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} + } + + if !userFav.DeletedAt.Valid { + return nil + } + + result := client.WithContext(ctx).Model(&userFav).Update("deleted_at", gorm.DeletedAt{}) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +} + +func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models.UserFavorite, error) { + var userFavorites models.UserFavorite + + if client == nil { + return models.UserFavorite{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return models.UserFavorite{}, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} + } + + result := client.WithContext(ctx).First(&userFavorites, "id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return models.UserFavorite{}, &otterError.Database{Reason: otterError.NoDataFound} + } + return models.UserFavorite{}, result.Error + } + + return userFavorites, nil +} + +func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error { + var userFavorite models.UserFavorite + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} + } + + if len(id) != 25 { + return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDToShort} + } + + result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +} -- 2.45.2 From 2b54f25eeacae08b7f9f0f1234d8d321a8d9a454 Mon Sep 17 00:00:00 2001 From: SoXX Date: Sun, 11 Aug 2024 00:13:46 +0200 Subject: [PATCH 13/77] feat(database): added all missing functions --- pkg/database/post.go | 127 +++++++++++++++++++++++++++++++ pkg/database/source.go | 151 ++++++++++++++++++++++++------------- pkg/database/tag.go | 81 ++++++++++++++++++++ pkg/database/tagAlias.go | 80 ++++++++++++++++++++ pkg/database/tagGroup.go | 80 ++++++++++++++++++++ pkg/database/userSource.go | 108 ++++++++++++++++++++++++++ pkg/error/validation.go | 33 ++++++-- pkg/models/const.go | 20 ++--- pkg/models/tag.go | 10 +-- 9 files changed, 615 insertions(+), 75 deletions(-) create mode 100644 pkg/database/post.go create mode 100644 pkg/database/tag.go create mode 100644 pkg/database/tagAlias.go create mode 100644 pkg/database/tagGroup.go create mode 100644 pkg/database/userSource.go diff --git a/pkg/database/post.go b/pkg/database/post.go new file mode 100644 index 0000000..989c39a --- /dev/null +++ b/pkg/database/post.go @@ -0,0 +1,127 @@ +package database + +import ( + "context" + "errors" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "gorm.io/gorm" +) + +func CreatePost(ctx context.Context, post models.Post) (models.Post, error) { + if client == nil { + return models.Post{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + result := client.WithContext(ctx).Create(&post) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return models.Post{}, &otterError.Database{Reason: otterError.DuplicateKey} + } + return models.Post{}, result.Error + } + + return post, nil +} + +func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if post == nil { + return &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty} + } + + if len(post) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty} + } + + if batchSize == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + } + + result := client.WithContext(ctx).CreateInBatches(&post, batchSize) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + + return nil +} + +func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) { + var post models.Post + + if client == nil { + return models.Post{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return models.Post{}, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty} + } + + result := client.WithContext(ctx).First(&post, "id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return models.Post{}, &otterError.Database{Reason: otterError.NoDataFound} + } + return models.Post{}, result.Error + } + + return post, nil +} + +func UpdatePost(ctx context.Context, anthrovePost models.Post) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(anthrovePost.ID) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty} + } + + updatePost := models.Post{ + Rating: anthrovePost.Rating, + Tags: anthrovePost.Tags, + References: anthrovePost.References, + } + + result := client.WithContext(ctx).Model(&updatePost).Update("deleted_at", gorm.DeletedAt{}) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +} + +func DeletePost(ctx context.Context, id models.PostID) error { + var userFavorite models.UserFavorite + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty} + } + + if len(id) != 25 { + return &otterError.EntityValidationFailed{Reason: otterError.PostIDToShort} + } + + result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +} diff --git a/pkg/database/source.go b/pkg/database/source.go index d808b74..89116a2 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -8,43 +8,40 @@ import ( "gorm.io/gorm" ) -func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) { +func CreateSource(ctx context.Context, source models.Source) (models.Source, error) { if client == nil { - return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } - result := client.WithContext(ctx).Create(&userSource) + result := client.WithContext(ctx).Create(&source) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return models.UserSource{}, &otterError.Database{Reason: otterError.DuplicateKey} + return models.Source{}, &otterError.Database{Reason: otterError.DuplicateKey} } - return models.UserSource{}, result.Error + return models.Source{}, result.Error } - return userSource, nil + return source, nil } -func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { +func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize int) error { if client == nil { return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } - if len(userSource.ID) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty} + if source == nil { + return &otterError.EntityValidationFailed{Reason: otterError.SourceListIsEmpty} } - updatedUserSource := models.UserSource{ - BaseModel: models.BaseModel[models.UserSourceID]{ - ID: userSource.ID, - }, - ScrapeTimeInterval: userSource.ScrapeTimeInterval, - AccountUsername: userSource.AccountUsername, - AccountID: userSource.AccountID, - LastScrapeTime: userSource.LastScrapeTime, - AccountValidate: userSource.AccountValidate, + if len(source) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.SourceListIsEmpty} } - result := client.WithContext(ctx).Updates(&updatedUserSource) + if batchSize == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + } + + result := client.WithContext(ctx).CreateInBatches(&source, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { return &otterError.Database{Reason: otterError.DuplicateKey} @@ -55,48 +52,96 @@ func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { return nil } -func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) { - var user models.UserSource - - if client == nil { - return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} - } - - if len(id) == 0 { - return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty} - } - - if len(id) != 25 { - return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} - } - - result := client.WithContext(ctx).First(&user, "id = ?", id) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound} - } - return models.UserSource{}, result.Error - } - - return user, nil -} - -func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { - var user models.UserSource - +func UpdateSource(ctx context.Context, source models.Source) error { if client == nil { return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } - if len(id) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty} + if len(source.ID) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty} } - if len(id) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} + updateSource := models.Source{ + DisplayName: source.DisplayName, + Domain: source.Domain, + Icon: source.Icon, } - result := client.WithContext(ctx).Delete(&user, id) + result := client.WithContext(ctx).Model(&updateSource).Update("deleted_at", gorm.DeletedAt{}) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +} + +func GetSourceByID(ctx context.Context, id models.SourceID) (models.Source, error) { + var source models.Source + + if client == nil { + return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty} + } + + if len(id) != 25 { + return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceIDToShort} + } + + result := client.WithContext(ctx).First(&source, "id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return models.Source{}, &otterError.Database{Reason: otterError.NoDataFound} + } + return models.Source{}, result.Error + } + + return source, nil +} + +func GetSourceByDomain(ctx context.Context, sourceDomain models.SourceDomain) (models.Source, error) { + var source models.Source + + if client == nil { + return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(sourceDomain) == 0 { + return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceDomainIsEmpty} + } + + result := client.WithContext(ctx).First(&source, "domain = ?", sourceDomain) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return models.Source{}, &otterError.Database{Reason: otterError.NoDataFound} + } + return models.Source{}, result.Error + } + + return source, nil +} + +func DeleteSource(ctx context.Context, id models.SourceID) error { + var source models.Source + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty} + } + + if len(id) != 25 { + return &otterError.EntityValidationFailed{Reason: otterError.SourceIDToShort} + } + + result := client.WithContext(ctx).Delete(&source, id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return &otterError.Database{Reason: otterError.NoDataFound} diff --git a/pkg/database/tag.go b/pkg/database/tag.go new file mode 100644 index 0000000..50fe70a --- /dev/null +++ b/pkg/database/tag.go @@ -0,0 +1,81 @@ +package database + +import ( + "context" + "errors" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "gorm.io/gorm" +) + +func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagType) error { + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + tag := models.Tag{ + Name: tagName, + Type: tagType, + } + + result := client.WithContext(ctx).Create(&tag) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + + return nil +} + +func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if tags == nil { + return &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty} + } + + if len(tags) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty} + } + + if batchSize == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + } + + result := client.WithContext(ctx).CreateInBatches(&tags, batchSize) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + + return nil +} + +func DeleteTag(ctx context.Context, tagName models.TagName) error { + var tag models.Tag + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(tagName) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty} + } + + result := client.WithContext(ctx).Delete(&tag, tagName) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +} diff --git a/pkg/database/tagAlias.go b/pkg/database/tagAlias.go new file mode 100644 index 0000000..58d231b --- /dev/null +++ b/pkg/database/tagAlias.go @@ -0,0 +1,80 @@ +package database + +import ( + "context" + "errors" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "gorm.io/gorm" +) + +func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + tag := models.TagAlias{ + Name: tagAliasName, + TagID: tagName, + } + + result := client.WithContext(ctx).Create(&tag) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + + return nil +} + +func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if tagsAliases == nil { + return &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty} + } + + if len(tagsAliases) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty} + } + + if batchSize == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + } + + result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + + return nil +} + +func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error { + var tagAlias models.TagAlias + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(tagAliasName) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.TagAliasNameIsEmpty} + } + + result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +} diff --git a/pkg/database/tagGroup.go b/pkg/database/tagGroup.go new file mode 100644 index 0000000..287cb60 --- /dev/null +++ b/pkg/database/tagGroup.go @@ -0,0 +1,80 @@ +package database + +import ( + "context" + "errors" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "gorm.io/gorm" +) + +func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagName models.TagName) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + tag := models.TagGroup{ + Name: tagGroupName, + TagID: tagName, + } + + result := client.WithContext(ctx).Create(&tag) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + + return nil +} + +func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if tagsGroups == nil { + return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty} + } + + if len(tagsGroups) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty} + } + + if batchSize == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + } + + result := client.WithContext(ctx).CreateInBatches(&tagsGroups, batchSize) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + + return nil +} + +func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error { + var tagGroup models.TagGroup + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(tagGroupName) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.TagGroupNameIsEmpty} + } + + result := client.WithContext(ctx).Delete(&tagGroup, tagGroupName) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +} diff --git a/pkg/database/userSource.go b/pkg/database/userSource.go new file mode 100644 index 0000000..bf1879d --- /dev/null +++ b/pkg/database/userSource.go @@ -0,0 +1,108 @@ +package database + +import ( + "context" + "errors" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "gorm.io/gorm" +) + +func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) { + if client == nil { + return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + result := client.WithContext(ctx).Create(&userSource) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return models.UserSource{}, &otterError.Database{Reason: otterError.DuplicateKey} + } + return models.UserSource{}, result.Error + } + + return userSource, nil +} + +func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(userSource.ID) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty} + } + + updatedUserSource := models.UserSource{ + BaseModel: models.BaseModel[models.UserSourceID]{ + ID: userSource.ID, + }, + ScrapeTimeInterval: userSource.ScrapeTimeInterval, + AccountUsername: userSource.AccountUsername, + AccountID: userSource.AccountID, + LastScrapeTime: userSource.LastScrapeTime, + AccountValidate: userSource.AccountValidate, + } + + result := client.WithContext(ctx).Updates(&updatedUserSource) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + + return nil +} + +func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) { + var user models.UserSource + + if client == nil { + return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty} + } + + if len(id) != 25 { + return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} + } + + result := client.WithContext(ctx).First(&user, "id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound} + } + return models.UserSource{}, result.Error + } + + return user, nil +} + +func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { + var user models.UserSource + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty} + } + + if len(id) != 25 { + return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} + } + + result := client.WithContext(ctx).Delete(&user, id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +} diff --git a/pkg/error/validation.go b/pkg/error/validation.go index 360e40b..0e27823 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -3,16 +3,33 @@ package error import "fmt" const ( - UserIDIsEmpty = "PostID cannot be empty" - UserIDToShort = "PostID needs to be 25 characters long" - SourceIDEmpty = "SourceID cannot be empty" - SourceIDToShort = "SourceID needs to be 25 characters long" - UserSourceIDEmpty = "UserSourceID cannot be empty" - UserSourceIDToShort = "UserSourceID needs to be 25 characters long" - TagIDEmpty = "tagID cannot be empty" + UserIDIsEmpty = "postID cannot be empty" + UserIDToShort = "postID needs to be 25 characters long" + + SourceListIsEmpty = "sourceList cannot be empty" + SourceIDIsEmpty = "SourceID cannot be empty" + SourceIDToShort = "sourceID needs to be 25 characters long" + SourceDomainIsEmpty = "source Domain cannot be empty" + + UserSourceIDIsEmpty = "userSourceID cannot be empty" + UserSourceIDToShort = "userSourceID needs to be 25 characters long" + + TagListIsEmpty = "tagList cannot be empty" + TagNameIsEmpty = "tagName cannot be empty" + + TagAliasListIsEmpty = "tagAliasList cannot be empty" + TagAliasNameIsEmpty = "tagAliasName cannot be empty" + + TagGroupListIsEmpty = "tagGroupList cannot be empty" + TagGroupNameIsEmpty = "tagGroupName cannot be empty" + UserFavoriteListIsEmpty = "userFavoriteList cannot be empty" UserFavoriteIDIsEmpty = "userFavoriteID cannot be empty" - UserFavoriteIDToShort = "UserFavoriteID needs to be 25 characters long" + UserFavoriteIDToShort = "userFavoriteID needs to be 25 characters long" + + PostListIsEmpty = "userFavoriteList cannot be empty" + PostIDIsEmpty = "userFavoriteID cannot be empty" + PostIDToShort = "PostID needs to be 25 characters long" BatchSizeIsEmpty = "batchSize cannot be empty" ) diff --git a/pkg/models/const.go b/pkg/models/const.go index 4f690a4..a6f25bc 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -3,17 +3,19 @@ package models import "time" type ( - UserID string - PostID string - SourceID string - SourceDomain string - PostURL string - TagGroupName string - TagAliasName string - TagID string + UserID string + PostID string + PostURL string + + SourceID string + SourceDomain string + + TagName string + TagGroupName string + TagAliasName string + ScrapeTimeInterval int UserLastScrapeTime time.Time - TagName string Rating string TagType string diff --git a/pkg/models/tag.go b/pkg/models/tag.go index 09fafb9..53b5c1a 100644 --- a/pkg/models/tag.go +++ b/pkg/models/tag.go @@ -2,7 +2,7 @@ package models // Tag models type Tag struct { - Name string `json:"name" gorm:"primaryKey"` + Name TagName `json:"name" gorm:"primaryKey"` Type TagType `json:"type" gorm:"column:tag_type"` Aliases []TagAlias `json:"aliases" gorm:"foreignKey:TagID"` Groups []TagGroup `json:"groups" gorm:"foreignKey:TagID"` @@ -15,8 +15,8 @@ func (Tag) TableName() string { // TagAlias model type TagAlias struct { - Name string `json:"name" gorm:"primaryKey"` - TagID string `json:"tag_id"` + Name TagAliasName `json:"name" gorm:"primaryKey"` + TagID TagName `json:"tag_id"` } func (TagAlias) TableName() string { @@ -25,8 +25,8 @@ func (TagAlias) TableName() string { // TagGroup model type TagGroup struct { - Name string `json:"name" gorm:"primaryKey"` - TagID string `json:"tag_id"` + Name TagGroupName `json:"name" gorm:"primaryKey"` + TagID TagName `json:"tag_id"` } func (TagGroup) TableName() string { -- 2.45.2 From 46e6cdfa52f7ae03ff0ed7d506bdca0c88311565 Mon Sep 17 00:00:00 2001 From: SoXX Date: Sun, 11 Aug 2024 00:23:47 +0200 Subject: [PATCH 14/77] feat(database): added user --- pkg/database/user.go | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 pkg/database/user.go diff --git a/pkg/database/user.go b/pkg/database/user.go new file mode 100644 index 0000000..d78505a --- /dev/null +++ b/pkg/database/user.go @@ -0,0 +1,79 @@ +package database + +import ( + "context" + "errors" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "gorm.io/gorm" +) + +func CreateUser(ctx context.Context, id models.UserID) (models.User, error) { + if client == nil { + return models.User{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + user := models.User{ + BaseModel: models.BaseModel[models.UserID]{ + ID: id, + }, + } + + result := client.WithContext(ctx).Create(&user) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return models.User{}, &otterError.Database{Reason: otterError.DuplicateKey} + } + return models.User{}, result.Error + } + + return user, nil +} + +func GetUserByID(ctx context.Context, id models.UserID) (models.User, error) { + var user models.User + + if client == nil { + return models.User{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return models.User{}, &otterError.EntityValidationFailed{Reason: otterError.UserIDIsEmpty} + } + + if len(id) != 25 { + return models.User{}, &otterError.EntityValidationFailed{Reason: otterError.UserIDToShort} + } + + result := client.WithContext(ctx).First(&user, "id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return models.User{}, &otterError.Database{Reason: otterError.NoDataFound} + } + return models.User{}, result.Error + } + + return user, nil +} + +func DeleteUser(ctx context.Context, id models.UserID) error { + var user models.User + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserIDIsEmpty} + } + + result := client.WithContext(ctx).Delete(&user, id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +} -- 2.45.2 From 9fe2d0edc38239c3d59fd6c5819bee005cfc406b Mon Sep 17 00:00:00 2001 From: SoXX Date: Sun, 11 Aug 2024 00:24:02 +0200 Subject: [PATCH 15/77] fix(database): make function more coherent with the rest --- pkg/database/tag.go | 10 +++++----- pkg/database/tagAlias.go | 14 +++++++------- pkg/database/tagGroup.go | 14 +++++++------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/pkg/database/tag.go b/pkg/database/tag.go index 50fe70a..b2a011d 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -8,10 +8,10 @@ import ( "gorm.io/gorm" ) -func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagType) error { +func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagType) (models.Tag, error) { if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.Tag{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } tag := models.Tag{ @@ -22,12 +22,12 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy result := client.WithContext(ctx).Create(&tag) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.Database{Reason: otterError.DuplicateKey} + return models.Tag{}, &otterError.Database{Reason: otterError.DuplicateKey} } - return result.Error + return models.Tag{}, result.Error } - return nil + return tag, nil } func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) error { diff --git a/pkg/database/tagAlias.go b/pkg/database/tagAlias.go index 58d231b..b77879d 100644 --- a/pkg/database/tagAlias.go +++ b/pkg/database/tagAlias.go @@ -8,25 +8,25 @@ import ( "gorm.io/gorm" ) -func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) error { +func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) (models.TagAlias, error) { if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.TagAlias{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } - tag := models.TagAlias{ + tagAlias := models.TagAlias{ Name: tagAliasName, TagID: tagName, } - result := client.WithContext(ctx).Create(&tag) + result := client.WithContext(ctx).Create(&tagAlias) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.Database{Reason: otterError.DuplicateKey} + return models.TagAlias{}, &otterError.Database{Reason: otterError.DuplicateKey} } - return result.Error + return models.TagAlias{}, result.Error } - return nil + return tagAlias, nil } func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error { diff --git a/pkg/database/tagGroup.go b/pkg/database/tagGroup.go index 287cb60..cf155fe 100644 --- a/pkg/database/tagGroup.go +++ b/pkg/database/tagGroup.go @@ -8,25 +8,25 @@ import ( "gorm.io/gorm" ) -func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagName models.TagName) error { +func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagName models.TagName) (models.TagGroup, error) { if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.TagGroup{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } - tag := models.TagGroup{ + tagGroup := models.TagGroup{ Name: tagGroupName, TagID: tagName, } - result := client.WithContext(ctx).Create(&tag) + result := client.WithContext(ctx).Create(&tagGroup) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.Database{Reason: otterError.DuplicateKey} + return models.TagGroup{}, &otterError.Database{Reason: otterError.DuplicateKey} } - return result.Error + return models.TagGroup{}, result.Error } - return nil + return tagGroup, nil } func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error { -- 2.45.2 From e839e7ba2a1036d4edb79ba7d08e9212849d6b27 Mon Sep 17 00:00:00 2001 From: SoXX Date: Sun, 11 Aug 2024 00:26:20 +0200 Subject: [PATCH 16/77] feat(database): return gorm itself --- pkg/database/client.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/database/client.go b/pkg/database/client.go index 89ff2ac..57d59e2 100644 --- a/pkg/database/client.go +++ b/pkg/database/client.go @@ -4,6 +4,7 @@ import ( "context" "embed" "fmt" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" migrate "github.com/rubenv/sql-migrate" log "github.com/sirupsen/logrus" @@ -66,3 +67,11 @@ func migrateDatabase(dbPool *gorm.DB, config models.DatabaseConfig) error { return nil } + +func GetGorm(ctx context.Context) (*gorm.DB, error) { + if client == nil { + return nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + return client, nil +} -- 2.45.2 From 2d9db01d721215f4696b51744a66d09557e2cd72 Mon Sep 17 00:00:00 2001 From: SoXX Date: Sun, 11 Aug 2024 22:17:00 +0200 Subject: [PATCH 17/77] feat(telemetry): Implement telemetry setup in database client Add OpenTelemetry integration to the database client by creating a `setupTelemetry` function. Initialize a tracer and configure logging with the otellogrus hook. Call this function during the connection process to enable tracing and logging for database operations. --- pkg/database/client.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/pkg/database/client.go b/pkg/database/client.go index 57d59e2..7433440 100644 --- a/pkg/database/client.go +++ b/pkg/database/client.go @@ -8,13 +8,22 @@ import ( "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" migrate "github.com/rubenv/sql-migrate" log "github.com/sirupsen/logrus" + "go.opentelemetry.io/contrib/bridges/otellogrus" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/trace" "gorm.io/driver/postgres" "gorm.io/gorm" ) -//go:embed migrations/*.sql -var embedMigrations embed.FS -var client *gorm.DB +const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/database" + +var ( + //go:embed migrations/*.sql + embedMigrations embed.FS + client *gorm.DB + tracer trace.Tracer + logger *log.Logger +) func Connect(_ context.Context, config models.DatabaseConfig) error { var localSSL string @@ -32,12 +41,14 @@ func Connect(_ context.Context, config models.DatabaseConfig) error { return err } - client = sqlDB err = migrateDatabase(sqlDB, config) if err != nil { return err } + setupTelemetry() + + client = sqlDB return nil } @@ -68,6 +79,14 @@ func migrateDatabase(dbPool *gorm.DB, config models.DatabaseConfig) error { return nil } +func setupTelemetry() { + tracer = otel.Tracer(tracingName) + + hook := otellogrus.NewHook(tracingName) + logger.AddHook(hook) + +} + func GetGorm(ctx context.Context) (*gorm.DB, error) { if client == nil { return nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} -- 2.45.2 From a368f592340398928f69f44f5529fa7b19d1129b Mon Sep 17 00:00:00 2001 From: SoXX Date: Sun, 11 Aug 2024 22:17:24 +0200 Subject: [PATCH 18/77] feat(telemetry): Implement OpenTelemetry tracing and logging in tag management functions --- pkg/database/tag.go | 102 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 5 deletions(-) diff --git a/pkg/database/tag.go b/pkg/database/tag.go index b2a011d..47bdf1a 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -5,12 +5,25 @@ import ( "errors" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" ) func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagType) (models.Tag, error) { + ctx, span := tracer.Start(ctx, "CreateTag") + defer span.End() + + span.SetAttributes( + attribute.String("tag_name", string(tagName)), + attribute.String("tag_type", string(tagType)), + ) + + span.AddEvent("Starting tag creation") if client == nil { + logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected) + span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected}) return models.Tag{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } @@ -19,63 +32,142 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy Type: tagType, } + logger.WithContext(ctx).WithFields(log.Fields{ + "name": tagName, + "type": tagType, + }).Debug("attempting to create tag") + result := client.WithContext(ctx).Create(&tag) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + + logger.WithContext(ctx).WithFields(log.Fields{ + "name": tagName, + "type": tagType, + }).Error(otterError.DuplicateKey) + + span.RecordError(&otterError.Database{Reason: otterError.DuplicateKey}) + return models.Tag{}, &otterError.Database{Reason: otterError.DuplicateKey} } + + logger.WithContext(ctx).WithFields(log.Fields{ + "name": tagName, + "type": tagType, + }).Error(result.Error) + + span.RecordError(result.Error) + return models.Tag{}, result.Error } + logger.WithContext(ctx).WithFields(log.Fields{ + "name": tagName, + "type": tagType, + }).Debug("tag created") + span.AddEvent("Tag created successfully") return tag, nil } func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) error { + ctx, span := tracer.Start(ctx, "CreateTagInBatch") + defer span.End() + + span.SetAttributes( + attribute.Int64("batch_size", int64(batchSize)), + attribute.Int64("tag_count", int64(len(tags))), + ) + + span.AddEvent("Starting batch tag creation") + if client == nil { + logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected) + span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected}) return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } - if tags == nil { - return &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty} - } - - if len(tags) == 0 { + if tags == nil || len(tags) == 0 { + logger.WithContext(ctx).Error(otterError.TagListIsEmpty) + span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty}) return &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty} } if batchSize == 0 { + logger.WithContext(ctx).Error(otterError.BatchSizeIsEmpty) + span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_length": len(tags), + }).Debug("attempting to create tags") + result := client.WithContext(ctx).CreateInBatches(&tags, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_length": len(tags), + }).Error(otterError.DuplicateKey) + span.RecordError(&otterError.Database{Reason: otterError.DuplicateKey}) return &otterError.Database{Reason: otterError.DuplicateKey} } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_length": len(tags), + }).Error(result.Error) + span.RecordError(result.Error) return result.Error } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_length": len(tags), + }).Debug("tags created") + + span.AddEvent("Batch tags created successfully") return nil } func DeleteTag(ctx context.Context, tagName models.TagName) error { + ctx, span := tracer.Start(ctx, "DeleteTag") + defer span.End() + + span.SetAttributes( + attribute.String("tag_name", string(tagName)), + ) + + span.AddEvent("Starting tag deletion") + var tag models.Tag if client == nil { + logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected) + span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected}) return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } if len(tagName) == 0 { + logger.WithContext(ctx).Error(otterError.TagNameIsEmpty) + span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty}) return &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty} } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_name": tagName, + }).Debug("attempting to delete tag") + result := client.WithContext(ctx).Delete(&tag, tagName) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { + span.RecordError(&otterError.Database{Reason: otterError.NoDataFound}) return &otterError.Database{Reason: otterError.NoDataFound} } + span.RecordError(result.Error) return result.Error } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_name": tagName, + }).Debug("tag deleted") + + span.AddEvent("Tag deleted successfully") return nil } -- 2.45.2 From bb64c08185d885a76e8fce88099e0ec4b0d749fe Mon Sep 17 00:00:00 2001 From: SoXX Date: Sun, 11 Aug 2024 22:17:40 +0200 Subject: [PATCH 19/77] chore(dependencies): updated dependencies --- go.mod | 12 +++++++----- go.sum | 24 ++++++++++++++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 0f5fce6..e8481bd 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,9 @@ require ( github.com/sirupsen/logrus v1.9.3 github.com/testcontainers/testcontainers-go v0.32.0 github.com/testcontainers/testcontainers-go/modules/postgres v0.32.0 + go.opentelemetry.io/contrib/bridges/otellogrus v0.3.0 + go.opentelemetry.io/otel v1.28.0 + go.opentelemetry.io/otel/trace v1.28.0 gorm.io/driver/postgres v1.5.9 gorm.io/gorm v1.25.11 ) @@ -29,7 +32,7 @@ require ( github.com/docker/go-units v0.5.0 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-gorp/gorp/v3 v3.1.0 // indirect - github.com/go-logr/logr v1.4.1 // indirect + github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -60,12 +63,11 @@ require ( github.com/tklauser/numcpus v0.6.1 // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect - go.opentelemetry.io/otel v1.24.0 // indirect - go.opentelemetry.io/otel/metric v1.24.0 // indirect - go.opentelemetry.io/otel/trace v1.24.0 // indirect + go.opentelemetry.io/otel/log v0.4.0 // indirect + go.opentelemetry.io/otel/metric v1.28.0 // indirect golang.org/x/crypto v0.22.0 // indirect golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.19.0 // indirect + golang.org/x/sys v0.21.0 // indirect golang.org/x/text v0.14.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect google.golang.org/grpc v1.59.0 // indirect diff --git a/go.sum b/go.sum index e1cb8d0..0556fae 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,8 @@ github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSw github.com/go-gorp/gorp/v3 v3.1.0 h1:ItKF/Vbuj31dmV4jxA1qblpSwkl9g1typ24xoe70IGs= github.com/go-gorp/gorp/v3 v3.1.0/go.mod h1:dLEjIyyRNiXvNZ8PSmzpt1GsWAUK8kjVhEpjH8TixEw= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= -github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= @@ -144,20 +144,24 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw= github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +go.opentelemetry.io/contrib/bridges/otellogrus v0.3.0 h1:QHEj9AK6bEiEA9S5OdDUE9KAx4xp6pRkYMnybHDmjZU= +go.opentelemetry.io/contrib/bridges/otellogrus v0.3.0/go.mod h1:HRlW/1YWrBrbzB6FvHU7jUuz33F74PEvQVBL+b+wUhM= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw= -go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= -go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= +go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo= +go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4= go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 h1:Mne5On7VWdx7omSrSSZvM4Kw7cS7NQkOOmLcgscI51U= go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0/go.mod h1:IPtUMKL4O3tH5y+iXVyAXqpAwMuzC1IrxVS81rummfE= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMeyr1aBvBiPVYihXIaeIZba6b8E1bYp7lbdxK8CQg= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU= -go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= -go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco= +go.opentelemetry.io/otel/log v0.4.0 h1:/vZ+3Utqh18e8TPjuc3ecg284078KWrR8BRz+PQAj3o= +go.opentelemetry.io/otel/log v0.4.0/go.mod h1:DhGnQvky7pHy82MIRV43iXh3FlKN8UUKftn0KbLOq6I= +go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q= +go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s= go.opentelemetry.io/otel/sdk v1.19.0 h1:6USY6zH+L8uMH8L3t1enZPR3WFEmSTADlqldyHtJi3o= go.opentelemetry.io/otel/sdk v1.19.0/go.mod h1:NedEbbS4w3C6zElbLdPJKOpJQOrGUJ+GfzpjUvI0v1A= -go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= -go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= +go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g= +go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI= go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I= go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -188,8 +192,8 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= -golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q= golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -- 2.45.2 From 4707ea3f00627db1b9957f0377d8c0a81e734c17 Mon Sep 17 00:00:00 2001 From: SoXX Date: Sun, 11 Aug 2024 22:34:28 +0200 Subject: [PATCH 20/77] feat(error): Add error handling utility function Implement the HandleError function in the new error.go file to streamline error logging and tracing. Include necessary imports for context, logrus, and OpenTelemetry tracing. --- internal/utils/error.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 internal/utils/error.go diff --git a/internal/utils/error.go b/internal/utils/error.go new file mode 100644 index 0000000..9cb381a --- /dev/null +++ b/internal/utils/error.go @@ -0,0 +1,13 @@ +package utils + +import ( + "context" + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/trace" +) + +func HandleError(ctx context.Context, span trace.Span, logger *log.Logger, fields log.Fields, error error) error { + logger.WithContext(ctx).WithFields(fields).Error(error) + span.RecordError(error) + return error +} -- 2.45.2 From c17b78cc48779d838df8277e54ec6e5ce4517759 Mon Sep 17 00:00:00 2001 From: SoXX Date: Sun, 11 Aug 2024 22:34:53 +0200 Subject: [PATCH 21/77] refactor(logging): Refactor error handling in CreateTag, CreateTagInBatch, and DeleteTag functions Improve error handling in the CreateTag, CreateTagInBatch, and DeleteTag functions by utilizing a new utility function. Simplify logging and error management, ensuring more consistent error responses throughout these operations. --- pkg/database/tag.go | 72 +++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/pkg/database/tag.go b/pkg/database/tag.go index 47bdf1a..c3d4e7d 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -3,6 +3,7 @@ package database import ( "context" "errors" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" log "github.com/sirupsen/logrus" @@ -22,9 +23,7 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy span.AddEvent("Starting tag creation") if client == nil { - logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected) - span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected}) - return models.Tag{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.Tag{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } tag := models.Tag{ @@ -41,24 +40,18 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - logger.WithContext(ctx).WithFields(log.Fields{ + loggerFields := log.Fields{ "name": tagName, "type": tagType, - }).Error(otterError.DuplicateKey) - - span.RecordError(&otterError.Database{Reason: otterError.DuplicateKey}) - - return models.Tag{}, &otterError.Database{Reason: otterError.DuplicateKey} + } + return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) } - logger.WithContext(ctx).WithFields(log.Fields{ + loggerFields := log.Fields{ "name": tagName, "type": tagType, - }).Error(result.Error) - - span.RecordError(result.Error) - - return models.Tag{}, result.Error + } + return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error) } logger.WithContext(ctx).WithFields(log.Fields{ @@ -81,21 +74,15 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) err span.AddEvent("Starting batch tag creation") if client == nil { - logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected) - span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected}) - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if tags == nil || len(tags) == 0 { - logger.WithContext(ctx).Error(otterError.TagListIsEmpty) - span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty}) - return &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty} + return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty}) } if batchSize == 0 { - logger.WithContext(ctx).Error(otterError.BatchSizeIsEmpty) - span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) - return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) } logger.WithContext(ctx).WithFields(log.Fields{ @@ -105,17 +92,16 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) err result := client.WithContext(ctx).CreateInBatches(&tags, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - logger.WithContext(ctx).WithFields(log.Fields{ + loggerFields := log.Fields{ "tag_length": len(tags), - }).Error(otterError.DuplicateKey) - span.RecordError(&otterError.Database{Reason: otterError.DuplicateKey}) - return &otterError.Database{Reason: otterError.DuplicateKey} + } + return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) } - logger.WithContext(ctx).WithFields(log.Fields{ + + loggerFields := log.Fields{ "tag_length": len(tags), - }).Error(result.Error) - span.RecordError(result.Error) - return result.Error + } + return utils.HandleError(ctx, span, logger, loggerFields, result.Error) } logger.WithContext(ctx).WithFields(log.Fields{ @@ -139,15 +125,11 @@ func DeleteTag(ctx context.Context, tagName models.TagName) error { var tag models.Tag if client == nil { - logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected) - span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected}) - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(tagName) == 0 { - logger.WithContext(ctx).Error(otterError.TagNameIsEmpty) - span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty}) - return &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty} + return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagNameIsEmpty}) } logger.WithContext(ctx).WithFields(log.Fields{ @@ -157,11 +139,17 @@ func DeleteTag(ctx context.Context, tagName models.TagName) error { result := client.WithContext(ctx).Delete(&tag, tagName) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - span.RecordError(&otterError.Database{Reason: otterError.NoDataFound}) - return &otterError.Database{Reason: otterError.NoDataFound} + + loggerFields := log.Fields{ + "tag_name": tagName, + } + return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound}) } - span.RecordError(result.Error) - return result.Error + + loggerFields := log.Fields{ + "tag_name": tagName, + } + return utils.HandleError(ctx, span, logger, loggerFields, result.Error) } logger.WithContext(ctx).WithFields(log.Fields{ -- 2.45.2 From 6bec8e3373e6b1f325c902a14d8e68b911732177 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 09:57:31 +0200 Subject: [PATCH 22/77] chore(tracing): Update log field names for clarity --- pkg/database/tag.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/database/tag.go b/pkg/database/tag.go index c3d4e7d..7f590d7 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -3,6 +3,7 @@ package database import ( "context" "errors" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" @@ -32,8 +33,8 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy } logger.WithContext(ctx).WithFields(log.Fields{ - "name": tagName, - "type": tagType, + "tag_name": tagName, + "tag_type": tagType, }).Debug("attempting to create tag") result := client.WithContext(ctx).Create(&tag) @@ -41,22 +42,22 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy if errors.Is(result.Error, gorm.ErrDuplicatedKey) { loggerFields := log.Fields{ - "name": tagName, - "type": tagType, + "tag_name": tagName, + "tag_type": tagType, } return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) } loggerFields := log.Fields{ - "name": tagName, - "type": tagType, + "tag_name": tagName, + "tag_type": tagType, } return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error) } logger.WithContext(ctx).WithFields(log.Fields{ - "name": tagName, - "type": tagType, + "tag_name": tagName, + "tag_type": tagType, }).Debug("tag created") span.AddEvent("Tag created successfully") return tag, nil -- 2.45.2 From ca1e1a1da539319eace63e8689799afbde7087ce Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 09:58:14 +0200 Subject: [PATCH 23/77] feat(tracing): added tracing --- pkg/database/tagAlias.go | 116 +++++++++++++++++++++++++++++++++------ 1 file changed, 99 insertions(+), 17 deletions(-) diff --git a/pkg/database/tagAlias.go b/pkg/database/tagAlias.go index b77879d..aa1e958 100644 --- a/pkg/database/tagAlias.go +++ b/pkg/database/tagAlias.go @@ -3,14 +3,28 @@ package database import ( "context" "errors" + + "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" ) func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) (models.TagAlias, error) { + ctx, span := tracer.Start(ctx, "CreateTagAlias") + defer span.End() + + span.SetAttributes( + attribute.String("tag_alias_name", string(tagAliasName)), + attribute.String("tag_name", string(tagName)), + ) + + span.AddEvent("Starting tag alias creation") + if client == nil { - return models.TagAlias{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.TagAlias{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } tagAlias := models.TagAlias{ @@ -18,63 +32,131 @@ func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagNa TagID: tagName, } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_alias_name": tagAliasName, + "tag_name": tagName, + }).Debug("attempting to create tag alias") + result := client.WithContext(ctx).Create(&tagAlias) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return models.TagAlias{}, &otterError.Database{Reason: otterError.DuplicateKey} + + loggerFields := log.Fields{ + "tag_alias_name": tagAliasName, + "tag_name": tagName, + } + return models.TagAlias{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) } - return models.TagAlias{}, result.Error + + loggerFields := log.Fields{ + "tag_alias_name": tagAliasName, + "tag_name": tagName, + } + return models.TagAlias{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error) } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_alias_name": tagAliasName, + "tag_name": tagName, + }).Debug("tag alias created") + span.AddEvent("Tag alias created successfully") return tagAlias, nil } func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error { + ctx, span := tracer.Start(ctx, "CreateTagAliasInBatch") + defer span.End() + + span.SetAttributes( + attribute.Int64("batch_size", int64(batchSize)), + attribute.Int64("tag_count", int64(len(tagsAliases))), + ) + + span.AddEvent("Starting batch tag creation") + if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - if tagsAliases == nil { - return &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty} - } - - if len(tagsAliases) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty} + if tagsAliases == nil || len(tagsAliases) == 0 { + return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty}) } if batchSize == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_alias_length": len(tagsAliases), + }).Debug("attempting to create tags aliases") + result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.Database{Reason: otterError.DuplicateKey} + loggerFields := log.Fields{ + "tag_alias_length": len(tagsAliases), + } + return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) } - return result.Error + + loggerFields := log.Fields{ + "tag_alias_length": len(tagsAliases), + } + return utils.HandleError(ctx, span, logger, loggerFields, result.Error) } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_alias_length": len(tagsAliases), + }).Debug("batch tags aliases created") + + span.AddEvent("Batch tags aliases created successfully") return nil } func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error { + ctx, span := tracer.Start(ctx, "DeleteTagAlias") + defer span.End() + + span.SetAttributes( + attribute.String("tag_alias_name", string(tagAliasName)), + ) + + span.AddEvent("Starting tag alias deletion") + var tagAlias models.TagAlias if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(tagAliasName) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.TagAliasNameIsEmpty} + return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty}) } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_alias_name": tagAliasName, + }).Debug("attempting to delete tag alias") + result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.Database{Reason: otterError.NoDataFound} + + loggerFields := log.Fields{ + "tag_alias_name": tagAliasName, + } + return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound}) } - return result.Error + + loggerFields := log.Fields{ + "tag_alias_name": tagAliasName, + } + return utils.HandleError(ctx, span, logger, loggerFields, result.Error) } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_alias_name": tagAliasName, + }).Debug("tag alias deleted") + + span.AddEvent("Tag alias deleted successfully") return nil } -- 2.45.2 From 098ecda8690275bae5c32d72ae623817bcf0b478 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 10:07:29 +0200 Subject: [PATCH 24/77] chore(tracing): Rename "tag_count" to "tag_aliases_count" --- pkg/database/tagAlias.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/database/tagAlias.go b/pkg/database/tagAlias.go index aa1e958..2138380 100644 --- a/pkg/database/tagAlias.go +++ b/pkg/database/tagAlias.go @@ -69,10 +69,10 @@ func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, b span.SetAttributes( attribute.Int64("batch_size", int64(batchSize)), - attribute.Int64("tag_count", int64(len(tagsAliases))), + attribute.Int64("tag_aliases_count", int64(len(tagsAliases))), ) - span.AddEvent("Starting batch tag creation") + span.AddEvent("Starting batch tag alias creation") if client == nil { return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) @@ -87,26 +87,26 @@ func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, b } logger.WithContext(ctx).WithFields(log.Fields{ - "tag_alias_length": len(tagsAliases), + "tag_aliases_count": len(tagsAliases), }).Debug("attempting to create tags aliases") result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { loggerFields := log.Fields{ - "tag_alias_length": len(tagsAliases), + "tag_aliases_count": len(tagsAliases), } return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) } loggerFields := log.Fields{ - "tag_alias_length": len(tagsAliases), + "tag_aliases_count": len(tagsAliases), } return utils.HandleError(ctx, span, logger, loggerFields, result.Error) } logger.WithContext(ctx).WithFields(log.Fields{ - "tag_alias_length": len(tagsAliases), + "tag_aliases_count": len(tagsAliases), }).Debug("batch tags aliases created") span.AddEvent("Batch tags aliases created successfully") -- 2.45.2 From 3f92bdb32568de485f12ada2da868c3b2e5d7e9c Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 10:07:42 +0200 Subject: [PATCH 25/77] feat(tracing): Add tracing, logging, and error handling to tagGroup database functions --- pkg/database/tagGroup.go | 117 +++++++++++++++++++++++++++++++++------ 1 file changed, 100 insertions(+), 17 deletions(-) diff --git a/pkg/database/tagGroup.go b/pkg/database/tagGroup.go index cf155fe..96c1a0d 100644 --- a/pkg/database/tagGroup.go +++ b/pkg/database/tagGroup.go @@ -3,14 +3,28 @@ package database import ( "context" "errors" + + "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" ) func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagName models.TagName) (models.TagGroup, error) { + ctx, span := tracer.Start(ctx, "CreateTagGroup") + defer span.End() + + span.SetAttributes( + attribute.String("tag_group_name", string(tagGroupName)), + attribute.String("tag_name", string(tagName)), + ) + + span.AddEvent("Starting tag group creation") + if client == nil { - return models.TagGroup{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.TagGroup{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } tagGroup := models.TagGroup{ @@ -18,63 +32,132 @@ func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagNa TagID: tagName, } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_group_name": tagGroupName, + "tag_name": tagName, + }).Debug("attempting to create tag group") + result := client.WithContext(ctx).Create(&tagGroup) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return models.TagGroup{}, &otterError.Database{Reason: otterError.DuplicateKey} + + loggerFields := log.Fields{ + "tag_group_name": tagGroupName, + "tag_name": tagName, + } + return models.TagGroup{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) } - return models.TagGroup{}, result.Error + + loggerFields := log.Fields{ + "tag_group_name": tagGroupName, + "tag_name": tagName, + } + return models.TagGroup{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error) } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_group_name": tagGroupName, + "tag_name": tagName, + }).Debug("tag group created") + span.AddEvent("Tag group created successfully") return tagGroup, nil } func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error { + ctx, span := tracer.Start(ctx, "CreateTagAliasInBatch") + defer span.End() + + span.SetAttributes( + attribute.Int64("batch_size", int64(batchSize)), + attribute.Int64("tag_group_count", int64(len(tagsGroups))), + ) + + span.AddEvent("Starting batch tag group creation") + if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - if tagsGroups == nil { - return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty} - } - - if len(tagsGroups) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty} + if tagsGroups == nil || len(tagsGroups) == 0 { + return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty}) } if batchSize == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_groups_count": len(tagsGroups), + }).Debug("attempting to create tags groups") + result := client.WithContext(ctx).CreateInBatches(&tagsGroups, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.Database{Reason: otterError.DuplicateKey} + loggerFields := log.Fields{ + "tag_group_count": len(tagsGroups), + } + return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) + } - return result.Error + loggerFields := log.Fields{ + "tag_group_count": len(tagsGroups), + } + return utils.HandleError(ctx, span, logger, loggerFields, result.Error) + } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_group_count": len(tagsGroups), + }).Debug("batch tags groups created") + + span.AddEvent("Batch tags groups created successfully") return nil } func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error { + ctx, span := tracer.Start(ctx, "DeleteTagGroup") + defer span.End() + + span.SetAttributes( + attribute.String("tag_group_name", string(tagGroupName)), + ) + + span.AddEvent("Starting tag group deletion") + var tagGroup models.TagGroup if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(tagGroupName) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.TagGroupNameIsEmpty} + return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagGroupNameIsEmpty}) } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_group_name": tagGroupName, + }).Debug("attempting to delete tag group") + result := client.WithContext(ctx).Delete(&tagGroup, tagGroupName) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.Database{Reason: otterError.NoDataFound} + + loggerFields := log.Fields{ + "tag_group_name": tagGroupName, + } + return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound}) } - return result.Error + + loggerFields := log.Fields{ + "tag_group_name": tagGroupName, + } + return utils.HandleError(ctx, span, logger, loggerFields, result.Error) } + logger.WithContext(ctx).WithFields(log.Fields{ + "tag_group_name": tagGroupName, + }).Debug("tag group deleted") + + span.AddEvent("Tag group deleted successfully") return nil } -- 2.45.2 From 99ea2d37ab47b79fe08c14812a1fa62a040a719d Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:14:53 +0200 Subject: [PATCH 26/77] chore(refactor): removed dead code --- internal/postgres/custom.go | 42 - internal/postgres/custom_test.go | 125 -- internal/postgres/post.go | 131 -- internal/postgres/post_test.go | 469 -------- internal/postgres/relationships.go | 126 -- internal/postgres/relationships_test.go | 392 ------ internal/postgres/source.go | 85 -- internal/postgres/source_test.go | 270 ----- internal/postgres/tag.go | 440 ------- internal/postgres/tag_test.go | 1452 ----------------------- internal/postgres/user.go | 398 ------- internal/postgres/user_test.go | 1370 --------------------- 12 files changed, 5300 deletions(-) delete mode 100644 internal/postgres/custom.go delete mode 100644 internal/postgres/custom_test.go delete mode 100644 internal/postgres/post.go delete mode 100644 internal/postgres/post_test.go delete mode 100644 internal/postgres/relationships.go delete mode 100644 internal/postgres/relationships_test.go delete mode 100644 internal/postgres/source.go delete mode 100644 internal/postgres/source_test.go delete mode 100644 internal/postgres/tag.go delete mode 100644 internal/postgres/tag_test.go delete mode 100644 internal/postgres/user.go delete mode 100644 internal/postgres/user_test.go diff --git a/internal/postgres/custom.go b/internal/postgres/custom.go deleted file mode 100644 index c360669..0000000 --- a/internal/postgres/custom.go +++ /dev/null @@ -1,42 +0,0 @@ -package postgres - -import ( - "context" - "database/sql" - "errors" - "gorm.io/gorm" -) - -func ExecuteRawStatement(ctx context.Context, db *gorm.DB, query string, args ...any) error { - if query == "" { - return errors.New("query can not be empty") - } - - if args == nil { - return errors.New("arguments can not be nil") - } - - result := db.WithContext(ctx).Exec(query, args...) - if result.Error != nil { - return result.Error - } - - return nil -} - -func QueryRawStatement(ctx context.Context, db *gorm.DB, query string, args ...any) (*sql.Rows, error) { - if query == "" { - return nil, errors.New("query can not be empty") - } - - if args == nil { - return nil, errors.New("arguments can not be nil") - } - - result, err := db.WithContext(ctx).Raw(query, args...).Rows() - if err != nil { - return nil, err - } - - return result, nil -} diff --git a/internal/postgres/custom_test.go b/internal/postgres/custom_test.go deleted file mode 100644 index 22c176b..0000000 --- a/internal/postgres/custom_test.go +++ /dev/null @@ -1,125 +0,0 @@ -package postgres - -import ( - "context" - "database/sql" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" - "gorm.io/gorm" - "reflect" - "testing" -) - -func TestExecuteRawStatement(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) - } - defer container.Terminate(ctx) - - // Test - type args struct { - ctx context.Context - db *gorm.DB - query string - args []any - } - tests := []struct { - name string - args args - want *sql.Rows - wantErr bool - }{ - { - name: "Test 01: Empty Query", - args: args{ - ctx: ctx, - db: gormDB, - query: "", - args: nil, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 02: Nil Query", - args: args{ - ctx: ctx, - db: gormDB, - query: "aasd", - args: nil, - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := ExecuteRawStatement(tt.args.ctx, tt.args.db, tt.args.query, tt.args.args...) - if (err != nil) != tt.wantErr { - t.Errorf("ExecuteRawStatement() error = %v, wantErr %v", err, tt.wantErr) - return - } - }) - } -} - -func TestQueryRawStatement(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) - } - defer container.Terminate(ctx) - - // Test - type args struct { - ctx context.Context - db *gorm.DB - query string - args []any - } - tests := []struct { - name string - args args - want *sql.Rows - wantErr bool - }{ - { - name: "Test 01: Empty Query", - args: args{ - ctx: ctx, - db: gormDB, - query: "", - args: nil, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 02: Nil Query", - args: args{ - ctx: ctx, - db: gormDB, - query: "aasd", - args: nil, - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := QueryRawStatement(tt.args.ctx, tt.args.db, tt.args.query, tt.args.args...) - if (err != nil) != tt.wantErr { - t.Errorf("QueryRawStatement() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("QueryRawStatement() got = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/internal/postgres/post.go b/internal/postgres/post.go deleted file mode 100644 index 8b36e51..0000000 --- a/internal/postgres/post.go +++ /dev/null @@ -1,131 +0,0 @@ -package postgres - -import ( - "context" - "errors" - - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - log "github.com/sirupsen/logrus" - "gorm.io/gorm" -) - -func CreatePost(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) error { - - if anthrovePost == nil { - return &otterError.EntityValidationFailed{Reason: "anthrovePost is nil"} - } - - result := db.WithContext(ctx).Create(&anthrovePost) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - if result.RowsAffected == 0 { - return &otterError.NoDataWritten{} - } - - log.WithFields(log.Fields{ - "anthrove_post_id": anthrovePost.ID, - "anthrove_post_rating": anthrovePost.Rating, - }).Trace("database: created anthrove post") - - return nil -} - -func CreatePostInBatch(ctx context.Context, db *gorm.DB, anthrovePost []models.Post, batchSize int) error { - if anthrovePost == nil { - return &otterError.EntityValidationFailed{Reason: "anthrovePost cannot be nil"} - } - - if len(anthrovePost) == 0 { - return &otterError.EntityValidationFailed{Reason: "anthrovePost cannot be empty"} - } - - if batchSize == 0 { - return &otterError.EntityValidationFailed{Reason: "batch size cannot be zero"} - } - - result := db.WithContext(ctx).CreateInBatches(anthrovePost, batchSize) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - if result.RowsAffected == 0 { - return &otterError.NoDataWritten{} - } - - log.WithFields(log.Fields{ - "tag_size": len(anthrovePost), - "batch_size": batchSize, - }).Trace("database: created tag node") - - return nil -} - -func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID) (*models.Post, error) { - - if anthrovePostID == "" { - return nil, &otterError.EntityValidationFailed{Reason: "anthrovePostID is not set"} - } - - if len(anthrovePostID) != 25 { - return nil, &otterError.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"} - } - - var post models.Post - result := db.WithContext(ctx).First(&post, "id = ?", anthrovePostID) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - return &post, nil -} - -func GetPostBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, error) { - - if sourceURL == "" { - return nil, &otterError.EntityValidationFailed{Reason: "sourceURL is not set"} - } - - var post models.Post - result := db.WithContext(ctx).Raw(`SELECT p.id AS id, p.rating as rating FROM "Post" AS p INNER JOIN "PostReference" AS pr ON p.id = pr.post_id AND pr.url = $1 LIMIT 1`, sourceURL).First(&post) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - return &post, nil -} - -func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID models.AnthroveSourceID) (*models.Post, error) { - - if sourceID == "" { - return nil, &otterError.EntityValidationFailed{Reason: "sourceID is not set"} - } - - var post models.Post - result := db.WithContext(ctx).Raw(`SELECT p.id AS id, p.rating as rating FROM "Post" AS p INNER JOIN "PostReference" AS pr ON p.id = pr.post_id AND pr.source_id = $1 LIMIT 1`, sourceID).First(&post) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - return &post, nil -} diff --git a/internal/postgres/post_test.go b/internal/postgres/post_test.go deleted file mode 100644 index 6fd63a4..0000000 --- a/internal/postgres/post_test.go +++ /dev/null @@ -1,469 +0,0 @@ -package postgres - -import ( - "context" - "fmt" - "testing" - - _ "github.com/lib/pq" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" - "gorm.io/gorm" -) - -func TestCreateAnthrovePostNode(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) - } - defer container.Terminate(ctx) - - // Setup Tests - - validPost := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), - }, - Rating: "safe", - } - - invalidPost := &models.Post{ - Rating: "error", - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthrovePost *models.Post - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthrovePostID and Rating", - args: args{ - ctx: context.Background(), - db: gormDB, - anthrovePost: validPost, - }, - wantErr: false, - }, - { - name: "Test 2: Invalid Rating", - args: args{ - ctx: context.Background(), - db: gormDB, - anthrovePost: invalidPost, - }, - wantErr: true, - }, - { - name: "Test 3: Nill", - args: args{ - ctx: context.Background(), - db: gormDB, - anthrovePost: nil, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreatePost(tt.args.ctx, tt.args.db, tt.args.anthrovePost); (err != nil) != tt.wantErr { - t.Errorf("CreatePost() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestGetPostByAnthroveID(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) - } - defer container.Terminate(ctx) - - // Setup Tests - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), - }, - Rating: "safe", - } - - err = CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal("Could not create post", err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthrovePostID models.AnthrovePostID - } - tests := []struct { - name string - args args - want *models.Post - wantErr bool - }{ - { - name: "Test 1: Valid anthrovePostID", - args: args{ - ctx: ctx, - db: gormDB, - anthrovePostID: post.ID, - }, - want: post, - wantErr: false, - }, - { - name: "Test 2: Invalid anthrovePostID", - args: args{ - ctx: ctx, - db: gormDB, - anthrovePostID: "1234", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: No anthrovePostID", - args: args{ - ctx: ctx, - db: gormDB, - anthrovePostID: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetPostByAnthroveID(tt.args.ctx, tt.args.db, tt.args.anthrovePostID) - if (err != nil) != tt.wantErr { - t.Errorf("GetPostByAnthroveID() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkPost(got, tt.want) { - t.Errorf("GetPostByAnthroveID() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetPostBySourceURL(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) - } - defer container.Terminate(ctx) - - // Setup Tests - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), - }, - - Rating: "safe", - } - - err = CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal("Could not create post", err) - } - - source := models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")), - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.net/icon.ico", - } - - err = CreateSource(ctx, gormDB, &source) - if err != nil { - t.Fatal("Could not create source", err) - } - - err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.org", models.PostReferenceConfig{}) - if err != nil { - t.Fatal("Could not create source reference", err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - sourceURL string - } - tests := []struct { - name string - args args - want *models.Post - wantErr bool - }{ - { - name: "Test 1: Valid sourceURL", - args: args{ - ctx: ctx, - db: gormDB, - sourceURL: "http://test.org", - }, - want: post, - wantErr: false, - }, - { - name: "Test 2: Invalid sourceURL", - args: args{ - ctx: ctx, - db: gormDB, - sourceURL: "1234", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: No sourceURL", - args: args{ - ctx: ctx, - db: gormDB, - sourceURL: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetPostBySourceURL(tt.args.ctx, tt.args.db, tt.args.sourceURL) - if (err != nil) != tt.wantErr { - t.Errorf("GetPostBySourceURL() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkPost(got, tt.want) { - t.Errorf("GetPostBySourceURL() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetPostBySourceID(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) - } - defer container.Terminate(ctx) - - // Setup Tests - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), - }, - Rating: "safe", - } - - err = CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal("Could not create post", err) - } - - source := models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")), - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.net/icon.ico", - } - - err = CreateSource(ctx, gormDB, &source) - if err != nil { - t.Fatal("Could not create source", err) - } - - err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.otg", models.PostReferenceConfig{}) - if err != nil { - t.Fatal("Could not create source reference", err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - sourceID models.AnthroveSourceID - } - - tests := []struct { - name string - args args - want *models.Post - wantErr bool - }{ - { - name: "Test 1: Valid sourceID", - args: args{ - ctx: ctx, - db: gormDB, - sourceID: source.ID, - }, - want: post, - wantErr: false, - }, - { - name: "Test 2: Invalid sourceID", - args: args{ - ctx: ctx, - db: gormDB, - sourceID: "1234", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: No sourceID", - args: args{ - ctx: ctx, - db: gormDB, - sourceID: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetPostBySourceID(tt.args.ctx, tt.args.db, tt.args.sourceID) - if (err != nil) != tt.wantErr { - t.Errorf("GetPostBySourceID() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkPost(got, tt.want) { - t.Errorf("GetPostBySourceID() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestCreatePostInBatch(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) - } - defer container.Terminate(ctx) - - // Setup Tests - - validPosts := []models.Post{ - { - Rating: models.SFW, - }, - { - Rating: models.NSFW, - }, - { - Rating: models.Questionable, - }, - } - - emptyPost := []models.Post{} - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthrovePost []models.Post - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - db: gormDB, - anthrovePost: validPosts, - batchSize: len(validPosts), - }, - wantErr: false, - }, - { - name: "Test 2: Emtpy Data", - args: args{ - ctx: ctx, - db: gormDB, - anthrovePost: emptyPost, - batchSize: 0, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Data", - args: args{ - ctx: ctx, - db: gormDB, - anthrovePost: nil, - batchSize: 0, - }, - wantErr: true, - }, - { - name: "Test 4: batchSize 0", - args: args{ - ctx: ctx, - db: gormDB, - anthrovePost: validPosts, - batchSize: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreatePostInBatch(tt.args.ctx, tt.args.db, tt.args.anthrovePost, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreatePostInBatch() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func checkPost(got *models.Post, want *models.Post) bool { - - if got == nil && want == nil { - return true - } else if got == nil || want == nil { - return false - } - - if got.ID != want.ID { - return false - } - - if got.Rating != want.Rating { - return false - } - - return true -} diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go deleted file mode 100644 index e0d6e3e..0000000 --- a/internal/postgres/relationships.go +++ /dev/null @@ -1,126 +0,0 @@ -package postgres - -import ( - "context" - "errors" - - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - log "github.com/sirupsen/logrus" - "gorm.io/gorm" -) - -func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error { - if anthrovePostID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthrovePostID) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - if sourceDomain == "" { - return &otterError.EntityValidationFailed{Reason: "sourceDomain cannot be empty"} - } - - result := db.WithContext(ctx).Exec(`INSERT INTO "PostReference" (post_id, source_id, url, full_file_url, preview_file_url, sample_file_url, source_post_id) SELECT $1, source.id, $2, $4, $5, $6, $7 FROM "Source" AS source WHERE domain = $3;`, anthrovePostID, postURL, sourceDomain, config.FullFileURL, config.PreviewFileURL, config.SampleFileURL, config.SourcePostID) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.NoDataFound{} - } - if errors.Is(result.Error, gorm.ErrCheckConstraintViolated) { - return &otterError.EntityAlreadyExists{} - } - - return result.Error - } - - if result.RowsAffected == 0 { - return &otterError.NoDataWritten{} - } - - log.WithFields(log.Fields{ - "anthrove_post_id": anthrovePostID, - "anthrove_source_domain": sourceDomain, - }).Trace("database: created anthrove post to source link") - - return nil -} - -func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error { - - if anthrovePostID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthrovePostID) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - if anthroveUserID == "" { - return &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} - } - - userFavorite := models.UserFavorite{ - UserID: string(anthroveUserID), - PostID: string(anthrovePostID), - } - - result := db.WithContext(ctx).Create(&userFavorite) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - if result.RowsAffected == 0 { - return &otterError.NoDataWritten{} - } - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "anthrove_post_id": anthrovePostID, - }).Trace("database: created user to post link") - - return nil -} - -func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) { - var count int64 - - if anthrovePostID == "" { - return false, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthrovePostID) != 25 { - return false, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - if anthroveUserID == "" { - return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} - } - - if len(anthroveUserID) != 25 { - return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} - } - - result := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return false, &otterError.NoDataFound{} - } - return false, result.Error - } - - exists := count > 0 - - log.WithFields(log.Fields{ - "relationship_exists": exists, - "relationship_anthrove_user_id": anthroveUserID, - "relationship_anthrove_post_id": anthrovePostID, - }).Trace("database: checked user post relationship") - - return exists, nil -} diff --git a/internal/postgres/relationships_test.go b/internal/postgres/relationships_test.go deleted file mode 100644 index f55c1ca..0000000 --- a/internal/postgres/relationships_test.go +++ /dev/null @@ -1,392 +0,0 @@ -package postgres - -import ( - "context" - "fmt" - "testing" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" - "gorm.io/gorm" -) - -func TestCheckUserToPostLink(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - err = CreateUser(ctx, gormDB, validUserID) - if err != nil { - t.Fatal(err) - } - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID, - }, - Rating: "safe", - } - - err = CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - anthrovePostID models.AnthrovePostID - } - tests := []struct { - name string - args args - want bool - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID and AnthrovePostID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - anthrovePostID: post.ID, - }, - want: true, - wantErr: false, - }, - { - name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - anthrovePostID: "qadw", - }, - want: false, - wantErr: true, - }, - { - name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - anthrovePostID: post.ID, - }, - want: false, - wantErr: true, - }, - { - name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - anthrovePostID: "123456", - }, - want: false, - wantErr: true, - }, - { - name: "Test 5: No AnthrovePostID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - anthrovePostID: "123456", - }, - want: false, - wantErr: true, - }, - { - name: "Test 6: No anthrovePostID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - anthrovePostID: "", - }, - want: false, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID) - if (err != nil) != tt.wantErr { - t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("CheckIfUserHasPostAsFavorite() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestCheckUserToPostLinkWithNoData(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - err = CreateUser(ctx, gormDB, validUserID) - if err != nil { - t.Fatal(err) - } - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID, - }, - Rating: "safe", - } - - err = CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - anthrovePostID models.AnthrovePostID - } - tests := []struct { - name string - args args - want bool - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID and AnthrovePostID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - anthrovePostID: post.ID, - }, - want: true, - wantErr: false, - }, - { - name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - anthrovePostID: "qadw", - }, - want: false, - wantErr: true, - }, - { - name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - anthrovePostID: post.ID, - }, - want: false, - wantErr: true, - }, - { - name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - anthrovePostID: "123456", - }, - want: false, - wantErr: true, - }, - { - name: "Test 5: No AnthrovePostID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - anthrovePostID: "123456", - }, - want: false, - wantErr: true, - }, - { - name: "Test 6: No anthrovePostID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - anthrovePostID: "", - }, - want: false, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID) - if (err != nil) != tt.wantErr { - t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("CheckIfUserHasPostAsFavorite() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestEstablishUserToPostLink(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - err = CreateUser(ctx, gormDB, validUserID) - if err != nil { - t.Fatal(err) - } - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID, - }, - Rating: "safe", - } - - err = CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - anthrovePostID models.AnthrovePostID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID and AnthrovePostID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - anthrovePostID: post.ID, - }, - wantErr: false, - }, - { - name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - anthrovePostID: "123456", - }, - wantErr: true, - }, - { - name: "Test 3: invalid AnthroveUserID and valid AnthrovePostID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - anthrovePostID: post.ID, - }, - wantErr: true, - }, - { - name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - anthrovePostID: "123456", - }, - wantErr: true, - }, - { - name: "Test 5: AnthrovePostID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - anthrovePostID: "", - }, - wantErr: true, - }, - { - name: "Test 6: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - anthrovePostID: validPostID, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - 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 deleted file mode 100644 index bae1244..0000000 --- a/internal/postgres/source.go +++ /dev/null @@ -1,85 +0,0 @@ -package postgres - -import ( - "context" - "errors" - - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - - log "github.com/sirupsen/logrus" - "gorm.io/gorm" -) - -// CreateSource creates a pgModels.Source -func CreateSource(ctx context.Context, db *gorm.DB, anthroveSource *models.Source) error { - - if anthroveSource.Domain == "" { - return &otterError.EntityValidationFailed{Reason: "Domain is required"} - } - - result := db.WithContext(ctx).Where(models.Source{Domain: anthroveSource.Domain}).FirstOrCreate(anthroveSource) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - if result.RowsAffected == 0 { - return &otterError.NoDataWritten{} - } - - log.WithFields(log.Fields{ - "node_source_url": anthroveSource.Domain, - "node_source_displayName": anthroveSource.DisplayName, - "node_source_icon": anthroveSource.Icon, - }).Trace("database: created source node") - - return nil -} - -// 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) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - log.WithFields(log.Fields{ - "tag_amount": result.RowsAffected, - }).Trace("database: get all source nodes") - - return sources, nil -} - -// GetSourceByDomain returns the first source it finds based on the domain -func GetSourceByDomain(ctx context.Context, db *gorm.DB, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) { - var sources models.Source - - if sourceDomain == "" { - return nil, &otterError.EntityValidationFailed{Reason: "AnthroveSourceDomain is not set"} - } - - result := db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&sources) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - log.WithFields(log.Fields{ - "tag_amount": result.RowsAffected, - }).Trace("database: get all source nodes") - - return &sources, nil -} diff --git a/internal/postgres/source_test.go b/internal/postgres/source_test.go deleted file mode 100644 index c29a1e7..0000000 --- a/internal/postgres/source_test.go +++ /dev/null @@ -1,270 +0,0 @@ -package postgres - -import ( - "context" - "fmt" - "testing" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" - "gorm.io/gorm" -) - -func TestCreateSourceNode(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validPostID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Post1")) - - validSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID}, - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - } - - invalidSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID}, - Domain: "notfound.intern", - } - - invalidSourceDomain := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID}, - Domain: "", - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveSource *models.Source - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid anthroveSource", - args: args{ - ctx: ctx, - db: gormDB, - anthroveSource: validSource, - }, - wantErr: false, - }, - { - name: "Test 2: inValid anthroveSource", - args: args{ - ctx: ctx, - db: gormDB, - anthroveSource: invalidSourceDomain, - }, - wantErr: true, - }, - { - name: "Test 3: unique anthroveSource", - args: args{ - ctx: ctx, - db: gormDB, - anthroveSource: invalidSource, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - 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) - } - }) - } -} - -func TestGetAllSourceNodes(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) - } - defer container.Terminate(ctx) - - // Setup Test - - sources := []models.Source{ - { - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - }, - { - DisplayName: "furaffinity", - Domain: "furaffinity.net", - Icon: "icon.furaffinity.net", - }, - { - DisplayName: "fenpaws", - Domain: "fenpa.ws", - Icon: "icon.fenpa.ws", - }, - } - - for _, source := range sources { - err = CreateSource(ctx, gormDB, &source) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - } - tests := []struct { - name string - args args - want []models.Source - wantErr bool - }{ - { - name: "Test 1: Get all entries", - args: args{ - ctx: ctx, - db: gormDB, - }, - want: sources, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllSource(tt.args.ctx, tt.args.db) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllSource() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkSourcesNode(got, tt.want) { - t.Errorf("GetAllSource() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetSourceNodesByURL(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) - } - defer container.Terminate(ctx) - - // Setup Test - - source := &models.Source{ - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - } - - err = CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - domain models.AnthroveSourceDomain - } - tests := []struct { - name string - args args - want *models.Source - wantErr bool - }{ - { - name: "Test 1: Valid URL", - args: args{ - ctx: ctx, - db: gormDB, - domain: "e621.net", - }, - want: source, - wantErr: false, - }, - { - name: "Test 2: Invalid URL", - args: args{ - ctx: ctx, - db: gormDB, - domain: "eeeee.net", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 2: No URL", - args: args{ - ctx: ctx, - db: gormDB, - domain: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetSourceByDomain(tt.args.ctx, tt.args.db, tt.args.domain) - if (err != nil) != tt.wantErr { - t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkSourceNode(got, tt.want) { - t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want) - } - }) - } -} - -func checkSourcesNode(got []models.Source, want []models.Source) bool { - for i, source := range want { - if source.DisplayName != got[i].DisplayName { - return false - } - if source.Domain != got[i].Domain { - return false - } - if source.Icon != got[i].Icon { - return false - } - } - - return true - -} - -func checkSourceNode(got *models.Source, want *models.Source) bool { - - if want == nil && got == nil { - return true - } - - if got.Domain != want.Domain { - return false - } - - return true - -} diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go deleted file mode 100644 index 2c17a87..0000000 --- a/internal/postgres/tag.go +++ /dev/null @@ -1,440 +0,0 @@ -package postgres - -import ( - "context" - "errors" - - "gorm.io/gorm/clause" - - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - log "github.com/sirupsen/logrus" - "gorm.io/gorm" -) - -func CreateTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName, tagType models.TagType) error { - - if tagName == "" { - return &otterError.EntityValidationFailed{Reason: "tagName cannot be empty"} - } - - if tagType == "" { - return &otterError.EntityValidationFailed{Reason: "tagType cannot be empty"} - } - - result := db.WithContext(ctx).Create(&models.Tag{Name: string(tagName), Type: tagType}) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - if result.RowsAffected == 0 { - return &otterError.NoDataWritten{} - } - - log.WithFields(log.Fields{ - "tag_name": tagName, - "tag_type": tagType, - }).Trace("database: created tag node") - - return nil -} - -func CreateTagInBatchAndUpdate(ctx context.Context, db *gorm.DB, tags []models.Tag, batchSize int) error { - if len(tags) == 0 { - return &otterError.EntityValidationFailed{Reason: "tags cannot be empty"} - } - - if tags == nil { - return &otterError.EntityValidationFailed{Reason: "tags cannot be nil"} - } - - if batchSize == 0 { - return &otterError.EntityValidationFailed{Reason: "batch size cannot be zero"} - } - - result := db.WithContext(ctx). - Clauses(clause.OnConflict{ - Columns: []clause.Column{{Name: "name"}}, - DoUpdates: clause.AssignmentColumns([]string{"tag_type"}), - }).CreateInBatches(tags, batchSize) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - if result.RowsAffected == 0 { - return &otterError.NoDataWritten{} - } - - log.WithFields(log.Fields{ - "tag_size": len(tags), - "batch_size": batchSize, - }).Trace("database: created tag node") - - return nil -} - -func DeleteTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName) error { - - if tagName == "" { - return &otterError.EntityValidationFailed{Reason: "tagName cannot be empty"} - } - - result := db.WithContext(ctx).Delete(&models.Tag{Name: string(tagName)}) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.NoDataFound{} - } - return result.Error - } - - log.WithFields(log.Fields{ - "tag_name": tagName, - }).Trace("database: deleted tag") - - return nil -} - -func GetAllTagByTagsType(ctx context.Context, db *gorm.DB, tagType models.TagType) ([]models.Tag, error) { - var tags []models.Tag - - if tagType == "" { - return nil, &otterError.EntityValidationFailed{Reason: "tagType cannot be empty"} - } - - result := db.WithContext(ctx).Model(&models.Tag{}).Where("tag_type = ?", tagType).Scan(&tags) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - log.WithFields(log.Fields{ - "tags_length": len(tags), - }).Trace("database: got tag") - - return tags, nil -} - -func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error { - - if anthrovePostID == "" { - return &otterError.EntityValidationFailed{Reason: "anthrovePostID cannot be empty"} - } - - if len(anthrovePostID) != 25 { - return &otterError.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"} - } - - if tag == nil { - return &otterError.EntityValidationFailed{Reason: "Tag is nil"} - } - - pgPost := models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: anthrovePostID, - }, - } - - err := db.WithContext(ctx).Model(&pgPost).Association("Tags").Append(tag) - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return &otterError.NoDataFound{} - } - return errors.Join(err, &otterError.NoRelationCreated{}) - } - - log.WithFields(log.Fields{ - "anthrove_post_id": anthrovePostID, - "tag_name": tag.Name, - "tag_type": tag.Type, - }).Trace("database: created tag node") - - return nil -} - -func GetTags(ctx context.Context, db *gorm.DB) ([]models.Tag, error) { - var tags []models.Tag - - result := db.WithContext(ctx).Find(&tags) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - log.WithFields(log.Fields{ - "tag_amount": len(tags), - }).Trace("database: got tags") - - return tags, nil -} - -func CreateTagAlias(ctx context.Context, db *gorm.DB, tagAliasName models.AnthroveTagAliasName, tagID models.AnthroveTagID) error { - - if tagAliasName == "" { - return &otterError.EntityValidationFailed{Reason: "tagAliasName cannot be empty"} - } - if tagID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveTagIDEmpty} - } - - result := db.WithContext(ctx).Clauses(clause.OnConflict{ - Columns: []clause.Column{{Name: "name"}}, - DoNothing: true, - }).Create(&models.TagAlias{ - Name: string(tagAliasName), - TagID: string(tagID), - }) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - log.WithFields(log.Fields{ - "tag_alias_name": tagAliasName, - "tag_alias_tag_id": tagID, - }).Trace("database: created tagAlias") - - return nil -} - -func CreateTagAliasInBatch(ctx context.Context, db *gorm.DB, tagAliases []models.TagAlias, batchSize int) error { - if len(tagAliases) == 0 { - return &otterError.EntityValidationFailed{Reason: "tagAliases cannot be empty"} - } - - if tagAliases == nil { - return &otterError.EntityValidationFailed{Reason: "tagAliases cannot be nil"} - } - - if batchSize == 0 { - return &otterError.EntityValidationFailed{Reason: "batch size cannot be zero"} - } - - result := db.WithContext(ctx).Clauses(clause.OnConflict{ - Columns: []clause.Column{{Name: "name"}}, - DoNothing: true, - }).CreateInBatches(tagAliases, batchSize) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - if result.RowsAffected == 0 { - return &otterError.NoDataWritten{} - } - - log.WithFields(log.Fields{ - "tag_size": len(tagAliases), - "batch_size": batchSize, - }).Trace("database: created tag node") - - return nil -} - -func GetAllTagAlias(ctx context.Context, db *gorm.DB) ([]models.TagAlias, error) { - var tagAliases []models.TagAlias - - result := db.WithContext(ctx).Find(&tagAliases) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - log.WithFields(log.Fields{ - "tag_alias_length": len(tagAliases), - }).Trace("database: created tagAlias") - - return tagAliases, nil -} - -func GetAllTagAliasByTag(ctx context.Context, db *gorm.DB, tagID models.AnthroveTagID) ([]models.TagAlias, error) { - var tagAliases []models.TagAlias - - if tagID == "" { - return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveTagIDEmpty} - } - - result := db.WithContext(ctx).Where("tag_id = ?", tagID).Find(&tagAliases) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - log.WithFields(log.Fields{ - "tag_alias_length": len(tagAliases), - "tag_alias_tag_id": tagID, - }).Trace("database: get specific tagAlias") - - return tagAliases, nil -} - -func DeleteTagAlias(ctx context.Context, db *gorm.DB, tagAliasName models.AnthroveTagAliasName) error { - - if tagAliasName == "" { - return &otterError.EntityValidationFailed{Reason: "tagAliasName cannot be empty"} - } - - result := db.WithContext(ctx).Delete(&models.TagAlias{Name: string(tagAliasName)}) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.NoDataFound{} - } - return result.Error - } - - log.WithFields(log.Fields{ - "tag_alias_name": tagAliasName, - }).Trace("database: deleted tagAlias") - - return nil -} - -func CreateTagGroup(ctx context.Context, db *gorm.DB, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) error { - - if tagGroupName == "" { - return &otterError.EntityValidationFailed{Reason: "tagGroupName cannot be empty"} - } - if tagID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveTagIDEmpty} - } - - result := db.WithContext(ctx).Create(&models.TagGroup{ - Name: string(tagGroupName), - TagID: string(tagID), - }) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - log.WithFields(log.Fields{ - "tag_group_name": tagGroupName, - "tag_group_tag_id": tagID, - }).Trace("database: created tagGroup") - - return nil -} - -func CreateTagGroupInBatch(ctx context.Context, db *gorm.DB, tagGroups []models.TagGroup, batchSize int) error { - if len(tagGroups) == 0 { - return &otterError.EntityValidationFailed{Reason: "tagAliases cannot be empty"} - } - - if tagGroups == nil { - return &otterError.EntityValidationFailed{Reason: "tagAliases cannot be nil"} - } - - if batchSize == 0 { - return &otterError.EntityValidationFailed{Reason: "batch size cannot be zero"} - } - - result := db.WithContext(ctx).CreateInBatches(tagGroups, batchSize) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - if result.RowsAffected == 0 { - return &otterError.NoDataWritten{} - } - - log.WithFields(log.Fields{ - "tag_size": len(tagGroups), - "batch_size": batchSize, - }).Trace("database: created tag node") - - return nil -} - -func GetAllTagGroup(ctx context.Context, db *gorm.DB) ([]models.TagGroup, error) { - var tagGroups []models.TagGroup - - result := db.WithContext(ctx).Find(&tagGroups) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - log.WithFields(log.Fields{ - "tag_alias_length": len(tagGroups), - }).Trace("database: created tagGroup") - - return tagGroups, nil -} - -func GetAllTagGroupByTag(ctx context.Context, db *gorm.DB, tagID models.AnthroveTagID) ([]models.TagGroup, error) { - var tagGroups []models.TagGroup - - if tagID == "" { - return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveTagIDEmpty} - } - - result := db.WithContext(ctx).Where("tag_id = ?", tagID).Find(&tagGroups) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - log.WithFields(log.Fields{ - "tag_alias_length": len(tagGroups), - "tag_alias_tag_id": tagID, - }).Trace("database: get specific tagGroup") - - return tagGroups, nil -} - -func DeleteTagGroup(ctx context.Context, db *gorm.DB, tagGroupName models.AnthroveTagGroupName) error { - - if tagGroupName == "" { - return &otterError.EntityValidationFailed{Reason: "tagGroupName cannot be empty"} - } - - result := db.WithContext(ctx).Delete(&models.TagGroup{Name: string(tagGroupName)}) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.NoDataFound{} - } - return result.Error - } - - log.WithFields(log.Fields{ - "tag_alias_name": tagGroupName, - }).Trace("database: deleted tagAlias") - - return nil -} diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go deleted file mode 100644 index 7c6f15b..0000000 --- a/internal/postgres/tag_test.go +++ /dev/null @@ -1,1452 +0,0 @@ -package postgres - -import ( - "context" - "fmt" - "reflect" - "testing" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" - "gorm.io/gorm" -) - -func TestCreateTagNodeWitRelation(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) - } - defer container.Terminate(ctx) - - // Setup Test - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), - }, - Rating: "safe", - } - - err = CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - tag := &models.Tag{ - Name: "JayTheFerret", - Type: "artist", - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - PostID models.AnthrovePostID - tag *models.Tag - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid PostID and Tag", - args: args{ - ctx: ctx, - db: gormDB, - PostID: post.ID, - tag: tag, - }, - wantErr: false, - }, - { - name: "Test 2: Valid PostID and no Tag", - args: args{ - ctx: ctx, - db: gormDB, - PostID: post.ID, - tag: nil, - }, - wantErr: true, - }, - { - name: "Test 3: Invalid PostID and valid Tag", - args: args{ - ctx: ctx, - db: gormDB, - PostID: "123456", - tag: tag, - }, - wantErr: true, - }, - { - name: "Test 4: No PostID and valid Tag", - args: args{ - ctx: ctx, - db: gormDB, - PostID: "", - tag: tag, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagAndReferenceToPost(tt.args.ctx, tt.args.db, tt.args.PostID, tt.args.tag); (err != nil) != tt.wantErr { - t.Errorf("CreatePostWithReferenceToTagAnd() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestGetTags(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) - } - defer container.Terminate(ctx) - - // Setup Test - - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: "artist", - }, - { - Name: "anthro", - Type: "general", - }, - { - Name: "soxx", - Type: "character", - }, - } - - for _, tag := range tags { - err = CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - } - tests := []struct { - name string - args args - want []models.Tag - wantErr bool - }{ - { - name: "Test 1: Get Tags", - args: args{ - ctx: ctx, - db: gormDB, - }, - want: tags, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetTags(tt.args.ctx, tt.args.db) - if (err != nil) != tt.wantErr { - t.Errorf("GetTags() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkTag(got, tt.want) { - t.Errorf("GetTags() got = %v, want %v", got, tt.want) - } - }) - } -} - -func checkTag(got []models.Tag, want []models.Tag) bool { - for i, tag := range want { - if tag.Type != got[i].Type { - return false - } - if tag.Name != got[i].Name { - return false - } - } - - return true - -} - -func TestCreateTag(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTag := models.Tag{ - Name: "JayTheFerret", - Type: "artist", - } - - invalidTag := models.Tag{} - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagName models.AnthroveTagName - tagType models.TagType - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tag", - args: args{ - ctx: ctx, - db: gormDB, - tagName: models.AnthroveTagName(validTag.Name), - tagType: validTag.Type, - }, - wantErr: false, - }, - { - name: "Test 2: Duplicate Tag", - args: args{ - ctx: ctx, - db: gormDB, - tagName: models.AnthroveTagName(validTag.Name), - tagType: validTag.Type, - }, - wantErr: true, - }, - { - name: "Test 3: Invalid Tag", - args: args{ - ctx: ctx, - db: gormDB, - tagName: models.AnthroveTagName(invalidTag.Name), - tagType: invalidTag.Type, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTag(tt.args.ctx, tt.args.db, tt.args.tagName, tt.args.tagType); (err != nil) != tt.wantErr { - t.Errorf("CreateTag() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCreateTagAlias(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validTagAliasName01 := models.AnthroveTagAliasName("httyd") - validTagAliasName02 := models.AnthroveTagAliasName("dragon") - - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagAliasName models.AnthroveTagAliasName - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: validTagAliasName01, - tagID: validTagID, - }, - wantErr: false, - }, - { - name: "Test 2: No TagAliasName", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: "", - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 4: No tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: validTagAliasName01, - tagID: "", - }, - wantErr: true, - }, - { - name: "Test 5: Duplicate tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: validTagAliasName01, - tagID: validTagID, - }, - wantErr: false, - }, - { - name: "Test 6: Invalide tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: validTagAliasName02, - tagID: "aaa", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagAlias(tt.args.ctx, tt.args.db, tt.args.tagAliasName, tt.args.tagID); (err != nil) != tt.wantErr { - t.Errorf("CreateTagAlias() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestGetAllTagAlias(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagAlias{ - { - Name: string(validTagAliases[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[2]), - TagID: string(validTagID), - }, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - } - tests := []struct { - name string - args args - want []models.TagAlias - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - }, - want: expectedResult, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllTagAlias(tt.args.ctx, tt.args.db) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagAlias() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagAlias() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetAllTagAliasByTag(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagAlias{ - { - Name: string(validTagAliases[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[2]), - TagID: string(validTagID), - }, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - want []models.TagAlias - wantErr bool - }{ - { - name: "Test 1: Valid TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: validTagID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: No TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Invalid TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: "adads", - }, - want: []models.TagAlias{}, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllTagAliasByTag(tt.args.ctx, tt.args.db, tt.args.tagID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagAliasByTag() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagAliasByTag() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestDeleteTagAlias(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagAliasName models.AnthroveTagAliasName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveTagAliasName", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: validTagAliases[0], - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveTagAliasName", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: "asdad", - }, - wantErr: false, - }, - { - name: "Test 3: No AnthroveTagAliasName", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := DeleteTagAlias(tt.args.ctx, tt.args.db, tt.args.tagAliasName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCreateTagGroup(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validTagGroupName01 := models.AnthroveTagGroupName("httyd") - validTagGroupName02 := models.AnthroveTagGroupName("dragon") - - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagGroupName models.AnthroveTagGroupName - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: validTagGroupName01, - tagID: validTagID, - }, - wantErr: false, - }, - { - name: "Test 2: No TagGroupName", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: "", - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 4: No tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: validTagGroupName01, - tagID: "", - }, - wantErr: true, - }, - { - name: "Test 5: Duplicate tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: validTagGroupName01, - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 6: Invalide tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: validTagGroupName02, - tagID: "aaa", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagGroup(tt.args.ctx, tt.args.db, tt.args.tagGroupName, tt.args.tagID); (err != nil) != tt.wantErr { - t.Errorf("CreateTagGroup() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestGetAllTagGroup(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagGroup{ - { - Name: string(validTagGroupes[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[2]), - TagID: string(validTagID), - }, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - } - tests := []struct { - name string - args args - want []models.TagGroup - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - }, - want: expectedResult, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllTagGroup(tt.args.ctx, tt.args.db) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagGroup() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagGroup() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetAllTagGroupByTag(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagGroup{ - { - Name: string(validTagGroupes[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[2]), - TagID: string(validTagID), - }, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - want []models.TagGroup - wantErr bool - }{ - { - name: "Test 1: Valid TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: validTagID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: No TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Invalid TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: "adads", - }, - want: []models.TagGroup{}, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllTagGroupByTag(tt.args.ctx, tt.args.db, tt.args.tagID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagGroupByTag() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagGroupByTag() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestDeleteTagGroup(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagGroupName models.AnthroveTagGroupName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveTagGroupName", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: validTagGroupes[0], - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveTagGroupName", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: "asdad", - }, - wantErr: false, - }, - { - name: "Test 3: No AnthroveTagGroupName", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := DeleteTagGroup(tt.args.ctx, tt.args.db, tt.args.tagGroupName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestDeleteTag(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) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagName models.AnthroveTagName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid TagName", - args: args{ - ctx: ctx, - db: gormDB, - tagName: models.AnthroveTagName(validTagID), - }, - wantErr: false, - }, - { - name: "Test 2: Invalid TagName", - args: args{ - ctx: ctx, - db: gormDB, - tagName: models.AnthroveTagName("aaa"), - }, - wantErr: false, - }, - { - name: "Test 3: No TagName", - args: args{ - ctx: ctx, - db: gormDB, - tagName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := DeleteTag(tt.args.ctx, tt.args.db, tt.args.tagName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestGetAllTagByTagType(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validTags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Character, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Alphyron", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - } - - expectetResult := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Character, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Alphyron", - Type: models.Character, - }, - } - - for _, tag := range validTags { - err = CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagType models.TagType - } - tests := []struct { - name string - args args - want []models.Tag - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - tagType: models.Character, - }, - want: expectetResult, - wantErr: false, - }, - { - name: "Test 2: invalid Tag Type", - args: args{ - ctx: ctx, - db: gormDB, - tagType: "aa", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: No Tag Type", - args: args{ - ctx: ctx, - db: gormDB, - tagType: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllTagByTagsType(tt.args.ctx, tt.args.db, tt.args.tagType) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagByTagsType() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkTag(got, tt.want) { - t.Errorf("GetAllTagByTagsType() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestCreateTagInBatchAndUpdate(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) - } - defer container.Terminate(ctx) - - // Setup Test - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - emptyTags := []models.Tag{} - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tags []models.Tag - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - db: gormDB, - tags: tags, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - db: gormDB, - tags: emptyTags, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - db: gormDB, - tags: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - db: gormDB, - tags: nil, - batchSize: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagInBatchAndUpdate(tt.args.ctx, tt.args.db, tt.args.tags, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagInBatchAndUpdate() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCreateTagAliasInBatch(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) - } - defer container.Terminate(ctx) - - // Setup Test - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - err = CreateTagInBatchAndUpdate(ctx, gormDB, tags, len(tags)) - if err != nil { - t.Fatal(err) - } - - tagAlias := []models.TagAlias{ - { - Name: "test1", - TagID: tags[0].Name, - }, - { - Name: "test2", - TagID: tags[1].Name, - }, - { - Name: "test3", - TagID: tags[2].Name, - }, - { - Name: "test4", - TagID: tags[3].Name, - }, - } - emptyTagAlias := []models.TagAlias{} - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagAliases []models.TagAlias - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagAliases: tagAlias, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagAliases: emptyTagAlias, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagAliases: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - db: gormDB, - tagAliases: tagAlias, - batchSize: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagAliasInBatch(tt.args.ctx, tt.args.db, tt.args.tagAliases, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagAliasInBatch() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCreateTagGroupInBatch(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) - } - defer container.Terminate(ctx) - - // Setup Test - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - err = CreateTagInBatchAndUpdate(ctx, gormDB, tags, len(tags)) - if err != nil { - t.Fatal(err) - } - - tagGroup := []models.TagGroup{ - { - Name: "test1", - TagID: tags[0].Name, - }, - { - Name: "test2", - TagID: tags[1].Name, - }, - { - Name: "test3", - TagID: tags[2].Name, - }, - { - Name: "test4", - TagID: tags[3].Name, - }, - } - emptyTagGroup := []models.TagGroup{} - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagGroups []models.TagGroup - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagGroups: tagGroup, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagGroups: emptyTagGroup, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagGroups: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - db: gormDB, - tagGroups: tagGroup, - batchSize: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagGroupInBatch(tt.args.ctx, tt.args.db, tt.args.tagGroups, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagGroupInBatch() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} diff --git a/internal/postgres/user.go b/internal/postgres/user.go deleted file mode 100644 index 625e285..0000000 --- a/internal/postgres/user.go +++ /dev/null @@ -1,398 +0,0 @@ -package postgres - -import ( - "context" - "errors" - "time" - - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - gonanoid "github.com/matoous/go-nanoid/v2" - log "github.com/sirupsen/logrus" - "gorm.io/gorm" -) - -// Workaround, should be changed later maybe, but its not that bad right now -type selectFrequencyTag struct { - tagName string `gorm:"tag_name"` - count int64 `gorm:"count"` - tagType models.TagType `gorm:"tag_type"` -} - -func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error { - - if anthroveUserID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthroveUserID) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - user := models.User{ - BaseModel: models.BaseModel[models.AnthroveUserID]{ - ID: anthroveUserID, - }, - } - - result := db.WithContext(ctx).FirstOrCreate(&user) - if result.Error != nil { - - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - return nil -} - -func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error { - - if anthroveUserID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthroveUserID) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - if accountId == "" { - return &otterError.EntityValidationFailed{Reason: "accountID cannot be empty"} - } - - if accountUsername == "" { - return &otterError.EntityValidationFailed{Reason: "accountUsername cannot be empty"} - } - - validationCode, err := gonanoid.New(25) - - if err != nil { - return err - } - - result := db.WithContext(ctx).Exec(`WITH userObj AS ( - INSERT INTO "User" (id) - VALUES ($1) - ON CONFLICT (id) DO NOTHING - ) - INSERT INTO "UserSource" (user_id, source_id, account_username, account_id, account_validate, account_validation_key) - SELECT $2, source.id, $3, $4, false, $5 - FROM "Source" AS source - WHERE source.id = $6;`, anthroveUserID, anthroveUserID, accountUsername, accountId, validationCode, sourceID) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.EntityAlreadyExists{} - } - return result.Error - } - - if result.RowsAffected == 0 { - return &otterError.NoDataWritten{} - } - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "source_id": sourceID, - "account_username": accountUsername, - "account_id": accountId, - }).Info("database: created user-source relationship") - - return nil -} - -func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (int64, error) { - var count int64 - - if anthroveUserID == "" { - return 0, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthroveUserID) != 25 { - return 0, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - result := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Count(&count) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return 0, &otterError.NoDataFound{} - } - return 0, result.Error - } - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "anthrove_user_fav_count": count, - }).Trace("database: got user favorite count") - - return count, nil -} - -func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) { - var userSources []models.UserSource - userSourceMap := make(map[string]models.UserSource) - - if anthroveUserID == "" { - return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthroveUserID) != 25 { - return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - result := db.WithContext(ctx).Model(&models.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - for _, userSource := range userSources { - var source models.Source - result = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - userSourceMap[source.DisplayName] = models.UserSource{ - UserID: userSource.AccountID, - AccountUsername: userSource.AccountUsername, - Source: models.Source{ - DisplayName: source.DisplayName, - Domain: source.Domain, - Icon: source.Icon, - }, - } - } - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - }).Trace("database: got user source link") - - return userSourceMap, nil -} - -func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (*models.UserSource, error) { - var userSource models.UserSource - - if anthroveUserID == "" { - return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthroveUserID) != 25 { - return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - if sourceID == "" { - return nil, &otterError.EntityValidationFailed{Reason: "sourceID cannot be empty"} - } - - if len(sourceID) != 25 { - return nil, &otterError.EntityValidationFailed{Reason: "sourceID needs to be 25 characters long"} - } - - result := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(&userSource) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "source_id": sourceID, - }).Trace("database: got specified user source link") - - return &userSource, nil -} - -func GetAllUsers(ctx context.Context, db *gorm.DB) ([]models.User, error) { - var users []models.User - - result := db.WithContext(ctx).Model(&models.User{}).Find(&users) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, result.Error - } - - log.WithFields(log.Fields{ - "anthrove_user_id_count": len(users), - }).Trace("database: got all anthrove user IDs") - - return users, nil -} - -// TODO: FIX THE TEST -func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) { - var favoritePosts []models.Post - - if anthroveUserID == "" { - return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthroveUserID) != 25 { - return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - db.WithContext(ctx).Joins("RIGHT JOIN \"UserFavorite\" AS of ON \"Post\".id = of.post_id AND of.user_id = ?", anthroveUserID).Preload("References").Offset(skip).Limit(limit).Find(&favoritePosts) - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "anthrove_user_fav_count": len(favoritePosts), - }).Trace("database: got all anthrove user favorites") - - return &models.FavoriteList{Posts: favoritePosts}, nil -} - -func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { - var queryUserFavorites []selectFrequencyTag - - if anthroveUserID == "" { - return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthroveUserID) != 25 { - return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - rows, err := db.WithContext(ctx).Raw( - `WITH user_posts AS ( - SELECT post_id FROM "UserFavorite" WHERE user_id = $1 - ) - SELECT post_tags.tag_name AS tag_name, count(*) AS count, (SELECT tag_type FROM "Tag" WHERE "Tag".name = post_tags.tag_name LIMIT 1) AS tag_type FROM post_tags, user_posts WHERE post_tags.post_id IN (user_posts.post_id) GROUP BY post_tags.tag_name ORDER BY tag_type DESC, tag_name DESC`, anthroveUserID).Rows() - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, err - } - - var userFavoritesFrequency = make([]models.TagsWithFrequency, 0) - defer rows.Close() - for rows.Next() { - var tagName string - var count int64 - var tagType string - rows.Scan(&tagName, &count, &tagType) - userFavoritesFrequency = append(userFavoritesFrequency, models.TagsWithFrequency{ - Frequency: count, - Tags: models.Tag{ - Name: tagName, - Type: models.TagType(tagType), - }, - }) - } - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "tag_amount": len(queryUserFavorites), - }).Trace("database: got user tag node with relation to faved posts") - - return userFavoritesFrequency, nil -} - -func UpdateUserSourceScrapeTimeInterval(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error { - - if anthroveUserID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthroveUserID) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - if sourceID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDEmpty} - } - - if len(sourceID) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDToShort} - } - - if scrapeTime == 0 { - return &otterError.EntityValidationFailed{Reason: "ScrapeTimeInterval cannot be empty"} - } - - userSource := &models.UserSource{ - UserID: string(anthroveUserID), - } - - result := db.WithContext(ctx).Model(&userSource).Update("scrape_time_interval", scrapeTime) - if result.Error != nil { - return result.Error - } - - return nil -} - -func UpdateUserSourceLastScrapeTime(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error { - - if anthroveUserID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthroveUserID) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - if sourceID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDEmpty} - } - - if len(sourceID) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDToShort} - } - - if time.Time.IsZero(time.Time(lastScrapeTime)) { - return &otterError.EntityValidationFailed{Reason: "LastScrapeTime cannot be empty"} - } - - userSource := &models.UserSource{ - UserID: string(anthroveUserID), - } - - result := db.WithContext(ctx).Model(&userSource).Update("last_scrape_time", time.Time(lastScrapeTime)) - if result.Error != nil { - return result.Error - } - - return nil -} - -func UpdateUserSourceValidation(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error { - - if anthroveUserID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} - } - - if len(anthroveUserID) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} - } - - if sourceID == "" { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDEmpty} - } - - if len(sourceID) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDToShort} - } - - userSource := &models.UserSource{ - UserID: string(anthroveUserID), - } - - result := db.WithContext(ctx).Model(&userSource).Update("account_validate", valid) - if result.Error != nil { - return result.Error - } - - return nil -} diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go deleted file mode 100644 index aa89e05..0000000 --- a/internal/postgres/user_test.go +++ /dev/null @@ -1,1370 +0,0 @@ -package postgres - -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" - "gorm.io/gorm" -) - -func TestCreateUser(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - }, - wantErr: true, - }, - { - name: "Test 3: No anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateUser(tt.args.ctx, tt.args.db, tt.args.anthroveUserID); (err != nil) != tt.wantErr { - t.Errorf("CreateUser() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCreateUserNodeWithSourceRelation(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - } - err = CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - userID string - username string - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid anthroveUserID, sourceID, userID, username", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: source.ID, - userID: "e1", - username: "marius", - }, - wantErr: false, - }, - { - name: "Test 2: Invalid anthroveUserID, valid sourceID, userID, username", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - sourceID: source.ID, - userID: "e1", - username: "marius", - }, - wantErr: true, - }, - { - name: "Test 3: Empty anthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: source.ID, - userID: "e1", - username: "marius", - }, - wantErr: true, - }, - { - name: "Test 4: invalid sourceID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "fa.net", - userID: "e1", - username: "marius", - }, - wantErr: true, - }, - { - name: "Test 5: no userID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: source.ID, - userID: "", - username: "marius", - }, - wantErr: true, - }, - { - name: "Test 6: no username", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: source.ID, - userID: "aa", - username: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateUserWithRelationToSource(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.userID, tt.args.username); (err != nil) != tt.wantErr { - t.Errorf("CreateUserWithRelationToSource() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestGetAllUsers(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) - } - defer container.Terminate(ctx) - - // Setup Test - validUserID01 := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - validUserID02 := models.AnthroveUserID(fmt.Sprintf("%025s", "User2")) - validUserID03 := models.AnthroveUserID(fmt.Sprintf("%025s", "User3")) - - users := []models.User{ - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID01}, - }, - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID02}, - }, - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID03}, - }, - } - - for _, user := range users { - err = CreateUser(ctx, gormDB, user.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - } - tests := []struct { - name string - args args - want []models.User - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - }, - want: users, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllUsers(tt.args.ctx, tt.args.db) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllUsers() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkUser(got, tt.want) { - t.Errorf("GetAllUsers() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetUserSourceBySourceID(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - expectedResult := &models.UserSource{ - UserID: string(validUserID), - AccountUsername: "euser", - Source: models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: source.ID}, - DisplayName: source.DisplayName, - Domain: source.Domain, - Icon: source.Icon, - }, - } - - err = CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult.UserID, expectedResult.AccountUsername) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - } - tests := []struct { - name string - args args - want *models.UserSource - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID and sourceID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: source.ID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveUserID and valid sourceID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - sourceID: source.ID, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Valid AnthroveUserID and invalid sourceID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "fa", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 4: No AnthroveUserID and Valid sourceID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: source.ID, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 5: Valid AnthroveUserID and No anthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "1", - sourceID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 6: No AnthroveUserID and No anthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 7: No anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "", - }, - want: nil, - wantErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetUserSourceBySourceID(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID) - if (err != nil) != tt.wantErr { - t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkUserSource(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() - container, gormDB, err := test.StartPostgresContainer(ctx) - if err != nil { - t.Fatalf("Could not start PostgreSQL container: %v", err) - } - defer container.Terminate(ctx) - - // Setup Test - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) - validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) - validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) - - expectedResultPosts := []models.Post{ - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, - Rating: "safe", - }, - { - - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, - Rating: "safe", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, - Rating: "questionable", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, - Rating: "safe", - }, - } - expectedResult := &models.FavoriteList{ - Posts: expectedResultPosts, - } - expectedResult2 := &models.FavoriteList{ - Posts: expectedResultPosts[2:], - } - expectedResult3 := &models.FavoriteList{ - Posts: expectedResultPosts[:3], - } - - err = CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - for _, expectedResultPost := range expectedResultPosts { - err = CreatePost(ctx, gormDB, &expectedResultPost) - if err != nil { - t.Fatal(err) - } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, expectedResultPost.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - skip int - limit int - } - tests := []struct { - name string - args args - want *models.FavoriteList - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - skip: 0, - limit: 2000, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: Skip first two", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - skip: 2, - limit: 2000, - }, - want: expectedResult2, - wantErr: false, - }, - { - name: "Test 3: Limit of 3", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - skip: 0, - limit: 3, - }, - want: expectedResult3, - wantErr: false, - }, - { - name: "Test 4: No anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - skip: 0, - limit: 3, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 5: Short anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "aaa", - skip: 0, - limit: 3, - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - 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("GetAllUserFavoritesWithPagination() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkFavoritePosts(got, tt.want) { - t.Errorf("GetAllUserFavoritesWithPagination() got = %v, want %v", got, tt.want) - } - }) - } -} - -func checkFavoritePosts(got *models.FavoriteList, want *models.FavoriteList) bool { - if got == nil && want == nil { - return true - } else if got == nil || want == nil { - return false - } - - for i, post := range got.Posts { - if post.ID == want.Posts[i].ID { - } else { - return false - } - } - - return true -} - -func TestGetUserFavoritesCount(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) - validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) - validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) - - expectedResultPosts := []models.Post{ - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, - Rating: "safe", - }, - { - - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, - Rating: "safe", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, - Rating: "questionable", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, - Rating: "safe", - }, - } - - err = CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - for _, post := range expectedResultPosts { - err = CreatePost(ctx, gormDB, &post) - if err != nil { - t.Fatal(err) - } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, post.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want int64 - wantErr bool - }{ - { - name: "Test 1: Valid anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - }, - want: 6, - wantErr: false, - }, - { - name: "Test 2: Invalid anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "2", - }, - want: 0, - wantErr: true, - }, - { - name: "Test 3: no anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - }, - want: 0, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetUserFavoritesCount(tt.args.ctx, tt.args.db, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetUserFavoritesCount() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("GetUserFavoritesCount() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetUserSourceLinks(t *testing.T) { - // Setup trow away containert - ctx := context.Background() - container, gormDB, err := test.StartPostgresContainer(ctx) - if err != nil { - t.Fatalf("Could not start PostgreSQL container: %v", err) - } - defer container.Terminate(ctx) - - // Setup Test - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - validSourceID2 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source2")) - - eSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, - DisplayName: "e621", - Domain: "e621.net", - } - err = CreateSource(ctx, gormDB, eSource) - if err != nil { - t.Fatal("Create Source e621:", err) - } - - faSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID2}, - DisplayName: "fa", - Domain: "fa.net", - } - err = CreateSource(ctx, gormDB, faSource) - if err != nil { - t.Fatal("Create Source fa:", err) - } - - expectedResult := make(map[string]models.UserSource) - expectedResult["e621"] = models.UserSource{ - UserID: "e1", - AccountUsername: "e621-user", - Source: models.Source{ - DisplayName: eSource.DisplayName, - Domain: eSource.Domain, - }, - } - expectedResult["fa"] = models.UserSource{ - UserID: "fa1", - AccountUsername: "fa-user", - Source: models.Source{ - DisplayName: faSource.DisplayName, - Domain: faSource.Domain, - }, - } - - err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) - if err != nil { - t.Fatal("CreateUserWithRelationToSource e621:", err) - } - err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) - if err != nil { - t.Fatal("CreateUserWithRelationToSource fa:", err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want map[string]models.UserSource - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 3: No AnthroveID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 1: AnthroveID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "aaa", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetUserSourceLinks(tt.args.ctx, tt.args.db, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllUserSources() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllUserSources() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { - // Setup trow away containert - ctx := context.Background() - container, gormDB, err := test.StartPostgresContainer(ctx) - if err != nil { - t.Fatalf("Could not start PostgreSQL container: %v", err) - } - defer container.Terminate(ctx) - - // Setup Test - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - - err = CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - posts := []models.Post{ - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, Rating: "safe"}, - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, Rating: "safe"}, - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, Rating: "explicit"}, - } - - for _, post := range posts { - err = CreatePost(ctx, gormDB, &post) - if err != nil { - t.Fatal(err) - } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, post.ID) - if err != nil { - t.Fatal(err) - } - } - - tags := []models.Tag{ - {Name: "JayTheFerret", Type: "artist"}, - {Name: "Ferret", Type: "species"}, - {Name: "Jay", Type: "character"}, - } - - for i, tag := range tags { - err = CreateTagAndReferenceToPost(ctx, gormDB, posts[i].ID, &tag) - if err != nil { - t.Fatal(err) - } - } - - expectedResult := []models.TagsWithFrequency{ - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[0].Name, - Type: tags[0].Type, - }, - }, - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[2].Name, - Type: tags[2].Type, - }, - }, - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[1].Name, - Type: tags[1].Type, - }, - }, - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want []models.TagsWithFrequency - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: No anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: short anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "aaa", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetUserTagWitRelationToFavedPosts(tt.args.ctx, tt.args.db, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagsFromUser() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagsFromUser() got = %v, want %v", got, tt.want) - } - }) - } -} - -func checkUser(got []models.User, want []models.User) bool { - for i, user := range want { - if user.ID != got[i].ID { - return false - } - } - return true -} - -func checkUserSource(got *models.UserSource, want *models.UserSource) bool { - - if got == nil && want == nil { - return true - } else if got == nil || want == nil { - return false - } - - if got.UserID != want.UserID { - return false - } - if got.AccountUsername != want.AccountUsername { - return false - } - if got.Source.DisplayName != want.Source.DisplayName { - return false - } - if got.Source.Domain != want.Source.Domain { - return false - } - if got.Source.Icon != want.Source.Icon { - return false - } - - return true -} - -func TestUpdateUserSourceScrapeTimeInterval(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - scrapeTime models.AnthroveScrapeTimeInterval - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "111", - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "", - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 5: scrapeTime is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: validSourceID, - scrapeTime: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := UpdateUserSourceScrapeTimeInterval(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.scrapeTime); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceScrapeTimeInterval() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestUpdateUserSourceLastScrapeTime(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - validScrapeTime := models.AnthroveUserLastScrapeTime(time.Now()) - inValidScrapeTime := models.AnthroveUserLastScrapeTime{} - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - lastScrapeTime models.AnthroveUserLastScrapeTime - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "111", - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "", - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 5: scrapeTime is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: validSourceID, - lastScrapeTime: inValidScrapeTime, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := UpdateUserSourceLastScrapeTime(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.lastScrapeTime); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceLastScrapeTime() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestUpdateUserSourceValidation(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) - } - defer container.Terminate(ctx) - - // Setup Test - - validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - invalidUserID := models.AnthroveUserID("XXX") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - valid bool - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: validSourceID, - valid: true, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - valid: true, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: validSourceID, - valid: true, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "111", - valid: true, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "", - valid: true, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := UpdateUserSourceValidation(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.valid); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceValidation() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} -- 2.45.2 From 505bc3b5224300ba61e0cec5975eaf81bed137e4 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:15:21 +0200 Subject: [PATCH 27/77] feat(tracing): Refactor error handling to include trace status update --- internal/utils/error.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/utils/error.go b/internal/utils/error.go index 9cb381a..f73c2cf 100644 --- a/internal/utils/error.go +++ b/internal/utils/error.go @@ -2,12 +2,15 @@ package utils import ( "context" + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" ) -func HandleError(ctx context.Context, span trace.Span, logger *log.Logger, fields log.Fields, error error) error { - logger.WithContext(ctx).WithFields(fields).Error(error) +func HandleError(ctx context.Context, span trace.Span, logger *log.Entry, error error) error { + logger.Error(error) span.RecordError(error) + span.SetStatus(codes.Error, error.Error()) return error } -- 2.45.2 From 9512cd10bc2f0c1c29637965e0ac11c334e8ac24 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:15:34 +0200 Subject: [PATCH 28/77] feat(tracing): Add utils for OpenTelemetry tracing and event logging --- internal/utils/tracing.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 internal/utils/tracing.go diff --git a/internal/utils/tracing.go b/internal/utils/tracing.go new file mode 100644 index 0000000..6e83a9f --- /dev/null +++ b/internal/utils/tracing.go @@ -0,0 +1,20 @@ +package utils + +import ( + "context" + + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/trace" +) + +func SetupTracing(ctx context.Context, tracer trace.Tracer, tracerName string) (context.Context, trace.Span, *log.Entry) { + ctx, span := tracer.Start(ctx, tracerName) + localLogger := log.WithContext(ctx) + + return ctx, span, localLogger +} + +func HandleEvent(span trace.Span, logger *log.Entry, eventName string) { + logger.Debug(eventName) + span.AddEvent(eventName) +} -- 2.45.2 From ffc3164cb78811457234c595de38805574fe4143 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:17:02 +0200 Subject: [PATCH 29/77] refactor(tracing): tracing & logging added the custom util functions to handel telemetry --- pkg/database/tag.go | 108 ++++++++++++--------------------------- pkg/database/tagAlias.go | 108 ++++++++++++--------------------------- pkg/database/tagGroup.go | 106 +++++++++++++------------------------- pkg/database/user.go | 85 ++++++++++++++++++++---------- 4 files changed, 161 insertions(+), 246 deletions(-) diff --git a/pkg/database/tag.go b/pkg/database/tag.go index 7f590d7..cd40dd0 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -13,18 +13,23 @@ import ( ) func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagType) (models.Tag, error) { - ctx, span := tracer.Start(ctx, "CreateTag") + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTag") defer span.End() + localLogger.WithFields(log.Fields{ + "tag_name": tagName, + "tag_type": tagType, + }) + span.SetAttributes( attribute.String("tag_name", string(tagName)), attribute.String("tag_type", string(tagType)), ) - span.AddEvent("Starting tag creation") + utils.HandleEvent(span, localLogger, "Starting tag creation") if client == nil { - return models.Tag{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } tag := models.Tag{ @@ -32,131 +37,86 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy Type: tagType, } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_name": tagName, - "tag_type": tagType, - }).Debug("attempting to create tag") - result := client.WithContext(ctx).Create(&tag) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - - loggerFields := log.Fields{ - "tag_name": tagName, - "tag_type": tagType, - } - return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) + return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - - loggerFields := log.Fields{ - "tag_name": tagName, - "tag_type": tagType, - } - return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error) + return models.Tag{}, utils.HandleError(ctx, span, localLogger, result.Error) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_name": tagName, - "tag_type": tagType, - }).Debug("tag created") - span.AddEvent("Tag created successfully") + utils.HandleEvent(span, localLogger, "Tag created successfully") return tag, nil } func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) error { - ctx, span := tracer.Start(ctx, "CreateTagInBatch") + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagInBatch") defer span.End() + localLogger.WithFields(log.Fields{ + "tag_count": len(tags), + "batch_size": batchSize, + }) + span.SetAttributes( attribute.Int64("batch_size", int64(batchSize)), attribute.Int64("tag_count", int64(len(tags))), ) - span.AddEvent("Starting batch tag creation") + utils.HandleEvent(span, localLogger, "Starting batch tag creation") if client == nil { - return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if tags == nil || len(tags) == 0 { - return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty}) + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty}) } if batchSize == 0 { - return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_length": len(tags), - }).Debug("attempting to create tags") - result := client.WithContext(ctx).CreateInBatches(&tags, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - loggerFields := log.Fields{ - "tag_length": len(tags), - } - return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - - loggerFields := log.Fields{ - "tag_length": len(tags), - } - return utils.HandleError(ctx, span, logger, loggerFields, result.Error) + return utils.HandleError(ctx, span, localLogger, result.Error) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_length": len(tags), - }).Debug("tags created") - - span.AddEvent("Batch tags created successfully") + utils.HandleEvent(span, localLogger, "Batch tags created successfully") return nil } func DeleteTag(ctx context.Context, tagName models.TagName) error { - ctx, span := tracer.Start(ctx, "DeleteTag") + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTag") defer span.End() + localLogger.WithFields(log.Fields{ + "tag_name": tagName, + }) + span.SetAttributes( attribute.String("tag_name", string(tagName)), ) - span.AddEvent("Starting tag deletion") + utils.HandleEvent(span, localLogger, "Starting tag deletion") var tag models.Tag if client == nil { - return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - if len(tagName) == 0 { - return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagNameIsEmpty}) - } - - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_name": tagName, - }).Debug("attempting to delete tag") - result := client.WithContext(ctx).Delete(&tag, tagName) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - - loggerFields := log.Fields{ - "tag_name": tagName, - } - return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - - loggerFields := log.Fields{ - "tag_name": tagName, - } - return utils.HandleError(ctx, span, logger, loggerFields, result.Error) + return utils.HandleError(ctx, span, localLogger, result.Error) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_name": tagName, - }).Debug("tag deleted") - - span.AddEvent("Tag deleted successfully") + utils.HandleEvent(span, localLogger, "Tag deleted successfully") return nil } diff --git a/pkg/database/tagAlias.go b/pkg/database/tagAlias.go index 2138380..7918787 100644 --- a/pkg/database/tagAlias.go +++ b/pkg/database/tagAlias.go @@ -13,18 +13,23 @@ import ( ) func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) (models.TagAlias, error) { - ctx, span := tracer.Start(ctx, "CreateTagAlias") + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAlias") defer span.End() + localLogger.WithFields(log.Fields{ + "tag_alias_name": tagAliasName, + "tag_name": tagName, + }) + span.SetAttributes( attribute.String("tag_alias_name", string(tagAliasName)), attribute.String("tag_name", string(tagName)), ) - span.AddEvent("Starting tag alias creation") + utils.HandleEvent(span, localLogger, "Starting tag alias creation") if client == nil { - return models.TagAlias{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } tagAlias := models.TagAlias{ @@ -32,131 +37,86 @@ func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagNa TagID: tagName, } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_alias_name": tagAliasName, - "tag_name": tagName, - }).Debug("attempting to create tag alias") - result := client.WithContext(ctx).Create(&tagAlias) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - - loggerFields := log.Fields{ - "tag_alias_name": tagAliasName, - "tag_name": tagName, - } - return models.TagAlias{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) + return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - - loggerFields := log.Fields{ - "tag_alias_name": tagAliasName, - "tag_name": tagName, - } - return models.TagAlias{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error) + return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, result.Error) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_alias_name": tagAliasName, - "tag_name": tagName, - }).Debug("tag alias created") - span.AddEvent("Tag alias created successfully") + utils.HandleEvent(span, localLogger, "Tag alias created successfully") return tagAlias, nil } func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error { - ctx, span := tracer.Start(ctx, "CreateTagAliasInBatch") + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch") defer span.End() + localLogger.WithFields(log.Fields{ + "tag_aliases_count": len(tagsAliases), + "batch_size": batchSize, + }) + span.SetAttributes( attribute.Int64("batch_size", int64(batchSize)), attribute.Int64("tag_aliases_count", int64(len(tagsAliases))), ) - span.AddEvent("Starting batch tag alias creation") + utils.HandleEvent(span, localLogger, "Starting batch tag alias creation") if client == nil { - return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if tagsAliases == nil || len(tagsAliases) == 0 { - return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty}) + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty}) } if batchSize == 0 { - return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_aliases_count": len(tagsAliases), - }).Debug("attempting to create tags aliases") - result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - loggerFields := log.Fields{ - "tag_aliases_count": len(tagsAliases), - } - return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - - loggerFields := log.Fields{ - "tag_aliases_count": len(tagsAliases), - } - return utils.HandleError(ctx, span, logger, loggerFields, result.Error) + return utils.HandleError(ctx, span, localLogger, result.Error) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_aliases_count": len(tagsAliases), - }).Debug("batch tags aliases created") - - span.AddEvent("Batch tags aliases created successfully") + utils.HandleEvent(span, localLogger, "Batch tags aliases created successfully") return nil } func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error { - ctx, span := tracer.Start(ctx, "DeleteTagAlias") + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagAlias") defer span.End() + localLogger.WithFields(log.Fields{ + "tag_alias_name": tagAliasName, + }) + span.SetAttributes( attribute.String("tag_alias_name", string(tagAliasName)), ) - span.AddEvent("Starting tag alias deletion") + utils.HandleEvent(span, localLogger, "Starting tag alias deletion") var tagAlias models.TagAlias if client == nil { - return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - if len(tagAliasName) == 0 { - return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty}) - } - - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_alias_name": tagAliasName, - }).Debug("attempting to delete tag alias") - result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - - loggerFields := log.Fields{ - "tag_alias_name": tagAliasName, - } - return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - - loggerFields := log.Fields{ - "tag_alias_name": tagAliasName, - } - return utils.HandleError(ctx, span, logger, loggerFields, result.Error) + return utils.HandleError(ctx, span, localLogger, result.Error) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_alias_name": tagAliasName, - }).Debug("tag alias deleted") - - span.AddEvent("Tag alias deleted successfully") + utils.HandleEvent(span, localLogger, "Tag alias deleted successfully") return nil } diff --git a/pkg/database/tagGroup.go b/pkg/database/tagGroup.go index 96c1a0d..a09a4e8 100644 --- a/pkg/database/tagGroup.go +++ b/pkg/database/tagGroup.go @@ -13,18 +13,24 @@ import ( ) func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagName models.TagName) (models.TagGroup, error) { - ctx, span := tracer.Start(ctx, "CreateTagGroup") + + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagGroup") defer span.End() + localLogger.WithFields(log.Fields{ + "tag_group_name": tagGroupName, + "tag_name": tagName, + }) + span.SetAttributes( attribute.String("tag_group_name", string(tagGroupName)), attribute.String("tag_name", string(tagName)), ) - span.AddEvent("Starting tag group creation") + utils.HandleEvent(span, localLogger, "Starting tag group creation") if client == nil { - return models.TagGroup{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } tagGroup := models.TagGroup{ @@ -32,132 +38,92 @@ func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagNa TagID: tagName, } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_group_name": tagGroupName, - "tag_name": tagName, - }).Debug("attempting to create tag group") - result := client.WithContext(ctx).Create(&tagGroup) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - - loggerFields := log.Fields{ - "tag_group_name": tagGroupName, - "tag_name": tagName, - } - return models.TagGroup{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) + return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - - loggerFields := log.Fields{ - "tag_group_name": tagGroupName, - "tag_name": tagName, - } - return models.TagGroup{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error) + return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, result.Error) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_group_name": tagGroupName, - "tag_name": tagName, - }).Debug("tag group created") - span.AddEvent("Tag group created successfully") + utils.HandleEvent(span, localLogger, "Tag group created successfully") return tagGroup, nil } func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error { - ctx, span := tracer.Start(ctx, "CreateTagAliasInBatch") + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch") defer span.End() + localLogger.WithFields(log.Fields{ + "tag_groups_count": len(tagsGroups), + "batch_size": batchSize, + }) + span.SetAttributes( attribute.Int64("batch_size", int64(batchSize)), attribute.Int64("tag_group_count", int64(len(tagsGroups))), ) - span.AddEvent("Starting batch tag group creation") + utils.HandleEvent(span, localLogger, "Starting batch tag group creation") if client == nil { - return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if tagsGroups == nil || len(tagsGroups) == 0 { - return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty}) + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty}) } if batchSize == 0 { - return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_groups_count": len(tagsGroups), - }).Debug("attempting to create tags groups") - result := client.WithContext(ctx).CreateInBatches(&tagsGroups, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - loggerFields := log.Fields{ - "tag_group_count": len(tagsGroups), - } - return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - loggerFields := log.Fields{ - "tag_group_count": len(tagsGroups), - } - return utils.HandleError(ctx, span, logger, loggerFields, result.Error) + return utils.HandleError(ctx, span, localLogger, result.Error) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_group_count": len(tagsGroups), - }).Debug("batch tags groups created") - - span.AddEvent("Batch tags groups created successfully") + utils.HandleEvent(span, localLogger, "Tag group created successfully") return nil } func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error { - ctx, span := tracer.Start(ctx, "DeleteTagGroup") + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagGroup") defer span.End() span.SetAttributes( attribute.String("tag_group_name", string(tagGroupName)), ) - span.AddEvent("Starting tag group deletion") + localLogger.WithFields(log.Fields{ + "tag_group_name": tagGroupName, + }) + + utils.HandleEvent(span, localLogger, "Starting tag group deletion") var tagGroup models.TagGroup if client == nil { - return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(tagGroupName) == 0 { - return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagGroupNameIsEmpty}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.TagGroupNameIsEmpty}) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_group_name": tagGroupName, - }).Debug("attempting to delete tag group") - result := client.WithContext(ctx).Delete(&tagGroup, tagGroupName) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - - loggerFields := log.Fields{ - "tag_group_name": tagGroupName, - } - return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound}) + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - - loggerFields := log.Fields{ - "tag_group_name": tagGroupName, - } - return utils.HandleError(ctx, span, logger, loggerFields, result.Error) + return utils.HandleError(ctx, span, localLogger, result.Error) } - logger.WithContext(ctx).WithFields(log.Fields{ - "tag_group_name": tagGroupName, - }).Debug("tag group deleted") - - span.AddEvent("Tag group deleted successfully") + utils.HandleEvent(span, localLogger, "Tag group deleted successfully") return nil } diff --git a/pkg/database/user.go b/pkg/database/user.go index d78505a..c7346e6 100644 --- a/pkg/database/user.go +++ b/pkg/database/user.go @@ -3,77 +3,106 @@ package database import ( "context" "errors" + + "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" ) -func CreateUser(ctx context.Context, id models.UserID) (models.User, error) { - if client == nil { - return models.User{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} - } +func CreateUser(ctx context.Context, user models.User) (models.User, error) { - user := models.User{ - BaseModel: models.BaseModel[models.UserID]{ - ID: id, - }, + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUser") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_id": user.ID, + }) + + span.SetAttributes( + attribute.String("user_id", string(user.ID)), + ) + + utils.HandleEvent(span, localLogger, "Starting user creation") + + if client == nil { + return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } result := client.WithContext(ctx).Create(&user) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return models.User{}, &otterError.Database{Reason: otterError.DuplicateKey} + return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - return models.User{}, result.Error + return models.User{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User created successfully") return user, nil } func GetUserByID(ctx context.Context, id models.UserID) (models.User, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserByID") + defer span.End() + + span.SetAttributes( + attribute.String("user_id", string(id)), + ) + + localLogger.WithFields(log.Fields{ + "user_id": id, + }) + + utils.HandleEvent(span, localLogger, "Starting user retrieval") + var user models.User if client == nil { - return models.User{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} - } - - if len(id) == 0 { - return models.User{}, &otterError.EntityValidationFailed{Reason: otterError.UserIDIsEmpty} - } - - if len(id) != 25 { - return models.User{}, &otterError.EntityValidationFailed{Reason: otterError.UserIDToShort} + return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } result := client.WithContext(ctx).First(&user, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return models.User{}, &otterError.Database{Reason: otterError.NoDataFound} + return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return models.User{}, result.Error + return models.User{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User retrieved successfully") return user, nil } func DeleteUser(ctx context.Context, id models.UserID) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUser") + defer span.End() + + span.SetAttributes( + attribute.String("user_id", string(id)), + ) + + localLogger.WithFields(log.Fields{ + "user_id": id, + }) + + utils.HandleEvent(span, localLogger, "Starting user deletion") + var user models.User if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} - } - - if len(id) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.UserIDIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } result := client.WithContext(ctx).Delete(&user, id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.Database{Reason: otterError.NoDataFound} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User deleted successfully") return nil } -- 2.45.2 From 4d262c8fffdfabd823dbf9ba87c14bb8b7cc0a65 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:32:53 +0200 Subject: [PATCH 30/77] refactor(tracing): tracing & logging added the custom util functions to handel telemetry --- pkg/database/userSource.go | 94 ++++++++++++++++++++++++++++++-------- 1 file changed, 76 insertions(+), 18 deletions(-) diff --git a/pkg/database/userSource.go b/pkg/database/userSource.go index bf1879d..6576128 100644 --- a/pkg/database/userSource.go +++ b/pkg/database/userSource.go @@ -3,34 +3,65 @@ package database import ( "context" "errors" + + "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" ) func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserSource") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_source_id": userSource.ID, + }) + + span.SetAttributes( + attribute.String("user_source_id", string(userSource.SourceID)), + ) + + utils.HandleEvent(span, localLogger, "Starting user source creation") + if client == nil { - return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } result := client.WithContext(ctx).Create(&userSource) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return models.UserSource{}, &otterError.Database{Reason: otterError.DuplicateKey} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - return models.UserSource{}, result.Error + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User source created successfully") return userSource, nil } func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserSource") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_source_id": userSource.ID, + }) + + span.SetAttributes( + attribute.String("user_source_id", string(userSource.ID)), + ) + + utils.HandleEvent(span, localLogger, "Starting user source update") + if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(userSource.ID) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.UserSourceIDIsEmpty}) } updatedUserSource := models.UserSource{ @@ -47,62 +78,89 @@ func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { result := client.WithContext(ctx).Updates(&updatedUserSource) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.Database{Reason: otterError.DuplicateKey} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User source updated successfully") return nil } func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserSourceByID") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_source_id": id, + }) + + span.SetAttributes( + attribute.String("user_source_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting get user source by ID") + var user models.UserSource if client == nil { - return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - if len(id) == 0 { - return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty}) } if len(id) != 25 { - return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}) } result := client.WithContext(ctx).First(&user, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, otterError.Database{Reason: otterError.NoDataFound}) } - return models.UserSource{}, result.Error + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User source retrieved successfully") return user, nil } func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserSource") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_source_id": id, + }) + + span.SetAttributes( + attribute.String("user_source_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting delete user source") + var user models.UserSource if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - if len(id) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty}) } if len(id) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}) } result := client.WithContext(ctx).Delete(&user, id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.Database{Reason: otterError.NoDataFound} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } return result.Error } + utils.HandleEvent(span, localLogger, "User source deleted successfully") return nil } -- 2.45.2 From d994ead6f6a397fae8ba088277e5cf34b2c55f56 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:36:34 +0200 Subject: [PATCH 31/77] refactor(tracing): tracing & logging added the custom util functions to handel telemetry --- pkg/database/source.go | 148 ++++++++++++++++++++++++++++++++--------- 1 file changed, 117 insertions(+), 31 deletions(-) diff --git a/pkg/database/source.go b/pkg/database/source.go index 89116a2..c5691d0 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -3,62 +3,105 @@ package database import ( "context" "errors" + + "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" ) func CreateSource(ctx context.Context, source models.Source) (models.Source, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSource") + defer span.End() + + localLogger.WithFields(log.Fields{ + "source_id": source.ID, + }) + + span.SetAttributes( + attribute.String("source_id", string(source.ID)), + ) + + utils.HandleEvent(span, localLogger, "Starting source creation") + if client == nil { - return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } result := client.WithContext(ctx).Create(&source) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return models.Source{}, &otterError.Database{Reason: otterError.DuplicateKey} + return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - return models.Source{}, result.Error + return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Source created successfully") return source, nil } func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize int) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSourceInBatch") + defer span.End() + + localLogger.WithFields(log.Fields{ + "source_count": len(source), + "batch_size": batchSize, + }) + + span.SetAttributes( + attribute.Int64("batch_size", int64(batchSize)), + attribute.Int64("source_count", int64(len(source))), + ) + + utils.HandleEvent(span, localLogger, "Starting batch source creation") + if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - if source == nil { - return &otterError.EntityValidationFailed{Reason: otterError.SourceListIsEmpty} - } - - if len(source) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.SourceListIsEmpty} + if source == nil || len(source) == 0 { + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceListIsEmpty}) } if batchSize == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) } result := client.WithContext(ctx).CreateInBatches(&source, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.Database{Reason: otterError.DuplicateKey} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Batch sources created successfully") return nil } func UpdateSource(ctx context.Context, source models.Source) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateSource") + defer span.End() + + localLogger.WithFields(log.Fields{ + "source_id": source.ID, + }) + + span.SetAttributes( + attribute.String("source_id", string(source.ID)), + ) + + utils.HandleEvent(span, localLogger, "Starting source update") + if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(source.ID) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty}) } updateSource := models.Source{ @@ -70,84 +113,127 @@ func UpdateSource(ctx context.Context, source models.Source) error { result := client.WithContext(ctx).Model(&updateSource).Update("deleted_at", gorm.DeletedAt{}) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.Database{Reason: otterError.NoDataFound} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Source updated successfully") return nil } func GetSourceByID(ctx context.Context, id models.SourceID) (models.Source, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetSourceByID") + defer span.End() + + localLogger.WithFields(log.Fields{ + "source_id": id, + }) + + span.SetAttributes( + attribute.String("source_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting get source by ID") + var source models.Source if client == nil { - return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(id) == 0 { - return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty} + return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty}) } if len(id) != 25 { - return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceIDToShort} + return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDToShort}) } result := client.WithContext(ctx).First(&source, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return models.Source{}, &otterError.Database{Reason: otterError.NoDataFound} + return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return models.Source{}, result.Error + return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Source retrieved successfully") return source, nil } func GetSourceByDomain(ctx context.Context, sourceDomain models.SourceDomain) (models.Source, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetSourceByDomain") + defer span.End() + + localLogger.WithFields(log.Fields{ + "source_domain": sourceDomain, + }) + + span.SetAttributes( + attribute.String("source_domain", string(sourceDomain)), + ) + + utils.HandleEvent(span, localLogger, "Starting get source by domain") + var source models.Source if client == nil { - return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(sourceDomain) == 0 { - return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceDomainIsEmpty} + return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceDomainIsEmpty}) } result := client.WithContext(ctx).First(&source, "domain = ?", sourceDomain) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return models.Source{}, &otterError.Database{Reason: otterError.NoDataFound} + return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return models.Source{}, result.Error + return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Source retrieved successfully") return source, nil } func DeleteSource(ctx context.Context, id models.SourceID) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteSource") + defer span.End() + + localLogger.WithFields(log.Fields{ + "source_id": id, + }) + + span.SetAttributes( + attribute.String("source_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting delete source") + var source models.Source if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(id) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty}) } if len(id) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.SourceIDToShort} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDToShort}) } result := client.WithContext(ctx).Delete(&source, id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.Database{Reason: otterError.NoDataFound} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Source deleted successfully") return nil } -- 2.45.2 From 24d51a3751e8385f1e4e11f5f17b5aa890973ed9 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:39:22 +0200 Subject: [PATCH 32/77] refactor(tracing): tracing & logging added the custom util functions to handel telemetry --- pkg/database/post.go | 124 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 98 insertions(+), 26 deletions(-) diff --git a/pkg/database/post.go b/pkg/database/post.go index 989c39a..a69698f 100644 --- a/pkg/database/post.go +++ b/pkg/database/post.go @@ -3,84 +3,141 @@ package database import ( "context" "errors" + + "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" ) func CreatePost(ctx context.Context, post models.Post) (models.Post, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePost") + defer span.End() + + localLogger.WithFields(log.Fields{ + "post_id": post.ID, + }) + + span.SetAttributes( + attribute.String("post_id", string(post.ID)), + ) + + utils.HandleEvent(span, localLogger, "Starting post creation") + if client == nil { - return models.Post{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } result := client.WithContext(ctx).Create(&post) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return models.Post{}, &otterError.Database{Reason: otterError.DuplicateKey} + return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - return models.Post{}, result.Error + return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Post created successfully") return post, nil } func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePostInBatch") + defer span.End() + + localLogger.WithFields(log.Fields{ + "post_count": len(post), + "batch_size": batchSize, + }) + + span.SetAttributes( + attribute.Int64("batch_size", int64(batchSize)), + attribute.Int64("post_count", int64(len(post))), + ) + + utils.HandleEvent(span, localLogger, "Starting batch post creation") + if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - if post == nil { - return &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty} - } - - if len(post) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty} + if post == nil || len(post) == 0 { + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty}) } if batchSize == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) } result := client.WithContext(ctx).CreateInBatches(&post, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.Database{Reason: otterError.DuplicateKey} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Batch posts created successfully") return nil } func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetPostByID") + defer span.End() + + localLogger.WithFields(log.Fields{ + "post_id": id, + }) + + span.SetAttributes( + attribute.String("post_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting get post by ID") + var post models.Post if client == nil { - return models.Post{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(id) == 0 { - return models.Post{}, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty} + return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty}) } result := client.WithContext(ctx).First(&post, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return models.Post{}, &otterError.Database{Reason: otterError.NoDataFound} + return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return models.Post{}, result.Error + return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Post retrieved successfully") return post, nil } func UpdatePost(ctx context.Context, anthrovePost models.Post) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePost") + defer span.End() + + localLogger.WithFields(log.Fields{ + "post_id": anthrovePost.ID, + }) + + span.SetAttributes( + attribute.String("post_id", string(anthrovePost.ID)), + ) + + utils.HandleEvent(span, localLogger, "Starting post update") + if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(anthrovePost.ID) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty}) } updatePost := models.Post{ @@ -92,36 +149,51 @@ func UpdatePost(ctx context.Context, anthrovePost models.Post) error { result := client.WithContext(ctx).Model(&updatePost).Update("deleted_at", gorm.DeletedAt{}) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.Database{Reason: otterError.NoDataFound} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Post updated successfully") return nil } func DeletePost(ctx context.Context, id models.PostID) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePost") + defer span.End() + + localLogger.WithFields(log.Fields{ + "post_id": id, + }) + + span.SetAttributes( + attribute.String("post_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting delete post") + var userFavorite models.UserFavorite if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(id) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty}) } if len(id) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.PostIDToShort} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDToShort}) } result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.Database{Reason: otterError.NoDataFound} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Post deleted successfully") return nil } -- 2.45.2 From 135e09ea8e809a0db5f5bc6a64dd299b1af2b47f Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:46:53 +0200 Subject: [PATCH 33/77] refactor(tracing): tracing & logging added the custom util functions to handel telemetry --- pkg/database/favorite.go | 124 +++++++++++++++++++++++++++++++-------- 1 file changed, 98 insertions(+), 26 deletions(-) diff --git a/pkg/database/favorite.go b/pkg/database/favorite.go index b2194b5..5a720ac 100644 --- a/pkg/database/favorite.go +++ b/pkg/database/favorite.go @@ -3,62 +3,105 @@ package database import ( "context" "errors" + + "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" ) func CreateUserFavorite(ctx context.Context, userFav models.UserFavorite) (models.UserFavorite, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavorite") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_favorite_id": userFav.ID, + }) + + span.SetAttributes( + attribute.String("user_favorite_id", string(userFav.ID)), + ) + + utils.HandleEvent(span, localLogger, "Starting user favorite creation") + if client == nil { - return models.UserFavorite{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } result := client.WithContext(ctx).Create(&userFav) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return models.UserFavorite{}, &otterError.Database{Reason: otterError.DuplicateKey} + return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - return models.UserFavorite{}, result.Error + return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User favorite created successfully") return userFav, nil } func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorite, batchSize int) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavoriteInBatch") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_favorite_count": len(userFav), + "batch_size": batchSize, + }) + + span.SetAttributes( + attribute.Int64("batch_size", int64(batchSize)), + attribute.Int64("user_favorite_count", int64(len(userFav))), + ) + + utils.HandleEvent(span, localLogger, "Starting batch user favorite creation") + if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - if userFav == nil { - return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty} - } - - if len(userFav) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty} + if userFav == nil || len(userFav) == 0 { + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty}) } if batchSize == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) } result := client.WithContext(ctx).CreateInBatches(&userFav, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.Database{Reason: otterError.DuplicateKey} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "Batch user favorites created successfully") return nil } func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserFavorite") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_favorite_id": userFav.ID, + }) + + span.SetAttributes( + attribute.String("user_favorite_id", string(userFav.ID)), + ) + + utils.HandleEvent(span, localLogger, "Starting user favorite update") + if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(userFav.ID) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty}) } if !userFav.DeletedAt.Valid { @@ -68,58 +111,87 @@ func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error result := client.WithContext(ctx).Model(&userFav).Update("deleted_at", gorm.DeletedAt{}) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.Database{Reason: otterError.NoDataFound} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User favorite updated successfully") return nil } func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models.UserFavorite, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserFavoritesByID") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_favorite_id": id, + }) + + span.SetAttributes( + attribute.String("user_favorite_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting get user favorite by ID") + var userFavorites models.UserFavorite if client == nil { - return models.UserFavorite{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(id) == 0 { - return models.UserFavorite{}, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} + return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty}) } result := client.WithContext(ctx).First(&userFavorites, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return models.UserFavorite{}, &otterError.Database{Reason: otterError.NoDataFound} + return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return models.UserFavorite{}, result.Error + return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User favorite retrieved successfully") return userFavorites, nil } func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserFavorite") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_favorite_id": id, + }) + + span.SetAttributes( + attribute.String("user_favorite_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting delete user favorite") + var userFavorite models.UserFavorite if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(id) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty}) } if len(id) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDToShort} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDToShort}) } result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.Database{Reason: otterError.NoDataFound} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User favorite deleted successfully") return nil } -- 2.45.2 From ae7bc131f78fef85e6aaf787d2f01ce35a2480ce Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:50:45 +0200 Subject: [PATCH 34/77] refactor(tracing): tracing & logging added the custom util functions to handel telemetry --- pkg/database/client.go | 68 ++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/pkg/database/client.go b/pkg/database/client.go index 7433440..901ae9f 100644 --- a/pkg/database/client.go +++ b/pkg/database/client.go @@ -4,12 +4,15 @@ import ( "context" "embed" "fmt" + + "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" migrate "github.com/rubenv/sql-migrate" log "github.com/sirupsen/logrus" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" "gorm.io/driver/postgres" "gorm.io/gorm" @@ -22,12 +25,28 @@ var ( embedMigrations embed.FS client *gorm.DB tracer trace.Tracer - logger *log.Logger + logger = log.New() ) -func Connect(_ context.Context, config models.DatabaseConfig) error { - var localSSL string +func Connect(ctx context.Context, config models.DatabaseConfig) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "Connect") + defer span.End() + localLogger.WithFields(log.Fields{ + "endpoint": config.Endpoint, + "port": config.Port, + "database": config.Database, + }) + + span.SetAttributes( + attribute.String("endpoint", config.Endpoint), + attribute.Int("port", config.Port), + attribute.String("database", config.Database), + ) + + utils.HandleEvent(span, localLogger, "Starting database connection") + + var localSSL string if config.SSL { localSSL = "require" } else { @@ -36,46 +55,58 @@ func Connect(_ context.Context, config models.DatabaseConfig) error { dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.Timezone) sqlDB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) - if err != nil { - return err + return utils.HandleError(ctx, span, localLogger, err) } - err = migrateDatabase(sqlDB, config) + err = migrateDatabase(ctx, sqlDB, config) if err != nil { - return err + return utils.HandleError(ctx, span, localLogger, err) } setupTelemetry() client = sqlDB + utils.HandleEvent(span, localLogger, "Database connected successfully") return nil } -func migrateDatabase(dbPool *gorm.DB, config models.DatabaseConfig) error { +func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.DatabaseConfig) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "migrateDatabase") + defer span.End() + + localLogger.WithFields(log.Fields{ + "database": config.Database, + }) + + span.SetAttributes( + attribute.String("database", config.Database), + ) + + utils.HandleEvent(span, localLogger, "Starting database migration") + dialect := "postgres" migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"} db, err := dbPool.DB() if err != nil { - return fmt.Errorf("postgres migration: %v", err) + return utils.HandleError(ctx, span, localLogger, err) } n, err := migrate.Exec(db, dialect, migrations, migrate.Up) if err != nil { - return fmt.Errorf("postgres migration: %v", err) + return utils.HandleError(ctx, span, localLogger, err) } if config.Debug { if n != 0 { - log.Infof("postgres migration: applied %d migrations!", n) - + localLogger.Infof("postgres migration: applied %d migrations!", n) } else { - log.Info("postgres migration: nothing to migrate") - + localLogger.Info("postgres migration: nothing to migrate") } } + utils.HandleEvent(span, localLogger, "Database migration completed successfully") return nil } @@ -84,13 +115,18 @@ func setupTelemetry() { hook := otellogrus.NewHook(tracingName) logger.AddHook(hook) - } func GetGorm(ctx context.Context) (*gorm.DB, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetGorm") + defer span.End() + + utils.HandleEvent(span, localLogger, "Retrieving GORM client") + if client == nil { - return nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return nil, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } + utils.HandleEvent(span, localLogger, "GORM client retrieved successfully") return client, nil } -- 2.45.2 From 9d9b354698b6e3ffe7d1802c9aeab6d5d1419c76 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:52:53 +0200 Subject: [PATCH 35/77] fix(tracing): wrong attribute type changed attribute types in to Int --- pkg/database/favorite.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/database/favorite.go b/pkg/database/favorite.go index 5a720ac..c730176 100644 --- a/pkg/database/favorite.go +++ b/pkg/database/favorite.go @@ -52,8 +52,8 @@ func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorit }) span.SetAttributes( - attribute.Int64("batch_size", int64(batchSize)), - attribute.Int64("user_favorite_count", int64(len(userFav))), + attribute.Int("batch_size", batchSize), + attribute.Int("user_favorite_count", len(userFav)), ) utils.HandleEvent(span, localLogger, "Starting batch user favorite creation") -- 2.45.2 From 7cf51f9b90278ab36af607ccd35af452dd977ed0 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:53:17 +0200 Subject: [PATCH 36/77] fix(tracing): wrong attribute type changed attribute types in to Int --- pkg/database/post.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/database/post.go b/pkg/database/post.go index a69698f..623995c 100644 --- a/pkg/database/post.go +++ b/pkg/database/post.go @@ -52,8 +52,8 @@ func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) e }) span.SetAttributes( - attribute.Int64("batch_size", int64(batchSize)), - attribute.Int64("post_count", int64(len(post))), + attribute.Int("batch_size", batchSize), + attribute.Int("post_count", len(post)), ) utils.HandleEvent(span, localLogger, "Starting batch post creation") -- 2.45.2 From e38171e53db15ea204e1acfd41097968cf007851 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:53:48 +0200 Subject: [PATCH 37/77] fix(tracing): wrong attribute type changed attribute types in to Int --- pkg/database/source.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/database/source.go b/pkg/database/source.go index c5691d0..eab4530 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -52,8 +52,8 @@ func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize }) span.SetAttributes( - attribute.Int64("batch_size", int64(batchSize)), - attribute.Int64("source_count", int64(len(source))), + attribute.Int("batch_size", batchSize), + attribute.Int("source_count", len(source)), ) utils.HandleEvent(span, localLogger, "Starting batch source creation") -- 2.45.2 From 6583ae7d29f3846a1fef8dbafe8940714e9fc9be Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:54:50 +0200 Subject: [PATCH 38/77] fix(tracing): wrong attribute type changed attribute types in to Int --- pkg/database/tag.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/database/tag.go b/pkg/database/tag.go index cd40dd0..342285a 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -59,8 +59,8 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) err }) span.SetAttributes( - attribute.Int64("batch_size", int64(batchSize)), - attribute.Int64("tag_count", int64(len(tags))), + attribute.Int("batch_size", batchSize), + attribute.Int("tag_count", len(tags)), ) utils.HandleEvent(span, localLogger, "Starting batch tag creation") -- 2.45.2 From 0a8f84bb159873525135f537d0d5e87ac6c8dcfa Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:54:53 +0200 Subject: [PATCH 39/77] fix(tracing): wrong attribute type changed attribute types in to Int --- pkg/database/tagGroup.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/database/tagGroup.go b/pkg/database/tagGroup.go index a09a4e8..39940b6 100644 --- a/pkg/database/tagGroup.go +++ b/pkg/database/tagGroup.go @@ -60,8 +60,8 @@ func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, ba }) span.SetAttributes( - attribute.Int64("batch_size", int64(batchSize)), - attribute.Int64("tag_group_count", int64(len(tagsGroups))), + attribute.Int("batch_size", batchSize), + attribute.Int("tag_group_count", len(tagsGroups)), ) utils.HandleEvent(span, localLogger, "Starting batch tag group creation") -- 2.45.2 From 5ffb558ab865edbfa409460d62501f60183a1212 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 11:54:57 +0200 Subject: [PATCH 40/77] fix(tracing): wrong attribute type changed attribute types in to Int --- pkg/database/tagAlias.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/database/tagAlias.go b/pkg/database/tagAlias.go index 7918787..e422672 100644 --- a/pkg/database/tagAlias.go +++ b/pkg/database/tagAlias.go @@ -59,8 +59,8 @@ func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, b }) span.SetAttributes( - attribute.Int64("batch_size", int64(batchSize)), - attribute.Int64("tag_aliases_count", int64(len(tagsAliases))), + attribute.Int("batch_size", batchSize), + attribute.Int("tag_aliases_count", len(tagsAliases)), ) utils.HandleEvent(span, localLogger, "Starting batch tag alias creation") -- 2.45.2 From bd76f838ec568ea6f1443e5db01cd572952b5956 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 15:53:45 +0200 Subject: [PATCH 41/77] refactor(tracing): fixed minor issues & added debug flag --- pkg/database/client.go | 16 ++++++++++------ pkg/database/favorite.go | 10 +++++----- pkg/database/post.go | 10 +++++----- pkg/database/source.go | 12 ++++++------ pkg/database/tag.go | 6 +++--- pkg/database/tagAlias.go | 6 +++--- pkg/database/tagGroup.go | 6 +++--- pkg/database/user.go | 6 +++--- pkg/database/userSource.go | 8 ++++---- 9 files changed, 42 insertions(+), 38 deletions(-) diff --git a/pkg/database/client.go b/pkg/database/client.go index 901ae9f..2f4c362 100644 --- a/pkg/database/client.go +++ b/pkg/database/client.go @@ -29,10 +29,16 @@ var ( ) func Connect(ctx context.Context, config models.DatabaseConfig) error { + setupTelemetry() + + if config.Debug { + log.SetLevel(log.DebugLevel) + } + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "Connect") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "endpoint": config.Endpoint, "port": config.Port, "database": config.Database, @@ -64,8 +70,6 @@ func Connect(ctx context.Context, config models.DatabaseConfig) error { return utils.HandleError(ctx, span, localLogger, err) } - setupTelemetry() - client = sqlDB utils.HandleEvent(span, localLogger, "Database connected successfully") return nil @@ -75,7 +79,7 @@ func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.Databas ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "migrateDatabase") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "database": config.Database, }) @@ -100,9 +104,9 @@ func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.Databas if config.Debug { if n != 0 { - localLogger.Infof("postgres migration: applied %d migrations!", n) + localLogger.Debug("applied %d migrations!", n) } else { - localLogger.Info("postgres migration: nothing to migrate") + localLogger.Debug("nothing to migrate") } } diff --git a/pkg/database/favorite.go b/pkg/database/favorite.go index c730176..335cf65 100644 --- a/pkg/database/favorite.go +++ b/pkg/database/favorite.go @@ -16,7 +16,7 @@ func CreateUserFavorite(ctx context.Context, userFav models.UserFavorite) (model ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavorite") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_favorite_id": userFav.ID, }) @@ -46,7 +46,7 @@ func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorit ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavoriteInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_favorite_count": len(userFav), "batch_size": batchSize, }) @@ -86,7 +86,7 @@ func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserFavorite") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_favorite_id": userFav.ID, }) @@ -124,7 +124,7 @@ func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserFavoritesByID") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_favorite_id": id, }) @@ -160,7 +160,7 @@ func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserFavorite") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_favorite_id": id, }) diff --git a/pkg/database/post.go b/pkg/database/post.go index 623995c..a92d231 100644 --- a/pkg/database/post.go +++ b/pkg/database/post.go @@ -16,7 +16,7 @@ func CreatePost(ctx context.Context, post models.Post) (models.Post, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePost") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "post_id": post.ID, }) @@ -46,7 +46,7 @@ func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) e ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePostInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "post_count": len(post), "batch_size": batchSize, }) @@ -86,7 +86,7 @@ func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetPostByID") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "post_id": id, }) @@ -122,7 +122,7 @@ func UpdatePost(ctx context.Context, anthrovePost models.Post) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePost") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "post_id": anthrovePost.ID, }) @@ -162,7 +162,7 @@ func DeletePost(ctx context.Context, id models.PostID) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePost") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "post_id": id, }) diff --git a/pkg/database/source.go b/pkg/database/source.go index eab4530..06fc8a7 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -16,7 +16,7 @@ func CreateSource(ctx context.Context, source models.Source) (models.Source, err ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_id": source.ID, }) @@ -46,7 +46,7 @@ func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSourceInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_count": len(source), "batch_size": batchSize, }) @@ -86,7 +86,7 @@ func UpdateSource(ctx context.Context, source models.Source) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_id": source.ID, }) @@ -126,7 +126,7 @@ func GetSourceByID(ctx context.Context, id models.SourceID) (models.Source, erro ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetSourceByID") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_id": id, }) @@ -166,7 +166,7 @@ func GetSourceByDomain(ctx context.Context, sourceDomain models.SourceDomain) (m ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetSourceByDomain") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_domain": sourceDomain, }) @@ -202,7 +202,7 @@ func DeleteSource(ctx context.Context, id models.SourceID) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_id": id, }) diff --git a/pkg/database/tag.go b/pkg/database/tag.go index 342285a..cdb7a14 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -16,7 +16,7 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTag") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_name": tagName, "tag_type": tagType, }) @@ -53,7 +53,7 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) err ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_count": len(tags), "batch_size": batchSize, }) @@ -93,7 +93,7 @@ func DeleteTag(ctx context.Context, tagName models.TagName) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTag") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_name": tagName, }) diff --git a/pkg/database/tagAlias.go b/pkg/database/tagAlias.go index e422672..f7cfc74 100644 --- a/pkg/database/tagAlias.go +++ b/pkg/database/tagAlias.go @@ -16,7 +16,7 @@ func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagNa ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAlias") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_alias_name": tagAliasName, "tag_name": tagName, }) @@ -53,7 +53,7 @@ func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, b ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_aliases_count": len(tagsAliases), "batch_size": batchSize, }) @@ -93,7 +93,7 @@ func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagAlias") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_alias_name": tagAliasName, }) diff --git a/pkg/database/tagGroup.go b/pkg/database/tagGroup.go index 39940b6..f90a7a6 100644 --- a/pkg/database/tagGroup.go +++ b/pkg/database/tagGroup.go @@ -17,7 +17,7 @@ func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagNa ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagGroup") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_group_name": tagGroupName, "tag_name": tagName, }) @@ -54,7 +54,7 @@ func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, ba ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_groups_count": len(tagsGroups), "batch_size": batchSize, }) @@ -100,7 +100,7 @@ func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error attribute.String("tag_group_name", string(tagGroupName)), ) - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_group_name": tagGroupName, }) diff --git a/pkg/database/user.go b/pkg/database/user.go index c7346e6..840c54e 100644 --- a/pkg/database/user.go +++ b/pkg/database/user.go @@ -17,7 +17,7 @@ func CreateUser(ctx context.Context, user models.User) (models.User, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUser") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_id": user.ID, }) @@ -51,7 +51,7 @@ func GetUserByID(ctx context.Context, id models.UserID) (models.User, error) { attribute.String("user_id", string(id)), ) - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_id": id, }) @@ -83,7 +83,7 @@ func DeleteUser(ctx context.Context, id models.UserID) error { attribute.String("user_id", string(id)), ) - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_id": id, }) diff --git a/pkg/database/userSource.go b/pkg/database/userSource.go index 6576128..dc18af0 100644 --- a/pkg/database/userSource.go +++ b/pkg/database/userSource.go @@ -16,7 +16,7 @@ func CreateUserSource(ctx context.Context, userSource models.UserSource) (models ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_source_id": userSource.ID, }) @@ -46,7 +46,7 @@ func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_source_id": userSource.ID, }) @@ -91,7 +91,7 @@ func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.User ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserSourceByID") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_source_id": id, }) @@ -130,7 +130,7 @@ func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_source_id": id, }) -- 2.45.2 From a4792f5d677ab363fa1ca9f90960aa7a42aa6d8e Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 15:54:16 +0200 Subject: [PATCH 42/77] chore(dependencies): Update dependencies to latest versions --- go.mod | 8 +++++--- go.sum | 16 ++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index e8481bd..4adf5d4 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.anthrove.art/Anthrove/otter-space-sdk/v2 go 1.22.0 require ( - github.com/lib/pq v1.10.9 + github.com/davecgh/go-spew v1.1.1 github.com/matoous/go-nanoid/v2 v2.1.0 github.com/rubenv/sql-migrate v1.7.0 github.com/sirupsen/logrus v1.9.3 @@ -57,6 +57,7 @@ require ( github.com/opencontainers/image-spec v1.1.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect + github.com/rogpeppe/go-internal v1.10.0 // indirect github.com/shirou/gopsutil/v3 v3.23.12 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect @@ -65,10 +66,11 @@ require ( go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect go.opentelemetry.io/otel/log v0.4.0 // indirect go.opentelemetry.io/otel/metric v1.28.0 // indirect + go.opentelemetry.io/otel/sdk v1.28.0 // indirect golang.org/x/crypto v0.22.0 // indirect - golang.org/x/sync v0.7.0 // indirect + golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.21.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/text v0.17.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect google.golang.org/grpc v1.59.0 // indirect google.golang.org/protobuf v1.33.0 // indirect diff --git a/go.sum b/go.sum index 0556fae..73406fa 100644 --- a/go.sum +++ b/go.sum @@ -110,8 +110,8 @@ github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY= github.com/poy/onpar v1.1.2/go.mod h1:6X8FLNoxyr9kkmnlqpK6LSoiOtrO6MICtWwEuWkLjzg= -github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= -github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/rubenv/sql-migrate v1.7.0 h1:HtQq1xyTN2ISmQDggnh0c9U3JlP8apWh8YO2jzlXpTI= github.com/rubenv/sql-migrate v1.7.0/go.mod h1:S4wtDEG1CKn+0ShpTtzWhFpHHI5PvCUtiGI+C+Z2THE= github.com/shirou/gopsutil/v3 v3.23.12 h1:z90NtUkp3bMtmICZKpC4+WaknU1eXtp5vtbQ11DgpE4= @@ -158,8 +158,8 @@ go.opentelemetry.io/otel/log v0.4.0 h1:/vZ+3Utqh18e8TPjuc3ecg284078KWrR8BRz+PQAj go.opentelemetry.io/otel/log v0.4.0/go.mod h1:DhGnQvky7pHy82MIRV43iXh3FlKN8UUKftn0KbLOq6I= go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q= go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s= -go.opentelemetry.io/otel/sdk v1.19.0 h1:6USY6zH+L8uMH8L3t1enZPR3WFEmSTADlqldyHtJi3o= -go.opentelemetry.io/otel/sdk v1.19.0/go.mod h1:NedEbbS4w3C6zElbLdPJKOpJQOrGUJ+GfzpjUvI0v1A= +go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE= +go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg= go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g= go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI= go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I= @@ -180,8 +180,8 @@ golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -198,8 +198,8 @@ golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q= golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 h1:vVKdlvoWBphwdxWKrFZEuM0kGgGLxUOYcY4U/2Vjg44= golang.org/x/time v0.0.0-20220210224613-90d013bbcef8/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -- 2.45.2 From 8668d504b9f84a5f5ebfd8c4b6e44e4b10ad613a Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 15:54:36 +0200 Subject: [PATCH 43/77] feat(tests): Add tag generator utility functions for tests --- test/generator.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 test/generator.go diff --git a/test/generator.go b/test/generator.go new file mode 100644 index 0000000..a8e4fcb --- /dev/null +++ b/test/generator.go @@ -0,0 +1,69 @@ +package test + +import ( + "fmt" + "math/rand" + + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "github.com/davecgh/go-spew/spew" + gonanoid "github.com/matoous/go-nanoid/v2" +) + +func GenerateRandomTags(numTags int) []models.Tag { + var tags []models.Tag + tagTypes := []models.TagType{"general", "species", "character", "artist", "lore", "meta", "invalid", "copyright"} + + for i := 0; i < numTags; i++ { + id, _ := gonanoid.New(10) + tagName := spew.Sprintf("tag_name_%s", id) + + tagType := tagTypes[rand.Intn(len(tagTypes))] + + tag := models.Tag{ + Name: models.TagName(tagName), + Type: tagType, + } + + tags = append(tags, tag) + } + + return tags +} + +func GenerateRandomTagGroups(tags []models.Tag, numGroups int) []models.TagGroup { + var tagGroups []models.TagGroup + + for i := 0; i < numGroups; i++ { + id, _ := gonanoid.New(10) + groupName := fmt.Sprintf("tag_group_%s", id) + randomTag := tags[rand.Intn(len(tags))] + + tagGroup := models.TagGroup{ + Name: models.TagGroupName(groupName), + TagID: randomTag.Name, + } + + tagGroups = append(tagGroups, tagGroup) + } + + return tagGroups +} + +func GenerateRandomTagAlias(tags []models.Tag, numGroups int) []models.TagAlias { + var tagAliases []models.TagAlias + + for i := 0; i < numGroups; i++ { + id, _ := gonanoid.New(10) + groupName := fmt.Sprintf("tag_alias_%s", id) + randomTag := tags[rand.Intn(len(tags))] + + tagAlias := models.TagAlias{ + Name: models.TagAliasName(groupName), + TagID: randomTag.Name, + } + + tagAliases = append(tagAliases, tagAlias) + } + + return tagAliases +} -- 2.45.2 From 78e17cf1006063e53ef47ebc1c64bcaaff7d3466 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 10:29:47 +0200 Subject: [PATCH 44/77] feat(scopes): Add pagination support with Paginate and AdvancedPagination functions - Introduce Paginate function for basic pagination - Add AdvancedPagination for pagination, sorting, and counting - Define Pagination struct to hold pagination data - Set MaxPageSizeLimit and DefaultPageSize in models/constants --- pkg/database/scopes.go | 84 ++++++++++++++++++++++++++++++++++++++++++ pkg/models/const.go | 5 +++ 2 files changed, 89 insertions(+) create mode 100644 pkg/database/scopes.go diff --git a/pkg/database/scopes.go b/pkg/database/scopes.go new file mode 100644 index 0000000..0161872 --- /dev/null +++ b/pkg/database/scopes.go @@ -0,0 +1,84 @@ +package database + +import ( + "math" + + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "gorm.io/gorm" +) + +type Pagination struct { + Limit int `json:"limit,omitempty;query:limit"` + Page int `json:"page,omitempty;query:page"` + Sort string `json:"sort,omitempty;query:sort"` + TotalRows int64 `json:"total_rows"` + TotalPages int `json:"total_pages"` +} + +// Paginate applies pagination to a GORM query. +// It takes two parameters: +// +// - page: the current page number (1-based index). If the page is less than or equal to 0, it defaults to 1. +// +// - pageSize: the number of records per page. If the pageSize is greater than the MaxPageSizeLimit defined in models, it defaults to MaxPageSizeLimit. If the pageSize is less than or equal to 0, it defaults to DefaultPageSize defined in models. +// +// The function calculates the offset based on the page and pageSize, and applies the offset and limit to the GORM DB instance. +func Paginate(page int, pageSize int) func(db *gorm.DB) *gorm.DB { + return func(db *gorm.DB) *gorm.DB { + + if page <= 0 { + page = 1 + } + + switch { + case pageSize > models.MaxPageSizeLimit: + pageSize = models.MaxPageSizeLimit + case pageSize <= 0: + pageSize = models.DefaultPageSize + } + + offset := (page - 1) * pageSize + return db.Offset(offset).Limit(pageSize) + } +} + +// AdvancedPagination applies pagination, sorting, and counting to a GORM query. +// Parameters: +// +// - value: The model or table to query. +// +// - pagination: A pointer to a Pagination struct containing page, limit, sort, total rows, and total pages information. +// +// - db: A pointer to the GORM database instance. +// +// The function calculates the offset based on the Page and Limit of the Pagination struct, and applies the offset and limit to the GORM DB instance. +func AdvancedPagination(value any, pagination *Pagination, db *gorm.DB) func(db *gorm.DB) *gorm.DB { + var totalRows int64 + + if pagination.Page <= 0 { + pagination.Page = 1 + } + + if pagination.Sort == "" { + pagination.Sort = "Id desc" + } + + switch { + case pagination.Limit > models.MaxPageSizeLimit: + pagination.Limit = models.MaxPageSizeLimit + case pagination.Limit <= 0: + pagination.Limit = models.DefaultPageSize + } + + db.Model(value).Count(&totalRows) + + pagination.TotalRows = totalRows + totalPages := int(math.Ceil(float64(totalRows) / float64(pagination.Limit))) + pagination.TotalPages = totalPages + + offset := (pagination.Page - 1) * pagination.Limit + + return func(db *gorm.DB) *gorm.DB { + return db.Offset(offset).Limit(pagination.Limit).Order(pagination.Sort) + } +} diff --git a/pkg/models/const.go b/pkg/models/const.go index a6f25bc..092a803 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -24,6 +24,11 @@ type ( UserFavoriteID string ) +const ( + MaxPageSizeLimit = 100 + DefaultPageSize = 50 +) + const ( SFW Rating = "safe" NSFW Rating = "explicit" -- 2.45.2 From b4e8152cb45d0dd4e1ad3395bf670b8ad8c39685 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 10:35:01 +0200 Subject: [PATCH 45/77] chore: removed dead code --- pkg/models/api.go | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 pkg/models/api.go diff --git a/pkg/models/api.go b/pkg/models/api.go deleted file mode 100644 index 37adbd0..0000000 --- a/pkg/models/api.go +++ /dev/null @@ -1,5 +0,0 @@ -package models - -type FavoriteList struct { - Posts []Post `json:"posts,omitempty"` -} -- 2.45.2 From cfc7e5362d2d0a3ef4062c5384a0ed6496e983f3 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 10:54:21 +0200 Subject: [PATCH 46/77] chore: Add comments and improve JSON annotations for better data structuring and clarity across the codebase --- internal/utils/error.go | 13 +++++++- internal/utils/slices.go | 11 ------- internal/utils/slices_test.go | 60 ----------------------------------- internal/utils/tracing.go | 2 ++ pkg/database/client.go | 18 ++++++----- pkg/error/validation.go | 5 --- pkg/models/orm.go | 4 +-- pkg/models/post.go | 2 +- pkg/models/tag.go | 6 ++-- pkg/models/userSource.go | 2 +- 10 files changed, 31 insertions(+), 92 deletions(-) delete mode 100644 internal/utils/slices.go delete mode 100644 internal/utils/slices_test.go diff --git a/internal/utils/error.go b/internal/utils/error.go index f73c2cf..aa80866 100644 --- a/internal/utils/error.go +++ b/internal/utils/error.go @@ -8,7 +8,18 @@ import ( "go.opentelemetry.io/otel/trace" ) -func HandleError(ctx context.Context, span trace.Span, logger *log.Entry, error error) error { +// HandleError logs the provided error, records it in the given trace span, +// sets the span status to error, and returns the error. +// +// Parameters: +// - ctx: context.Context, the context in which the error occurred. +// - span: trace.Span, the trace span where the error will be recorded. +// - logger: *log.Entry, a log entry used to log the error message. +// - error: error, the error to be handled. +// +// Returns: +// - error: The same error that was passed in. +func HandleError(_ context.Context, span trace.Span, logger *log.Entry, error error) error { logger.Error(error) span.RecordError(error) span.SetStatus(codes.Error, error.Error()) diff --git a/internal/utils/slices.go b/internal/utils/slices.go deleted file mode 100644 index dfb5aed..0000000 --- a/internal/utils/slices.go +++ /dev/null @@ -1,11 +0,0 @@ -package utils - -func GetOrDefault(data map[string]any, key string, defaultVal any) any { - val, ok := data[key] - - if !ok { - return defaultVal - } - - return val -} diff --git a/internal/utils/slices_test.go b/internal/utils/slices_test.go deleted file mode 100644 index 9f8cdc6..0000000 --- a/internal/utils/slices_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package utils - -import ( - "reflect" - "testing" -) - -func TestGetOrDefault(t *testing.T) { - type args struct { - data map[string]any - key string - defaultVal any - } - tests := []struct { - name string - args args - want any - }{ - { - name: "Test 1: Nil map", - args: args{ - data: nil, - key: "key1", - defaultVal: "default", - }, - want: "default", - }, - { - name: "Test 2: Existing key", - args: args{ - data: map[string]interface{}{ - "key1": "value1", - "key2": "value2", - }, - key: "key1", - defaultVal: "default", - }, - want: "value1", - }, - { - name: "Test 3: Non-existing key", - args: args{ - data: map[string]interface{}{ - "key1": "value1", - "key2": "value2", - }, - key: "key3", - defaultVal: "default", - }, - want: "default", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := GetOrDefault(tt.args.data, tt.args.key, tt.args.defaultVal); !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetOrDefault() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/internal/utils/tracing.go b/internal/utils/tracing.go index 6e83a9f..e71be80 100644 --- a/internal/utils/tracing.go +++ b/internal/utils/tracing.go @@ -7,6 +7,7 @@ import ( "go.opentelemetry.io/otel/trace" ) +// SetupTracing initializes a new trace span and logger for the given context and tracer. func SetupTracing(ctx context.Context, tracer trace.Tracer, tracerName string) (context.Context, trace.Span, *log.Entry) { ctx, span := tracer.Start(ctx, tracerName) localLogger := log.WithContext(ctx) @@ -14,6 +15,7 @@ func SetupTracing(ctx context.Context, tracer trace.Tracer, tracerName string) ( return ctx, span, localLogger } +// HandleEvent logs the provided event name and adds it to the given trace span. func HandleEvent(span trace.Span, logger *log.Entry, eventName string) { logger.Debug(eventName) span.AddEvent(eventName) diff --git a/pkg/database/client.go b/pkg/database/client.go index 2f4c362..c4efd76 100644 --- a/pkg/database/client.go +++ b/pkg/database/client.go @@ -28,9 +28,16 @@ var ( logger = log.New() ) +// Connect to the Database func Connect(ctx context.Context, config models.DatabaseConfig) error { - setupTelemetry() + // Setup open telemetry + tracer = otel.Tracer(tracingName) + + hook := otellogrus.NewHook(tracingName) + logger.AddHook(hook) + + // Debug enabled? if config.Debug { log.SetLevel(log.DebugLevel) } @@ -75,6 +82,7 @@ func Connect(ctx context.Context, config models.DatabaseConfig) error { return nil } +// migrateDatabase handels the migration of ann SQL files in the migrations subfolder func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.DatabaseConfig) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "migrateDatabase") defer span.End() @@ -114,13 +122,7 @@ func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.Databas return nil } -func setupTelemetry() { - tracer = otel.Tracer(tracingName) - - hook := otellogrus.NewHook(tracingName) - logger.AddHook(hook) -} - +// GetGorm returns a ready to use gorm.DB client func GetGorm(ctx context.Context) (*gorm.DB, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetGorm") defer span.End() diff --git a/pkg/error/validation.go b/pkg/error/validation.go index 0e27823..143635e 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -3,9 +3,6 @@ package error import "fmt" const ( - UserIDIsEmpty = "postID cannot be empty" - UserIDToShort = "postID needs to be 25 characters long" - SourceListIsEmpty = "sourceList cannot be empty" SourceIDIsEmpty = "SourceID cannot be empty" SourceIDToShort = "sourceID needs to be 25 characters long" @@ -15,10 +12,8 @@ const ( UserSourceIDToShort = "userSourceID needs to be 25 characters long" TagListIsEmpty = "tagList cannot be empty" - TagNameIsEmpty = "tagName cannot be empty" TagAliasListIsEmpty = "tagAliasList cannot be empty" - TagAliasNameIsEmpty = "tagAliasName cannot be empty" TagGroupListIsEmpty = "tagGroupList cannot be empty" TagGroupNameIsEmpty = "tagGroupName cannot be empty" diff --git a/pkg/models/orm.go b/pkg/models/orm.go index a4ec52f..2cf6cf5 100644 --- a/pkg/models/orm.go +++ b/pkg/models/orm.go @@ -13,8 +13,8 @@ type ID interface { type BaseModel[T ID] struct { ID T `json:"id" gorm:"primaryKey"` - CreatedAt time.Time `json:"-"` - UpdatedAt time.Time `json:"-"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` } diff --git a/pkg/models/post.go b/pkg/models/post.go index 0d0dda3..479e4cf 100644 --- a/pkg/models/post.go +++ b/pkg/models/post.go @@ -4,7 +4,7 @@ package models type Post struct { BaseModel[PostID] Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"` - Tags []Tag `json:"-" gorm:"many2many:post_tags;"` + Tags []Tag `json:"tags,omitempty" gorm:"many2many:post_tags;"` Favorites []UserFavorite `json:"-" gorm:"foreignKey:PostID"` References []PostReference `json:"references" gorm:"foreignKey:PostID"` } diff --git a/pkg/models/tag.go b/pkg/models/tag.go index 53b5c1a..4e87361 100644 --- a/pkg/models/tag.go +++ b/pkg/models/tag.go @@ -4,9 +4,9 @@ package models type Tag struct { Name TagName `json:"name" gorm:"primaryKey"` Type TagType `json:"type" gorm:"column:tag_type"` - Aliases []TagAlias `json:"aliases" gorm:"foreignKey:TagID"` - Groups []TagGroup `json:"groups" gorm:"foreignKey:TagID"` - Posts []Post `json:"posts" gorm:"many2many:post_tags;"` + Aliases []TagAlias `json:"aliases,omitempty" gorm:"foreignKey:TagID"` + Groups []TagGroup `json:"groups,omitempty" gorm:"foreignKey:TagID"` + Posts []Post `json:"posts,omitempty" gorm:"many2many:post_tags;"` } func (Tag) TableName() string { diff --git a/pkg/models/userSource.go b/pkg/models/userSource.go index 187c2a9..171c95e 100644 --- a/pkg/models/userSource.go +++ b/pkg/models/userSource.go @@ -13,7 +13,7 @@ type UserSource struct { AccountID string `json:"account_id"` LastScrapeTime time.Time `json:"last_scrape_time"` AccountValidate bool `json:"account_validate"` - AccountValidationKey string `json:"-"` + AccountValidationKey string `json:"account_validation_key"` } func (UserSource) TableName() string { -- 2.45.2 From 75bbbb4408d9fc741ba3c7f8d8dcb0a3ee12f981 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 11:07:14 +0200 Subject: [PATCH 47/77] chore(tests): refactored & fixed old test cases --- pkg/error/database_test.go | 79 +++++--------------------- pkg/models/postReference_test.go | 41 ++------------ pkg/models/post_test.go | 35 ++---------- pkg/models/source_test.go | 37 ++----------- pkg/models/tag_test.go | 95 +++++--------------------------- pkg/models/userFavorite_test.go | 36 ++---------- pkg/models/userSource_test.go | 39 ++----------- pkg/models/user_test.go | 31 ++--------- 8 files changed, 51 insertions(+), 342 deletions(-) diff --git a/pkg/error/database_test.go b/pkg/error/database_test.go index 5fd2e1f..96143cd 100644 --- a/pkg/error/database_test.go +++ b/pkg/error/database_test.go @@ -2,79 +2,26 @@ package error import "testing" -func TestEntityAlreadyExists_Error(t *testing.T) { +func TestDatabase_Error(t *testing.T) { + type fields struct { + Reason string + } tests := []struct { - name string - want string + name string + fields fields + want string }{ { - name: "Test : Valid error String", - want: "EntityAlreadyExists error", + name: "Test 1: Reason", + fields: fields{Reason: "TEST ERROR"}, + want: "Database error: TEST ERROR", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - e := &EntityAlreadyExists{} - if got := e.Error(); got != tt.want { - t.Errorf("Error() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestNoDataFound_Error(t *testing.T) { - tests := []struct { - name string - want string - }{ - { - name: "Test : Valid error String", - want: "NoDataFound error", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - e := &NoDataFound{} - if got := e.Error(); got != tt.want { - t.Errorf("Error() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestNoDataWritten_Error(t *testing.T) { - tests := []struct { - name string - want string - }{ - { - name: "Test : Valid error String", - want: "NoDataWritten error", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - e := &NoDataWritten{} - if got := e.Error(); got != tt.want { - t.Errorf("Error() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestNoRelationCreated_Error(t *testing.T) { - tests := []struct { - name string - want string - }{ - { - name: "Test : Valid error String", - want: "relationship creation error", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - e := &NoRelationCreated{} + e := Database{ + Reason: tt.fields.Reason, + } if got := e.Error(); got != tt.want { t.Errorf("Error() = %v, want %v", got, tt.want) } diff --git a/pkg/models/postReference_test.go b/pkg/models/postReference_test.go index d203024..5353383 100644 --- a/pkg/models/postReference_test.go +++ b/pkg/models/postReference_test.go @@ -3,42 +3,9 @@ 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, - PostReferenceConfig: PostReferenceConfig{ - 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) - } - }) + postReference := PostReference{} + expectedTableName := "PostReference" + if tableName := postReference.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/pkg/models/post_test.go b/pkg/models/post_test.go index b4c856e..aadf104 100644 --- a/pkg/models/post_test.go +++ b/pkg/models/post_test.go @@ -3,36 +3,9 @@ package models import "testing" func TestPost_TableName(t *testing.T) { - type fields struct { - BaseModel BaseModel[PostID] - Rating Rating - Tags []Tag - Favorites []UserFavorite - 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) - } - }) + post := Post{} + expectedTableName := "Post" + if tableName := post.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/pkg/models/source_test.go b/pkg/models/source_test.go index 876db3a..50b04c8 100644 --- a/pkg/models/source_test.go +++ b/pkg/models/source_test.go @@ -3,38 +3,9 @@ package models import "testing" func TestSource_TableName(t *testing.T) { - type fields struct { - BaseModel BaseModel[SourceID] - 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) - } - }) + source := Source{} + expectedTableName := "Source" + if tableName := source.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/pkg/models/tag_test.go b/pkg/models/tag_test.go index 6f66b18..eb2d729 100644 --- a/pkg/models/tag_test.go +++ b/pkg/models/tag_test.go @@ -3,94 +3,25 @@ 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) - } - }) + tagAlias := TagAlias{} + expectedTableName := "TagAlias" + if tableName := tagAlias.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } 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) - } - }) + tagGroup := TagGroup{} + expectedTableName := "TagGroup" + if tableName := tagGroup.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } -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) - } - }) +func TestTags_TableName(t *testing.T) { + tag := Tag{} + expectedTableName := "Tag" + if tableName := tag.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/pkg/models/userFavorite_test.go b/pkg/models/userFavorite_test.go index 5a09532..84fb0b2 100644 --- a/pkg/models/userFavorite_test.go +++ b/pkg/models/userFavorite_test.go @@ -1,37 +1,11 @@ package models -import ( - "testing" - "time" -) +import "testing" 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 UserFavorite", - fields: fields{}, - want: "UserFavorite", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - us := UserFavorite{ - 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) - } - }) + userFavorite := UserFavorite{} + expectedTableName := "UserFavorites" + if tableName := userFavorite.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/pkg/models/userSource_test.go b/pkg/models/userSource_test.go index 30b8efa..c68fc89 100644 --- a/pkg/models/userSource_test.go +++ b/pkg/models/userSource_test.go @@ -3,40 +3,9 @@ package models import "testing" func TestUserSource_TableName(t *testing.T) { - type fields struct { - User User - UserID UserID - Source Source - SourceID SourceID - 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) - } - }) + userSource := UserSource{} + expectedTableName := "UserSource" + if tableName := userSource.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/pkg/models/user_test.go b/pkg/models/user_test.go index 09d3f2f..505ea43 100644 --- a/pkg/models/user_test.go +++ b/pkg/models/user_test.go @@ -3,32 +3,9 @@ package models import "testing" func TestUser_TableName(t *testing.T) { - type fields struct { - BaseModel BaseModel[UserID] - Favorites []UserFavorite - 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) - } - }) + user := User{} + expectedTableName := "User" + if tableName := user.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } -- 2.45.2 From ad076f585b4476d5c8a1691fda805e1d4e852541 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 12:52:58 +0200 Subject: [PATCH 48/77] fix: Use Debugf for formatted debug message in migrateDatabase --- pkg/database/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/database/client.go b/pkg/database/client.go index c4efd76..0ada34f 100644 --- a/pkg/database/client.go +++ b/pkg/database/client.go @@ -112,7 +112,7 @@ func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.Databas if config.Debug { if n != 0 { - localLogger.Debug("applied %d migrations!", n) + localLogger.Debugf("applied %d migrations!", n) } else { localLogger.Debug("nothing to migrate") } -- 2.45.2 From 1345fa9c9ebf1010d20e89454b9529b60581b277 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 12:54:02 +0200 Subject: [PATCH 49/77] refactor: Simplify migration logging by removing redundant config.Debug check --- pkg/database/client.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/database/client.go b/pkg/database/client.go index 0ada34f..e1df507 100644 --- a/pkg/database/client.go +++ b/pkg/database/client.go @@ -110,12 +110,10 @@ func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.Databas return utils.HandleError(ctx, span, localLogger, err) } - if config.Debug { - if n != 0 { - localLogger.Debugf("applied %d migrations!", n) - } else { - localLogger.Debug("nothing to migrate") - } + if n != 0 { + localLogger.Debugf("applied %d migrations!", n) + } else { + localLogger.Debug("nothing to migrate") } utils.HandleEvent(span, localLogger, "Database migration completed successfully") -- 2.45.2 From 51b305a7252537c9a9f1eb08f1231694eb55b7ce Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 12:54:24 +0200 Subject: [PATCH 50/77] chore(dependencies): Add github.com/lib/pq v1.10.9 to project dependencies --- go.mod | 1 + 1 file changed, 1 insertion(+) diff --git a/go.mod b/go.mod index 4adf5d4..f914338 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.22.0 require ( github.com/davecgh/go-spew v1.1.1 + github.com/lib/pq v1.10.9 github.com/matoous/go-nanoid/v2 v2.1.0 github.com/rubenv/sql-migrate v1.7.0 github.com/sirupsen/logrus v1.9.3 -- 2.45.2 From 89ab1dca02d7ef4a5a981c617ef6304b95dbc4cb Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 12:55:06 +0200 Subject: [PATCH 51/77] fix(testing): Add lib/pq import and update postgres container setup new version of testing library changed functions --- test/helper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/helper.go b/test/helper.go index 8cd3a9e..493f7fa 100644 --- a/test/helper.go +++ b/test/helper.go @@ -15,6 +15,7 @@ import ( "gorm.io/gorm" "gorm.io/gorm/logger" + _ "github.com/lib/pq" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" ) @@ -28,8 +29,7 @@ const ( func StartPostgresContainer(ctx context.Context) (*postgrescontainer.PostgresContainer, *gorm.DB, error) { - pgContainer, err := postgrescontainer.RunContainer(ctx, - testcontainers.WithImage("postgres:alpine"), + pgContainer, err := postgrescontainer.Run(ctx, "postgres:alpine", postgrescontainer.WithDatabase(databaseName), postgrescontainer.WithUsername(databaseUser), postgrescontainer.WithPassword(databasePassword), -- 2.45.2 From 176fdfb8b10187880022619cff3109056e4a90cd Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 12:55:20 +0200 Subject: [PATCH 52/77] feat(test): Add unit tests for CreateUser, GetUserByID, and DeleteUser functions with setup and teardown --- pkg/database/user_test.go | 209 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 pkg/database/user_test.go diff --git a/pkg/database/user_test.go b/pkg/database/user_test.go new file mode 100644 index 0000000..7128522 --- /dev/null +++ b/pkg/database/user_test.go @@ -0,0 +1,209 @@ +package database + +import ( + "context" + "fmt" + "testing" + + "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" +) + +var ( + validUser = models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}} + invalidUser = models.User{BaseModel: models.BaseModel[models.UserID]{ID: "invalid"}} +) + +func TestCreateUser(t *testing.T) { + + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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) + + type args struct { + ctx context.Context + user models.User + } + tests := []struct { + name string + args args + want models.User + wantErr bool + }{ + { + name: "Test 01: Create Valid User", + args: args{ + ctx: ctx, + user: validUser, + }, + want: validUser, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := CreateUser(tt.args.ctx, tt.args.user) + if (err != nil) != tt.wantErr { + t.Errorf("CreateUser() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkUserID(got, tt.want) { + t.Errorf("CreateUser() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGetUserByID(t *testing.T) { + + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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) + + validUser, err = CreateUser(ctx, validUser) + if err != nil { + t.Fatal(err) + } + + // Setup Test + + // Test + type args struct { + ctx context.Context + id models.UserID + } + tests := []struct { + name string + args args + want models.User + wantErr bool + }{ + { + name: "Test 01: Get Valid User", + args: args{ + ctx: ctx, + id: validUser.ID, + }, + want: validUser, + wantErr: false, + }, + { + name: "Test 02: Get not existing User", + args: args{ + ctx: ctx, + id: invalidUser.ID, + }, + want: models.User{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetUserByID(tt.args.ctx, tt.args.id) + if (err != nil) != tt.wantErr { + t.Errorf("GetUserByID() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkUserID(got, tt.want) { + t.Errorf("GetUserByID() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestDeleteUser(t *testing.T) { + + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Test + + _, err = CreateUser(ctx, validUser) + if err != nil { + t.Fatal(err) + } + + // Test + type args struct { + ctx context.Context + id models.UserID + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Delete Existing User", + args: args{ + ctx: ctx, + id: validUser.ID, + }, + wantErr: false, + }, + { + name: "Test 02: Delete not existing User", + args: args{ + ctx: ctx, + id: validUser.ID, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeleteUser(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr { + t.Errorf("DeleteUser() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func checkUserID(got models.User, want models.User) bool { + if got.ID != want.ID { + return false + } + + return true +} -- 2.45.2 From 9ad1df51cf75542b19977a8931dfa99f466ca5aa Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:00:16 +0200 Subject: [PATCH 53/77] fix(migration): Change scrape_time_interval from INT to TEXT in initial database migration script --- pkg/database/migrations/001_inital_database.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/database/migrations/001_inital_database.sql b/pkg/database/migrations/001_inital_database.sql index 1d4fe22..60d779d 100644 --- a/pkg/database/migrations/001_inital_database.sql +++ b/pkg/database/migrations/001_inital_database.sql @@ -87,7 +87,7 @@ CREATE TABLE "UserSource" deleted_at TIMESTAMP NULL NULL, user_id TEXT REFERENCES "User" (id), source_id TEXT REFERENCES "Source" (id), - scrape_time_interval INT, + scrape_time_interval TEXT, account_username TEXT, account_id TEXT, last_scrape_time TIMESTAMP, -- 2.45.2 From 71488dade911422f9ed1ee2e87a03b5398e1137c Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:00:36 +0200 Subject: [PATCH 54/77] feat(database): Enable error translation in GORM configuration for better error handling --- pkg/database/client.go | 4 +++- test/helper.go | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/database/client.go b/pkg/database/client.go index e1df507..692e03c 100644 --- a/pkg/database/client.go +++ b/pkg/database/client.go @@ -67,7 +67,9 @@ func Connect(ctx context.Context, config models.DatabaseConfig) error { } dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.Timezone) - sqlDB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + sqlDB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{ + TranslateError: true, + }) if err != nil { return utils.HandleError(ctx, span, localLogger, err) } diff --git a/test/helper.go b/test/helper.go index 493f7fa..4b31cc0 100644 --- a/test/helper.go +++ b/test/helper.go @@ -80,7 +80,8 @@ func migrateDatabase(connectionString string) error { func getGormDB(connectionString string) (*gorm.DB, error) { return gorm.Open(postgres.Open(connectionString), &gorm.Config{ - Logger: logger.Default.LogMode(logger.Info), + Logger: logger.Default.LogMode(logger.Info), + TranslateError: true, }) } -- 2.45.2 From 3fadaac69a813d8984a31fbb9d3a184a4a7253d2 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:01:48 +0200 Subject: [PATCH 55/77] fix: added documentation & fixed logging --- pkg/database/userSource.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/pkg/database/userSource.go b/pkg/database/userSource.go index dc18af0..229c6ca 100644 --- a/pkg/database/userSource.go +++ b/pkg/database/userSource.go @@ -16,14 +16,6 @@ func CreateUserSource(ctx context.Context, userSource models.UserSource) (models ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserSource") defer span.End() - localLogger = localLogger.WithFields(log.Fields{ - "user_source_id": userSource.ID, - }) - - span.SetAttributes( - attribute.String("user_source_id", string(userSource.SourceID)), - ) - utils.HandleEvent(span, localLogger, "Starting user source creation") if client == nil { @@ -38,10 +30,25 @@ func CreateUserSource(ctx context.Context, userSource models.UserSource) (models return models.UserSource{}, utils.HandleError(ctx, span, localLogger, result.Error) } + localLogger = localLogger.WithFields(log.Fields{ + "user_source_id": userSource.ID, + }) + + span.SetAttributes( + attribute.String("user_source_id", string(userSource.SourceID)), + ) + utils.HandleEvent(span, localLogger, "User source created successfully") return userSource, nil } +// UpdateUserSource updates the user source information in the database. +// Only a few parameter can be updated: +// - AccountID +// - ScrapeTimeInterval +// - AccountUsername +// - LastScrapeTime +// - AccountValidate func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserSource") defer span.End() -- 2.45.2 From 146315f9916a628f7105c50ec9fd04d6db41fd1c Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:02:15 +0200 Subject: [PATCH 56/77] feat(testing): first tests added the first set of new tests --- pkg/database/userSource_test.go | 498 ++++++++++++++++++++++++++++++++ pkg/database/user_test.go | 35 ++- 2 files changed, 518 insertions(+), 15 deletions(-) create mode 100644 pkg/database/userSource_test.go diff --git a/pkg/database/userSource_test.go b/pkg/database/userSource_test.go new file mode 100644 index 0000000..ca2fd31 --- /dev/null +++ b/pkg/database/userSource_test.go @@ -0,0 +1,498 @@ +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 TestCreateUserSource(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 User ot test with + validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}} + + validUser, err = CreateUser(ctx, validUser) + if err != nil { + t.Fatalf("CreateUser err: %v", err) + } + // -- + + // -- 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 UserSource model + validUSerSource := models.UserSource{ + BaseModel: models.BaseModel[models.UserSourceID]{ + ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: gorm.DeletedAt{}, + }, + User: models.User{}, + UserID: validUser.ID, + Source: models.Source{}, + SourceID: validSource.ID, + ScrapeTimeInterval: "P1D", + AccountUsername: "marry", + AccountID: "poppens", + LastScrapeTime: time.Now(), + AccountValidate: false, + AccountValidationKey: "im-a-key", + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + userSource models.UserSource + } + tests := []struct { + name string + args args + want models.UserSource + wantErr bool + }{ + { + name: "Test 01: Valid User Source", + args: args{ + ctx: ctx, + userSource: validUSerSource, + }, + want: validUSerSource, + wantErr: false, + }, + { + name: "Test 02: Invalid User Source", + args: args{ + ctx: ctx, + userSource: models.UserSource{}, + }, + want: models.UserSource{}, + wantErr: true, + }, + { + name: "Test 03: Duplicate User Source", + args: args{ + ctx: ctx, + userSource: validUSerSource, + }, + want: models.UserSource{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := CreateUserSource(tt.args.ctx, tt.args.userSource) + if (err != nil) != tt.wantErr { + t.Errorf("CreateUserSource() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("CreateUserSource() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestUpdateUserSource(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 User ot test with + validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}} + + validUser, err = CreateUser(ctx, validUser) + if err != nil { + t.Fatalf("CreateUser err: %v", err) + } + // -- + + // -- 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 UserSource model + validUserSource := models.UserSource{ + BaseModel: models.BaseModel[models.UserSourceID]{ + ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: gorm.DeletedAt{}, + }, + User: models.User{}, + UserID: validUser.ID, + Source: models.Source{}, + SourceID: validSource.ID, + ScrapeTimeInterval: "P1D", + AccountUsername: "marry", + AccountID: "poppens", + LastScrapeTime: time.Now(), + AccountValidate: false, + AccountValidationKey: "im-a-key", + } + validUserSource, err = CreateUserSource(ctx, validUserSource) + if err != nil { + t.Fatalf("CreateUserSource err: %v", err) + } + // -- + + // -- Create Updates models for UserSource + validUpdateSourceUser := validUserSource + validUpdateSourceUser.AccountID = "1234" + validUpdateSourceUser.ScrapeTimeInterval = "P2D" + validUpdateSourceUser.AccountUsername = "Update_Username" + validUpdateSourceUser.LastScrapeTime = time.Now() + validUpdateSourceUser.AccountValidate = true + + invalidUpdateSourceUser := models.UserSource{} + // -- + + // -- -- Tests + type args struct { + ctx context.Context + userSource models.UserSource + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid Update for UserSource", + args: args{ + ctx: ctx, + userSource: validUpdateSourceUser, + }, + wantErr: false, + }, + { + name: "Test 02: Invalid Update for UserSource", + args: args{ + ctx: ctx, + userSource: invalidUpdateSourceUser, + }, + wantErr: true, + }, + { + name: "Test 03: Empty ID for Update for UserSource", + args: args{ + ctx: ctx, + userSource: models.UserSource{BaseModel: models.BaseModel[models.UserSourceID]{ID: ""}}, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := UpdateUserSource(tt.args.ctx, tt.args.userSource); (err != nil) != tt.wantErr { + t.Errorf("UpdateUserSource() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestGetUserSourceByID(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 User ot test with + validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}} + + validUser, err = CreateUser(ctx, validUser) + if err != nil { + t.Fatalf("CreateUser err: %v", err) + } + // -- + + // -- 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 UserSource model + validUserSource := models.UserSource{ + BaseModel: models.BaseModel[models.UserSourceID]{ + ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: gorm.DeletedAt{}, + }, + User: models.User{}, + UserID: validUser.ID, + Source: models.Source{}, + SourceID: validSource.ID, + ScrapeTimeInterval: "P1D", + AccountUsername: "marry", + AccountID: "poppens", + LastScrapeTime: time.Now(), + AccountValidate: false, + AccountValidationKey: "im-a-key", + } + validUserSource, err = CreateUserSource(ctx, validUserSource) + if err != nil { + t.Fatalf("CreateUserSource err: %v", err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + id models.UserSourceID + } + tests := []struct { + name string + args args + want models.UserSource + wantErr bool + }{ + { + name: "Test 01: Valid UserSource ID", + args: args{ + ctx: ctx, + id: validUserSource.ID, + }, + want: validUserSource, + wantErr: false, + }, + { + name: "Test 03: Empty UserSourceID", + args: args{ + ctx: ctx, + id: "", + }, + wantErr: true, + }, + { + name: "Test 04: Short UserSourceID", + args: args{ + ctx: ctx, + id: "111", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetUserSourceByID(tt.args.ctx, tt.args.id) + if (err != nil) != tt.wantErr { + t.Errorf("GetUserSourceByID() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkUserSourceID(got, tt.want) { + t.Errorf("GetUserSourceByID() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestDeleteUserSource(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 User ot test with + validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}} + + validUser, err = CreateUser(ctx, validUser) + if err != nil { + t.Fatalf("CreateUser err: %v", err) + } + // -- + + // -- 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 UserSource model + validUSerSource := models.UserSource{ + BaseModel: models.BaseModel[models.UserSourceID]{ + ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: gorm.DeletedAt{}, + }, + User: models.User{}, + UserID: validUser.ID, + Source: models.Source{}, + SourceID: validSource.ID, + ScrapeTimeInterval: "P1D", + AccountUsername: "marry", + AccountID: "poppens", + LastScrapeTime: time.Now(), + AccountValidate: false, + AccountValidationKey: "im-a-key", + } + validUSerSource, err = CreateUserSource(ctx, validUSerSource) + if err != nil { + t.Fatalf("CreateUserSource err: %v", err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + id models.UserSourceID + } + var tests = []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Delete Valid UserSource", + args: args{ + ctx: ctx, + id: validUSerSource.ID, + }, + wantErr: false, + }, + { + name: "Test 02: Delete not existed UserSource", + args: args{ + ctx: ctx, + id: validUSerSource.ID, + }, + wantErr: false, + }, + { + name: "Test 03: Empty UserSourceID", + args: args{ + ctx: ctx, + id: "", + }, + wantErr: true, + }, + { + name: "Test 04: Short UserSourceID", + args: args{ + ctx: ctx, + id: "111", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeleteUserSource(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr { + t.Errorf("DeleteUserSource() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func checkUserSourceID(got models.UserSource, want models.UserSource) bool { + if got.ID != want.ID { + return false + } + + return true +} diff --git a/pkg/database/user_test.go b/pkg/database/user_test.go index 7128522..a18474d 100644 --- a/pkg/database/user_test.go +++ b/pkg/database/user_test.go @@ -11,13 +11,7 @@ import ( "go.opentelemetry.io/otel" ) -var ( - validUser = models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}} - invalidUser = models.User{BaseModel: models.BaseModel[models.UserID]{ID: "invalid"}} -) - func TestCreateUser(t *testing.T) { - // Setup trow away container ctx := context.Background() container, gormDB, err := test.StartPostgresContainer(ctx) @@ -35,6 +29,10 @@ func TestCreateUser(t *testing.T) { defer container.Terminate(ctx) + // Setup Tests + validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}} + + // Tests type args struct { ctx context.Context user models.User @@ -54,6 +52,15 @@ func TestCreateUser(t *testing.T) { want: validUser, wantErr: false, }, + { + name: "Test 02: Duplicate User", + args: args{ + ctx: ctx, + user: validUser, + }, + want: models.User{}, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -70,7 +77,6 @@ func TestCreateUser(t *testing.T) { } func TestGetUserByID(t *testing.T) { - // Setup trow away container ctx := context.Background() container, gormDB, err := test.StartPostgresContainer(ctx) @@ -88,14 +94,11 @@ func TestGetUserByID(t *testing.T) { defer container.Terminate(ctx) - validUser, err = CreateUser(ctx, validUser) - if err != nil { - t.Fatal(err) - } + // Setup Tests + validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}} + invalidUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: "invalid"}} - // Setup Test - - // Test + // Tests type args struct { ctx context.Context id models.UserID @@ -157,7 +160,9 @@ func TestDeleteUser(t *testing.T) { logger.AddHook(hook) defer container.Terminate(ctx) - // Setup Test + + // Setup Tests + validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}} _, err = CreateUser(ctx, validUser) if err != nil { -- 2.45.2 From d6cbea3f586bfc22b98f0d11b74b65aff5dd59a3 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:25:36 +0200 Subject: [PATCH 57/77] feat(error): more error handling added missing checks --- pkg/database/tagGroup.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/database/tagGroup.go b/pkg/database/tagGroup.go index f90a7a6..08f8f12 100644 --- a/pkg/database/tagGroup.go +++ b/pkg/database/tagGroup.go @@ -33,6 +33,14 @@ func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagNa return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } + if tagGroupName == "" { + return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagGroupNameIsEmpty}) + } + + if tagName == "" { + return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty}) + } + tagGroup := models.TagGroup{ Name: tagGroupName, TagID: tagName, -- 2.45.2 From 1cbdd1f4ed07670450d1061905189a487e3ee1f6 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:26:12 +0200 Subject: [PATCH 58/77] feat(error): added additional constant added missing log content for tag --- pkg/error/validation.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/error/validation.go b/pkg/error/validation.go index 143635e..fdaf0d9 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -11,6 +11,7 @@ const ( UserSourceIDIsEmpty = "userSourceID cannot be empty" UserSourceIDToShort = "userSourceID needs to be 25 characters long" + TagNameIsEmpty = "tagName cannot be empty" TagListIsEmpty = "tagList cannot be empty" TagAliasListIsEmpty = "tagAliasList cannot be empty" -- 2.45.2 From 3fcdb0e9ead904be552abc98a7ba2653fb1ef284 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:26:33 +0200 Subject: [PATCH 59/77] feat(test): added tagGroups fully tested tag groups --- pkg/database/tagGroup_test.go | 292 ++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 pkg/database/tagGroup_test.go diff --git a/pkg/database/tagGroup_test.go b/pkg/database/tagGroup_test.go new file mode 100644 index 0000000..497f8db --- /dev/null +++ b/pkg/database/tagGroup_test.go @@ -0,0 +1,292 @@ +package database + +import ( + "context" + "reflect" + "testing" + + "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" +) + +func TestCreateTagGroup(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tag to test with + validTag := models.Tag{ + Name: "valid_tag", + Type: models.General, + } + validTag, err = CreateTag(ctx, validTag.Name, validTag.Type) + if err != nil { + t.Fatalf("CreateTag err: %v", err) + } + // -- + + // -- Create TagGroup to test with + validTagGroup := models.TagGroup{ + Name: "valid_tag_group_name", + TagID: validTag.Name, + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + tagGroupName models.TagGroupName + tagName models.TagName + } + var tests = []struct { + name string + args args + want models.TagGroup + wantErr bool + }{ + { + name: "Test 01: Valid TagGroup", + args: args{ + ctx: ctx, + tagGroupName: validTagGroup.Name, + tagName: validTag.Name, + }, + want: validTagGroup, + wantErr: false, + }, + { + name: "Test 02: Duplicate TagGroup", + args: args{ + ctx: ctx, + tagGroupName: validTagGroup.Name, + tagName: validTag.Name, + }, + want: models.TagGroup{}, + wantErr: true, + }, + { + name: "Test 03: TagGroup name is empty", + args: args{ + ctx: ctx, + tagGroupName: "", + tagName: validTag.Name, + }, + want: models.TagGroup{}, + wantErr: true, + }, + { + name: "Test 04: tagName name is empty", + args: args{ + ctx: ctx, + tagGroupName: validTagGroup.Name, + tagName: "", + }, + want: models.TagGroup{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := CreateTagGroup(tt.args.ctx, tt.args.tagGroupName, tt.args.tagName) + if (err != nil) != tt.wantErr { + t.Errorf("CreateTagGroup() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("CreateTagGroup() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestCreateTagGroupInBatch(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tags to test with + validTags := test.GenerateRandomTags(5) + err = CreateTagInBatch(ctx, validTags, len(validTags)) + if err != nil { + t.Fatalf("CreateTags err: %v", err) + } + // -- + + // -- Create TagGroup to test with + validTagGroup := test.GenerateRandomTagGroups(validTags, 5) + // -- + + // -- -- Tests + type args struct { + ctx context.Context + tagsGroups []models.TagGroup + batchSize int + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid TagGroups", + args: args{ + ctx: ctx, + tagsGroups: validTagGroup, + batchSize: len(validTags), + }, + wantErr: false, + }, + { + name: "Test 02: Duplicate TagGroups", + args: args{ + ctx: ctx, + tagsGroups: validTagGroup, + batchSize: len(validTags), + }, + wantErr: true, + }, + { + name: "Test 03: Nil TagGroups", + args: args{ + ctx: ctx, + tagsGroups: nil, + batchSize: len(validTags), + }, + wantErr: true, + }, + { + name: "Test 04: Empty TagGroups", + args: args{ + ctx: ctx, + tagsGroups: []models.TagGroup{}, + batchSize: len(validTags), + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CreateTagGroupInBatch(tt.args.ctx, tt.args.tagsGroups, tt.args.batchSize); (err != nil) != tt.wantErr { + t.Errorf("CreateTagGroupInBatch() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestDeleteTagGroup(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tag to test with + validTag := models.Tag{ + Name: "valid_tag", + Type: models.General, + } + validTag, err = CreateTag(ctx, validTag.Name, validTag.Type) + if err != nil { + t.Fatalf("CreateTag err: %v", err) + } + // -- + + // -- Create TagGroup to test with + validTagGroup := models.TagGroup{ + Name: "valid_tag_group_name", + TagID: validTag.Name, + } + validTagGroup, err = CreateTagGroup(ctx, validTagGroup.Name, validTagGroup.TagID) + if err != nil { + t.Fatalf("CreateTagGroup err: %v", err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + tagGroupName models.TagGroupName + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid TagGroup", + args: args{ + ctx: ctx, + tagGroupName: validTagGroup.Name, + }, + wantErr: false, + }, + { + name: "Test 02: Not existing TagGroup", + args: args{ + ctx: ctx, + tagGroupName: validTagGroup.Name, + }, + wantErr: false, + }, + { + name: "Test 03: Empty TagGroupName ", + args: args{ + ctx: ctx, + tagGroupName: "", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeleteTagGroup(tt.args.ctx, tt.args.tagGroupName); (err != nil) != tt.wantErr { + t.Errorf("DeleteTagGroup() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} -- 2.45.2 From a9ac2a34fbe16b76fea15bb92642337f1754ef96 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:44:16 +0200 Subject: [PATCH 60/77] feat(error): added additional constant --- pkg/error/validation.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/error/validation.go b/pkg/error/validation.go index fdaf0d9..f8356c7 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -14,6 +14,7 @@ const ( TagNameIsEmpty = "tagName cannot be empty" TagListIsEmpty = "tagList cannot be empty" + TagAliasNameIsEmpty = "tagAliasName cannot be empty" TagAliasListIsEmpty = "tagAliasList cannot be empty" TagGroupListIsEmpty = "tagGroupList cannot be empty" -- 2.45.2 From 5562d75e3a46437f08cea6fe13aafb1603ab32c3 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:44:27 +0200 Subject: [PATCH 61/77] feat(error): more error handling added missing checks --- pkg/database/tagAlias.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/database/tagAlias.go b/pkg/database/tagAlias.go index f7cfc74..4cf7338 100644 --- a/pkg/database/tagAlias.go +++ b/pkg/database/tagAlias.go @@ -32,6 +32,14 @@ func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagNa return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } + if tagAliasName == "" { + return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagAliasNameIsEmpty}) + } + + if tagName == "" { + return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty}) + } + tagAlias := models.TagAlias{ Name: tagAliasName, TagID: tagName, @@ -109,6 +117,10 @@ func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } + if len(tagAliasName) == 0 { + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty}) + } + result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { -- 2.45.2 From f0f216bdaa0c0a1d5b4b37935469725aa0662f0d Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:44:50 +0200 Subject: [PATCH 62/77] feat(test): added tagAlias fully tested tag groups --- pkg/database/tagAlias_test.go | 292 ++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 pkg/database/tagAlias_test.go diff --git a/pkg/database/tagAlias_test.go b/pkg/database/tagAlias_test.go new file mode 100644 index 0000000..ee8109f --- /dev/null +++ b/pkg/database/tagAlias_test.go @@ -0,0 +1,292 @@ +package database + +import ( + "context" + "reflect" + "testing" + + "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" +) + +func TestCreateTagAlias(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tag to test with + validTag := models.Tag{ + Name: "valid_tag", + Type: models.General, + } + validTag, err = CreateTag(ctx, validTag.Name, validTag.Type) + if err != nil { + t.Fatalf("CreateTag err: %v", err) + } + // -- + + // -- Create TagAlias to test with + validTagAlias := models.TagAlias{ + Name: "valid_tag_alias_name", + TagID: validTag.Name, + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + tagAliasName models.TagAliasName + tagName models.TagName + } + tests := []struct { + name string + args args + want models.TagAlias + wantErr bool + }{ + { + name: "Test 01: Valid tagAlias", + args: args{ + ctx: ctx, + tagAliasName: validTagAlias.Name, + tagName: validTag.Name, + }, + want: validTagAlias, + wantErr: false, + }, + { + name: "Test 02: Duplicate tagAlias", + args: args{ + ctx: ctx, + tagAliasName: validTagAlias.Name, + tagName: validTag.Name, + }, + want: models.TagAlias{}, + wantErr: true, + }, + { + name: "Test 03: tagAlias name is empty", + args: args{ + ctx: ctx, + tagAliasName: "", + tagName: validTag.Name, + }, + want: models.TagAlias{}, + wantErr: true, + }, + { + name: "Test 04: tagName name is empty", + args: args{ + ctx: ctx, + tagAliasName: validTagAlias.Name, + tagName: "", + }, + want: models.TagAlias{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := CreateTagAlias(tt.args.ctx, tt.args.tagAliasName, tt.args.tagName) + if (err != nil) != tt.wantErr { + t.Errorf("CreateTagAlias() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("CreateTagAlias() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestCreateTagAliasInBatch(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tags to test with + validTags := test.GenerateRandomTags(5) + err = CreateTagInBatch(ctx, validTags, len(validTags)) + if err != nil { + t.Fatalf("CreateTags err: %v", err) + } + // -- + + // -- Create TagAlias to test with + validTagGroup := test.GenerateRandomTagAlias(validTags, 5) + // -- + + // -- -- Tests + type args struct { + ctx context.Context + tagsAliases []models.TagAlias + batchSize int + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid TagAliases", + args: args{ + ctx: ctx, + tagsAliases: validTagGroup, + batchSize: len(validTags), + }, + wantErr: false, + }, + { + name: "Test 02: Duplicate TagAliases", + args: args{ + ctx: ctx, + tagsAliases: validTagGroup, + batchSize: len(validTags), + }, + wantErr: true, + }, + { + name: "Test 03: Nil TagAliases", + args: args{ + ctx: ctx, + tagsAliases: nil, + batchSize: len(validTags), + }, + wantErr: true, + }, + { + name: "Test 04: Empty TagAliases", + args: args{ + ctx: ctx, + tagsAliases: []models.TagAlias{}, + batchSize: len(validTags), + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CreateTagAliasInBatch(tt.args.ctx, tt.args.tagsAliases, tt.args.batchSize); (err != nil) != tt.wantErr { + t.Errorf("CreateTagAliasInBatch() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestDeleteTagAlias(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tag to test with + validTag := models.Tag{ + Name: "valid_tag", + Type: models.General, + } + validTag, err = CreateTag(ctx, validTag.Name, validTag.Type) + if err != nil { + t.Fatalf("CreateTag err: %v", err) + } + // -- + + // -- Create TagAlias to test with + validTagAlias := models.TagAlias{ + Name: "valid_tag_group_name", + TagID: validTag.Name, + } + validTagAlias, err = CreateTagAlias(ctx, validTagAlias.Name, validTagAlias.TagID) + if err != nil { + t.Fatalf("CreateTagGroup err: %v", err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + tagAliasName models.TagAliasName + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid TagAlias", + args: args{ + ctx: ctx, + tagAliasName: validTagAlias.Name, + }, + wantErr: false, + }, + { + name: "Test 02: Not existing TagAlias", + args: args{ + ctx: ctx, + tagAliasName: validTagAlias.Name, + }, + wantErr: false, + }, + { + name: "Test 03: Empty TagAliasName ", + args: args{ + ctx: ctx, + tagAliasName: "", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeleteTagAlias(tt.args.ctx, tt.args.tagAliasName); (err != nil) != tt.wantErr { + t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} -- 2.45.2 From 6dc78db4a4c73a292d36c4528df0182da7ea6855 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:48:31 +0200 Subject: [PATCH 63/77] fix(test): user not created fix an issue where the user was not created --- pkg/database/user_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/database/user_test.go b/pkg/database/user_test.go index a18474d..cc9cb54 100644 --- a/pkg/database/user_test.go +++ b/pkg/database/user_test.go @@ -98,6 +98,11 @@ func TestGetUserByID(t *testing.T) { validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}} invalidUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: "invalid"}} + validUser, err = CreateUser(ctx, validUser) + if err != nil { + logger.Fatal(err) + } + // Tests type args struct { ctx context.Context -- 2.45.2 From 5cc5a22d283ba4d92355445f52e0b26f9c637019 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 15:07:53 +0200 Subject: [PATCH 64/77] feat(error): added additional constant added missing log content for tag --- pkg/error/validation.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/error/validation.go b/pkg/error/validation.go index f8356c7..889744b 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -12,6 +12,7 @@ const ( UserSourceIDToShort = "userSourceID needs to be 25 characters long" TagNameIsEmpty = "tagName cannot be empty" + TagTypeIsEmpty = "tagType cannot be empty" TagListIsEmpty = "tagList cannot be empty" TagAliasNameIsEmpty = "tagAliasName cannot be empty" -- 2.45.2 From 92ef2c50596b6829daada3addc4e8354c595c882 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 15:08:10 +0200 Subject: [PATCH 65/77] feat(error): more error handling added missing checks --- pkg/database/tag.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/database/tag.go b/pkg/database/tag.go index cdb7a14..5e21837 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -32,6 +32,14 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } + if tagName == "" { + return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty}) + } + + if tagType == "" { + return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagTypeIsEmpty}) + } + tag := models.Tag{ Name: tagName, Type: tagType, @@ -109,6 +117,10 @@ func DeleteTag(ctx context.Context, tagName models.TagName) error { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } + if len(tagName) == 0 { + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty}) + } + result := client.WithContext(ctx).Delete(&tag, tagName) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { -- 2.45.2 From 2bbfc79e436c1d8954aa8d555576915ba13f6fc0 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 15:08:35 +0200 Subject: [PATCH 66/77] feat(test): added tag fully tested tags --- pkg/database/tag_test.go | 262 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 262 insertions(+) create mode 100644 pkg/database/tag_test.go diff --git a/pkg/database/tag_test.go b/pkg/database/tag_test.go new file mode 100644 index 0000000..5bea94b --- /dev/null +++ b/pkg/database/tag_test.go @@ -0,0 +1,262 @@ +package database + +import ( + "context" + "reflect" + "testing" + + "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" +) + +func TestCreateTag(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tag to test with + validTag := models.Tag{ + Name: "valid_tag", + Type: models.General, + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + tagName models.TagName + tagType models.TagType + } + tests := []struct { + name string + args args + want models.Tag + wantErr bool + }{ + { + name: "Test 01: Valid tag", + args: args{ + ctx: ctx, + tagType: validTag.Type, + tagName: validTag.Name, + }, + want: validTag, + wantErr: false, + }, + { + name: "Test 02: Duplicate tag", + args: args{ + ctx: ctx, + tagType: validTag.Type, + tagName: validTag.Name, + }, + want: models.Tag{}, + wantErr: true, + }, + { + name: "Test 03: tagName is empty", + args: args{ + ctx: ctx, + tagType: "", + tagName: validTag.Name, + }, + want: models.Tag{}, + wantErr: true, + }, + { + name: "Test 04: tagName name is empty", + args: args{ + ctx: ctx, + tagType: validTag.Type, + tagName: "", + }, + want: models.Tag{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := CreateTag(tt.args.ctx, tt.args.tagName, tt.args.tagType) + if (err != nil) != tt.wantErr { + t.Errorf("CreateTag() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("CreateTag() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestCreateTagInBatch(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tags to test with + validTags := test.GenerateRandomTags(5) + // -- + + // -- -- Tests + type args struct { + ctx context.Context + tags []models.Tag + batchSize int + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid Tags", + args: args{ + ctx: ctx, + tags: validTags, + batchSize: len(validTags), + }, + wantErr: false, + }, + { + name: "Test 02: Duplicate Tags", + args: args{ + ctx: ctx, + tags: validTags, + batchSize: len(validTags), + }, + wantErr: true, + }, + { + name: "Test 03: Nil Tags", + args: args{ + ctx: ctx, + tags: nil, + batchSize: len(validTags), + }, + wantErr: true, + }, + { + name: "Test 04: Empty Tags", + args: args{ + ctx: ctx, + tags: []models.Tag{}, + batchSize: len(validTags), + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CreateTagInBatch(tt.args.ctx, tt.args.tags, tt.args.batchSize); (err != nil) != tt.wantErr { + t.Errorf("CreateTagInBatch() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestDeleteTag(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tag to test with + validTag := models.Tag{ + Name: "valid_tag", + Type: models.General, + } + validTag, err = CreateTag(ctx, validTag.Name, validTag.Type) + if err != nil { + t.Fatalf("CreateTag err: %v", err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + tagName models.TagName + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid Tag", + args: args{ + ctx: ctx, + tagName: validTag.Name, + }, + wantErr: false, + }, + { + name: "Test 02: Not existing Tag", + args: args{ + ctx: ctx, + tagName: validTag.Name, + }, + wantErr: false, + }, + { + name: "Test 03: Empty TagName ", + args: args{ + ctx: ctx, + tagName: "", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeleteTag(tt.args.ctx, tt.args.tagName); (err != nil) != tt.wantErr { + t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} -- 2.45.2 From b7e3877b68deab2dc1d7343a531b73026a63709b Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 15:41:36 +0200 Subject: [PATCH 67/77] feat(test): Add GenerateRandomSources function for creating random sources --- test/generator.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/generator.go b/test/generator.go index a8e4fcb..2e91ad7 100644 --- a/test/generator.go +++ b/test/generator.go @@ -3,10 +3,12 @@ package test import ( "fmt" "math/rand" + "time" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" "github.com/davecgh/go-spew/spew" gonanoid "github.com/matoous/go-nanoid/v2" + "gorm.io/gorm" ) func GenerateRandomTags(numTags int) []models.Tag { @@ -67,3 +69,33 @@ func GenerateRandomTagAlias(tags []models.Tag, numGroups int) []models.TagAlias return tagAliases } + +func GenerateRandomSources(numTags int) []models.Source { + var sources []models.Source + + for i := 0; i < numTags; i++ { + id, _ := gonanoid.New(10) + displayName, _ := gonanoid.New(10) + domain, _ := gonanoid.New(10) + icon, _ := gonanoid.New(10) + id = spew.Sprintf("source_name_%s", id) + + source := models.Source{ + BaseModel: models.BaseModel[models.SourceID]{ + ID: models.SourceID(id), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: gorm.DeletedAt{}, + }, + DisplayName: displayName, + Domain: models.SourceDomain(domain), + Icon: icon, + UserSources: nil, + References: nil, + } + + sources = append(sources, source) + } + + return sources +} -- 2.45.2 From de28bdbb0b61c415a33d83f02a6f6135dfbcd9b9 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 15:41:55 +0200 Subject: [PATCH 68/77] chore: Remove unused function --- test/helper.go | 42 ------------------------------------------ 1 file changed, 42 deletions(-) diff --git a/test/helper.go b/test/helper.go index 4b31cc0..da9d4f3 100644 --- a/test/helper.go +++ b/test/helper.go @@ -3,12 +3,8 @@ package test import ( "context" "database/sql" - "net/url" - "strconv" - "strings" "time" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" migrate "github.com/rubenv/sql-migrate" postgrescontainer "github.com/testcontainers/testcontainers-go/modules/postgres" "gorm.io/driver/postgres" @@ -84,41 +80,3 @@ func getGormDB(connectionString string) (*gorm.DB, error) { TranslateError: true, }) } - -func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgrescontainer.PostgresContainer) (*models.DatabaseConfig, error) { - var err error - - connectionString, err := pgContainer.ConnectionString(ctx) - if err != nil { - return nil, err - } - - connectionStringUrl, err := url.Parse(connectionString) - if err != nil { - return nil, err - } - - split := strings.Split(connectionStringUrl.Host, ":") - host := split[0] - - port, err := strconv.Atoi(split[1]) - if err != nil { - return nil, err - } - - database := strings.TrimPrefix(connectionStringUrl.Path, "/") - - username := connectionStringUrl.User.Username() - password, _ := connectionStringUrl.User.Password() - - return &models.DatabaseConfig{ - Endpoint: host, - Username: username, - Password: password, - Database: database, - Port: port, - SSL: false, - Timezone: "Europe/Berlin", - Debug: true, - }, nil -} -- 2.45.2 From 450e06eb25db72a3531b4a8344794e8890addac7 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 15:43:03 +0200 Subject: [PATCH 69/77] refactor: Refactor Domain field to use SourceDomain type in Source struct --- pkg/models/source.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/models/source.go b/pkg/models/source.go index aa90bbb..be62f2e 100644 --- a/pkg/models/source.go +++ b/pkg/models/source.go @@ -4,7 +4,7 @@ package models type Source struct { BaseModel[SourceID] DisplayName string `json:"display_name" ` - Domain string `json:"domain" gorm:"not null;unique"` + Domain SourceDomain `json:"domain" gorm:"not null;unique"` Icon string `json:"icon" gorm:"not null"` UserSources []UserSource `json:"-" gorm:"foreignKey:SourceID"` References []PostReference `json:"references" gorm:"foreignKey:SourceID"` -- 2.45.2 From 5e9b4ff5327fbef4c768a4df7f358b8a62252bc9 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 15:43:45 +0200 Subject: [PATCH 70/77] refactor(tracing): Refactor source ID logging and tracing --- pkg/database/source.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/pkg/database/source.go b/pkg/database/source.go index 06fc8a7..d2e56b8 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -16,14 +16,6 @@ func CreateSource(ctx context.Context, source models.Source) (models.Source, err ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSource") defer span.End() - localLogger = localLogger.WithFields(log.Fields{ - "source_id": source.ID, - }) - - span.SetAttributes( - attribute.String("source_id", string(source.ID)), - ) - utils.HandleEvent(span, localLogger, "Starting source creation") if client == nil { @@ -38,6 +30,14 @@ func CreateSource(ctx context.Context, source models.Source) (models.Source, err return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error) } + localLogger = localLogger.WithFields(log.Fields{ + "source_id": source.ID, + }) + + span.SetAttributes( + attribute.String("source_id", string(source.ID)), + ) + utils.HandleEvent(span, localLogger, "Source created successfully") return source, nil } @@ -82,6 +82,11 @@ func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize return nil } +// UpdateSource updates th source information in the database. +// Only a few parameter can be updated: +// - DisplayName +// - Domain +// - Icon func UpdateSource(ctx context.Context, source models.Source) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateSource") defer span.End() @@ -105,12 +110,15 @@ func UpdateSource(ctx context.Context, source models.Source) error { } updateSource := models.Source{ + BaseModel: models.BaseModel[models.SourceID]{ + ID: source.ID, + }, DisplayName: source.DisplayName, Domain: source.Domain, Icon: source.Icon, } - result := client.WithContext(ctx).Model(&updateSource).Update("deleted_at", gorm.DeletedAt{}) + result := client.WithContext(ctx).Updates(&updateSource) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) -- 2.45.2 From 00b8fe6b710edcb047ff48070b39141de78dfb47 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 15:44:12 +0200 Subject: [PATCH 71/77] feat(test): added sources fully tested sources --- pkg/database/source_test.go | 546 ++++++++++++++++++++++++++++++++++++ 1 file changed, 546 insertions(+) create mode 100644 pkg/database/source_test.go diff --git a/pkg/database/source_test.go b/pkg/database/source_test.go new file mode 100644 index 0000000..0006052 --- /dev/null +++ b/pkg/database/source_test.go @@ -0,0 +1,546 @@ +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 TestCreateSource(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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{ + BaseModel: models.BaseModel[models.SourceID]{ + ID: models.SourceID(fmt.Sprintf("%025s", "Source1")), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: gorm.DeletedAt{}, + }, + DisplayName: "e621", + Domain: "e621.net", + Icon: "e621.net/icon.png", + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + source models.Source + } + tests := []struct { + name string + args args + want models.Source + wantErr bool + }{ + { + name: "Test 01: Valid Source", + args: args{ + ctx: ctx, + source: validSource, + }, + want: validSource, + wantErr: false, + }, + { + name: "Test 02: Duplicate Source", + args: args{ + ctx: ctx, + source: validSource, + }, + want: models.Source{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := CreateSource(tt.args.ctx, tt.args.source) + if (err != nil) != tt.wantErr { + t.Errorf("CreateSource() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("CreateSource() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestCreateSourceInBatch(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Sources to test with + validSources := test.GenerateRandomSources(5) + // -- + + // -- -- Tests + type args struct { + ctx context.Context + source []models.Source + batchSize int + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid Sources", + args: args{ + ctx: ctx, + source: validSources, + batchSize: len(validSources), + }, + wantErr: false, + }, + { + name: "Test 02: Duplicate Sources", + args: args{ + ctx: ctx, + source: validSources, + batchSize: len(validSources), + }, + wantErr: true, + }, + { + name: "Test 03: Nil Sources", + args: args{ + ctx: ctx, + source: nil, + batchSize: len(validSources), + }, + wantErr: true, + }, + { + name: "Test 04: Empty Sources", + args: args{ + ctx: ctx, + source: []models.Source{}, + batchSize: len(validSources), + }, + wantErr: true, + }, + { + name: "Test 08: Empty Batch Size", + args: args{ + ctx: ctx, + source: []models.Source{}, + batchSize: 0, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CreateSourceInBatch(tt.args.ctx, tt.args.source, tt.args.batchSize); (err != nil) != tt.wantErr { + t.Errorf("CreateSourceInBatch() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestUpdateSource(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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{ + BaseModel: models.BaseModel[models.SourceID]{ + ID: models.SourceID(fmt.Sprintf("%025s", "Source1")), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: gorm.DeletedAt{}, + }, + 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 Updates models for UserSource + validUpdateSource := validSource + validUpdateSource.DisplayName = "eeeee" + validUpdateSource.Domain = "aaaaa" + validUpdateSource.Icon = "nnnn" + + invalidUpdateSource := models.Source{} + // -- + + // -- -- Tests + type args struct { + ctx context.Context + source models.Source + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid Update for Source", + args: args{ + ctx: ctx, + source: validUpdateSource, + }, + wantErr: false, + }, + { + name: "Test 02: Invalid Update for Source", + args: args{ + ctx: ctx, + source: invalidUpdateSource, + }, + wantErr: true, + }, + { + name: "Test 03: Empty ID for Update for Source", + args: args{ + ctx: ctx, + source: models.Source{BaseModel: models.BaseModel[models.SourceID]{ID: ""}}, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := UpdateSource(tt.args.ctx, tt.args.source); (err != nil) != tt.wantErr { + t.Errorf("UpdateSource() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestGetSourceByID(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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{ + BaseModel: models.BaseModel[models.SourceID]{ + ID: models.SourceID(fmt.Sprintf("%025s", "Source1")), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: gorm.DeletedAt{}, + }, + DisplayName: "e621", + Domain: "e621.net", + Icon: "e621.net/icon.png", + } + + validSource, err = CreateSource(ctx, validSource) + if err != nil { + t.Fatalf("CreateSource err: %v", err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + id models.SourceID + } + tests := []struct { + name string + args args + want models.Source + wantErr bool + }{ + { + name: "Test 01: Valid Source ID", + args: args{ + ctx: ctx, + id: validSource.ID, + }, + want: validSource, + wantErr: false, + }, + { + name: "Test 03: Empty SourceID", + args: args{ + ctx: ctx, + id: "", + }, + wantErr: true, + }, + { + name: "Test 04: Short SourceID", + args: args{ + ctx: ctx, + id: "111", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetSourceByID(tt.args.ctx, tt.args.id) + if (err != nil) != tt.wantErr { + t.Errorf("GetSourceByID() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkSourceID(got, tt.want) { + t.Errorf("GetSourceByID() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGetSourceByDomain(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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{ + BaseModel: models.BaseModel[models.SourceID]{ + ID: models.SourceID(fmt.Sprintf("%025s", "Source1")), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: gorm.DeletedAt{}, + }, + DisplayName: "e621", + Domain: "e621.net", + Icon: "e621.net/icon.png", + } + + validSource, err = CreateSource(ctx, validSource) + if err != nil { + t.Fatalf("CreateSource err: %v", err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + sourceDomain models.SourceDomain + } + tests := []struct { + name string + args args + want models.Source + wantErr bool + }{ + { + name: "Test 01: Valid SourceURL", + args: args{ + ctx: ctx, + sourceDomain: validSource.Domain, + }, + want: validSource, + wantErr: false, + }, + { + name: "Test 02: Empty SourceURL", + args: args{ + ctx: ctx, + sourceDomain: "", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetSourceByDomain(tt.args.ctx, tt.args.sourceDomain) + if (err != nil) != tt.wantErr { + t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkSourceID(got, tt.want) { + t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestDeleteSource(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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{ + BaseModel: models.BaseModel[models.SourceID]{ + ID: models.SourceID(fmt.Sprintf("%025s", "Source1")), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: gorm.DeletedAt{}, + }, + DisplayName: "e621", + Domain: "e621.net", + Icon: "e621.net/icon.png", + } + + validSource, err = CreateSource(ctx, validSource) + if err != nil { + t.Fatalf("CreateSource err: %v", err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + id models.SourceID + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Delete Valid Source", + args: args{ + ctx: ctx, + id: validSource.ID, + }, + wantErr: false, + }, + { + name: "Test 02: Delete not existed Source", + args: args{ + ctx: ctx, + id: validSource.ID, + }, + wantErr: false, + }, + { + name: "Test 03: Empty SourceID", + args: args{ + ctx: ctx, + id: "", + }, + wantErr: true, + }, + { + name: "Test 04: Short SourceID", + args: args{ + ctx: ctx, + id: "111", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeleteSource(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr { + t.Errorf("DeleteSource() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func checkSourceID(got models.Source, want models.Source) bool { + if got.ID != want.ID { + return false + } + + return true +} -- 2.45.2 From 7632daee84b81757008d10c8ce16d89c1d3b4006 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 21:17:17 +0200 Subject: [PATCH 72/77] feat(test): added post fully tested post --- pkg/database/post.go | 8 + pkg/database/post_test.go | 453 ++++++++++++++++++++++++++++++++++++++ test/generator.go | 25 +++ 3 files changed, 486 insertions(+) create mode 100644 pkg/database/post_test.go diff --git a/pkg/database/post.go b/pkg/database/post.go index a92d231..59ea061 100644 --- a/pkg/database/post.go +++ b/pkg/database/post.go @@ -118,6 +118,11 @@ func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) { return post, nil } +// UpdatePost updates the user post information in the database. +// Only a few parameter can be updated: +// - Rating +// - Tags +// - References func UpdatePost(ctx context.Context, anthrovePost models.Post) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePost") defer span.End() @@ -141,6 +146,9 @@ func UpdatePost(ctx context.Context, anthrovePost models.Post) error { } updatePost := models.Post{ + BaseModel: models.BaseModel[models.PostID]{ + ID: anthrovePost.ID, + }, Rating: anthrovePost.Rating, Tags: anthrovePost.Tags, References: anthrovePost.References, diff --git a/pkg/database/post_test.go b/pkg/database/post_test.go new file mode 100644 index 0000000..eba1030 --- /dev/null +++ b/pkg/database/post_test.go @@ -0,0 +1,453 @@ +package database + +import ( + "context" + "fmt" + "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" + "reflect" + "testing" + "time" +) + +func TestCreatePost(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 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, + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + post models.Post + } + tests := []struct { + name string + args args + want models.Post + wantErr bool + }{ + { + name: "Test 01: Valid Post", + args: args{ + ctx: ctx, + post: validPost, + }, + want: validPost, + wantErr: false, + }, + { + name: "Test 02: Duplicate Post", + args: args{ + ctx: ctx, + post: validPost, + }, + want: models.Post{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := CreatePost(tt.args.ctx, tt.args.post) + if (err != nil) != tt.wantErr { + t.Errorf("CreatePost() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("CreatePost() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestCreatePostInBatch(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Posts to test with + validPosts := test.GenerateRandomPosts(5) + // -- + + // -- -- Tests + type args struct { + ctx context.Context + post []models.Post + batchSize int + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid Posts", + args: args{ + ctx: ctx, + post: validPosts, + batchSize: len(validPosts), + }, + wantErr: false, + }, + { + name: "Test 02: Duplicate Posts", + args: args{ + ctx: ctx, + post: validPosts, + batchSize: len(validPosts), + }, + wantErr: true, + }, + { + name: "Test 03: Nil Posts", + args: args{ + ctx: ctx, + post: nil, + batchSize: len(validPosts), + }, + wantErr: true, + }, + { + name: "Test 04: Empty Posts", + args: args{ + ctx: ctx, + post: []models.Post{}, + batchSize: len(validPosts), + }, + wantErr: true, + }, + { + name: "Test 08: Empty Batch Size", + args: args{ + ctx: ctx, + post: []models.Post{}, + batchSize: 0, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CreatePostInBatch(tt.args.ctx, tt.args.post, tt.args.batchSize); (err != nil) != tt.wantErr { + t.Errorf("CreatePostInBatch() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestGetPostByID(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 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 { + logger.Fatal(err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + id models.PostID + } + tests := []struct { + name string + args args + want models.Post + wantErr bool + }{ + { + name: "Test 01: Valid PostID", + args: args{ + ctx: ctx, + id: validPost.ID, + }, + want: validPost, + wantErr: false, + }, + { + name: "Test 03: Empty PostID", + args: args{ + ctx: ctx, + id: "", + }, + wantErr: true, + }, + { + name: "Test 04: Short PostID", + args: args{ + ctx: ctx, + id: "111", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetPostByID(tt.args.ctx, tt.args.id) + if (err != nil) != tt.wantErr { + t.Errorf("GetPostByID() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkPostID(got, tt.want) { + t.Errorf("GetPostByID() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestUpdatePost(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 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 { + logger.Fatal(err) + } + // -- + + // -- Create Updates models for Post + validUpdatePost := validPost + validUpdatePost.Rating = models.NSFW + + invalidUpdatePost := models.Post{} + // -- + + // -- -- Tests + type args struct { + ctx context.Context + anthrovePost models.Post + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid Update for Post", + args: args{ + ctx: ctx, + anthrovePost: validUpdatePost, + }, + wantErr: false, + }, + { + name: "Test 02: Invalid Update for Post", + args: args{ + ctx: ctx, + anthrovePost: invalidUpdatePost, + }, + wantErr: true, + }, + { + name: "Test 03: Empty ID for Update for Post", + args: args{ + ctx: ctx, + anthrovePost: models.Post{BaseModel: models.BaseModel[models.PostID]{ID: ""}}, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := UpdatePost(tt.args.ctx, tt.args.anthrovePost); (err != nil) != tt.wantErr { + t.Errorf("UpdatePost() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestDeletePost(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 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 { + logger.Fatal(err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + id models.PostID + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Delete Valid Post", + args: args{ + ctx: ctx, + id: validPost.ID, + }, + wantErr: false, + }, + { + name: "Test 02: Delete not existed Post", + args: args{ + ctx: ctx, + id: validPost.ID, + }, + wantErr: false, + }, + { + name: "Test 03: Empty PostID", + args: args{ + ctx: ctx, + id: "", + }, + wantErr: true, + }, + { + name: "Test 04: Short PostID", + args: args{ + ctx: ctx, + id: "111", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeletePost(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr { + t.Errorf("DeletePost() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func checkPostID(got models.Post, want models.Post) bool { + if got.ID != want.ID { + return false + } + + return true +} diff --git a/test/generator.go b/test/generator.go index 2e91ad7..333704a 100644 --- a/test/generator.go +++ b/test/generator.go @@ -99,3 +99,28 @@ func GenerateRandomSources(numTags int) []models.Source { return sources } + +func GenerateRandomPosts(numTags int) []models.Post { + var sources []models.Post + ratings := []models.Rating{"safe", "explicit", "questionable", "unknown"} + + for i := 0; i < numTags; i++ { + id, _ := gonanoid.New(10) + id = spew.Sprintf("source_name_%s", id) + rating := ratings[rand.Intn(len(ratings))] + + source := models.Post{ + BaseModel: models.BaseModel[models.PostID]{ + ID: models.PostID(id), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: gorm.DeletedAt{}, + }, + Rating: rating, + } + + sources = append(sources, source) + } + + return sources +} -- 2.45.2 From 8f6ca691bf614952f0889f34c72a2eb2975f3c31 Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 14 Aug 2024 13:43:11 +0200 Subject: [PATCH 73/77] feat(test): added post, tags & fixed issues fully tested post & tags. Also found bugs with the tests. Those are now fixed. --- pkg/database/favorite.go | 8 +- pkg/database/favorite_test.go | 761 ++++++++++++++++++ .../migrations/001_inital_database.sql | 3 +- pkg/database/post_test.go | 9 +- pkg/models/userFavorite.go | 4 +- test/generator.go | 45 +- 6 files changed, 810 insertions(+), 20 deletions(-) create mode 100644 pkg/database/favorite_test.go diff --git a/pkg/database/favorite.go b/pkg/database/favorite.go index 335cf65..32c71a8 100644 --- a/pkg/database/favorite.go +++ b/pkg/database/favorite.go @@ -82,6 +82,8 @@ func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorit 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 { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserFavorite") 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}) } - if !userFav.DeletedAt.Valid { + if userFav.DeletedAt.Valid == true { 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 errors.Is(result.Error, gorm.ErrRecordNotFound) { 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}) } - result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id) + result := client.WithContext(ctx).Delete(&userFavorite, id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) diff --git a/pkg/database/favorite_test.go b/pkg/database/favorite_test.go new file mode 100644 index 0000000..9311a9c --- /dev/null +++ b/pkg/database/favorite_test.go @@ -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 +} diff --git a/pkg/database/migrations/001_inital_database.sql b/pkg/database/migrations/001_inital_database.sql index 60d779d..95bde1d 100644 --- a/pkg/database/migrations/001_inital_database.sql +++ b/pkg/database/migrations/001_inital_database.sql @@ -2,7 +2,8 @@ CREATE TYPE Rating AS ENUM ( 'safe', 'questionable', - 'explicit' + 'explicit', + 'unknown' ); CREATE TYPE TagType AS ENUM ( diff --git a/pkg/database/post_test.go b/pkg/database/post_test.go index eba1030..0b35721 100644 --- a/pkg/database/post_test.go +++ b/pkg/database/post_test.go @@ -3,14 +3,15 @@ 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" - "reflect" - "testing" - "time" ) func TestCreatePost(t *testing.T) { @@ -110,7 +111,7 @@ func TestCreatePostInBatch(t *testing.T) { // -- -- Setup Tests // -- Create Posts to test with - validPosts := test.GenerateRandomPosts(5) + validPosts := test.GenerateRandomPosts(20) // -- // -- -- Tests diff --git a/pkg/models/userFavorite.go b/pkg/models/userFavorite.go index 920d22a..16fc87d 100644 --- a/pkg/models/userFavorite.go +++ b/pkg/models/userFavorite.go @@ -2,8 +2,8 @@ package models type UserFavorite struct { BaseModel[UserFavoriteID] - UserID string `json:"user_id"` - PostID string `json:"post_id"` + UserID UserID `json:"user_id"` + PostID PostID `json:"post_id"` UserSourceID UserSourceID `json:"user_source_id"` UserSource UserSource `json:"-" gorm:"foreignKey:ID;references:UserSourceID"` } diff --git a/test/generator.go b/test/generator.go index 333704a..1642ad3 100644 --- a/test/generator.go +++ b/test/generator.go @@ -11,11 +11,11 @@ import ( "gorm.io/gorm" ) -func GenerateRandomTags(numTags int) []models.Tag { +func GenerateRandomTags(num int) []models.Tag { var tags []models.Tag 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) tagName := spew.Sprintf("tag_name_%s", id) @@ -32,10 +32,10 @@ func GenerateRandomTags(numTags int) []models.Tag { return tags } -func GenerateRandomTagGroups(tags []models.Tag, numGroups int) []models.TagGroup { +func GenerateRandomTagGroups(tags []models.Tag, num int) []models.TagGroup { var tagGroups []models.TagGroup - for i := 0; i < numGroups; i++ { + for i := 0; i < num; i++ { id, _ := gonanoid.New(10) groupName := fmt.Sprintf("tag_group_%s", id) randomTag := tags[rand.Intn(len(tags))] @@ -51,10 +51,10 @@ func GenerateRandomTagGroups(tags []models.Tag, numGroups int) []models.TagGroup return tagGroups } -func GenerateRandomTagAlias(tags []models.Tag, numGroups int) []models.TagAlias { +func GenerateRandomTagAlias(tags []models.Tag, num int) []models.TagAlias { var tagAliases []models.TagAlias - for i := 0; i < numGroups; i++ { + for i := 0; i < num; i++ { id, _ := gonanoid.New(10) groupName := fmt.Sprintf("tag_alias_%s", id) randomTag := tags[rand.Intn(len(tags))] @@ -70,10 +70,10 @@ func GenerateRandomTagAlias(tags []models.Tag, numGroups int) []models.TagAlias return tagAliases } -func GenerateRandomSources(numTags int) []models.Source { +func GenerateRandomSources(num int) []models.Source { var sources []models.Source - for i := 0; i < numTags; i++ { + for i := 0; i < num; i++ { id, _ := gonanoid.New(10) displayName, _ := gonanoid.New(10) domain, _ := gonanoid.New(10) @@ -100,11 +100,11 @@ func GenerateRandomSources(numTags int) []models.Source { return sources } -func GenerateRandomPosts(numTags int) []models.Post { +func GenerateRandomPosts(num int) []models.Post { var sources []models.Post ratings := []models.Rating{"safe", "explicit", "questionable", "unknown"} - for i := 0; i < numTags; i++ { + for i := 0; i < num; i++ { id, _ := gonanoid.New(10) id = spew.Sprintf("source_name_%s", id) rating := ratings[rand.Intn(len(ratings))] @@ -124,3 +124,28 @@ func GenerateRandomPosts(numTags int) []models.Post { 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 +} -- 2.45.2 From d10aa731bc9f622fa9325e7adee1a7143cfda0bc Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 14 Aug 2024 14:53:08 +0200 Subject: [PATCH 74/77] feat(test): added scopes fixed issues fully tested scopes. Also found bugs with the tests. Those are now fixed. --- pkg/database/client_test.go | 174 +++++++++++++++++++++++++++++ pkg/database/scopes.go | 2 +- pkg/database/scopes_test.go | 216 ++++++++++++++++++++++++++++++++++++ test/helper.go | 45 ++++++++ 4 files changed, 436 insertions(+), 1 deletion(-) create mode 100644 pkg/database/client_test.go create mode 100644 pkg/database/scopes_test.go diff --git a/pkg/database/client_test.go b/pkg/database/client_test.go new file mode 100644 index 0000000..f81c91f --- /dev/null +++ b/pkg/database/client_test.go @@ -0,0 +1,174 @@ +package database + +import ( + "context" + "testing" + + "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 TestConnect(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, _, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + + defer container.Terminate(ctx) + + // -- -- Setup Tests + + // -- Create Database config to test with + validDatabaseConfig, err := test.DatabaseModesFromConnectionString(ctx, container) + if err != nil { + t.Fatalf("Could not get valid database config: %v", err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + config models.DatabaseConfig + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: Valid DatabaseConfig", + args: args{ + ctx: ctx, + config: validDatabaseConfig, + }, + wantErr: false, + }, + { + name: "Test 02: invalid DatabaseConfig", + args: args{ + ctx: ctx, + config: models.DatabaseConfig{}, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := Connect(tt.args.ctx, tt.args.config); (err != nil) != tt.wantErr { + t.Errorf("Connect() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestGetGorm(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, _, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + + defer container.Terminate(ctx) + + // -- -- Setup Tests + + // -- Create Database config to test with + validDatabaseConfig, err := test.DatabaseModesFromConnectionString(ctx, container) + if err != nil { + t.Fatalf("Could not get valid database config: %v", err) + } + + err = Connect(ctx, validDatabaseConfig) + if err != nil { + t.Fatalf("Could not connect to valid database: %v", err) + } + // -- + + // -- -- Tests + type args struct { + ctx context.Context + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: GetGorm", + args: args{ + ctx: ctx, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := GetGorm(tt.args.ctx) + if (err == nil) != !tt.wantErr { + t.Errorf("GetGorm() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func Test_migrateDatabase(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) + } + + // Setup open telemetry + tracer = otel.Tracer(tracingName) + + hook := otellogrus.NewHook(tracingName) + logger.AddHook(hook) + + defer container.Terminate(ctx) + + // -- -- Setup Tests + + // -- Create Database config to test with + validDatabaseConfig, err := test.DatabaseModesFromConnectionString(ctx, container) + if err != nil { + t.Fatalf("Could not get valid database config: %v", err) + } + validDatabaseConfig.Debug = true + // -- + + // -- -- Tests + type args struct { + ctx context.Context + dbPool *gorm.DB + config models.DatabaseConfig + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 01: MigrateDatabase", + args: args{ + ctx: ctx, + dbPool: gormDB, + config: validDatabaseConfig, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := migrateDatabase(tt.args.ctx, tt.args.dbPool, tt.args.config); (err != nil) != tt.wantErr { + t.Errorf("migrateDatabase() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/database/scopes.go b/pkg/database/scopes.go index 0161872..70c59a0 100644 --- a/pkg/database/scopes.go +++ b/pkg/database/scopes.go @@ -60,7 +60,7 @@ func AdvancedPagination(value any, pagination *Pagination, db *gorm.DB) func(db } if pagination.Sort == "" { - pagination.Sort = "Id desc" + pagination.Sort = "id desc" } switch { diff --git a/pkg/database/scopes_test.go b/pkg/database/scopes_test.go new file mode 100644 index 0000000..7ab5ed9 --- /dev/null +++ b/pkg/database/scopes_test.go @@ -0,0 +1,216 @@ +package database + +import ( + "context" + "reflect" + "testing" + + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" + "github.com/davecgh/go-spew/spew" + "go.opentelemetry.io/contrib/bridges/otellogrus" + "go.opentelemetry.io/otel" + "gorm.io/gorm" +) + +func TestPaginate(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tags to test with + validTags := test.GenerateRandomTags(500) + err = CreateTagInBatch(ctx, validTags, len(validTags)) + if err != nil { + logger.Fatalf("Could not create tags: %v", err) + } + // -- + + // -- -- Tests + type args struct { + page int + pageSize int + } + tests := []struct { + name string + args args + want []models.Tag + }{ + { + name: "Test 01: Valid Page & PageSize", + args: args{ + page: 1, + pageSize: 5, + }, + want: validTags[:5], + }, + { + name: "Test 02: Second page with Valid Page & PageSize", + args: args{ + page: 2, + pageSize: 5, + }, + want: validTags[5:10], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var tags []models.Tag + result := client.WithContext(ctx).Scopes(Paginate(tt.args.page, tt.args.pageSize)).Find(&tags) + + if result.Error != nil { + t.Errorf("Paginate() = %v", result.Error) + } + + if !reflect.DeepEqual(tt.want, tags) { + t.Errorf("Length of tags: %d", len(tags)) + t.Errorf("Length of want: %d", len(tt.want)) + t.Errorf("Paginate() = %v, want %v", tags, tt.want) + } + + }) + } +} + +func TestAdvancedPagination(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tags to test with + validTags := []models.Tag{ + { + Name: "a", + Type: models.General, + }, + { + Name: "b", + Type: models.General, + }, + { + Name: "c", + Type: models.Lore, + }, + { + Name: "d", + Type: models.Artist, + }, + { + Name: "e", + Type: models.Artist, + }, + { + Name: "f", + Type: models.Copyright, + }, + { + Name: "g", + Type: models.Meta, + }, + { + Name: "h", + Type: models.Species, + }, + { + Name: "i", + Type: models.Invalid, + }, + { + Name: "j", + Type: models.General, + }, + } + err = CreateTagInBatch(ctx, validTags, len(validTags)) + if err != nil { + logger.Fatalf("Could not create tags: %v", err) + } + // -- + + // -- -- Tests + type args struct { + value any + pagination *Pagination + db *gorm.DB + } + tests := []struct { + name string + args args + want []models.Tag + }{ + { + name: "Test 01: Valid Data", + args: args{ + value: validTags, + pagination: &Pagination{ + Limit: 5, + Page: 1, + Sort: "name asc", + }, + db: client, + }, + want: validTags[:5], + }, + { + name: "Test 02: Second page with Valid Data", + args: args{ + value: validTags, + pagination: &Pagination{ + Limit: 5, + Page: 2, + Sort: "name asc", + }, + db: client, + }, + want: validTags[5:10], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var tags []models.Tag + result := client.WithContext(ctx).Scopes(AdvancedPagination(tt.args.value, tt.args.pagination, tt.args.db)).Find(&tags) + + spew.Dump(tt.args.pagination) + + if result.Error != nil { + t.Errorf("Paginate() = %v", result.Error) + } + + if !reflect.DeepEqual(tt.want, tags) { + t.Errorf("Length of tags: %d", len(tags)) + t.Errorf("Length of want: %d", len(tt.want)) + t.Errorf("Paginate() = %v, want %v", tags, tt.want) + } + + }) + } +} diff --git a/test/helper.go b/test/helper.go index da9d4f3..2ef87e3 100644 --- a/test/helper.go +++ b/test/helper.go @@ -3,8 +3,12 @@ package test import ( "context" "database/sql" + "net/url" + "strconv" + "strings" "time" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" migrate "github.com/rubenv/sql-migrate" postgrescontainer "github.com/testcontainers/testcontainers-go/modules/postgres" "gorm.io/driver/postgres" @@ -80,3 +84,44 @@ func getGormDB(connectionString string) (*gorm.DB, error) { TranslateError: true, }) } + +func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgrescontainer.PostgresContainer) (models.DatabaseConfig, error) { + var err error + var databaseConfig models.DatabaseConfig + + connectionString, err := pgContainer.ConnectionString(ctx) + if err != nil { + return databaseConfig, err + } + + connectionStringUrl, err := url.Parse(connectionString) + if err != nil { + return databaseConfig, err + } + + split := strings.Split(connectionStringUrl.Host, ":") + host := split[0] + + port, err := strconv.Atoi(split[1]) + if err != nil { + return databaseConfig, err + } + + database := strings.TrimPrefix(connectionStringUrl.Path, "/") + + username := connectionStringUrl.User.Username() + password, _ := connectionStringUrl.User.Password() + + databaseConfig = models.DatabaseConfig{ + Endpoint: host, + Username: username, + Password: password, + Database: database, + Port: port, + SSL: false, + Timezone: "Europe/Berlin", + Debug: true, + } + + return databaseConfig, nil +} -- 2.45.2 From 44bedd20a153f3ba5ecff86452777d438144d766 Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 14 Aug 2024 14:58:06 +0200 Subject: [PATCH 75/77] feat(version): Update to version 3 Updated the module to version 3. BREAKING CHANGE: This SDK version is not compatible with programs using the v2 implementation. --- README.md | 8 ++++---- go.mod | 2 +- pkg/database/client.go | 8 ++++---- pkg/database/client_test.go | 4 ++-- pkg/database/favorite.go | 6 +++--- pkg/database/favorite_test.go | 4 ++-- pkg/database/post.go | 6 +++--- pkg/database/post_test.go | 4 ++-- pkg/database/scopes.go | 2 +- pkg/database/scopes_test.go | 4 ++-- pkg/database/source.go | 6 +++--- pkg/database/source_test.go | 4 ++-- pkg/database/tag.go | 6 +++--- pkg/database/tagAlias.go | 6 +++--- pkg/database/tagAlias_test.go | 4 ++-- pkg/database/tagGroup.go | 6 +++--- pkg/database/tagGroup_test.go | 4 ++-- pkg/database/tag_test.go | 4 ++-- pkg/database/user.go | 6 +++--- pkg/database/userSource.go | 6 +++--- pkg/database/userSource_test.go | 4 ++-- pkg/database/user_test.go | 4 ++-- test/generator.go | 2 +- test/helper.go | 2 +- 24 files changed, 56 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 7360a56..c61749c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![Build Check Runner](https://git.anthrove.art/Anthrove/otter-space-sdk/v2/actions/workflows/build_check.yaml/badge.svg) +![Build Check Runner](https://git.anthrove.art/Anthrove/otter-space-sdk/v3/actions/workflows/build_check.yaml/badge.svg) [![Bugs](https://sonarqube.dragse.de/api/project_badges/measure?project=Anthrove---OtterSpace-SDK&metric=bugs&token=sqb_96012ffdd64ce721d7f9c82bfa77aa27a5c1fd38)](https://sonarqube.dragse.de/dashboard?id=Anthrove---OtterSpace-SDK) [![Code Smells](https://sonarqube.dragse.de/api/project_badges/measure?project=Anthrove---OtterSpace-SDK&metric=code_smells&token=sqb_96012ffdd64ce721d7f9c82bfa77aa27a5c1fd38)](https://sonarqube.dragse.de/dashboard?id=Anthrove---OtterSpace-SDK) [![Coverage](https://sonarqube.dragse.de/api/project_badges/measure?project=Anthrove---OtterSpace-SDK&metric=coverage&token=sqb_96012ffdd64ce721d7f9c82bfa77aa27a5c1fd38)](https://sonarqube.dragse.de/dashboard?id=Anthrove---OtterSpace-SDK) @@ -22,7 +22,7 @@ The OtterSpace SDK is a Go package for interacting with the OtterSpace API. It p To install the OtterSpace SDK, you can use `go get`: ```shell -go get git.anthrove.art/Anthrove/otter-space-sdk/v2 +go get git.anthrove.art/Anthrove/otter-space-sdk/v3 ```` ## Usage @@ -35,8 +35,8 @@ import ( "context" "log" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/database" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" ) func main() { diff --git a/go.mod b/go.mod index f914338..2206750 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module git.anthrove.art/Anthrove/otter-space-sdk/v2 +module git.anthrove.art/Anthrove/otter-space-sdk/v3 go 1.22.0 diff --git a/pkg/database/client.go b/pkg/database/client.go index 692e03c..e45cdd7 100644 --- a/pkg/database/client.go +++ b/pkg/database/client.go @@ -5,9 +5,9 @@ import ( "embed" "fmt" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" migrate "github.com/rubenv/sql-migrate" log "github.com/sirupsen/logrus" "go.opentelemetry.io/contrib/bridges/otellogrus" @@ -18,7 +18,7 @@ import ( "gorm.io/gorm" ) -const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/database" +const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database" var ( //go:embed migrations/*.sql diff --git a/pkg/database/client_test.go b/pkg/database/client_test.go index f81c91f..3f63553 100644 --- a/pkg/database/client_test.go +++ b/pkg/database/client_test.go @@ -4,8 +4,8 @@ import ( "context" "testing" - "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/v3/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/test" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" "gorm.io/gorm" diff --git a/pkg/database/favorite.go b/pkg/database/favorite.go index 32c71a8..661f842 100644 --- a/pkg/database/favorite.go +++ b/pkg/database/favorite.go @@ -4,9 +4,9 @@ import ( "context" "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" diff --git a/pkg/database/favorite_test.go b/pkg/database/favorite_test.go index 9311a9c..d6885a9 100644 --- a/pkg/database/favorite_test.go +++ b/pkg/database/favorite_test.go @@ -7,8 +7,8 @@ import ( "testing" "time" - "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/v3/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/test" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" "gorm.io/gorm" diff --git a/pkg/database/post.go b/pkg/database/post.go index 59ea061..c2873f9 100644 --- a/pkg/database/post.go +++ b/pkg/database/post.go @@ -4,9 +4,9 @@ import ( "context" "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" diff --git a/pkg/database/post_test.go b/pkg/database/post_test.go index 0b35721..68977e6 100644 --- a/pkg/database/post_test.go +++ b/pkg/database/post_test.go @@ -7,8 +7,8 @@ import ( "testing" "time" - "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/v3/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/test" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" "gorm.io/gorm" diff --git a/pkg/database/scopes.go b/pkg/database/scopes.go index 70c59a0..36b99b6 100644 --- a/pkg/database/scopes.go +++ b/pkg/database/scopes.go @@ -3,7 +3,7 @@ package database import ( "math" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" "gorm.io/gorm" ) diff --git a/pkg/database/scopes_test.go b/pkg/database/scopes_test.go index 7ab5ed9..fba7b3a 100644 --- a/pkg/database/scopes_test.go +++ b/pkg/database/scopes_test.go @@ -5,8 +5,8 @@ import ( "reflect" "testing" - "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/v3/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/test" "github.com/davecgh/go-spew/spew" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" diff --git a/pkg/database/source.go b/pkg/database/source.go index d2e56b8..5dda285 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -4,9 +4,9 @@ import ( "context" "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" diff --git a/pkg/database/source_test.go b/pkg/database/source_test.go index 0006052..d99f036 100644 --- a/pkg/database/source_test.go +++ b/pkg/database/source_test.go @@ -7,8 +7,8 @@ import ( "testing" "time" - "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/v3/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/test" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" "gorm.io/gorm" diff --git a/pkg/database/tag.go b/pkg/database/tag.go index 5e21837..25d13fd 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -4,9 +4,9 @@ import ( "context" "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" diff --git a/pkg/database/tagAlias.go b/pkg/database/tagAlias.go index 4cf7338..10f643f 100644 --- a/pkg/database/tagAlias.go +++ b/pkg/database/tagAlias.go @@ -4,9 +4,9 @@ import ( "context" "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" diff --git a/pkg/database/tagAlias_test.go b/pkg/database/tagAlias_test.go index ee8109f..e5bd1eb 100644 --- a/pkg/database/tagAlias_test.go +++ b/pkg/database/tagAlias_test.go @@ -5,8 +5,8 @@ import ( "reflect" "testing" - "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/v3/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/test" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" ) diff --git a/pkg/database/tagGroup.go b/pkg/database/tagGroup.go index 08f8f12..ef4dca2 100644 --- a/pkg/database/tagGroup.go +++ b/pkg/database/tagGroup.go @@ -4,9 +4,9 @@ import ( "context" "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" diff --git a/pkg/database/tagGroup_test.go b/pkg/database/tagGroup_test.go index 497f8db..8d087a4 100644 --- a/pkg/database/tagGroup_test.go +++ b/pkg/database/tagGroup_test.go @@ -5,8 +5,8 @@ import ( "reflect" "testing" - "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/v3/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/test" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" ) diff --git a/pkg/database/tag_test.go b/pkg/database/tag_test.go index 5bea94b..0c36ff6 100644 --- a/pkg/database/tag_test.go +++ b/pkg/database/tag_test.go @@ -5,8 +5,8 @@ import ( "reflect" "testing" - "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/v3/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/test" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" ) diff --git a/pkg/database/user.go b/pkg/database/user.go index 840c54e..b6e2b69 100644 --- a/pkg/database/user.go +++ b/pkg/database/user.go @@ -4,9 +4,9 @@ import ( "context" "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" diff --git a/pkg/database/userSource.go b/pkg/database/userSource.go index 229c6ca..c21c712 100644 --- a/pkg/database/userSource.go +++ b/pkg/database/userSource.go @@ -4,9 +4,9 @@ import ( "context" "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" - otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" diff --git a/pkg/database/userSource_test.go b/pkg/database/userSource_test.go index ca2fd31..4a4b192 100644 --- a/pkg/database/userSource_test.go +++ b/pkg/database/userSource_test.go @@ -7,8 +7,8 @@ import ( "testing" "time" - "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/v3/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/test" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" "gorm.io/gorm" diff --git a/pkg/database/user_test.go b/pkg/database/user_test.go index cc9cb54..2affc6b 100644 --- a/pkg/database/user_test.go +++ b/pkg/database/user_test.go @@ -5,8 +5,8 @@ import ( "fmt" "testing" - "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/v3/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/test" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" ) diff --git a/test/generator.go b/test/generator.go index 1642ad3..d6aee26 100644 --- a/test/generator.go +++ b/test/generator.go @@ -5,7 +5,7 @@ import ( "math/rand" "time" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" "github.com/davecgh/go-spew/spew" gonanoid "github.com/matoous/go-nanoid/v2" "gorm.io/gorm" diff --git a/test/helper.go b/test/helper.go index 2ef87e3..4d31273 100644 --- a/test/helper.go +++ b/test/helper.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" migrate "github.com/rubenv/sql-migrate" postgrescontainer "github.com/testcontainers/testcontainers-go/modules/postgres" "gorm.io/driver/postgres" -- 2.45.2 From 2eeae0e5c778735b3cba02d761c00703c8b2dda1 Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 14 Aug 2024 15:03:26 +0200 Subject: [PATCH 76/77] chore(readme): Update README example to include full DatabaseConfig initialization and use database.Connect function --- README.md | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c61749c..5096c0a 100644 --- a/README.md +++ b/README.md @@ -33,23 +33,33 @@ package main import ( "context" - "log" "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database" "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" ) func main() { - ctx := context.Background() - cfg := models.DatabaseConfig{} - - pgClient := database.NewPostgresqlConnection() - err := pgClient.Connect(ctx, cfg) - if err != nil { - log.Panic(err) + ctx := context.Background() + var err error + + config := models.DatabaseConfig{ + Endpoint: "", + Username: "", + Password: "", + Database: "", + Port: 5432, + SSL: false, + Timezone: "Europe/Berlin", + Debug: false, } - + + err = database.Connect(ctx, config) + if err != nil { + panic(err) + } + } + ``` This example creates a new client, connects to the OtterSpace API, and then the client can be used to interact with the API. -- 2.45.2 From 975e084b166930a9ae52d250d07cdbff4c681d35 Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 14 Aug 2024 15:21:29 +0200 Subject: [PATCH 77/77] feat(scope): added sorting --- pkg/database/scopes.go | 17 ++++-- pkg/database/scopes_test.go | 118 +++++++++++++++++++++++++++++++++++- 2 files changed, 127 insertions(+), 8 deletions(-) diff --git a/pkg/database/scopes.go b/pkg/database/scopes.go index 36b99b6..958d1a3 100644 --- a/pkg/database/scopes.go +++ b/pkg/database/scopes.go @@ -59,10 +59,6 @@ func AdvancedPagination(value any, pagination *Pagination, db *gorm.DB) func(db pagination.Page = 1 } - if pagination.Sort == "" { - pagination.Sort = "id desc" - } - switch { case pagination.Limit > models.MaxPageSizeLimit: pagination.Limit = models.MaxPageSizeLimit @@ -79,6 +75,17 @@ func AdvancedPagination(value any, pagination *Pagination, db *gorm.DB) func(db offset := (pagination.Page - 1) * pagination.Limit return func(db *gorm.DB) *gorm.DB { - return db.Offset(offset).Limit(pagination.Limit).Order(pagination.Sort) + return db.Offset(offset).Limit(pagination.Limit) } } + +// OrderBy applies an order operation to a GORM query. +// Parameters: +// +// - sort: a SQL order query like "id desc". +func OrderBy(sort string) func(db *gorm.DB) *gorm.DB { + return func(db *gorm.DB) *gorm.DB { + return db.Order(sort) + } + +} diff --git a/pkg/database/scopes_test.go b/pkg/database/scopes_test.go index fba7b3a..8a42d9d 100644 --- a/pkg/database/scopes_test.go +++ b/pkg/database/scopes_test.go @@ -7,7 +7,6 @@ import ( "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" "git.anthrove.art/Anthrove/otter-space-sdk/v3/test" - "github.com/davecgh/go-spew/spew" "go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/otel" "gorm.io/gorm" @@ -199,8 +198,6 @@ func TestAdvancedPagination(t *testing.T) { var tags []models.Tag result := client.WithContext(ctx).Scopes(AdvancedPagination(tt.args.value, tt.args.pagination, tt.args.db)).Find(&tags) - spew.Dump(tt.args.pagination) - if result.Error != nil { t.Errorf("Paginate() = %v", result.Error) } @@ -214,3 +211,118 @@ func TestAdvancedPagination(t *testing.T) { }) } } + +func TestOrderBy(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + logger.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 Tags to test with + validTags := []models.Tag{ + { + Name: "a", + Type: models.General, + }, + { + Name: "b", + Type: models.General, + }, + { + Name: "c", + Type: models.Lore, + }, + { + Name: "d", + Type: models.Artist, + }, + { + Name: "e", + Type: models.Artist, + }, + { + Name: "f", + Type: models.Copyright, + }, + { + Name: "g", + Type: models.Meta, + }, + { + Name: "h", + Type: models.Species, + }, + { + Name: "i", + Type: models.Invalid, + }, + { + Name: "j", + Type: models.General, + }, + } + err = CreateTagInBatch(ctx, validTags, len(validTags)) + if err != nil { + logger.Fatalf("Could not create tags: %v", err) + } + // -- + + // -- -- Tests + type args struct { + sort string + } + tests := []struct { + name string + args args + want []models.Tag + wantErr bool + }{ + { + name: "Test 01: Valid Data", + args: args{ + sort: "name asc", + }, + want: validTags, + wantErr: false, + }, + { + name: "Test 01: Invalid Data", + args: args{ + sort: "name desc", + }, + want: validTags, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var tags []models.Tag + result := client.WithContext(ctx).Scopes(OrderBy(tt.args.sort)).Find(&tags) + + if result.Error != nil { + t.Errorf("Paginate() = %v", result.Error) + } + + if !reflect.DeepEqual(tt.want, tags) != tt.wantErr { + t.Errorf("Length of tags: %d", len(tags)) + t.Errorf("Length of want: %d", len(tt.want)) + t.Errorf("Paginate() = %v, want %v", tags, tt.want) + } + + }) + } +} -- 2.45.2