diff --git a/internal/postgres/post.go b/internal/postgres/post.go index b618b04..d22d286 100644 --- a/internal/postgres/post.go +++ b/internal/postgres/post.go @@ -37,7 +37,6 @@ func CreatePost(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) err return nil } -// TODO: Make Tests 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"} diff --git a/internal/postgres/post_test.go b/internal/postgres/post_test.go index a9c5177..1886de6 100644 --- a/internal/postgres/post_test.go +++ b/internal/postgres/post_test.go @@ -3,11 +3,11 @@ package postgres import ( "context" "fmt" + _ "github.com/lib/pq" "testing" "git.dragse.it/anthrove/otter-space-sdk/v2/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/v2/test" - _ "github.com/lib/pq" "gorm.io/gorm" ) @@ -361,6 +361,93 @@ func TestGetPostBySourceID(t *testing.T) { } } +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 { diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go index 0ba7089..802dd91 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -3577,3 +3577,89 @@ func Test_postgresqlConnection_CreateTagInBatchAndUpdate(t *testing.T) { }) } } + +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) + } + }) + } +}