2024-06-19 22:13:39 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-25 20:22:05 +00:00
|
|
|
"fmt"
|
2024-06-26 09:23:27 +00:00
|
|
|
"testing"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-25 20:22:05 +00:00
|
|
|
validPostID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
validSource := &models.Source{
|
2024-06-25 20:22:05 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
|
2024-06-19 22:13:39 +00:00
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "icon.e621.net",
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
invalidSource := &models.Source{
|
2024-06-26 09:23:27 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
|
|
|
|
Domain: "notfound.intern",
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidSourceDomain := &models.Source{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
|
|
|
|
Domain: "",
|
2024-06-19 22:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2024-06-22 23:18:23 +00:00
|
|
|
anthroveSource: validSource,
|
2024-06-19 22:13:39 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: inValid anthroveSource",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-26 09:23:27 +00:00
|
|
|
anthroveSource: invalidSourceDomain,
|
2024-06-19 22:13:39 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: unique anthroveSource",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-26 09:23:27 +00:00
|
|
|
anthroveSource: invalidSource,
|
2024-06-19 22:13:39 +00:00
|
|
|
},
|
2024-06-25 20:22:05 +00:00
|
|
|
wantErr: true,
|
2024-06-19 22:13:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-23 19:23:38 +00:00
|
|
|
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)
|
2024-06-19 22:13:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateSource(ctx, gormDB, &source)
|
2024-06-19 22:13:39 +00:00
|
|
|
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) {
|
2024-06-23 19:23:38 +00:00
|
|
|
got, err := GetAllSource(tt.args.ctx, tt.args.db)
|
2024-06-19 22:13:39 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-23 19:23:38 +00:00
|
|
|
t.Errorf("GetAllSource() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-19 22:13:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !checkSourcesNode(got, tt.want) {
|
2024-06-23 19:23:38 +00:00
|
|
|
t.Errorf("GetAllSource() got = %v, want %v", got, tt.want)
|
2024-06-19 22:13:39 +00:00
|
|
|
}
|
|
|
|
})
|
2024-06-26 09:23:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2024-06-19 22:13:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateSource(ctx, gormDB, source)
|
2024-06-19 22:13:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
2024-06-23 20:35:46 +00:00
|
|
|
domain models.AnthroveSourceDomain
|
2024-06-19 22:13:39 +00:00
|
|
|
}
|
|
|
|
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) {
|
2024-06-23 20:35:46 +00:00
|
|
|
got, err := GetSourceByDomain(tt.args.ctx, tt.args.db, tt.args.domain)
|
2024-06-19 22:13:39 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-23 20:35:46 +00:00
|
|
|
t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-19 22:13:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !checkSourceNode(got, tt.want) {
|
2024-06-23 20:35:46 +00:00
|
|
|
t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want)
|
2024-06-19 22:13:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|