2024-06-19 21:32:42 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-22 23:18:23 +00:00
|
|
|
"fmt"
|
2024-07-06 20:19:55 +00:00
|
|
|
_ "github.com/lib/pq"
|
2024-06-26 07:10:04 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-07-02 08:11:42 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/v2/pkg/models"
|
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/v2/test"
|
2024-06-19 21:32:42 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateAnthrovePostNode(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)
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
// Setup Tests
|
|
|
|
|
|
|
|
validPost := &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",
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidPost := &models.Post{
|
|
|
|
Rating: "error",
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
anthrovePost *models.Post
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid AnthrovePostID and Rating",
|
|
|
|
args: args{
|
2024-06-22 23:18:23 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
db: gormDB,
|
|
|
|
anthrovePost: validPost,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 2: Invalid Rating",
|
2024-06-19 21:32:42 +00:00
|
|
|
args: args{
|
2024-06-22 23:18:23 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
db: gormDB,
|
|
|
|
anthrovePost: invalidPost,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2024-06-26 09:23:27 +00:00
|
|
|
{
|
|
|
|
name: "Test 3: Nill",
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
db: gormDB,
|
|
|
|
anthrovePost: nil,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-22 23:18:23 +00:00
|
|
|
if err := CreatePost(tt.args.ctx, tt.args.db, tt.args.anthrovePost); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreatePost() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
func TestGetPostByAnthroveID(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)
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
// Setup Tests
|
2024-06-19 21:32:42 +00:00
|
|
|
|
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 {
|
2024-06-22 23:18:23 +00:00
|
|
|
t.Fatal("Could not create post", err)
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
2024-06-23 20:35:46 +00:00
|
|
|
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 *models.Post
|
2024-06-19 21:32:42 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 1: Valid anthrovePostID",
|
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-19 21:32:42 +00:00
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
want: post,
|
2024-06-19 21:32:42 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 2: 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: "1234",
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 3: No anthrovePostID",
|
2024-06-19 21:32:42 +00:00
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthrovePostID: "",
|
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
want: nil,
|
2024-06-19 21:32:42 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
2024-06-22 23:18:23 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := GetPostByAnthroveID(tt.args.ctx, tt.args.db, tt.args.anthrovePostID)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetPostByAnthroveID() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !checkPost(got, tt.want) {
|
|
|
|
t.Errorf("GetPostByAnthroveID() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPostBySourceURL(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 Tests
|
|
|
|
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
|
|
|
},
|
2024-06-27 14:11:16 +00:00
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
Rating: "safe",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CreatePost(ctx, gormDB, post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create post", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
source := models.Source{
|
2024-06-25 07:09:18 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
|
|
|
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
|
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "https://e621.net/icon.ico",
|
|
|
|
}
|
2024-06-19 21:32:42 +00:00
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateSource(ctx, gormDB, &source)
|
2024-06-22 23:18:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create source", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:05:21 +00:00
|
|
|
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.org", models.PostReferenceConfig{})
|
2024-06-22 23:18:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create source reference", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
|
|
|
sourceURL string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *models.Post
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid sourceURL",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-27 14:11:16 +00:00
|
|
|
sourceURL: "http://test.org",
|
2024-06-22 23:18:23 +00:00
|
|
|
},
|
|
|
|
want: post,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid sourceURL",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
sourceURL: "1234",
|
|
|
|
},
|
|
|
|
want: nil,
|
2024-06-25 20:22:05 +00:00
|
|
|
wantErr: true,
|
2024-06-22 23:18:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: No sourceURL",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
sourceURL: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
2024-06-25 20:22:05 +00:00
|
|
|
wantErr: true,
|
2024-06-22 23:18:23 +00:00
|
|
|
},
|
|
|
|
}
|
2024-06-19 21:32:42 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-07-01 19:38:09 +00:00
|
|
|
got, err := GetPostBySourceURL(tt.args.ctx, tt.args.db, tt.args.sourceURL)
|
2024-06-19 21:32:42 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-07-01 19:38:09 +00:00
|
|
|
t.Errorf("GetPostBySourceURL() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-19 21:32:42 +00:00
|
|
|
return
|
|
|
|
}
|
2024-06-22 23:18:23 +00:00
|
|
|
if !checkPost(got, tt.want) {
|
2024-07-01 19:38:09 +00:00
|
|
|
t.Errorf("GetPostBySourceURL() got = %v, want %v", got, tt.want)
|
2024-06-19 21:32:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-22 23:18:23 +00:00
|
|
|
|
|
|
|
func TestGetPostBySourceID(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 Tests
|
|
|
|
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create post", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
source := models.Source{
|
2024-06-25 07:09:18 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
|
|
|
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
|
|
|
|
},
|
2024-06-22 23:18:23 +00:00
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "https://e621.net/icon.ico",
|
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateSource(ctx, gormDB, &source)
|
2024-06-22 23:18:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create source", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:05:21 +00:00
|
|
|
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.otg", models.PostReferenceConfig{})
|
2024-06-22 23:18:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create source reference", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
2024-06-23 20:35:46 +00:00
|
|
|
sourceID models.AnthroveSourceID
|
2024-06-22 23:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *models.Post
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
2024-06-25 07:09:18 +00:00
|
|
|
sourceID: source.ID,
|
2024-06-22 23:18:23 +00:00
|
|
|
},
|
|
|
|
want: post,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
sourceID: "1234",
|
|
|
|
},
|
|
|
|
want: nil,
|
2024-06-25 20:22:05 +00:00
|
|
|
wantErr: true,
|
2024-06-22 23:18:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: No sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
sourceID: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
2024-06-25 20:22:05 +00:00
|
|
|
wantErr: true,
|
2024-06-22 23:18:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := GetPostBySourceID(tt.args.ctx, tt.args.db, tt.args.sourceID)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetPostBySourceID() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !checkPost(got, tt.want) {
|
|
|
|
t.Errorf("GetPostBySourceID() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-06 20:19:55 +00:00
|
|
|
func TestCreatePostInBatch(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 Tests
|
|
|
|
|
|
|
|
validPosts := []models.Post{
|
|
|
|
{
|
|
|
|
Rating: models.SFW,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rating: models.NSFW,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rating: models.Questionable,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyPost := []models.Post{}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
|
|
|
anthrovePost []models.Post
|
|
|
|
batchSize int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthrovePost: validPosts,
|
|
|
|
batchSize: len(validPosts),
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Emtpy Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthrovePost: emptyPost,
|
|
|
|
batchSize: 0,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Nil Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthrovePost: nil,
|
|
|
|
batchSize: 0,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: batchSize 0",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthrovePost: validPosts,
|
|
|
|
batchSize: 0,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if err := CreatePostInBatch(tt.args.ctx, tt.args.db, tt.args.anthrovePost, tt.args.batchSize); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreatePostInBatch() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
func checkPost(got *models.Post, want *models.Post) bool {
|
|
|
|
|
2024-06-24 20:07:34 +00:00
|
|
|
if got == nil && want == nil {
|
2024-06-22 23:18:23 +00:00
|
|
|
return true
|
2024-06-24 20:07:34 +00:00
|
|
|
} else if got == nil || want == nil {
|
|
|
|
return false
|
2024-06-22 23:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if got.ID != want.ID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.Rating != want.Rating {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|