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"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
|
|
|
BaseModel: models.BaseModel{
|
|
|
|
ID: fmt.Sprintf("%025s", "1"),
|
|
|
|
},
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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{
|
|
|
|
BaseModel: models.BaseModel{
|
|
|
|
ID: fmt.Sprintf("%025s", "1"),
|
|
|
|
},
|
|
|
|
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-23 20:35:46 +00:00
|
|
|
anthrovePostID: models.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{
|
|
|
|
BaseModel: models.BaseModel{
|
|
|
|
ID: fmt.Sprintf("%025s", "1"),
|
|
|
|
},
|
|
|
|
Rating: "safe",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CreatePost(ctx, gormDB, post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create post", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
source := models.Source{
|
|
|
|
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")},
|
|
|
|
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-23 20:35:46 +00:00
|
|
|
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, models.AnthrovePostID(post.ID), models.AnthroveSourceDomain(source.Domain))
|
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,
|
|
|
|
sourceURL: source.Domain,
|
|
|
|
},
|
|
|
|
want: post,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid sourceURL",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
sourceURL: "1234",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: No sourceURL",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
sourceURL: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
2024-06-19 21:32:42 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-23 20:35:46 +00:00
|
|
|
got, err := GetPostByURL(tt.args.ctx, tt.args.db, tt.args.sourceURL)
|
2024-06-19 21:32:42 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-23 20:35:46 +00:00
|
|
|
t.Errorf("GetPostByURL() 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-06-23 20:35:46 +00:00
|
|
|
t.Errorf("GetPostByURL() 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{
|
|
|
|
BaseModel: models.BaseModel{
|
|
|
|
ID: fmt.Sprintf("%025s", "1"),
|
|
|
|
},
|
|
|
|
Rating: "safe",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CreatePost(ctx, gormDB, post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create post", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
source := models.Source{
|
|
|
|
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")},
|
|
|
|
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-23 20:35:46 +00:00
|
|
|
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, models.AnthrovePostID(post.ID), models.AnthroveSourceDomain(source.Domain))
|
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-23 20:35:46 +00:00
|
|
|
sourceID: models.AnthroveSourceID(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,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: No sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
sourceID: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkPost(got *models.Post, want *models.Post) bool {
|
|
|
|
|
|
|
|
if got == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.ID != want.ID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.Rating != want.Rating {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|