package postgres import ( "context" "fmt" "testing" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" "gorm.io/gorm" ) func TestCheckUserToPostLink(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 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: validPostID, }, Rating: "safe", } err = CreatePost(ctx, gormDB, post) if err != nil { t.Fatal(err) } err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID) if err != nil { t.Fatal(err) } // Test type args struct { ctx context.Context db *gorm.DB anthroveUserID models.AnthroveUserID anthrovePostID models.AnthrovePostID } tests := []struct { name string args args want bool wantErr bool }{ { name: "Test 1: Valid AnthroveUserID and AnthrovePostID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, anthrovePostID: post.ID, }, want: true, wantErr: false, }, { 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", }, 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) { got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID) if (err != nil) != tt.wantErr { t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr) return } if got != tt.want { t.Errorf("CheckIfUserHasPostAsFavorite() got = %v, want %v", got, tt.want) } }) } } func TestCheckUserToPostLinkWithNoData(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 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: validPostID, }, Rating: "safe", } err = CreatePost(ctx, gormDB, post) if err != nil { t.Fatal(err) } err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID) if err != nil { t.Fatal(err) } // Test type args struct { ctx context.Context db *gorm.DB anthroveUserID models.AnthroveUserID anthrovePostID models.AnthrovePostID } tests := []struct { name string args args want bool wantErr bool }{ { name: "Test 1: Valid AnthroveUserID and AnthrovePostID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, anthrovePostID: post.ID, }, want: true, wantErr: false, }, { 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", }, 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) { got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID) if (err != nil) != tt.wantErr { t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr) return } if got != tt.want { t.Errorf("CheckIfUserHasPostAsFavorite() got = %v, want %v", got, tt.want) } }) } } func TestEstablishUserToPostLink(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 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: validPostID, }, Rating: "safe", } err = CreatePost(ctx, gormDB, post) if err != nil { t.Fatal(err) } // Test type args struct { ctx context.Context db *gorm.DB anthroveUserID models.AnthroveUserID anthrovePostID models.AnthrovePostID } tests := []struct { name string args args wantErr bool }{ { name: "Test 1: Valid AnthroveUserID and AnthrovePostID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, anthrovePostID: post.ID, }, wantErr: false, }, { name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, anthrovePostID: "123456", }, wantErr: true, }, { name: "Test 3: invalid AnthroveUserID and valid AnthrovePostID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: invalidUserID, anthrovePostID: post.ID, }, wantErr: true, }, { name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: invalidUserID, anthrovePostID: "123456", }, 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) { if err := CreateReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID); (err != nil) != tt.wantErr { t.Errorf("CreateReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr) } }) } }