fix(errors): more tests

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-26 11:23:27 +02:00
parent 6e3edfc1fa
commit cb04347dd5
7 changed files with 336 additions and 39 deletions

View File

@ -63,6 +63,15 @@ func TestCreateAnthrovePostNode(t *testing.T) {
}, },
wantErr: true, wantErr: true,
}, },
{
name: "Test 3: Nill",
args: args{
ctx: context.Background(),
db: gormDB,
anthrovePost: nil,
},
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View File

@ -3,10 +3,11 @@ package postgres
import ( import (
"context" "context"
"fmt" "fmt"
"testing"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test" "git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm" "gorm.io/gorm"
"testing"
) )
func TestCheckUserToPostLink(t *testing.T) { func TestCheckUserToPostLink(t *testing.T) {
@ -105,6 +106,28 @@ func TestCheckUserToPostLink(t *testing.T) {
want: false, want: false,
wantErr: true, wantErr: true,
}, },
{
name: "Test 5: No AnthrovePostID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
anthrovePostID: "123456",
},
want: false,
wantErr: true,
},
{
name: "Test 6: No anthrovePostID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "",
},
want: false,
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -120,7 +143,8 @@ func TestCheckUserToPostLink(t *testing.T) {
} }
} }
func TestEstablishAnthrovePostToSourceLink(t *testing.T) { func TestCheckUserToPostLinkWithNoData(t *testing.T) {
// Setup trow away container // Setup trow away container
ctx := context.Background() ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx) container, gormDB, err := test.StartPostgresContainer(ctx)
@ -130,9 +154,20 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
defer container.Terminate(ctx) defer container.Terminate(ctx)
// Setup Test // Setup Test
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
invalidUserID := models.AnthroveUserID("XXX")
validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
err = CreateUser(ctx, gormDB, validUserID)
if err != nil {
t.Fatal(err)
}
post := &models.Post{ post := &models.Post{
BaseModel: models.BaseModel[models.AnthrovePostID]{ BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), ID: validPostID,
}, },
Rating: "safe", Rating: "safe",
} }
@ -142,74 +177,100 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
source := &models.Source{ err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID)
DisplayName: "e621",
Domain: "e621.net",
Icon: "icon.e621.net",
}
err = CreateSource(ctx, gormDB, source)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Test // Test
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthrovePostID models.AnthrovePostID anthroveUserID models.AnthroveUserID
sourceDomain models.AnthroveSourceDomain anthrovePostID models.AnthrovePostID
anthrovePostRelationship *models.PostReference
} }
tests := []struct { tests := []struct {
name string name string
args args args args
want bool
wantErr bool wantErr bool
}{ }{
{ {
name: "Test 1: Valid AnthrovePostID and anthroveSourceDomain", name: "Test 1: Valid AnthroveUserID and AnthrovePostID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: validUserID,
anthrovePostID: post.ID, anthrovePostID: post.ID,
sourceDomain: "e621.net",
}, },
want: true,
wantErr: false, wantErr: false,
}, },
{ {
name: "Test 2: Invalid AnthrovePostID and Valid anthroveSourceDomain", name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: validUserID,
anthrovePostID: "qadw",
},
want: false,
wantErr: true,
},
{
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: post.ID,
},
want: false,
wantErr: true,
},
{
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "123456", anthrovePostID: "123456",
sourceDomain: "e621.net",
}, },
want: false,
wantErr: true, wantErr: true,
}, },
{ {
name: "Test 3: Invalid anthroveSourceDomain and Valid AnthrovePostID", name: "Test 5: No AnthrovePostID given",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: "1234", anthroveUserID: "",
sourceDomain: "fa.banana", anthrovePostID: "123456",
}, },
want: false,
wantErr: true, wantErr: true,
}, },
{ {
name: "Test 4: Invalid anthroveSourceDomain and Invalid AnthrovePostID", name: "Test 6: No anthrovePostID given",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: "696969", anthroveUserID: invalidUserID,
sourceDomain: "hehe.funny.number", anthrovePostID: "",
}, },
want: false,
wantErr: true, wantErr: true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.sourceDomain); (err != nil) != tt.wantErr { got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID)
t.Errorf("CreateReferenceBetweenPostAndSource() error = %v, wantErr %v", err, tt.wantErr) if (err != nil) != tt.wantErr {
t.Errorf("CheckReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CheckReferenceBetweenUserAndPost() got = %v, want %v", got, tt.want)
} }
}) })
} }
@ -300,6 +361,26 @@ func TestEstablishUserToPostLink(t *testing.T) {
}, },
wantErr: true, wantErr: true,
}, },
{
name: "Test 5: AnthrovePostID is empty",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "",
},
wantErr: true,
},
{
name: "Test 6: anthroveUserID is empty",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
anthrovePostID: validPostID,
},
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View File

@ -3,10 +3,11 @@ package postgres
import ( import (
"context" "context"
"fmt" "fmt"
"testing"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test" "git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm" "gorm.io/gorm"
"testing"
) )
func TestCreateSourceNode(t *testing.T) { func TestCreateSourceNode(t *testing.T) {
@ -30,7 +31,13 @@ func TestCreateSourceNode(t *testing.T) {
} }
invalidSource := &models.Source{ invalidSource := &models.Source{
Domain: "", BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
Domain: "notfound.intern",
}
invalidSourceDomain := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
Domain: "",
} }
// Test // Test
@ -58,7 +65,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveSource: invalidSource, anthroveSource: invalidSourceDomain,
}, },
wantErr: true, wantErr: true,
}, },
@ -67,7 +74,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveSource: validSource, anthroveSource: invalidSource,
}, },
wantErr: true, wantErr: true,
}, },
@ -152,6 +159,50 @@ func TestGetAllSourceNodes(t *testing.T) {
} }
} }
func TestGetAllSourceWithNoData(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)
// Test
type args struct {
ctx context.Context
db *gorm.DB
}
tests := []struct {
name string
args args
want []models.Source
wantErr bool
}{
{
name: "Test 1: Get all entries",
args: args{
ctx: ctx,
db: gormDB,
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAllSource(tt.args.ctx, tt.args.db)
if (err != nil) != tt.wantErr {
t.Errorf("GetAllSource() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkSourcesNode(got, tt.want) {
t.Errorf("GetAllSource() got = %v, want %v", got, tt.want)
}
})
}
}
func TestGetSourceNodesByURL(t *testing.T) { func TestGetSourceNodesByURL(t *testing.T) {
// Setup trow away container // Setup trow away container
ctx := context.Background() ctx := context.Background()

View File

@ -16,7 +16,7 @@ func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
return &otterError.EntityValidationFailed{Reason: "Tag is nil"} return &otterError.EntityValidationFailed{Reason: "Tag is nil"}
} }
result := db.WithContext(ctx).Where(tag).Create(tag) result := db.WithContext(ctx).Create(tag)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{} return &otterError.EntityAlreadyExists{}
@ -77,7 +77,6 @@ func GetTags(ctx context.Context, db *gorm.DB) ([]models.Tag, error) {
var tags []models.Tag var tags []models.Tag
result := db.WithContext(ctx).Find(&tags) result := db.WithContext(ctx).Find(&tags)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{} return nil, &otterError.NoDataFound{}

View File

@ -3,10 +3,11 @@ package postgres
import ( import (
"context" "context"
"fmt" "fmt"
"testing"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test" "git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm" "gorm.io/gorm"
"testing"
) )
func TestCreateTagNodeWitRelation(t *testing.T) { func TestCreateTagNodeWitRelation(t *testing.T) {
@ -54,7 +55,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
PostID: models.AnthrovePostID(post.ID), PostID: post.ID,
tag: tag, tag: tag,
}, },
wantErr: false, wantErr: false,
@ -64,7 +65,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
PostID: models.AnthrovePostID(post.ID), PostID: post.ID,
tag: nil, tag: nil,
}, },
wantErr: true, wantErr: true,
@ -180,3 +181,68 @@ func checkTag(got []models.Tag, want []models.Tag) bool {
return true return true
} }
func TestCreateTag(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
validTag := models.Tag{
Name: "JayTheFerret",
Type: "artist",
}
invalidTag := models.Tag{}
// Test
type args struct {
ctx context.Context
db *gorm.DB
tag *models.Tag
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid Tag",
args: args{
ctx: ctx,
db: gormDB,
tag: &validTag,
},
wantErr: false,
},
{
name: "Test 2: Duplicate Tag",
args: args{
ctx: ctx,
db: gormDB,
tag: &validTag,
},
wantErr: true,
},
{
name: "Test 3: Invalid Tag",
args: args{
ctx: ctx,
db: gormDB,
tag: &invalidTag,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateTag(tt.args.ctx, tt.args.db, tt.args.tag); (err != nil) != tt.wantErr {
t.Errorf("CreateTag() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -34,6 +34,10 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove
return result.Error return result.Error
} }
if result.RowsAffected == 0 {
return &otterError.EntityAlreadyExists{}
}
return nil return nil
} }
@ -325,7 +329,10 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov
for _, userFavorite := range userFavorites { for _, userFavorite := range userFavorites {
var post models.Post var post models.Post
result = db.WithContext(ctx).Preload("Tags").First(&post, "id = ?", userFavorite.PostID) result = db.WithContext(ctx).Preload("Tags", func(db *gorm.DB) *gorm.DB {
return db.Order("tag_type ASC")
}).First(&post, "id = ?", userFavorite.PostID)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{} return nil, &otterError.NoDataFound{}

View File

@ -54,6 +54,15 @@ func TestCreateUser(t *testing.T) {
}, },
wantErr: true, wantErr: true,
}, },
{
name: "Test 3: No anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
},
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -347,7 +356,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
wantErr: true, wantErr: true,
}, },
{ {
name: "Test 5: Valid AnthroveUserID and No SourceDisplayName", name: "Test 5: Valid AnthroveUserID and No anthroveUserID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
@ -358,7 +367,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
wantErr: true, wantErr: true,
}, },
{ {
name: "Test 6: No AnthroveUserID and No SourceDisplayName", name: "Test 6: No AnthroveUserID and No anthroveUserID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
@ -368,6 +377,17 @@ func TestGetUserSourceBySourceID(t *testing.T) {
want: nil, want: nil,
wantErr: true, wantErr: true,
}, },
{
name: "Test 7: No anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: validUserID,
sourceID: "",
},
want: nil,
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -505,6 +525,30 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
want: expectedResult3, want: expectedResult3,
wantErr: false, wantErr: false,
}, },
{
name: "Test 4: No anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
skip: 0,
limit: 3,
},
want: nil,
wantErr: true,
},
{
name: "Test 5: Short anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "aaa",
skip: 0,
limit: 3,
},
want: nil,
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -725,6 +769,26 @@ func TestGetUserSourceLinks(t *testing.T) {
want: expectedResult, want: expectedResult,
wantErr: false, wantErr: false,
}, },
{
name: "Test 3: No AnthroveID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
},
want: nil,
wantErr: true,
},
{
name: "Test 1: AnthroveID to short",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "aaa",
},
want: nil,
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -772,7 +836,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, models.AnthrovePostID(post.ID)) err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, post.ID)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -837,6 +901,26 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
want: expectedResult, want: expectedResult,
wantErr: false, wantErr: false,
}, },
{
name: "Test 2: No anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
},
want: nil,
wantErr: true,
},
{
name: "Test 3: short anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "aaa",
},
want: nil,
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {