added pools #14

Merged
SoXX merged 16 commits from dev/pools into main 2024-10-15 09:03:46 +00:00
2 changed files with 369 additions and 0 deletions
Showing only changes of commit a5420cc8c2 - Show all commits

117
pkg/database/pool_post.go Normal file
View File

@ -0,0 +1,117 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreatePoolPost(ctx context.Context, poolID models.PoolID, postID models.PostID, orderPosition int) (models.PoolPost, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePoolPost")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": poolID,
"post_id": postID,
})
span.SetAttributes(
attribute.String("pool_id", string(poolID)),
attribute.String("post_id", string(postID)),
)
utils.HandleEvent(span, localLogger, "Starting poolPost creation")
if client == nil {
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if poolID == "" {
Alphyron marked this conversation as resolved Outdated

Either you use len(poolID) == 0 like in pool.go or you use this method in pool.go. But not both!

Either you use `len(poolID) == 0` like in `pool.go` or you use this method in pool.go. But not both!
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
}
if len(poolID) != 25 {
Alphyron marked this conversation as resolved Outdated

Please recheck this, you want to check if the length nis not 25!

Please recheck this, you want to check if the length nis not 25!
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDToShort})
Alphyron marked this conversation as resolved Outdated

its not really to short, is just had a wrong length....

its not really to short, is just had a wrong length....

Please recheck this, you want to check if the length nis not 25!

Please recheck this, you want to check if the length nis not 25!
}
if postID == "" {
Alphyron marked this conversation as resolved Outdated

Either you use len(postID) == 0 like in pool.go or you use this method in pool.go. But not both!

Either you use len(postID) == 0 like in pool.go or you use this method in pool.go. But not both!
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
}
if len(postID) != 25 {
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDToShort})
Alphyron marked this conversation as resolved Outdated

its not really to short, is just had a wrong length....

its not really to short, is just had a wrong length....
}
poolPost := models.PoolPost{
PoolID: poolID,
PostID: postID,
OrderPosition: orderPosition,
}
result := client.WithContext(ctx).Create(&poolPost)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "poolPost created successfully")
return poolPost, nil
}
func DeletePoolPost(ctx context.Context, poolID models.PoolID, postID models.PostID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePoolPost")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": poolID,
"post_id": postID,
})
span.SetAttributes(
attribute.String("pool_id", string(poolID)),
attribute.String("post_id", string(postID)),
)
utils.HandleEvent(span, localLogger, "Starting delete poolPost")
var poolPost models.PoolPost
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if poolID == "" {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
Alphyron marked this conversation as resolved
Review

Either you use len(poolID) == 0 like in pool.go or you use this method in pool.go. But not both!

Either you use len(poolID) == 0 like in pool.go or you use this method in pool.go. But not both!
}
if len(poolID) != 25 {
Alphyron marked this conversation as resolved Outdated

Please recheck this, you want to check if the length nis not 25!

Please recheck this, you want to check if the length nis not 25!
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDToShort})
Alphyron marked this conversation as resolved Outdated

its not really to short, is just had a wrong length....

its not really to short, is just had a wrong length....

Please recheck this, you want to check if the length nis not 25!

Please recheck this, you want to check if the length nis not 25!
}
if postID == "" {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
Alphyron marked this conversation as resolved
Review

Either you use len(postID) == 0 like in pool.go or you use this method in pool.go. But not both!

Either you use len(postID) == 0 like in pool.go or you use this method in pool.go. But not both!
}
if len(postID) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDToShort})
Alphyron marked this conversation as resolved Outdated

its not really to short, is just had a wrong length....

its not really to short, is just had a wrong length....
}
result := client.WithContext(ctx).Delete(&poolPost, "pool_id = ? AND post_id = ?", poolID, postID)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "poolPost deleted successfully")
return nil
}

View File

@ -0,0 +1,252 @@
package database
import (
"context"
"fmt"
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)
func TestCreatePoolPost(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Pool to test with
pool := models.Pool{
BaseModel: models.BaseModel[models.PoolID]{
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
},
Name: "Test Pool 01",
Category: models.Collection,
}
pool, err = CreatePool(ctx, pool)
if err != nil {
logger.Error(err)
}
// -- Create Post to test with
post := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
},
Rating: models.SFW,
}
post, err = CreatePost(ctx, post)
if err != nil {
logger.Error(err)
}
// -- Create PoolPost to test with
validPoolPost := models.PoolPost{
PoolID: pool.ID,
PostID: post.ID,
OrderPosition: 0,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
poolID models.PoolID
postID models.PostID
orderPosition int
}
tests := []struct {
name string
args args
want models.PoolPost
wantErr bool
}{
{
name: "Test 01: Valid post & pool ID",
args: args{
ctx: ctx,
postID: post.ID,
poolID: pool.ID,
orderPosition: 0,
},
want: validPoolPost,
wantErr: false,
},
{
name: "Test 02: Duplicate",
args: args{
ctx: ctx,
postID: post.ID,
poolID: pool.ID,
orderPosition: 0,
},
want: models.PoolPost{},
wantErr: true,
},
{
name: "Test 03: poolID is empty",
args: args{
ctx: ctx,
postID: post.ID,
poolID: "",
orderPosition: 0,
},
want: models.PoolPost{},
wantErr: true,
},
{
name: "Test 04: postID name is empty",
args: args{
ctx: ctx,
postID: "",
poolID: pool.ID,
orderPosition: 0,
},
want: models.PoolPost{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreatePoolPost(tt.args.ctx, tt.args.poolID, tt.args.postID, tt.args.orderPosition)
if (err != nil) != tt.wantErr {
t.Errorf("CreatePoolPost() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreatePoolPost() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeletePoolPost(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Pool to test with
pool := models.Pool{
BaseModel: models.BaseModel[models.PoolID]{
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
},
Name: "Test Pool 01",
Category: models.Collection,
}
pool, err = CreatePool(ctx, pool)
if err != nil {
logger.Fatal(err)
}
// -- Create Post to test with
post := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
},
Rating: models.SFW,
}
post, err = CreatePost(ctx, post)
if err != nil {
logger.Fatal(err)
}
// -- Create PoolPost to test with
validPoolPost := models.PoolPost{
PoolID: pool.ID,
PostID: post.ID,
OrderPosition: 0,
}
validPoolPost, err = CreatePoolPost(ctx, validPoolPost.PoolID, validPoolPost.PostID, validPoolPost.OrderPosition)
if err != nil {
logger.Fatal(err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
poolID models.PoolID
postID models.PostID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Post & pool id",
args: args{
ctx: ctx,
poolID: validPoolPost.PoolID,
postID: validPoolPost.PostID,
},
wantErr: false,
},
{
name: "Test 02: Not existing postID",
args: args{
ctx: ctx,
poolID: validPoolPost.PoolID,
postID: "",
},
wantErr: true,
},
{
name: "Test 03: Not existing poolID",
args: args{
ctx: ctx,
poolID: "",
postID: validPoolPost.PostID,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeletePoolPost(tt.args.ctx, tt.args.poolID, tt.args.postID); (err != nil) != tt.wantErr {
t.Errorf("DeletePoolPost() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}