454 lines
10 KiB
Go
454 lines
10 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels"
|
|
"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
|
|
anthroveUserID models.AnthroveUserID
|
|
}
|
|
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
|
|
|
|
source := &pgModels.Source{
|
|
DisplayName: "e621",
|
|
Domain: "e621.net",
|
|
Icon: "icon.e621.net",
|
|
}
|
|
err = CreateSourceNode(ctx, gormDB, source)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Test
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
anthroveUserID models.AnthroveUserID
|
|
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",
|
|
sourceDomain: "e621.net",
|
|
userID: "e1",
|
|
username: "marius",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Test 2: Invalid anthroveUserID, valid sourceDomain, userID, username",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "2",
|
|
sourceDomain: "e621.net",
|
|
userID: "e1",
|
|
username: "marius",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 3: Empty anthroveUserID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "",
|
|
sourceDomain: "e621.net",
|
|
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",
|
|
sourceDomain: "e621.net",
|
|
userID: "",
|
|
username: "marius",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 6: no username",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "1",
|
|
sourceDomain: "e621.net",
|
|
userID: "aa",
|
|
username: "",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := CreateUserNodeWithSourceRelation(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceDomain, tt.args.userID, tt.args.username); (err != nil) != tt.wantErr {
|
|
t.Errorf("CreateUserNodeWithSourceRelation() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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
|
|
|
|
users := []models.AnthroveUserID{"1", "2", "3"}
|
|
|
|
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
|
|
want []models.AnthroveUserID
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetAnthroveUser(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
|
|
|
|
user := graphModels.AnthroveUser{
|
|
UserID: "1",
|
|
}
|
|
err = CreateUser(ctx, gormDB, user.UserID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Test
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
anthroveUserID models.AnthroveUserID
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want *graphModels.AnthroveUser
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Test 1: Valid AnthroveUserID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "1",
|
|
},
|
|
want: &user,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Test 2: Invalid AnthroveUserID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "2",
|
|
},
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Test 3: No AnthroveUserID",
|
|
args: args{
|
|
ctx: ctx,
|
|
db: gormDB,
|
|
anthroveUserID: "",
|
|
},
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := GetAnthroveUser(tt.args.ctx, tt.args.db, tt.args.anthroveUserID)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetAnthroveUser() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("GetAnthroveUser() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetSpecifiedUserSourceLink(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
anthroveUserID models.AnthroveUserID
|
|
sourceDisplayName string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want map[string]graphModels.AnthroveUserRelationship
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := GetSpecifiedUserSourceLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceDisplayName)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetSpecifiedUserSourceLink() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("GetSpecifiedUserSourceLink() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
anthroveUserID models.AnthroveUserID
|
|
skip int
|
|
limit int
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want *graphModels.FavoriteList
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := GetUserFavoriteNodeWithPagination(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.skip, tt.args.limit)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetUserFavoriteNodeWithPagination() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("GetUserFavoriteNodeWithPagination() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetUserFavoritesCount(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
anthroveUserID models.AnthroveUserID
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want int64
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetUserSourceLink(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
anthroveUserID models.AnthroveUserID
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want map[string]graphModels.AnthroveUserRelationship
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := GetUserSourceLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetUserSourceLink() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("GetUserSourceLink() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
anthroveUserID models.AnthroveUserID
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []graphModels.TagsWithFrequency
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := GetUserTagNodeWitRelationToFavedPosts(tt.args.ctx, tt.args.db, tt.args.anthroveUserID)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetUserTagNodeWitRelationToFavedPosts() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("GetUserTagNodeWitRelationToFavedPosts() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|