package database import ( "context" "reflect" "testing" "git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models" "git.anthrove.art/Anthrove/otter-space-sdk/v4/test" "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) 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 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) } }) } }