2024-06-19 22:13:39 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-22 20:06:36 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
2024-06-19 22:13:39 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/test"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
validAnthroveSource := &models.Source{
|
2024-06-19 22:13:39 +00:00
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "icon.e621.net",
|
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
invalidAnthroveSource := &models.Source{
|
2024-06-19 22:13:39 +00:00
|
|
|
Domain: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
2024-06-22 20:06:36 +00:00
|
|
|
anthroveSource *models.Source
|
2024-06-19 22:13:39 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid anthroveSource",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveSource: validAnthroveSource,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: inValid anthroveSource",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveSource: invalidAnthroveSource,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: unique anthroveSource",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveSource: validAnthroveSource,
|
|
|
|
},
|
2024-06-21 13:08:42 +00:00
|
|
|
wantErr: false,
|
2024-06-19 22:13:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if err := CreateSourceNode(tt.args.ctx, tt.args.db, tt.args.anthroveSource); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateSourceNode() 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
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
sources := []models.Source{
|
2024-06-19 22:13:39 +00:00
|
|
|
{
|
|
|
|
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 = CreateSourceNode(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
|
2024-06-22 20:06:36 +00:00
|
|
|
want []models.Source
|
2024-06-19 22:13:39 +00:00
|
|
|
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 := GetAllSourceNodes(tt.args.ctx, tt.args.db)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetAllSourceNodes() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !checkSourcesNode(got, tt.want) {
|
|
|
|
t.Errorf("GetAllSourceNodes() 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
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
source := &models.Source{
|
2024-06-19 22:13:39 +00:00
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "icon.e621.net",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CreateSourceNode(ctx, gormDB, source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
|
|
|
domain string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2024-06-22 20:06:36 +00:00
|
|
|
want *models.Source
|
2024-06-19 22:13:39 +00:00
|
|
|
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 := GetSourceNodesByURL(tt.args.ctx, tt.args.db, tt.args.domain)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetSourceNodesByURL() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !checkSourceNode(got, tt.want) {
|
|
|
|
t.Errorf("GetSourceNodesByURL() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
func checkSourcesNode(got []models.Source, want []models.Source) bool {
|
2024-06-19 22:13:39 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
func checkSourceNode(got *models.Source, want *models.Source) bool {
|
2024-06-19 22:13:39 +00:00
|
|
|
|
|
|
|
if want == nil && got == nil {
|
|
|
|
return true
|
|
|
|
}
|
2024-06-21 13:08:42 +00:00
|
|
|
|
2024-06-19 22:13:39 +00:00
|
|
|
if got.Domain != want.Domain {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|