BREAKING CHANGE: V2 of thr SDK #12

Merged
fenpaws merged 124 commits from develop/postgresql into main 2024-07-01 20:46:28 +00:00
7 changed files with 336 additions and 39 deletions
Showing only changes of commit cb04347dd5 - Show all commits

View File

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

View File

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

View File

@ -3,10 +3,11 @@ package postgres
import (
"context"
"fmt"
"testing"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm"
"testing"
)
func TestCreateSourceNode(t *testing.T) {
@ -30,6 +31,12 @@ func TestCreateSourceNode(t *testing.T) {
}
invalidSource := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
Domain: "notfound.intern",
}
invalidSourceDomain := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
Domain: "",
}
@ -58,7 +65,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveSource: invalidSource,
anthroveSource: invalidSourceDomain,
},
wantErr: true,
},
@ -67,7 +74,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveSource: validSource,
anthroveSource: invalidSource,
},
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) {
// Setup trow away container
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"}
}
result := db.WithContext(ctx).Where(tag).Create(tag)
result := db.WithContext(ctx).Create(tag)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
@ -77,7 +77,6 @@ func GetTags(ctx context.Context, db *gorm.DB) ([]models.Tag, error) {
var tags []models.Tag
result := db.WithContext(ctx).Find(&tags)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}

View File

@ -3,10 +3,11 @@ package postgres
import (
"context"
"fmt"
"testing"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm"
"testing"
)
func TestCreateTagNodeWitRelation(t *testing.T) {
@ -54,7 +55,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
PostID: models.AnthrovePostID(post.ID),
PostID: post.ID,
tag: tag,
},
wantErr: false,
@ -64,7 +65,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
PostID: models.AnthrovePostID(post.ID),
PostID: post.ID,
tag: nil,
},
wantErr: true,
@ -180,3 +181,68 @@ func checkTag(got []models.Tag, want []models.Tag) bool {
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
}
if result.RowsAffected == 0 {
return &otterError.EntityAlreadyExists{}
}
return nil
}
@ -325,7 +329,10 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov
for _, userFavorite := range userFavorites {
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 errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}

View File

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