240 lines
4.9 KiB
Go
240 lines
4.9 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
|
|
"go.opentelemetry.io/contrib/bridges/otellogrus"
|
|
"go.opentelemetry.io/otel"
|
|
)
|
|
|
|
func TestCreatePoolReference(t *testing.T) {
|
|
// Setup throwaway 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 Pool to test with
|
|
pool := models.Pool{
|
|
BaseModel: models.BaseModel[models.PoolID]{
|
|
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
|
|
},
|
|
Name: "Test Pool 01",
|
|
Category: models.Collection,
|
|
}
|
|
|
|
pool, err = CreatePool(ctx, pool)
|
|
if err != nil {
|
|
logger.Error(err)
|
|
}
|
|
|
|
// -- Create Source to test with
|
|
source := models.Source{
|
|
BaseModel: models.BaseModel[models.SourceID]{
|
|
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
|
|
},
|
|
DisplayName: "test",
|
|
Domain: "aaa",
|
|
Icon: "bbb",
|
|
}
|
|
|
|
source, err = CreateSource(ctx, source)
|
|
if err != nil {
|
|
logger.Fatal(err)
|
|
}
|
|
|
|
// --
|
|
|
|
// -- -- Tests
|
|
type args struct {
|
|
ctx context.Context
|
|
poolID models.PoolID
|
|
sourceID models.SourceID
|
|
url string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want models.PoolReference
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Test 01: Valid pool & source ID",
|
|
args: args{
|
|
ctx: ctx,
|
|
poolID: pool.ID,
|
|
sourceID: source.ID,
|
|
url: "http://example.com",
|
|
},
|
|
want: models.PoolReference{
|
|
PoolID: pool.ID,
|
|
SourceID: source.ID,
|
|
URL: "http://example.com",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Test 02: Duplicate",
|
|
args: args{
|
|
ctx: ctx,
|
|
poolID: pool.ID,
|
|
sourceID: source.ID,
|
|
url: "http://example.com",
|
|
},
|
|
want: models.PoolReference{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 03: poolID is empty",
|
|
args: args{
|
|
ctx: ctx,
|
|
poolID: "",
|
|
sourceID: source.ID,
|
|
url: "http://example.com",
|
|
},
|
|
want: models.PoolReference{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 04: sourceID is empty",
|
|
args: args{
|
|
ctx: ctx,
|
|
poolID: pool.ID,
|
|
sourceID: "",
|
|
url: "http://example.com",
|
|
},
|
|
want: models.PoolReference{},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := CreatePoolReference(tt.args.ctx, tt.args.poolID, tt.args.sourceID, tt.args.url)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("CreatePoolReference() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("CreatePoolReference() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDeletePoolReference(t *testing.T) {
|
|
// Setup throwaway 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 Pool to test with
|
|
pool := models.Pool{
|
|
BaseModel: models.BaseModel[models.PoolID]{
|
|
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
|
|
},
|
|
Name: "Test Pool 01",
|
|
Category: models.Collection,
|
|
}
|
|
|
|
pool, err = CreatePool(ctx, pool)
|
|
if err != nil {
|
|
logger.Error(err)
|
|
}
|
|
|
|
// -- Create Source to test with
|
|
source := models.Source{
|
|
BaseModel: models.BaseModel[models.SourceID]{
|
|
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
|
|
},
|
|
DisplayName: "test",
|
|
Domain: "aaa",
|
|
Icon: "bbb",
|
|
}
|
|
|
|
source, err = CreateSource(ctx, source)
|
|
if err != nil {
|
|
logger.Fatal(err)
|
|
}
|
|
|
|
// --
|
|
|
|
// -- -- Tests
|
|
type args struct {
|
|
ctx context.Context
|
|
poolID models.PoolID
|
|
sourceID models.SourceID
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Test 01: Valid Reference & pool id",
|
|
args: args{
|
|
ctx: ctx,
|
|
poolID: pool.ID,
|
|
sourceID: source.ID,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Test 02: Not existing sourceID",
|
|
args: args{
|
|
ctx: ctx,
|
|
poolID: pool.ID,
|
|
sourceID: "",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 03: Not existing poolID",
|
|
args: args{
|
|
ctx: ctx,
|
|
poolID: "",
|
|
sourceID: source.ID,
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := DeletePoolReference(tt.args.ctx, tt.args.poolID, tt.args.sourceID); (err != nil) != tt.wantErr {
|
|
t.Errorf("DeletePoolReference() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|