Compare commits
No commits in common. "84aee5c2dda5d203ebf2eadfd4bf1ceb01d2de14" and "83a5c894384d718c1b10905c4b8eae41b9ae976d" have entirely different histories.
84aee5c2dd
...
83a5c89438
@ -2,7 +2,6 @@ package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"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"
|
||||
@ -12,10 +11,6 @@ import (
|
||||
|
||||
func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error {
|
||||
|
||||
if anthroveUserID == "" {
|
||||
return fmt.Errorf("anthroveUserID cannot be empty")
|
||||
}
|
||||
|
||||
user := pgModels.User{
|
||||
BaseModel: pgModels.BaseModel{
|
||||
ID: string(anthroveUserID),
|
||||
@ -34,11 +29,6 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove
|
||||
}
|
||||
|
||||
func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error {
|
||||
|
||||
if anthroveUserID == "" || username == "" || userID == "" {
|
||||
return fmt.Errorf("anthroveUserID cannot be empty")
|
||||
}
|
||||
|
||||
user := pgModels.User{
|
||||
BaseModel: pgModels.BaseModel{
|
||||
ID: string(anthroveUserID),
|
||||
@ -91,10 +81,6 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthrove
|
||||
}
|
||||
|
||||
func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (int64, error) {
|
||||
if anthroveUserID == "" {
|
||||
return 0, fmt.Errorf("anthroveUserID cannot be empty")
|
||||
}
|
||||
|
||||
var count int64
|
||||
err := db.WithContext(ctx).Model(&pgModels.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Count(&count).Error
|
||||
if err != nil {
|
||||
@ -153,14 +139,10 @@ func GetUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.A
|
||||
}
|
||||
|
||||
func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]graphModels.AnthroveUserRelationship, error) {
|
||||
if anthroveUserID == "" || sourceDisplayName == "" {
|
||||
return nil, fmt.Errorf("anthroveUserID or sourceDisplayName is empty")
|
||||
}
|
||||
|
||||
var userSources []pgModels.UserSource
|
||||
userSourceMap := make(map[string]graphModels.AnthroveUserRelationship)
|
||||
|
||||
err := db.WithContext(ctx).Model(&pgModels.UserSource{}).InnerJoins("Source", db.Where("display_name = ?", sourceDisplayName)).Where("user_id = ?", string(anthroveUserID)).First(&userSources).Error
|
||||
err := db.WithContext(ctx).Model(&pgModels.UserSource{}).Joins("Source").Where("user_id = ? AND display_name = ?", string(anthroveUserID), sourceDisplayName).Find(&userSources).Error
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"anthrove_user_id": anthroveUserID,
|
||||
@ -199,11 +181,6 @@ func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID
|
||||
}
|
||||
|
||||
func GetAnthroveUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (*graphModels.AnthroveUser, error) {
|
||||
|
||||
if anthroveUserID == "" {
|
||||
return nil, fmt.Errorf("anthroveUserID cannot be empty")
|
||||
}
|
||||
|
||||
var user pgModels.User
|
||||
var userSources []pgModels.UserSource
|
||||
anthroveUser := &graphModels.AnthroveUser{
|
||||
@ -325,13 +302,10 @@ func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, ant
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tagFrequency := make(map[struct {
|
||||
name string
|
||||
typeName string
|
||||
}]int)
|
||||
tagFrequency := make(map[string]int)
|
||||
for _, userFavorite := range userFavorites {
|
||||
var post pgModels.Post
|
||||
err = db.WithContext(ctx).Preload("Tags").First(&post, "id = ?", userFavorite.PostID).Error
|
||||
var postTags []pgModels.Tag
|
||||
err = db.WithContext(ctx).Model(&pgModels.Post{}).Where("id = ?", userFavorite.PostID).Association("Tags").Find(&postTags)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"post_id": userFavorite.PostID,
|
||||
@ -339,21 +313,17 @@ func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, ant
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, tag := range post.Tags {
|
||||
tagFrequency[struct {
|
||||
name string
|
||||
typeName string
|
||||
}{name: tag.Name, typeName: string(tag.Type)}]++
|
||||
for _, tag := range postTags {
|
||||
tagFrequency[tag.Name]++
|
||||
}
|
||||
}
|
||||
|
||||
var tagsWithFrequency []graphModels.TagsWithFrequency
|
||||
for data, frequency := range tagFrequency {
|
||||
for tagName, frequency := range tagFrequency {
|
||||
tagsWithFrequency = append(tagsWithFrequency, graphModels.TagsWithFrequency{
|
||||
Frequency: int64(frequency),
|
||||
Tags: graphModels.AnthroveTag{
|
||||
Name: data.name,
|
||||
Type: data.typeName,
|
||||
Name: tagName,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@ -1,915 +0,0 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"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) {
|
||||
// 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",
|
||||
}
|
||||
err = CreateSourceNode(ctx, gormDB, source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expectedResult := make(map[string]graphModels.AnthroveUserRelationship)
|
||||
expectedResult["e621"] = graphModels.AnthroveUserRelationship{
|
||||
UserID: "e1",
|
||||
Username: "euser",
|
||||
Source: graphModels.AnthroveSource{
|
||||
DisplayName: source.DisplayName,
|
||||
Domain: source.Domain,
|
||||
},
|
||||
}
|
||||
|
||||
err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", source.Domain, expectedResult["e621"].UserID, expectedResult["e621"].Username)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Test
|
||||
|
||||
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
|
||||
}{
|
||||
{
|
||||
name: "Test 1: Valid AnthroveUserID and SourceDisplayName",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
anthroveUserID: "1",
|
||||
sourceDisplayName: "e621",
|
||||
},
|
||||
want: expectedResult,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Test 2: Invalid AnthroveUserID and valid SourceDisplayName",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
anthroveUserID: "22",
|
||||
sourceDisplayName: "e621",
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 3: Valid AnthroveUserID and invalid SourceDisplayName",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
anthroveUserID: "1",
|
||||
sourceDisplayName: "fa",
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 4: No AnthroveUserID and Valid SourceDisplayName",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
anthroveUserID: "",
|
||||
sourceDisplayName: "e621",
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 5: Valid AnthroveUserID and No SourceDisplayName",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
anthroveUserID: "1",
|
||||
sourceDisplayName: "",
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 6: No AnthroveUserID and No SourceDisplayName",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
anthroveUserID: "",
|
||||
sourceDisplayName: "",
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
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, expectedResult %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
|
||||
// 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
|
||||
|
||||
expectedResultPostsGraph := []graphModels.FavoritePost{
|
||||
{
|
||||
AnthrovePost: graphModels.AnthrovePost{
|
||||
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post1")),
|
||||
Rating: "safe",
|
||||
},
|
||||
},
|
||||
{
|
||||
AnthrovePost: graphModels.AnthrovePost{
|
||||
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post2")),
|
||||
Rating: "safe",
|
||||
},
|
||||
},
|
||||
{
|
||||
AnthrovePost: graphModels.AnthrovePost{
|
||||
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post3")),
|
||||
Rating: "explicit",
|
||||
},
|
||||
},
|
||||
{
|
||||
AnthrovePost: graphModels.AnthrovePost{
|
||||
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post4")),
|
||||
Rating: "explicit",
|
||||
},
|
||||
},
|
||||
{
|
||||
AnthrovePost: graphModels.AnthrovePost{
|
||||
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post5")),
|
||||
Rating: "questionable",
|
||||
},
|
||||
},
|
||||
{
|
||||
AnthrovePost: graphModels.AnthrovePost{
|
||||
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post6")),
|
||||
Rating: "safe",
|
||||
},
|
||||
},
|
||||
}
|
||||
expectedResult := &graphModels.FavoriteList{
|
||||
Posts: expectedResultPostsGraph,
|
||||
}
|
||||
expectedResult2 := &graphModels.FavoriteList{
|
||||
Posts: expectedResultPostsGraph[2:],
|
||||
}
|
||||
expectedResult3 := &graphModels.FavoriteList{
|
||||
Posts: expectedResultPostsGraph[:3],
|
||||
}
|
||||
|
||||
err = CreateUser(ctx, gormDB, "1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expectedResultPostsPg := []pgModels.Post{
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post1"},
|
||||
Rating: "safe",
|
||||
},
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post2"},
|
||||
Rating: "safe",
|
||||
},
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post3"},
|
||||
Rating: "explicit",
|
||||
},
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post4"},
|
||||
Rating: "explicit",
|
||||
},
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post5"},
|
||||
Rating: "questionable",
|
||||
},
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post6"},
|
||||
Rating: "safe",
|
||||
},
|
||||
}
|
||||
|
||||
for _, expectedResultPost := range expectedResultPostsPg {
|
||||
err = CreateAnthrovePostNode(ctx, gormDB, models.AnthrovePostID(expectedResultPost.ID), expectedResultPost.Rating)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = EstablishUserToPostLink(ctx, gormDB, "1", models.AnthrovePostID(expectedResultPost.ID))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Test
|
||||
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
|
||||
}{
|
||||
{
|
||||
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,
|
||||
},
|
||||
}
|
||||
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) {
|
||||
// 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)
|
||||
}
|
||||
|
||||
expectedResultPostsPg := []pgModels.Post{
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post1"},
|
||||
Rating: "safe",
|
||||
},
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post2"},
|
||||
Rating: "safe",
|
||||
},
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post3"},
|
||||
Rating: "explicit",
|
||||
},
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post4"},
|
||||
Rating: "explicit",
|
||||
},
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post5"},
|
||||
Rating: "questionable",
|
||||
},
|
||||
{
|
||||
BaseModel: pgModels.BaseModel{ID: "Post6"},
|
||||
Rating: "safe",
|
||||
},
|
||||
}
|
||||
|
||||
for _, expectedResultPost := range expectedResultPostsPg {
|
||||
err = CreateAnthrovePostNode(ctx, gormDB, models.AnthrovePostID(expectedResultPost.ID), expectedResultPost.Rating)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = EstablishUserToPostLink(ctx, gormDB, "1", models.AnthrovePostID(expectedResultPost.ID))
|
||||
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 int64
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
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,
|
||||
},
|
||||
}
|
||||
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) {
|
||||
// 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
|
||||
|
||||
esource := &pgModels.Source{
|
||||
DisplayName: "e621",
|
||||
Domain: "e621.net",
|
||||
}
|
||||
err = CreateSourceNode(ctx, gormDB, esource)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
fasource := &pgModels.Source{
|
||||
DisplayName: "fa",
|
||||
Domain: "fa.net",
|
||||
}
|
||||
err = CreateSourceNode(ctx, gormDB, fasource)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expectedResult := make(map[string]graphModels.AnthroveUserRelationship)
|
||||
expectedResult["e621"] = graphModels.AnthroveUserRelationship{
|
||||
UserID: "e1",
|
||||
Username: "euser",
|
||||
Source: graphModels.AnthroveSource{
|
||||
DisplayName: esource.DisplayName,
|
||||
Domain: esource.Domain,
|
||||
},
|
||||
}
|
||||
expectedResult["fa"] = graphModels.AnthroveUserRelationship{
|
||||
UserID: "fa1",
|
||||
Username: "fauser",
|
||||
Source: graphModels.AnthroveSource{
|
||||
DisplayName: fasource.DisplayName,
|
||||
Domain: fasource.Domain,
|
||||
},
|
||||
}
|
||||
|
||||
err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", esource.Domain, expectedResult["e621"].UserID, expectedResult["e621"].Username)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", fasource.Domain, expectedResult["fa"].UserID, expectedResult["fa"].Username)
|
||||
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 map[string]graphModels.AnthroveUserRelationship
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test 1: Get Data",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
anthroveUserID: "1",
|
||||
},
|
||||
want: expectedResult,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
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) {
|
||||
// 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)
|
||||
}
|
||||
|
||||
posts := []pgModels.Post{
|
||||
{BaseModel: pgModels.BaseModel{ID: fmt.Sprintf("%-25s", "Post1")}, Rating: "safe"},
|
||||
{BaseModel: pgModels.BaseModel{ID: fmt.Sprintf("%-25s", "Post2")}, Rating: "safe"},
|
||||
{BaseModel: pgModels.BaseModel{ID: fmt.Sprintf("%-25s", "Post3")}, Rating: "explicit"},
|
||||
}
|
||||
|
||||
for _, post := range posts {
|
||||
err = CreateAnthrovePostNode(ctx, gormDB, models.AnthrovePostID(post.ID), post.Rating)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = EstablishUserToPostLink(ctx, gormDB, "1", models.AnthrovePostID(post.ID))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
tags := []pgModels.Tag{
|
||||
{Name: "JayTheFerret", Type: "artist"},
|
||||
{Name: "Ferret", Type: "species"},
|
||||
{Name: "Jay", Type: "character"},
|
||||
}
|
||||
|
||||
for i, tag := range tags {
|
||||
err = CreateTagNodeWitRelation(ctx, gormDB, models.AnthrovePostID(posts[i].ID), &tag)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
expectedResult := []graphModels.TagsWithFrequency{
|
||||
{
|
||||
Frequency: 1,
|
||||
Tags: graphModels.AnthroveTag{
|
||||
Name: tags[0].Name,
|
||||
Type: string(tags[0].Type),
|
||||
},
|
||||
},
|
||||
{
|
||||
Frequency: 1,
|
||||
Tags: graphModels.AnthroveTag{
|
||||
Name: tags[1].Name,
|
||||
Type: string(tags[1].Type),
|
||||
},
|
||||
},
|
||||
{
|
||||
Frequency: 1,
|
||||
Tags: graphModels.AnthroveTag{
|
||||
Name: tags[2].Name,
|
||||
Type: string(tags[2].Type),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Test
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
anthroveUserID models.AnthroveUserID
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []graphModels.TagsWithFrequency
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
anthroveUserID: "1",
|
||||
},
|
||||
want: expectedResult,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ CREATE TYPE TagType AS ENUM (
|
||||
|
||||
CREATE TABLE "Post"
|
||||
(
|
||||
id CHAR(25) PRIMARY KEY,
|
||||
id CHAR(25) UNIQUE PRIMARY KEY,
|
||||
rating Rating,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
@ -26,7 +26,7 @@ CREATE TABLE "Post"
|
||||
|
||||
CREATE TABLE "Source"
|
||||
(
|
||||
id CHAR(25) PRIMARY KEY,
|
||||
id CHAR(25) UNIQUE PRIMARY KEY,
|
||||
display_name TEXT NULL,
|
||||
icon TEXT NULL,
|
||||
domain TEXT NOT NULL UNIQUE,
|
||||
@ -46,7 +46,7 @@ CREATE TABLE "Tag"
|
||||
|
||||
CREATE TABLE "User"
|
||||
(
|
||||
id TEXT PRIMARY KEY,
|
||||
id TEXT UNIQUE PRIMARY KEY,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP NULL
|
||||
@ -80,8 +80,8 @@ CREATE TABLE "TagGroup"
|
||||
|
||||
CREATE TABLE "UserFavorites"
|
||||
(
|
||||
user_id TEXT REFERENCES "User" (id),
|
||||
post_id TEXT REFERENCES "Post" (id),
|
||||
user_id TEXT UNIQUE REFERENCES "User" (id),
|
||||
post_id TEXT UNIQUE REFERENCES "Post" (id),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (user_id, post_id)
|
||||
);
|
||||
|
@ -16,7 +16,7 @@ type BaseModel struct {
|
||||
func (base *BaseModel) BeforeCreate(db *gorm.DB) error {
|
||||
|
||||
if base.ID == "" {
|
||||
id, err := gonanoid.New(25)
|
||||
id, err := gonanoid.New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -2,12 +2,7 @@ package pgModels
|
||||
|
||||
type UserSource struct {
|
||||
UserID string `gorm:"primaryKey"`
|
||||
Source Source `gorm:"foreignKey:ID;references:SourceID"`
|
||||
SourceID string `gorm:"primaryKey"`
|
||||
AccountUsername string
|
||||
AccountID string
|
||||
}
|
||||
|
||||
func (UserSource) TableName() string {
|
||||
return "UserSource"
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
postgrescontainer "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"time"
|
||||
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
@ -74,7 +73,5 @@ func migrateDatabase(connectionString string) error {
|
||||
}
|
||||
|
||||
func getGormDB(connectionString string) (*gorm.DB, error) {
|
||||
return gorm.Open(postgres.Open(connectionString), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Info),
|
||||
})
|
||||
return gorm.Open(postgres.Open(connectionString), &gorm.Config{})
|
||||
}
|
||||
|
Reference in New Issue
Block a user