Compare commits

..

3 Commits

Author SHA1 Message Date
615a6b1a25 fix(errors): fix unneeded error handling
Some checks failed
Gitea Build Check / Build (push) Failing after 5m41s
Signed-off-by: SoXX <soxx@fenpa.ws>
2024-06-26 11:31:42 +02:00
cb04347dd5 fix(errors): more tests
Signed-off-by: SoXX <soxx@fenpa.ws>
2024-06-26 11:23:27 +02:00
6e3edfc1fa fix(errors): renamed error package
Signed-off-by: SoXX <soxx@fenpa.ws>
2024-06-26 10:02:54 +02:00
12 changed files with 373 additions and 123 deletions

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"errors" "errors"
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors" otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -14,19 +14,19 @@ import (
func CreatePost(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) error { func CreatePost(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) error {
if anthrovePost == nil { if anthrovePost == nil {
return &errors2.EntityValidationFailed{Reason: "anthrovePost is nil"} return &otterError.EntityValidationFailed{Reason: "anthrovePost is nil"}
} }
result := db.WithContext(ctx).Create(&anthrovePost) result := db.WithContext(ctx).Create(&anthrovePost)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &errors2.EntityAlreadyExists{} return &otterError.EntityAlreadyExists{}
} }
return result.Error return result.Error
} }
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
return &errors2.NoDataWritten{} return &otterError.NoDataWritten{}
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -40,18 +40,18 @@ func CreatePost(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) err
func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID) (*models.Post, error) { func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID) (*models.Post, error) {
if anthrovePostID == "" { if anthrovePostID == "" {
return nil, &errors2.EntityValidationFailed{Reason: "anthrovePostID is not set"} return nil, &otterError.EntityValidationFailed{Reason: "anthrovePostID is not set"}
} }
if len(anthrovePostID) != 25 { if len(anthrovePostID) != 25 {
return nil, &errors2.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"} return nil, &otterError.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"}
} }
var post models.Post var post models.Post
result := db.WithContext(ctx).First(&post, "id = ?", anthrovePostID) result := db.WithContext(ctx).First(&post, "id = ?", anthrovePostID)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }
@ -62,7 +62,7 @@ func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models
func GetPostByURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, error) { func GetPostByURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, error) {
if sourceURL == "" { if sourceURL == "" {
return nil, &errors2.EntityValidationFailed{Reason: "sourceURL is not set"} return nil, &otterError.EntityValidationFailed{Reason: "sourceURL is not set"}
} }
var post models.Post var post models.Post
@ -70,7 +70,7 @@ func GetPostByURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.P
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }
@ -81,7 +81,7 @@ func GetPostByURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.P
func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID models.AnthroveSourceID) (*models.Post, error) { func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID models.AnthroveSourceID) (*models.Post, error) {
if sourceID == "" { if sourceID == "" {
return nil, &errors2.EntityValidationFailed{Reason: "sourceID is not set"} return nil, &otterError.EntityValidationFailed{Reason: "sourceID is not set"}
} }
var post models.Post var post models.Post
@ -89,7 +89,7 @@ func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID models.Anthrov
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }

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

@ -4,7 +4,7 @@ import (
"context" "context"
"errors" "errors"
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors" otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gorm.io/gorm" "gorm.io/gorm"
@ -14,22 +14,22 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr
var source models.Source var source models.Source
if anthrovePostID == "" { if anthrovePostID == "" {
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
} }
if len(anthrovePostID) != 25 { if len(anthrovePostID) != 25 {
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
if sourceDomain == "" { if sourceDomain == "" {
return &errors2.EntityValidationFailed{Reason: "sourceDomain cannot be empty"} return &otterError.EntityValidationFailed{Reason: "sourceDomain cannot be empty"}
} }
// Find the source // Find the source
result := db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&source) result := db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&source)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &errors2.NoDataFound{} return &otterError.NoDataFound{}
} }
return result.Error return result.Error
} }
@ -43,13 +43,13 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &errors2.EntityAlreadyExists{} return &otterError.EntityAlreadyExists{}
} }
return result.Error return result.Error
} }
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
return &errors2.NoDataWritten{} return &otterError.NoDataWritten{}
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -63,15 +63,15 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr
func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error { func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error {
if anthrovePostID == "" { if anthrovePostID == "" {
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
} }
if len(anthrovePostID) != 25 { if len(anthrovePostID) != 25 {
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
if anthroveUserID == "" { if anthroveUserID == "" {
return &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} return &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
} }
userFavorite := models.UserFavorites{ userFavorite := models.UserFavorites{
@ -82,13 +82,13 @@ func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrov
result := db.WithContext(ctx).Create(&userFavorite) result := db.WithContext(ctx).Create(&userFavorite)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &errors2.EntityAlreadyExists{} return &otterError.EntityAlreadyExists{}
} }
return result.Error return result.Error
} }
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
return &errors2.NoDataWritten{} return &otterError.NoDataWritten{}
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -103,25 +103,25 @@ func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrove
var count int64 var count int64
if anthrovePostID == "" { if anthrovePostID == "" {
return false, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} return false, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
} }
if len(anthrovePostID) != 25 { if len(anthrovePostID) != 25 {
return false, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} return false, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
if anthroveUserID == "" { if anthroveUserID == "" {
return false, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
} }
if len(anthroveUserID) != 25 { if len(anthroveUserID) != 25 {
return false, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} return false, &otterError.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) {
return false, &errors2.NoDataFound{} return false, &otterError.NoDataFound{}
} }
return false, result.Error return false, result.Error
} }

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,12 +177,7 @@ 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)
} }
@ -156,60 +186,91 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthroveUserID models.AnthroveUserID
anthrovePostID models.AnthrovePostID anthrovePostID models.AnthrovePostID
sourceDomain models.AnthroveSourceDomain
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

@ -4,7 +4,7 @@ import (
"context" "context"
"errors" "errors"
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors" otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -15,20 +15,20 @@ import (
func CreateSource(ctx context.Context, db *gorm.DB, anthroveSource *models.Source) error { func CreateSource(ctx context.Context, db *gorm.DB, anthroveSource *models.Source) error {
if anthroveSource.Domain == "" { if anthroveSource.Domain == "" {
return &errors2.EntityValidationFailed{Reason: "Domain is required"} return &otterError.EntityValidationFailed{Reason: "Domain is required"}
} }
result := db.WithContext(ctx).Where(models.Source{Domain: anthroveSource.Domain}).FirstOrCreate(anthroveSource) result := db.WithContext(ctx).Where(models.Source{Domain: anthroveSource.Domain}).FirstOrCreate(anthroveSource)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &errors2.EntityAlreadyExists{} return &otterError.EntityAlreadyExists{}
} }
return result.Error return result.Error
} }
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
return &errors2.NoDataWritten{} return &otterError.NoDataWritten{}
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -48,7 +48,7 @@ func GetAllSource(ctx context.Context, db *gorm.DB) ([]models.Source, error) {
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }
@ -65,14 +65,14 @@ func GetSourceByDomain(ctx context.Context, db *gorm.DB, sourceDomain models.Ant
var sources models.Source var sources models.Source
if sourceDomain == "" { if sourceDomain == "" {
return nil, &errors2.EntityValidationFailed{Reason: "AnthroveSourceDomain is not set"} return nil, &otterError.EntityValidationFailed{Reason: "AnthroveSourceDomain is not set"}
} }
result := db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&sources) result := db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&sources)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }

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,6 +31,12 @@ func TestCreateSourceNode(t *testing.T) {
} }
invalidSource := &models.Source{ invalidSource := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
Domain: "notfound.intern",
}
invalidSourceDomain := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
Domain: "", Domain: "",
} }
@ -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,
}, },

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"errors" "errors"
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors" otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gorm.io/gorm" "gorm.io/gorm"
@ -13,19 +13,19 @@ import (
func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error { func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
if tag == nil { if tag == nil {
return &errors2.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 &errors2.EntityAlreadyExists{} return &otterError.EntityAlreadyExists{}
} }
return result.Error return result.Error
} }
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
return &errors2.NoDataWritten{} return &otterError.NoDataWritten{}
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -39,15 +39,15 @@ func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error { func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error {
if anthrovePostID == "" { if anthrovePostID == "" {
return &errors2.EntityValidationFailed{Reason: "anthrovePostID cannot be empty"} return &otterError.EntityValidationFailed{Reason: "anthrovePostID cannot be empty"}
} }
if len(anthrovePostID) != 25 { if len(anthrovePostID) != 25 {
return &errors2.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"} return &otterError.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"}
} }
if tag == nil { if tag == nil {
return &errors2.EntityValidationFailed{Reason: "Tag is nil"} return &otterError.EntityValidationFailed{Reason: "Tag is nil"}
} }
pgPost := models.Post{ pgPost := models.Post{
@ -59,9 +59,9 @@ func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostI
err := db.WithContext(ctx).Model(&pgPost).Association("Tags").Append(tag) err := db.WithContext(ctx).Model(&pgPost).Association("Tags").Append(tag)
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return &errors2.NoDataFound{} return &otterError.NoDataFound{}
} }
return errors.Join(err, &errors2.NoRelationCreated{}) return errors.Join(err, &otterError.NoRelationCreated{})
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -77,10 +77,9 @@ 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, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }

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

@ -4,7 +4,7 @@ import (
"context" "context"
"errors" "errors"
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors" otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gorm.io/gorm" "gorm.io/gorm"
@ -13,11 +13,11 @@ import (
func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error { func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error {
if anthroveUserID == "" { if anthroveUserID == "" {
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
} }
if len(anthroveUserID) != 25 { if len(anthroveUserID) != 25 {
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
user := models.User{ user := models.User{
@ -28,8 +28,9 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove
result := db.WithContext(ctx).FirstOrCreate(&user) result := db.WithContext(ctx).FirstOrCreate(&user)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &errors2.EntityAlreadyExists{} return &otterError.EntityAlreadyExists{}
} }
return result.Error return result.Error
} }
@ -40,19 +41,19 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove
func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error { func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error {
if anthroveUserID == "" { if anthroveUserID == "" {
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
} }
if len(anthroveUserID) != 25 { if len(anthroveUserID) != 25 {
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
if accountId == "" { if accountId == "" {
return &errors2.EntityValidationFailed{Reason: "accountID cannot be empty"} return &otterError.EntityValidationFailed{Reason: "accountID cannot be empty"}
} }
if accountUsername == "" { if accountUsername == "" {
return &errors2.EntityValidationFailed{Reason: "accountUsername cannot be empty"} return &otterError.EntityValidationFailed{Reason: "accountUsername cannot be empty"}
} }
err := CreateUser(ctx, db, anthroveUserID) err := CreateUser(ctx, db, anthroveUserID)
@ -67,7 +68,7 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs
result := db.WithContext(ctx).Where(&source).First(&source) result := db.WithContext(ctx).Where(&source).First(&source)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &errors2.NoDataFound{} return &otterError.NoDataFound{}
} }
return result.Error return result.Error
} }
@ -82,11 +83,11 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs
result = db.WithContext(ctx).FirstOrCreate(&userSource) result = db.WithContext(ctx).FirstOrCreate(&userSource)
if result.Error != nil { if result.Error != nil {
return errors.Join(result.Error, &errors2.NoRelationCreated{}) return errors.Join(result.Error, &otterError.NoRelationCreated{})
} }
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
return &errors2.NoDataWritten{} return &otterError.NoDataWritten{}
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -103,17 +104,17 @@ func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID mode
var count int64 var count int64
if anthroveUserID == "" { if anthroveUserID == "" {
return 0, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} return 0, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
} }
if len(anthroveUserID) != 25 { if len(anthroveUserID) != 25 {
return 0, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} return 0, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Count(&count) result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Count(&count)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return 0, &errors2.NoDataFound{} return 0, &otterError.NoDataFound{}
} }
return 0, result.Error return 0, result.Error
} }
@ -131,17 +132,17 @@ func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models.
userSourceMap := make(map[string]models.UserSource) userSourceMap := make(map[string]models.UserSource)
if anthroveUserID == "" { if anthroveUserID == "" {
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
} }
if len(anthroveUserID) != 25 { if len(anthroveUserID) != 25 {
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
result := db.WithContext(ctx).Model(&models.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources) result := db.WithContext(ctx).Model(&models.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }
@ -151,7 +152,7 @@ func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models.
result = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source) result = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }
@ -179,25 +180,25 @@ func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID mo
userSourceMap := make(map[string]models.UserSource) userSourceMap := make(map[string]models.UserSource)
if anthroveUserID == "" { if anthroveUserID == "" {
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
} }
if len(anthroveUserID) != 25 { if len(anthroveUserID) != 25 {
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
if sourceID == "" { if sourceID == "" {
return nil, &errors2.EntityValidationFailed{Reason: "sourceID cannot be empty"} return nil, &otterError.EntityValidationFailed{Reason: "sourceID cannot be empty"}
} }
if len(sourceID) != 25 { if len(sourceID) != 25 {
return nil, &errors2.EntityValidationFailed{Reason: "sourceID needs to be 25 characters long"} return nil, &otterError.EntityValidationFailed{Reason: "sourceID needs to be 25 characters long"}
} }
result := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(&userSources) result := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(&userSources)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }
@ -207,7 +208,7 @@ func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID mo
result = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source) result = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }
@ -238,7 +239,7 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveU
result := db.WithContext(ctx).Model(&models.User{}).Find(&users) result := db.WithContext(ctx).Model(&models.User{}).Find(&users)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }
@ -259,17 +260,17 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse
var favoritePosts []models.Post var favoritePosts []models.Post
if anthroveUserID == "" { if anthroveUserID == "" {
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
} }
if len(anthroveUserID) != 25 { if len(anthroveUserID) != 25 {
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, err return nil, err
} }
@ -279,7 +280,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse
err = db.WithContext(ctx).Model(&models.Post{}).Where("id = ?", userFavorite.PostID).First(&post).Error err = db.WithContext(ctx).Model(&models.Post{}).Where("id = ?", userFavorite.PostID).First(&post).Error
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, err return nil, err
} }
@ -303,17 +304,17 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov
var userFavorites []models.UserFavorites var userFavorites []models.UserFavorites
if anthroveUserID == "" { if anthroveUserID == "" {
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
} }
if len(anthroveUserID) != 25 { if len(anthroveUserID) != 25 {
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
result := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites) result := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }
@ -325,10 +326,13 @@ 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, &errors2.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, result.Error
} }

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) {
@ -663,7 +707,7 @@ func TestGetUserSourceLinks(t *testing.T) {
} }
err = CreateSource(ctx, gormDB, eSource) err = CreateSource(ctx, gormDB, eSource)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal("Create Source e621:", err)
} }
faSource := &models.Source{ faSource := &models.Source{
@ -673,7 +717,7 @@ func TestGetUserSourceLinks(t *testing.T) {
} }
err = CreateSource(ctx, gormDB, faSource) err = CreateSource(ctx, gormDB, faSource)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal("Create Source fa:", err)
} }
expectedResult := make(map[string]models.UserSource) expectedResult := make(map[string]models.UserSource)
@ -696,11 +740,11 @@ func TestGetUserSourceLinks(t *testing.T) {
err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal("CreateUserWithRelationToSource e621:", err)
} }
err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal("CreateUserWithRelationToSource fa:", err)
} }
// Test // Test
@ -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) {

View File

@ -1,4 +1,4 @@
package errors package error
type EntityAlreadyExists struct{} type EntityAlreadyExists struct{}

View File

@ -1,4 +1,4 @@
package errors package error
import "fmt" import "fmt"