2024-06-20 13:24:55 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-20 20:45:03 +00:00
|
|
|
"fmt"
|
2024-06-20 13:24:55 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/test"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateUser(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
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
2024-06-22 23:18:23 +00:00
|
|
|
anthroveUserID string
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid AnthroveUserID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid AnthroveUserID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if err := CreateUser(tt.args.ctx, tt.args.db, tt.args.anthroveUserID); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateUser() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateUserNodeWithSourceRelation(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
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
source := &models.Source{
|
2024-06-20 13:24:55 +00:00
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "icon.e621.net",
|
|
|
|
}
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateSource(ctx, gormDB, source)
|
2024-06-20 13:24:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
2024-06-22 23:18:23 +00:00
|
|
|
anthroveUserID string
|
2024-06-20 13:24:55 +00:00
|
|
|
sourceDomain string
|
|
|
|
userID string
|
|
|
|
username string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid anthroveUserID, sourceDomain, userID, username",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
2024-06-22 23:18:23 +00:00
|
|
|
sourceDomain: source.Domain,
|
2024-06-20 13:24:55 +00:00
|
|
|
userID: "e1",
|
|
|
|
username: "marius",
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid anthroveUserID, valid sourceDomain, userID, username",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "2",
|
2024-06-22 23:18:23 +00:00
|
|
|
sourceDomain: source.Domain,
|
2024-06-20 13:24:55 +00:00
|
|
|
userID: "e1",
|
|
|
|
username: "marius",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Empty anthroveUserID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "",
|
2024-06-22 23:18:23 +00:00
|
|
|
sourceDomain: source.Domain,
|
2024-06-20 13:24:55 +00:00
|
|
|
userID: "e1",
|
|
|
|
username: "marius",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: invalid sourceDomain",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
sourceDomain: "fa.net",
|
|
|
|
userID: "e1",
|
|
|
|
username: "marius",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 5: no userID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
2024-06-22 23:18:23 +00:00
|
|
|
sourceDomain: source.Domain,
|
2024-06-20 13:24:55 +00:00
|
|
|
userID: "",
|
|
|
|
username: "marius",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 6: no username",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
2024-06-22 23:18:23 +00:00
|
|
|
sourceDomain: source.Domain,
|
2024-06-20 13:24:55 +00:00
|
|
|
userID: "aa",
|
|
|
|
username: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-23 19:23:38 +00:00
|
|
|
if err := CreateUserWithRelationToSource(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceDomain, tt.args.userID, tt.args.username); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateUserWithRelationToSource() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAllAnthroveUserIDs(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
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
users := []string{"1", "2", "3"}
|
2024-06-20 13:24:55 +00:00
|
|
|
|
|
|
|
for _, user := range users {
|
|
|
|
err = CreateUser(ctx, gormDB, user)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2024-06-22 23:18:23 +00:00
|
|
|
want []string
|
2024-06-20 13:24:55 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Get Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
},
|
|
|
|
want: users,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := GetAllAnthroveUserIDs(tt.args.ctx, tt.args.db)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetAllAnthroveUserIDs() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("GetAllAnthroveUserIDs() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
func TestGetUserSourceBySourceID(t *testing.T) {
|
2024-06-20 13:24:55 +00:00
|
|
|
// 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
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
expectedResult := make(map[string]models.UserSource)
|
|
|
|
expectedResult["e621"] = models.UserSource{
|
|
|
|
UserID: "e1",
|
|
|
|
AccountUsername: "euser",
|
|
|
|
Source: models.Source{
|
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
2024-06-20 13:24:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
source := &models.Source{
|
2024-06-23 19:23:38 +00:00
|
|
|
BaseModel: models.BaseModel{ID: expectedResult["e621"].Source.ID},
|
2024-06-22 23:18:23 +00:00
|
|
|
DisplayName: expectedResult["e621"].Source.DisplayName,
|
|
|
|
Domain: expectedResult["e621"].Source.Domain,
|
2024-06-20 20:45:03 +00:00
|
|
|
}
|
2024-06-22 23:18:23 +00:00
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateSource(ctx, gormDB, source)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateUserWithRelationToSource(ctx, gormDB, "1", source.Domain, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
// Test
|
2024-06-20 13:24:55 +00:00
|
|
|
type args struct {
|
2024-06-23 19:23:38 +00:00
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
sourceID string
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2024-06-22 23:18:23 +00:00
|
|
|
want map[string]models.UserSource
|
2024-06-20 13:24:55 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-23 19:23:38 +00:00
|
|
|
name: "Test 1: Valid AnthroveUserID and sourceID",
|
2024-06-20 20:45:03 +00:00
|
|
|
args: args{
|
2024-06-23 19:23:38 +00:00
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
sourceID: source.ID,
|
2024-06-20 20:45:03 +00:00
|
|
|
},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2024-06-23 19:23:38 +00:00
|
|
|
name: "Test 2: Invalid AnthroveUserID and valid sourceID",
|
2024-06-20 20:45:03 +00:00
|
|
|
args: args{
|
2024-06-23 19:23:38 +00:00
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "22",
|
|
|
|
sourceID: source.ID,
|
2024-06-20 20:45:03 +00:00
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2024-06-23 19:23:38 +00:00
|
|
|
name: "Test 3: Valid AnthroveUserID and invalid sourceID",
|
2024-06-20 20:45:03 +00:00
|
|
|
args: args{
|
2024-06-23 19:23:38 +00:00
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
sourceID: "fa",
|
2024-06-20 20:45:03 +00:00
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2024-06-23 19:23:38 +00:00
|
|
|
name: "Test 4: No AnthroveUserID and Valid sourceID",
|
2024-06-20 20:45:03 +00:00
|
|
|
args: args{
|
2024-06-23 19:23:38 +00:00
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "",
|
|
|
|
sourceID: source.ID,
|
2024-06-20 20:45:03 +00:00
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 5: Valid AnthroveUserID and No SourceDisplayName",
|
|
|
|
args: args{
|
2024-06-23 19:23:38 +00:00
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
sourceID: "",
|
2024-06-20 20:45:03 +00:00
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 6: No AnthroveUserID and No SourceDisplayName",
|
|
|
|
args: args{
|
2024-06-23 19:23:38 +00:00
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "",
|
|
|
|
sourceID: "",
|
2024-06-20 20:45:03 +00:00
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-23 19:23:38 +00:00
|
|
|
got, err := GetUserSourceBySourceID(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID)
|
2024-06-20 13:24:55 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-23 19:23:38 +00:00
|
|
|
t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-20 13:24:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
2024-06-23 19:23:38 +00:00
|
|
|
t.Errorf("GetUserSourceBySourceID() got = %v, want %v", got, tt.want)
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
|
2024-06-20 20:45:03 +00:00
|
|
|
// Setup trow away containert
|
|
|
|
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
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
expectedResultPosts := []models.Post{
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post1")},
|
|
|
|
Rating: "safe",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
|
|
|
|
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post2")},
|
|
|
|
Rating: "safe",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post3")},
|
|
|
|
Rating: "explicit",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post4")},
|
|
|
|
Rating: "explicit",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post5")},
|
|
|
|
Rating: "questionable",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post6")},
|
|
|
|
Rating: "safe",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
}
|
2024-06-22 23:18:23 +00:00
|
|
|
expectedResult := &models.FavoriteList{
|
|
|
|
Posts: expectedResultPosts,
|
2024-06-20 20:45:03 +00:00
|
|
|
}
|
2024-06-22 23:18:23 +00:00
|
|
|
expectedResult2 := &models.FavoriteList{
|
|
|
|
Posts: expectedResultPosts[2:],
|
2024-06-20 20:45:03 +00:00
|
|
|
}
|
2024-06-22 23:18:23 +00:00
|
|
|
expectedResult3 := &models.FavoriteList{
|
|
|
|
Posts: expectedResultPosts[:3],
|
2024-06-20 20:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = CreateUser(ctx, gormDB, "1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
for _, expectedResultPost := range expectedResultPosts {
|
|
|
|
err = CreatePost(ctx, gormDB, &expectedResultPost)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", expectedResultPost.ID)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
2024-06-20 13:24:55 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
2024-06-22 23:18:23 +00:00
|
|
|
anthroveUserID string
|
2024-06-20 13:24:55 +00:00
|
|
|
skip int
|
|
|
|
limit int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2024-06-22 23:18:23 +00:00
|
|
|
want *models.FavoriteList
|
2024-06-20 13:24:55 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
|
|
|
name: "Test 1: Valid AnthroveUserID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
skip: 0,
|
|
|
|
limit: 2000,
|
|
|
|
},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Skip first two",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
skip: 2,
|
|
|
|
limit: 2000,
|
|
|
|
},
|
|
|
|
want: expectedResult2,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Limit of 3",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
skip: 0,
|
|
|
|
limit: 3,
|
|
|
|
},
|
|
|
|
want: expectedResult3,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-23 19:23:38 +00:00
|
|
|
got, err := GetUserFavoriteWithPagination(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.skip, tt.args.limit)
|
2024-06-20 13:24:55 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-23 19:23:38 +00:00
|
|
|
t.Errorf("GetUserFavoriteWithPagination() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-20 13:24:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
2024-06-23 19:23:38 +00:00
|
|
|
t.Errorf("GetUserFavoriteWithPagination() got = %v, want %v", got, tt.want)
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUserFavoritesCount(t *testing.T) {
|
2024-06-20 20:45:03 +00:00
|
|
|
// Setup trow away containert
|
|
|
|
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
|
|
|
|
|
|
|
|
err = CreateUser(ctx, gormDB, "1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
expectedResultPosts := []models.Post{
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 20:06:36 +00:00
|
|
|
BaseModel: models.BaseModel{ID: "Post1"},
|
2024-06-20 20:45:03 +00:00
|
|
|
Rating: "safe",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 20:06:36 +00:00
|
|
|
BaseModel: models.BaseModel{ID: "Post2"},
|
2024-06-20 20:45:03 +00:00
|
|
|
Rating: "safe",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 20:06:36 +00:00
|
|
|
BaseModel: models.BaseModel{ID: "Post3"},
|
2024-06-20 20:45:03 +00:00
|
|
|
Rating: "explicit",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 20:06:36 +00:00
|
|
|
BaseModel: models.BaseModel{ID: "Post4"},
|
2024-06-20 20:45:03 +00:00
|
|
|
Rating: "explicit",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 20:06:36 +00:00
|
|
|
BaseModel: models.BaseModel{ID: "Post5"},
|
2024-06-20 20:45:03 +00:00
|
|
|
Rating: "questionable",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 20:06:36 +00:00
|
|
|
BaseModel: models.BaseModel{ID: "Post6"},
|
2024-06-20 20:45:03 +00:00
|
|
|
Rating: "safe",
|
2024-06-22 20:06:36 +00:00
|
|
|
},
|
2024-06-20 20:45:03 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
for _, post := range expectedResultPosts {
|
|
|
|
err = CreatePost(ctx, gormDB, &post)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", post.ID)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
2024-06-20 13:24:55 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want int64
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
|
|
|
name: "Test 1: Valid anthroveUserID and 6 favorite posts",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
},
|
|
|
|
want: 6,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid anthroveUserID and 6 favorite posts",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "2",
|
|
|
|
},
|
|
|
|
want: 0,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: no anthroveUserID and 6 favorite posts",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "",
|
|
|
|
},
|
|
|
|
want: 0,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := GetUserFavoritesCount(tt.args.ctx, tt.args.db, tt.args.anthroveUserID)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetUserFavoritesCount() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("GetUserFavoritesCount() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
func TestGetUserSourceLinks(t *testing.T) {
|
2024-06-20 20:45:03 +00:00
|
|
|
// Setup trow away containert
|
|
|
|
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
|
2024-06-22 23:18:23 +00:00
|
|
|
eSource := &models.Source{
|
2024-06-20 20:45:03 +00:00
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
}
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateSource(ctx, gormDB, eSource)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
faSource := &models.Source{
|
2024-06-20 20:45:03 +00:00
|
|
|
DisplayName: "fa",
|
|
|
|
Domain: "fa.net",
|
|
|
|
}
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateSource(ctx, gormDB, faSource)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
expectedResult := make(map[string]models.UserSource)
|
|
|
|
expectedResult["e621"] = models.UserSource{
|
|
|
|
UserID: "e1",
|
|
|
|
AccountUsername: "e621-user",
|
|
|
|
Source: models.Source{
|
|
|
|
DisplayName: eSource.DisplayName,
|
|
|
|
Domain: eSource.Domain,
|
2024-06-20 20:45:03 +00:00
|
|
|
},
|
|
|
|
}
|
2024-06-22 23:18:23 +00:00
|
|
|
expectedResult["fa"] = models.UserSource{
|
|
|
|
UserID: "fa1",
|
|
|
|
AccountUsername: "fa-user",
|
|
|
|
Source: models.Source{
|
|
|
|
DisplayName: faSource.DisplayName,
|
|
|
|
Domain: faSource.Domain,
|
2024-06-20 20:45:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateUserWithRelationToSource(ctx, gormDB, "1", eSource.Domain, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateUserWithRelationToSource(ctx, gormDB, "1", faSource.Domain, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-06-22 23:18:23 +00:00
|
|
|
|
2024-06-20 20:45:03 +00:00
|
|
|
// Test
|
2024-06-20 13:24:55 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2024-06-22 23:18:23 +00:00
|
|
|
want map[string]models.UserSource
|
2024-06-20 13:24:55 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
|
|
|
name: "Test 1: Get Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-22 21:23:23 +00:00
|
|
|
got, err := GetUserSourceLinks(tt.args.ctx, tt.args.db, tt.args.anthroveUserID)
|
2024-06-20 13:24:55 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-22 21:23:23 +00:00
|
|
|
t.Errorf("GetUserSourceLinks() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-20 13:24:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
2024-06-22 21:23:23 +00:00
|
|
|
t.Errorf("GetUserSourceLinks() got = %v, want %v", got, tt.want)
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
|
2024-06-20 20:45:03 +00:00
|
|
|
// Setup trow away containert
|
|
|
|
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
|
|
|
|
err = CreateUser(ctx, gormDB, "1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
posts := []models.Post{
|
|
|
|
{BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post1")}, Rating: "safe"},
|
|
|
|
{BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post2")}, Rating: "safe"},
|
|
|
|
{BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post3")}, Rating: "explicit"},
|
2024-06-20 20:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, post := range posts {
|
2024-06-22 23:18:23 +00:00
|
|
|
err = CreatePost(ctx, gormDB, &post)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", post.ID)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
tags := []models.Tag{
|
2024-06-20 20:45:03 +00:00
|
|
|
{Name: "JayTheFerret", Type: "artist"},
|
|
|
|
{Name: "Ferret", Type: "species"},
|
|
|
|
{Name: "Jay", Type: "character"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tag := range tags {
|
2024-06-23 19:23:38 +00:00
|
|
|
err = CreateTagAndReferenceToPost(ctx, gormDB, posts[i].ID, &tag)
|
2024-06-20 20:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
expectedResult := []models.TagsWithFrequency{
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
|
|
|
Frequency: 1,
|
2024-06-22 23:18:23 +00:00
|
|
|
Tags: models.Tag{
|
2024-06-20 20:45:03 +00:00
|
|
|
Name: tags[0].Name,
|
2024-06-22 23:18:23 +00:00
|
|
|
Type: tags[0].Type,
|
2024-06-20 20:45:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Frequency: 1,
|
2024-06-22 23:18:23 +00:00
|
|
|
Tags: models.Tag{
|
2024-06-20 20:45:03 +00:00
|
|
|
Name: tags[1].Name,
|
2024-06-22 23:18:23 +00:00
|
|
|
Type: tags[1].Type,
|
2024-06-20 20:45:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Frequency: 1,
|
2024-06-22 23:18:23 +00:00
|
|
|
Tags: models.Tag{
|
2024-06-20 20:45:03 +00:00
|
|
|
Name: tags[2].Name,
|
2024-06-22 23:18:23 +00:00
|
|
|
Type: tags[2].Type,
|
2024-06-20 20:45:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
2024-06-20 13:24:55 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
db *gorm.DB
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2024-06-22 23:18:23 +00:00
|
|
|
want []models.TagsWithFrequency
|
2024-06-20 13:24:55 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
2024-06-20 20:45:03 +00:00
|
|
|
{
|
2024-06-22 23:18:23 +00:00
|
|
|
name: "Test 1: Get Data",
|
2024-06-20 20:45:03 +00:00
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
db: gormDB,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-23 19:23:38 +00:00
|
|
|
got, err := GetUserTagWitRelationToFavedPosts(tt.args.ctx, tt.args.db, tt.args.anthroveUserID)
|
2024-06-20 13:24:55 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-23 19:23:38 +00:00
|
|
|
t.Errorf("GetUserTagWitRelationToFavedPosts() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-20 13:24:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
2024-06-23 19:23:38 +00:00
|
|
|
t.Errorf("GetUserTagWitRelationToFavedPosts() got = %v, want %v", got, tt.want)
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|