315 lines
6.3 KiB
Go
315 lines
6.3 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
"git.dragse.it/anthrove/otter-space-sdk/test"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestCreateSourceNode(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
|
|
|
|
validPostID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
validSource := &models.Source{
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
|
|
DisplayName: "e621",
|
|
Domain: "e621.net",
|
|
Icon: "icon.e621.net",
|
|
}
|
|
|
|
invalidSource := &models.Source{
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
|
|
Domain: "notfound.intern",
|
|
}
|
|
|
|
invalidSourceDomain := &models.Source{
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
|
|
Domain: "",
|
|
}
|
|
|
|
// Test
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
anthroveSource *models.Source
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Test 1: Valid anthroveSource",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveSource: validSource,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Test 2: inValid anthroveSource",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveSource: invalidSourceDomain,
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 3: unique anthroveSource",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveSource: invalidSource,
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := CreateSource(tt.args.ctx, tt.args.db, tt.args.anthroveSource); (err != nil) != tt.wantErr {
|
|
t.Errorf("CreateSource() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetAllSourceNodes(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
|
|
|
|
sources := []models.Source{
|
|
{
|
|
DisplayName: "e621",
|
|
Domain: "e621.net",
|
|
Icon: "icon.e621.net",
|
|
},
|
|
{
|
|
DisplayName: "furaffinity",
|
|
Domain: "furaffinity.net",
|
|
Icon: "icon.furaffinity.net",
|
|
},
|
|
{
|
|
DisplayName: "fenpaws",
|
|
Domain: "fenpa.ws",
|
|
Icon: "icon.fenpa.ws",
|
|
},
|
|
}
|
|
|
|
for _, source := range sources {
|
|
err = CreateSource(ctx, gormDB, &source)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Test
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []models.Source
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Test 1: Get all entries",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
},
|
|
want: sources,
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := GetAllSource(tt.args.ctx, tt.args.db)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetAllSource() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !checkSourcesNode(got, tt.want) {
|
|
t.Errorf("GetAllSource() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetAllSourceWithNoData(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)
|
|
|
|
// Test
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []models.Source
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Test 1: Get all entries",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
},
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := GetAllSource(tt.args.ctx, tt.args.db)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetAllSource() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !checkSourcesNode(got, tt.want) {
|
|
t.Errorf("GetAllSource() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetSourceNodesByURL(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
|
|
|
|
source := &models.Source{
|
|
DisplayName: "e621",
|
|
Domain: "e621.net",
|
|
Icon: "icon.e621.net",
|
|
}
|
|
|
|
err = CreateSource(ctx, gormDB, source)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Test
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
domain models.AnthroveSourceDomain
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want *models.Source
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Test 1: Valid URL",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
domain: "e621.net",
|
|
},
|
|
want: source,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Test 2: Invalid URL",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
domain: "eeeee.net",
|
|
},
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 2: No URL",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
domain: "",
|
|
},
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := GetSourceByDomain(tt.args.ctx, tt.args.db, tt.args.domain)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !checkSourceNode(got, tt.want) {
|
|
t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func checkSourcesNode(got []models.Source, want []models.Source) bool {
|
|
for i, source := range want {
|
|
if source.DisplayName != got[i].DisplayName {
|
|
return false
|
|
}
|
|
if source.Domain != got[i].Domain {
|
|
return false
|
|
}
|
|
if source.Icon != got[i].Icon {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
func checkSourceNode(got *models.Source, want *models.Source) bool {
|
|
|
|
if want == nil && got == nil {
|
|
return true
|
|
}
|
|
|
|
if got.Domain != want.Domain {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
|
|
}
|