tests(errors): fix test cases to reflect the new validation and error handling
Some checks failed
Gitea Build Check / Build (push) Failing after 5m49s
Some checks failed
Gitea Build Check / Build (push) Failing after 5m49s
Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
parent
d94ae83c2f
commit
f66fc6b6d7
@ -223,7 +223,7 @@ func TestGetPostBySourceURL(t *testing.T) {
|
|||||||
sourceURL: "1234",
|
sourceURL: "1234",
|
||||||
},
|
},
|
||||||
want: nil,
|
want: nil,
|
||||||
wantErr: false,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test 3: No sourceURL",
|
name: "Test 3: No sourceURL",
|
||||||
@ -233,7 +233,7 @@ func TestGetPostBySourceURL(t *testing.T) {
|
|||||||
sourceURL: "",
|
sourceURL: "",
|
||||||
},
|
},
|
||||||
want: nil,
|
want: nil,
|
||||||
wantErr: false,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@ -323,7 +323,7 @@ func TestGetPostBySourceID(t *testing.T) {
|
|||||||
sourceID: "1234",
|
sourceID: "1234",
|
||||||
},
|
},
|
||||||
want: nil,
|
want: nil,
|
||||||
wantErr: false,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test 3: No sourceID",
|
name: "Test 3: No sourceID",
|
||||||
@ -333,7 +333,7 @@ func TestGetPostBySourceID(t *testing.T) {
|
|||||||
sourceID: "",
|
sourceID: "",
|
||||||
},
|
},
|
||||||
want: nil,
|
want: nil,
|
||||||
wantErr: false,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
@ -113,6 +113,10 @@ func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrove
|
|||||||
return false, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
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)
|
result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||||
|
@ -20,14 +20,20 @@ func TestCheckUserToPostLink(t *testing.T) {
|
|||||||
defer container.Terminate(ctx)
|
defer container.Terminate(ctx)
|
||||||
|
|
||||||
// Setup Test
|
// 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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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",
|
||||||
}
|
}
|
||||||
@ -37,7 +43,7 @@ func TestCheckUserToPostLink(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(post.ID))
|
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -60,7 +66,7 @@ func TestCheckUserToPostLink(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "1",
|
anthroveUserID: validUserID,
|
||||||
anthrovePostID: post.ID,
|
anthrovePostID: post.ID,
|
||||||
},
|
},
|
||||||
want: true,
|
want: true,
|
||||||
@ -71,33 +77,33 @@ func TestCheckUserToPostLink(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "1",
|
anthroveUserID: validUserID,
|
||||||
anthrovePostID: "qadw",
|
anthrovePostID: "qadw",
|
||||||
},
|
},
|
||||||
want: false,
|
want: false,
|
||||||
wantErr: false,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
|
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
|
||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "123",
|
anthroveUserID: invalidUserID,
|
||||||
anthrovePostID: post.ID,
|
anthrovePostID: post.ID,
|
||||||
},
|
},
|
||||||
want: false,
|
want: false,
|
||||||
wantErr: false,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
|
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
|
||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "123",
|
anthroveUserID: invalidUserID,
|
||||||
anthrovePostID: "123456",
|
anthrovePostID: "123456",
|
||||||
},
|
},
|
||||||
want: false,
|
want: false,
|
||||||
wantErr: false,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@ -219,14 +225,20 @@ func TestEstablishUserToPostLink(t *testing.T) {
|
|||||||
defer container.Terminate(ctx)
|
defer container.Terminate(ctx)
|
||||||
|
|
||||||
// Setup Test
|
// 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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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",
|
||||||
}
|
}
|
||||||
@ -253,7 +265,7 @@ func TestEstablishUserToPostLink(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "1",
|
anthroveUserID: validUserID,
|
||||||
anthrovePostID: post.ID,
|
anthrovePostID: post.ID,
|
||||||
},
|
},
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
@ -263,18 +275,18 @@ func TestEstablishUserToPostLink(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "1",
|
anthroveUserID: validUserID,
|
||||||
anthrovePostID: "123456",
|
anthrovePostID: "123456",
|
||||||
},
|
},
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
|
name: "Test 3: invalid AnthroveUserID and valid AnthrovePostID",
|
||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "123",
|
anthroveUserID: invalidUserID,
|
||||||
anthrovePostID: "1234",
|
anthrovePostID: post.ID,
|
||||||
},
|
},
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
@ -283,7 +295,7 @@ func TestEstablishUserToPostLink(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "123",
|
anthroveUserID: invalidUserID,
|
||||||
anthrovePostID: "123456",
|
anthrovePostID: "123456",
|
||||||
},
|
},
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
|
@ -2,6 +2,7 @@ package postgres
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"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"
|
||||||
@ -19,7 +20,10 @@ func TestCreateSourceNode(t *testing.T) {
|
|||||||
|
|
||||||
// Setup Test
|
// Setup Test
|
||||||
|
|
||||||
|
validPostID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Post1"))
|
||||||
|
|
||||||
validSource := &models.Source{
|
validSource := &models.Source{
|
||||||
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
|
||||||
DisplayName: "e621",
|
DisplayName: "e621",
|
||||||
Domain: "e621.net",
|
Domain: "e621.net",
|
||||||
Icon: "icon.e621.net",
|
Icon: "icon.e621.net",
|
||||||
@ -65,7 +69,7 @@ func TestCreateSourceNode(t *testing.T) {
|
|||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveSource: validSource,
|
anthroveSource: validSource,
|
||||||
},
|
},
|
||||||
wantErr: false,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
@ -21,6 +21,9 @@ func TestCreateUser(t *testing.T) {
|
|||||||
|
|
||||||
// Setup Test
|
// Setup Test
|
||||||
|
|
||||||
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
||||||
|
invalidUserID := models.AnthroveUserID("XXX")
|
||||||
|
|
||||||
// Test
|
// Test
|
||||||
type args struct {
|
type args struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
@ -37,7 +40,7 @@ func TestCreateUser(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "1",
|
anthroveUserID: validUserID,
|
||||||
},
|
},
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
@ -46,7 +49,7 @@ func TestCreateUser(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "",
|
anthroveUserID: invalidUserID,
|
||||||
},
|
},
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
@ -71,9 +74,14 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
|
|||||||
|
|
||||||
// Setup Test
|
// Setup Test
|
||||||
|
|
||||||
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
||||||
|
invalidUserID := models.AnthroveUserID("XXX")
|
||||||
|
|
||||||
|
validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
|
||||||
|
|
||||||
source := &models.Source{
|
source := &models.Source{
|
||||||
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
||||||
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
|
ID: validSourceID,
|
||||||
},
|
},
|
||||||
DisplayName: "e621",
|
DisplayName: "e621",
|
||||||
Domain: "e621.net",
|
Domain: "e621.net",
|
||||||
@ -103,7 +111,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "1",
|
anthroveUserID: validUserID,
|
||||||
sourceID: source.ID,
|
sourceID: source.ID,
|
||||||
userID: "e1",
|
userID: "e1",
|
||||||
username: "marius",
|
username: "marius",
|
||||||
@ -115,7 +123,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "2",
|
anthroveUserID: invalidUserID,
|
||||||
sourceID: source.ID,
|
sourceID: source.ID,
|
||||||
userID: "e1",
|
userID: "e1",
|
||||||
username: "marius",
|
username: "marius",
|
||||||
@ -139,7 +147,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "1",
|
anthroveUserID: validUserID,
|
||||||
sourceID: "fa.net",
|
sourceID: "fa.net",
|
||||||
userID: "e1",
|
userID: "e1",
|
||||||
username: "marius",
|
username: "marius",
|
||||||
@ -151,7 +159,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "1",
|
anthroveUserID: validUserID,
|
||||||
sourceID: source.ID,
|
sourceID: source.ID,
|
||||||
userID: "",
|
userID: "",
|
||||||
username: "marius",
|
username: "marius",
|
||||||
@ -163,7 +171,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "1",
|
anthroveUserID: validUserID,
|
||||||
sourceID: source.ID,
|
sourceID: source.ID,
|
||||||
userID: "aa",
|
userID: "aa",
|
||||||
username: "",
|
username: "",
|
||||||
@ -190,8 +198,11 @@ func TestGetAllAnthroveUserIDs(t *testing.T) {
|
|||||||
defer container.Terminate(ctx)
|
defer container.Terminate(ctx)
|
||||||
|
|
||||||
// Setup Test
|
// 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 {
|
for _, user := range users {
|
||||||
err = CreateUser(ctx, gormDB, user)
|
err = CreateUser(ctx, gormDB, user)
|
||||||
@ -246,6 +257,9 @@ func TestGetUserSourceBySourceID(t *testing.T) {
|
|||||||
|
|
||||||
// Setup Test
|
// Setup Test
|
||||||
|
|
||||||
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
||||||
|
invalidUserID := models.AnthroveUserID("XXX")
|
||||||
|
|
||||||
expectedResult := make(map[string]models.UserSource)
|
expectedResult := make(map[string]models.UserSource)
|
||||||
expectedResult["e621"] = models.UserSource{
|
expectedResult["e621"] = models.UserSource{
|
||||||
UserID: "e1",
|
UserID: "e1",
|
||||||
@ -269,7 +283,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
|
|||||||
t.Fatal(err)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -292,7 +306,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "1",
|
anthroveUserID: validUserID,
|
||||||
sourceID: source.ID,
|
sourceID: source.ID,
|
||||||
},
|
},
|
||||||
want: expectedResult,
|
want: expectedResult,
|
||||||
@ -303,7 +317,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "22",
|
anthroveUserID: invalidUserID,
|
||||||
sourceID: source.ID,
|
sourceID: source.ID,
|
||||||
},
|
},
|
||||||
want: nil,
|
want: nil,
|
||||||
@ -314,7 +328,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
db: gormDB,
|
db: gormDB,
|
||||||
anthroveUserID: "1",
|
anthroveUserID: validUserID,
|
||||||
sourceID: "fa",
|
sourceID: "fa",
|
||||||
},
|
},
|
||||||
want: nil,
|
want: nil,
|
||||||
@ -368,6 +382,8 @@ func TestGetUserSourceBySourceID(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: FIX THE FOUR REMAINING TESTS
|
||||||
|
|
||||||
func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
|
func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
|
||||||
// Setup trow away containert
|
// Setup trow away containert
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
Reference in New Issue
Block a user