tests(errors): fix test cases to reflect the new validation and error handling
Some checks failed
Gitea Build Check / Build (push) Failing after 5m49s

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-25 22:22:05 +02:00
parent d94ae83c2f
commit f66fc6b6d7
5 changed files with 72 additions and 36 deletions

View File

@ -223,7 +223,7 @@ func TestGetPostBySourceURL(t *testing.T) {
sourceURL: "1234",
},
want: nil,
wantErr: false,
wantErr: true,
},
{
name: "Test 3: No sourceURL",
@ -233,7 +233,7 @@ func TestGetPostBySourceURL(t *testing.T) {
sourceURL: "",
},
want: nil,
wantErr: false,
wantErr: true,
},
}
for _, tt := range tests {
@ -323,7 +323,7 @@ func TestGetPostBySourceID(t *testing.T) {
sourceID: "1234",
},
want: nil,
wantErr: false,
wantErr: true,
},
{
name: "Test 3: No sourceID",
@ -333,7 +333,7 @@ func TestGetPostBySourceID(t *testing.T) {
sourceID: "",
},
want: nil,
wantErr: false,
wantErr: true,
},
}
for _, tt := range tests {

View File

@ -113,6 +113,10 @@ func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrove
return false, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
}
if len(anthroveUserID) != 25 {
return false, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
}
result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {

View File

@ -20,14 +20,20 @@ func TestCheckUserToPostLink(t *testing.T) {
defer container.Terminate(ctx)
// Setup Test
err = CreateUser(ctx, gormDB, "1")
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{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
ID: validPostID,
},
Rating: "safe",
}
@ -37,7 +43,7 @@ func TestCheckUserToPostLink(t *testing.T) {
t.Fatal(err)
}
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(post.ID))
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID)
if err != nil {
t.Fatal(err)
}
@ -60,7 +66,7 @@ func TestCheckUserToPostLink(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthroveUserID: validUserID,
anthrovePostID: post.ID,
},
want: true,
@ -71,33 +77,33 @@ func TestCheckUserToPostLink(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthroveUserID: validUserID,
anthrovePostID: "qadw",
},
want: false,
wantErr: false,
wantErr: true,
},
{
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "123",
anthroveUserID: invalidUserID,
anthrovePostID: post.ID,
},
want: false,
wantErr: false,
wantErr: true,
},
{
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "123",
anthroveUserID: invalidUserID,
anthrovePostID: "123456",
},
want: false,
wantErr: false,
wantErr: true,
},
}
for _, tt := range tests {
@ -219,14 +225,20 @@ func TestEstablishUserToPostLink(t *testing.T) {
defer container.Terminate(ctx)
// Setup Test
err = CreateUser(ctx, gormDB, "1")
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{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
ID: validPostID,
},
Rating: "safe",
}
@ -253,7 +265,7 @@ func TestEstablishUserToPostLink(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthroveUserID: validUserID,
anthrovePostID: post.ID,
},
wantErr: false,
@ -263,18 +275,18 @@ func TestEstablishUserToPostLink(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthroveUserID: validUserID,
anthrovePostID: "123456",
},
wantErr: true,
},
{
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
name: "Test 3: invalid AnthroveUserID and valid AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "123",
anthrovePostID: "1234",
anthroveUserID: invalidUserID,
anthrovePostID: post.ID,
},
wantErr: true,
},
@ -283,7 +295,7 @@ func TestEstablishUserToPostLink(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "123",
anthroveUserID: invalidUserID,
anthrovePostID: "123456",
},
wantErr: true,

View File

@ -2,6 +2,7 @@ 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"
@ -19,7 +20,10 @@ func TestCreateSourceNode(t *testing.T) {
// Setup Test
validPostID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Post1"))
validSource := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
DisplayName: "e621",
Domain: "e621.net",
Icon: "icon.e621.net",
@ -65,7 +69,7 @@ func TestCreateSourceNode(t *testing.T) {
db: gormDB,
anthroveSource: validSource,
},
wantErr: false,
wantErr: true,
},
}
for _, tt := range tests {

View File

@ -21,6 +21,9 @@ func TestCreateUser(t *testing.T) {
// Setup Test
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
invalidUserID := models.AnthroveUserID("XXX")
// Test
type args struct {
ctx context.Context
@ -37,7 +40,7 @@ func TestCreateUser(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthroveUserID: validUserID,
},
wantErr: false,
},
@ -46,7 +49,7 @@ func TestCreateUser(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
anthroveUserID: invalidUserID,
},
wantErr: true,
},
@ -71,9 +74,14 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
// Setup Test
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
invalidUserID := models.AnthroveUserID("XXX")
validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
source := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
ID: validSourceID,
},
DisplayName: "e621",
Domain: "e621.net",
@ -103,7 +111,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthroveUserID: validUserID,
sourceID: source.ID,
userID: "e1",
username: "marius",
@ -115,7 +123,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "2",
anthroveUserID: invalidUserID,
sourceID: source.ID,
userID: "e1",
username: "marius",
@ -139,7 +147,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthroveUserID: validUserID,
sourceID: "fa.net",
userID: "e1",
username: "marius",
@ -151,7 +159,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthroveUserID: validUserID,
sourceID: source.ID,
userID: "",
username: "marius",
@ -163,7 +171,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthroveUserID: validUserID,
sourceID: source.ID,
userID: "aa",
username: "",
@ -190,8 +198,11 @@ func TestGetAllAnthroveUserIDs(t *testing.T) {
defer container.Terminate(ctx)
// Setup Test
validUserID01 := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
validUserID02 := models.AnthroveUserID(fmt.Sprintf("%025s", "User2"))
validUserID03 := models.AnthroveUserID(fmt.Sprintf("%025s", "User3"))
users := []models.AnthroveUserID{"1", "2", "3"}
users := []models.AnthroveUserID{validUserID01, validUserID02, validUserID03}
for _, user := range users {
err = CreateUser(ctx, gormDB, user)
@ -246,6 +257,9 @@ func TestGetUserSourceBySourceID(t *testing.T) {
// Setup Test
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
invalidUserID := models.AnthroveUserID("XXX")
expectedResult := make(map[string]models.UserSource)
expectedResult["e621"] = models.UserSource{
UserID: "e1",
@ -269,7 +283,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
t.Fatal(err)
}
err = CreateUserWithRelationToSource(ctx, gormDB, "1", models.AnthroveSourceID(expectedResult["e621"].SourceID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, models.AnthroveSourceID(expectedResult["e621"].SourceID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
if err != nil {
t.Fatal(err)
}
@ -292,7 +306,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthroveUserID: validUserID,
sourceID: source.ID,
},
want: expectedResult,
@ -303,7 +317,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "22",
anthroveUserID: invalidUserID,
sourceID: source.ID,
},
want: nil,
@ -314,7 +328,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthroveUserID: validUserID,
sourceID: "fa",
},
want: nil,
@ -368,6 +382,8 @@ func TestGetUserSourceBySourceID(t *testing.T) {
}
}
// TODO: FIX THE FOUR REMAINING TESTS
func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
// Setup trow away containert
ctx := context.Background()