281 lines
6.4 KiB
Go
281 lines
6.4 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels"
|
|
"git.dragse.it/anthrove/otter-space-sdk/test"
|
|
"gorm.io/gorm"
|
|
"testing"
|
|
)
|
|
|
|
func TestEstablishAnthrovePostToSourceLink(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
|
|
|
|
err = CreateAnthrovePostNode(ctx, gormDB, "1234", "safe")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
source := &pgModels.Source{
|
|
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
|
|
anthrovePostID models.AnthrovePostID
|
|
anthroveSourceDomain string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Test 1: Valid AnthrovePostID and anthroveSourceDomain",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthrovePostID: "1234",
|
|
anthroveSourceDomain: "e621.net",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Test 2: Invalid AnthrovePostID and Valid anthroveSourceDomain",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthrovePostID: "123456",
|
|
anthroveSourceDomain: "e621.net",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 3: Invalid anthroveSourceDomain and Valid AnthrovePostID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthrovePostID: "1234",
|
|
anthroveSourceDomain: "fa.banana",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 4: Invalid anthroveSourceDomain and Invalid AnthrovePostID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthrovePostID: "696969",
|
|
anthroveSourceDomain: "hehe.funny.number",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := EstablishAnthrovePostToSourceLink(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.anthroveSourceDomain); (err != nil) != tt.wantErr {
|
|
t.Errorf("EstablishAnthrovePostToSourceLink() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEstablishUserToPostLink(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
|
|
err = CreateUser(ctx, gormDB, "1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = CreateAnthrovePostNode(ctx, gormDB, "1234", "safe")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Test
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
anthroveUserID models.AnthroveUserID
|
|
anthrovePostID models.AnthrovePostID
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Test 1: Valid AnthroveUserID and AnthrovePostID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "1",
|
|
anthrovePostID: "1234",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "1",
|
|
anthrovePostID: "123456",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "123",
|
|
anthrovePostID: "1234",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "123",
|
|
anthrovePostID: "123456",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := EstablishUserToPostLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID); (err != nil) != tt.wantErr {
|
|
t.Errorf("EstablishUserToPostLink() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCheckUserToPostLink(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
|
|
|
|
err = CreateUser(ctx, gormDB, "1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = CreateAnthrovePostNode(ctx, gormDB, "1234", "safe")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = EstablishUserToPostLink(ctx, gormDB, "1", "1234")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Test
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
anthroveUserID models.AnthroveUserID
|
|
anthrovePostID models.AnthrovePostID
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want bool
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Test 1: Valid AnthroveUserID and AnthrovePostID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "1",
|
|
anthrovePostID: "1234",
|
|
},
|
|
want: true,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "1",
|
|
anthrovePostID: "123456",
|
|
},
|
|
want: false,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "123",
|
|
anthrovePostID: "1234",
|
|
},
|
|
want: false,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "123",
|
|
anthrovePostID: "123456",
|
|
},
|
|
want: false,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := CheckUserToPostLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("CheckUserToPostLink() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("CheckUserToPostLink() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|