2024-06-19 21:32:42 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-22 23:18:23 +00:00
|
|
|
"fmt"
|
2024-06-19 21:32:42 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/test"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
func TestCheckUserToPostLink(t *testing.T) {
|
2024-06-19 21:32:42 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
invalidUserID := models.AnthroveUserID("XXX")
|
|
|
|
|
|
|
|
validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
|
|
|
|
err = CreateUser(ctx, gormDB, validUserID)
|
2024-06-19 21:32:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
post := &models.Post{
|
2024-06-25 07:09:18 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
2024-06-25 20:22:05 +00:00
|
|
|
ID: validPostID,
|
2024-06-22 23:18:23 +00:00
|
|
|
},
|
|
|
|
Rating: "safe",
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
2024-06-22 23:18:23 +00:00
|
|
|
|
|
|
|
err = CreatePost(ctx, gormDB, post)
|
2024-06-19 21:32:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-25 20:22:05 +00:00
|
|
|
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID)
|
2024-06-22 23:18:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-06-19 21:32:42 +00:00
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
// Test
|
2024-06-19 21:32:42 +00:00
|
|
|
type args struct {
|
2024-06-22 23:18:23 +00:00
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
2024-06-23 20:35:46 +00:00
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
anthrovePostID models.AnthrovePostID
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2024-06-22 23:18:23 +00:00
|
|
|
want bool
|
2024-06-19 21:32:42 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 1: Valid AnthroveUserID and AnthrovePostID",
|
2024-06-19 21:32:42 +00:00
|
|
|
args: args{
|
2024-06-22 23:18:23 +00:00
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-25 20:22:05 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-25 07:09:18 +00:00
|
|
|
anthrovePostID: post.ID,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
want: true,
|
2024-06-19 21:32:42 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
|
2024-06-19 21:32:42 +00:00
|
|
|
args: args{
|
2024-06-22 23:18:23 +00:00
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-25 20:22:05 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-22 23:18:23 +00:00
|
|
|
anthrovePostID: "qadw",
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
want: false,
|
2024-06-25 20:22:05 +00:00
|
|
|
wantErr: true,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
|
2024-06-19 21:32:42 +00:00
|
|
|
args: args{
|
2024-06-22 23:18:23 +00:00
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-25 20:22:05 +00:00
|
|
|
anthroveUserID: invalidUserID,
|
2024-06-25 07:09:18 +00:00
|
|
|
anthrovePostID: post.ID,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
want: false,
|
2024-06-25 20:22:05 +00:00
|
|
|
wantErr: true,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
|
2024-06-19 21:32:42 +00:00
|
|
|
args: args{
|
2024-06-22 23:18:23 +00:00
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-25 20:22:05 +00:00
|
|
|
anthroveUserID: invalidUserID,
|
2024-06-22 23:18:23 +00:00
|
|
|
anthrovePostID: "123456",
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
want: false,
|
2024-06-25 20:22:05 +00:00
|
|
|
wantErr: true,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-23 19:23:38 +00:00
|
|
|
got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID)
|
2024-06-22 23:18:23 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-23 19:23:38 +00:00
|
|
|
t.Errorf("CheckReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-22 23:18:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
2024-06-23 19:23:38 +00:00
|
|
|
t.Errorf("CheckReferenceBetweenUserAndPost() got = %v, want %v", got, tt.want)
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
|
2024-06-19 21:32:42 +00:00
|
|
|
// 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 23:18:23 +00:00
|
|
|
post := &models.Post{
|
2024-06-25 07:09:18 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
|
|
|
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
|
2024-06-22 23:18:23 +00:00
|
|
|
},
|
|
|
|
Rating: "safe",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CreatePost(ctx, gormDB, post)
|
2024-06-19 21:32:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
source := &models.Source{
|
|
|
|
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 21:32:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
2024-06-22 23:18:23 +00:00
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
2024-06-23 20:35:46 +00:00
|
|
|
anthrovePostID models.AnthrovePostID
|
|
|
|
sourceDomain models.AnthroveSourceDomain
|
2024-06-22 23:18:23 +00:00
|
|
|
anthrovePostRelationship *models.PostReference
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 1: Valid AnthrovePostID and anthroveSourceDomain",
|
2024-06-19 21:32:42 +00:00
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-25 07:09:18 +00:00
|
|
|
anthrovePostID: post.ID,
|
2024-06-22 23:18:23 +00:00
|
|
|
sourceDomain: "e621.net",
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 2: Invalid AnthrovePostID and Valid anthroveSourceDomain",
|
2024-06-19 21:32:42 +00:00
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthrovePostID: "123456",
|
2024-06-22 23:18:23 +00:00
|
|
|
sourceDomain: "e621.net",
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 3: Invalid anthroveSourceDomain and Valid AnthrovePostID",
|
2024-06-19 21:32:42 +00:00
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthrovePostID: "1234",
|
2024-06-22 23:18:23 +00:00
|
|
|
sourceDomain: "fa.banana",
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 4: Invalid anthroveSourceDomain and Invalid AnthrovePostID",
|
2024-06-19 21:32:42 +00:00
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-22 23:18:23 +00:00
|
|
|
anthrovePostID: "696969",
|
|
|
|
sourceDomain: "hehe.funny.number",
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-23 19:23:38 +00:00
|
|
|
if err := CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.sourceDomain); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateReferenceBetweenPostAndSource() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
func TestEstablishUserToPostLink(t *testing.T) {
|
2024-06-19 21:32:42 +00:00
|
|
|
// 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
|
|
|
|
|
|
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
invalidUserID := models.AnthroveUserID("XXX")
|
|
|
|
|
|
|
|
validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
|
|
|
|
err = CreateUser(ctx, gormDB, validUserID)
|
2024-06-19 21:32:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
post := &models.Post{
|
2024-06-25 07:09:18 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
2024-06-25 20:22:05 +00:00
|
|
|
ID: validPostID,
|
2024-06-22 23:18:23 +00:00
|
|
|
},
|
|
|
|
Rating: "safe",
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
err = CreatePost(ctx, gormDB, post)
|
2024-06-19 21:32:42 +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
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
anthrovePostID models.AnthrovePostID
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid AnthroveUserID and AnthrovePostID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-25 20:22:05 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-25 07:09:18 +00:00
|
|
|
anthrovePostID: post.ID,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-25 20:22:05 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-19 21:32:42 +00:00
|
|
|
anthrovePostID: "123456",
|
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
wantErr: true,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-25 20:22:05 +00:00
|
|
|
name: "Test 3: invalid AnthroveUserID and valid AnthrovePostID",
|
2024-06-19 21:32:42 +00:00
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-25 20:22:05 +00:00
|
|
|
anthroveUserID: invalidUserID,
|
|
|
|
anthrovePostID: post.ID,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
wantErr: true,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-25 20:22:05 +00:00
|
|
|
anthroveUserID: invalidUserID,
|
2024-06-19 21:32:42 +00:00
|
|
|
anthrovePostID: "123456",
|
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
wantErr: true,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-23 19:23:38 +00:00
|
|
|
if err := CreateReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|