From 5f9ace9bd35077cf7eb55801e46f18f900bb6ba9 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 1 Jul 2024 13:42:53 +0200 Subject: [PATCH] feat: implemented all functions Signed-off-by: SoXX --- internal/postgres/user.go | 99 +++++++++ internal/postgres/user_test.go | 365 +++++++++++++++++++++++++++++++++ pkg/database/postgres.go | 9 +- pkg/database/postgres_test.go | 357 ++++++++++++++++++++++++++++++++ pkg/error/validation.go | 6 +- pkg/models/const.go | 4 +- 6 files changed, 831 insertions(+), 9 deletions(-) diff --git a/internal/postgres/user.go b/internal/postgres/user.go index 042154f..5037a17 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -3,6 +3,7 @@ package postgres import ( "context" "errors" + "time" otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" @@ -320,3 +321,101 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov 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 index 8dfa13a..c8be97a 100644 --- a/internal/postgres/user_test.go +++ b/internal/postgres/user_test.go @@ -5,6 +5,7 @@ import ( "fmt" "reflect" "testing" + "time" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/test" @@ -986,3 +987,367 @@ func checkUserSource(got *models.UserSource, want *models.UserSource) bool { 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) + } + }) + } +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 39c7a54..6d3dd07 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -151,18 +151,15 @@ func (p *postgresqlConnection) GetSourceByDomain(ctx context.Context, sourceDoma // NEW FUNCTIONS func (p *postgresqlConnection) UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error { - //TODO implement me - panic("implement me") + 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 { - //TODO implement me - panic("implement me") + 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 { - //TODO implement me - panic("implement me") + return postgres.UpdateUserSourceValidation(ctx, p.db, anthroveUserID, sourceID, valid) } func (p *postgresqlConnection) CreateTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName, tagID models.AnthroveTagID) error { diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go index d53f9e2..93534cc 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -5,6 +5,7 @@ import ( "fmt" "reflect" "testing" + "time" "git.dragse.it/anthrove/otter-space-sdk/internal/postgres" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" @@ -2716,3 +2717,359 @@ func Test_postgresqlConnection_DeleteTagGroup(t *testing.T) { }) } } + +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) + } + }) + } +} diff --git a/pkg/error/validation.go b/pkg/error/validation.go index 889ae13..48b8632 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -3,8 +3,10 @@ package error import "fmt" const ( - AnthroveUserIDIsEmpty = "anthrovePostID cannot be empty" - AnthroveUserIDToShort = "anthrovePostID needs to be 25 characters long" + 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" ) type EntityValidationFailed struct { diff --git a/pkg/models/const.go b/pkg/models/const.go index 0e24228..00fc13b 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -1,5 +1,7 @@ package models +import "time" + type AnthroveUserID string type AnthrovePostID string type AnthroveSourceID string @@ -9,7 +11,7 @@ type AnthroveTagGroupName string type AnthroveTagAliasName string type AnthroveTagID string type AnthroveScrapeTimeInterval int -type AnthroveUserLastScrapeTime int +type AnthroveUserLastScrapeTime time.Time type Rating string type TagType string