2024-08-14 12:53:08 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2024-08-29 13:02:39 +00:00
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
|
2024-08-14 12:53:08 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-08-14 13:21:29 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|