otter-space-sdk/internal/postgres/post_test.go

470 lines
9.6 KiB
Go

package postgres
import (
"context"
"fmt"
"testing"
_ "github.com/lib/pq"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/test"
"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)
// Setup Tests
validPost := &models.Post{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
},
Rating: "safe",
}
invalidPost := &models.Post{
Rating: "error",
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthrovePost *models.Post
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid AnthrovePostID and Rating",
args: args{
ctx: context.Background(),
db: gormDB,
anthrovePost: validPost,
},
wantErr: false,
},
{
name: "Test 2: Invalid Rating",
args: args{
ctx: context.Background(),
db: gormDB,
anthrovePost: invalidPost,
},
wantErr: true,
},
{
name: "Test 3: Nill",
args: args{
ctx: context.Background(),
db: gormDB,
anthrovePost: nil,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
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)
}
})
}
}
func TestGetPostByAnthroveID(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[models.AnthrovePostID]{
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil {
t.Fatal("Could not create post", err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthrovePostID models.AnthrovePostID
}
tests := []struct {
name string
args args
want *models.Post
wantErr bool
}{
{
name: "Test 1: Valid anthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: post.ID,
},
want: post,
wantErr: false,
},
{
name: "Test 2: Invalid anthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: "1234",
},
want: nil,
wantErr: true,
},
{
name: "Test 3: No anthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: "",
},
want: nil,
wantErr: true,
},
}
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[models.AnthrovePostID]{
ID: models.AnthrovePostID(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[models.AnthroveSourceID]{
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
},
DisplayName: "e621",
Domain: "e621.net",
Icon: "https://e621.net/icon.ico",
}
err = CreateSource(ctx, gormDB, &source)
if err != nil {
t.Fatal("Could not create source", err)
}
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.org", models.PostReferenceConfig{})
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: "http://test.org",
},
want: post,
wantErr: false,
},
{
name: "Test 2: Invalid sourceURL",
args: args{
ctx: ctx,
db: gormDB,
sourceURL: "1234",
},
want: nil,
wantErr: true,
},
{
name: "Test 3: No sourceURL",
args: args{
ctx: ctx,
db: gormDB,
sourceURL: "",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetPostBySourceURL(tt.args.ctx, tt.args.db, tt.args.sourceURL)
if (err != nil) != tt.wantErr {
t.Errorf("GetPostBySourceURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkPost(got, tt.want) {
t.Errorf("GetPostBySourceURL() got = %v, want %v", got, tt.want)
}
})
}
}
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[models.AnthrovePostID]{
ID: models.AnthrovePostID(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[models.AnthroveSourceID]{
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
},
DisplayName: "e621",
Domain: "e621.net",
Icon: "https://e621.net/icon.ico",
}
err = CreateSource(ctx, gormDB, &source)
if err != nil {
t.Fatal("Could not create source", err)
}
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.otg", models.PostReferenceConfig{})
if err != nil {
t.Fatal("Could not create source reference", err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
sourceID models.AnthroveSourceID
}
tests := []struct {
name string
args args
want *models.Post
wantErr bool
}{
{
name: "Test 1: Valid sourceID",
args: args{
ctx: ctx,
db: gormDB,
sourceID: source.ID,
},
want: post,
wantErr: false,
},
{
name: "Test 2: Invalid sourceID",
args: args{
ctx: ctx,
db: gormDB,
sourceID: "1234",
},
want: nil,
wantErr: true,
},
{
name: "Test 3: No sourceID",
args: args{
ctx: ctx,
db: gormDB,
sourceID: "",
},
want: nil,
wantErr: true,
},
}
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 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)
}
})
}
}
func checkPost(got *models.Post, want *models.Post) bool {
if got == nil && want == nil {
return true
} else if got == nil || want == nil {
return false
}
if got.ID != want.ID {
return false
}
if got.Rating != want.Rating {
return false
}
return true
}