test(postgres): added tag and fixed bugs

Signed-off-by: soxx <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-20 14:54:06 +02:00 committed by soxx
parent e6931570c3
commit 83a5c89438
2 changed files with 196 additions and 0 deletions

View File

@ -2,14 +2,35 @@ package postgres
import (
"context"
"fmt"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func CreateTag(ctx context.Context, db *gorm.DB, tag *pgModels.Tag) error {
resultTag := db.WithContext(ctx).Where(tag).Create(tag)
if resultTag.Error != nil {
return resultTag.Error
}
log.WithFields(log.Fields{
"tag_name": tag.Name,
"tag_type": tag.Type,
}).Trace("database: created tag node")
return nil
}
func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.AnthrovePostID, tag *pgModels.Tag) error {
if PostID == "" {
return fmt.Errorf("PostID is empty")
}
resultTag := db.WithContext(ctx).Where(tag).FirstOrCreate(tag)
if resultTag.Error != nil {

View File

@ -0,0 +1,175 @@
package postgres
import (
"context"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels"
"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
err = CreateAnthrovePostNode(ctx, gormDB, "1234", "safe")
if err != nil {
t.Fatal(err)
}
tag := &pgModels.Tag{
Name: "JayTheFerret",
Type: "artist",
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
PostID models.AnthrovePostID
tag *pgModels.Tag
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid PostID and Tag",
args: args{
ctx: ctx,
db: gormDB,
PostID: "1234",
tag: tag,
},
wantErr: false,
},
{
name: "Test 2: Valid PostID and invalid Tag",
args: args{
ctx: ctx,
db: gormDB,
PostID: "1234",
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 := CreateTagNodeWitRelation(tt.args.ctx, tt.args.db, tt.args.PostID, tt.args.tag); (err != nil) != tt.wantErr {
t.Errorf("CreateTagNodeWitRelation() 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 := []pgModels.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 []pgModels.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 []pgModels.Tag, want []pgModels.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
}