otter-space-sdk/internal/postgres/tag_test.go
2024-06-23 22:35:46 +02:00

183 lines
3.3 KiB
Go

package postgres
import (
"context"
"fmt"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm"
"testing"
)
func TestCreateTagNodeWitRelation(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
post := &models.Post{
BaseModel: models.BaseModel{
ID: fmt.Sprintf("%025s", "1"),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil {
t.Fatal(err)
}
tag := &models.Tag{
Name: "JayTheFerret",
Type: "artist",
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
PostID models.AnthrovePostID
tag *models.Tag
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid PostID and Tag",
args: args{
ctx: ctx,
db: gormDB,
PostID: models.AnthrovePostID(post.ID),
tag: tag,
},
wantErr: false,
},
{
name: "Test 2: Valid PostID and no Tag",
args: args{
ctx: ctx,
db: gormDB,
PostID: models.AnthrovePostID(post.ID),
tag: nil,
},
wantErr: true,
},
{
name: "Test 3: Invalid PostID and valid Tag",
args: args{
ctx: ctx,
db: gormDB,
PostID: "123456",
tag: tag,
},
wantErr: true,
},
{
name: "Test 4: No PostID and valid Tag",
args: args{
ctx: ctx,
db: gormDB,
PostID: "",
tag: tag,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateTagAndReferenceToPost(tt.args.ctx, tt.args.db, tt.args.PostID, tt.args.tag); (err != nil) != tt.wantErr {
t.Errorf("CreateTagAndReferenceToPost() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetTags(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
tags := []models.Tag{
{
Name: "JayTheFerret",
Type: "artist",
},
{
Name: "anthro",
Type: "general",
},
{
Name: "soxx",
Type: "character",
},
}
for _, tag := range tags {
err = CreateTag(ctx, gormDB, &tag)
if err != nil {
t.Fatal(err)
}
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
}
tests := []struct {
name string
args args
want []models.Tag
wantErr bool
}{
{
name: "Test 1: Get Tags",
args: args{
ctx: ctx,
db: gormDB,
},
want: tags,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetTags(tt.args.ctx, tt.args.db)
if (err != nil) != tt.wantErr {
t.Errorf("GetTags() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkTag(got, tt.want) {
t.Errorf("GetTags() got = %v, want %v", got, tt.want)
}
})
}
}
func checkTag(got []models.Tag, want []models.Tag) bool {
for i, tag := range want {
if tag.Type != got[i].Type {
return false
}
if tag.Name != got[i].Name {
return false
}
}
return true
}