test(postgres): fixed the tests & also squashed some bugs

Signed-off-by: soxx <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-23 01:18:23 +02:00
parent bb7cb23c2a
commit 7842976f4b
11 changed files with 556 additions and 408 deletions

View File

@ -9,15 +9,8 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func CreateAnthrovePostNode(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) error { func CreatePost(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) error {
post := models.Post{ err := db.WithContext(ctx).Create(&anthrovePost).Error
BaseModel: models.BaseModel{
ID: string(anthrovePost.ID),
},
Rating: anthrovePost.Rating,
}
err := db.WithContext(ctx).Create(&post).Error
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@ package postgres
import ( import (
"context" "context"
"fmt"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test" "git.dragse.it/anthrove/otter-space-sdk/test"
_ "github.com/lib/pq" _ "github.com/lib/pq"
@ -19,11 +20,24 @@ func TestCreateAnthrovePostNode(t *testing.T) {
} }
defer container.Terminate(ctx) defer container.Terminate(ctx)
// Setup Tests
validPost := &models.Post{
BaseModel: models.BaseModel{
ID: fmt.Sprintf("%025s", "1"),
},
Rating: "safe",
}
invalidPost := &models.Post{
Rating: "error",
}
// Test
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthrovePostID models.AnthrovePostID anthrovePost *models.Post
anthroveRating models.Rating
} }
tests := []struct { tests := []struct {
name string name string
@ -33,34 +47,32 @@ func TestCreateAnthrovePostNode(t *testing.T) {
{ {
name: "Test 1: Valid AnthrovePostID and Rating", name: "Test 1: Valid AnthrovePostID and Rating",
args: args{ args: args{
ctx: context.Background(), ctx: context.Background(),
db: gormDB, db: gormDB,
anthrovePostID: "1234", anthrovePost: validPost,
anthroveRating: models.Rating("safe"),
}, },
wantErr: false, wantErr: false,
}, },
{ {
name: "Test 2: Invalid AnthrovePostID and Rating", name: "Test 2: Invalid Rating",
args: args{ args: args{
ctx: context.Background(), ctx: context.Background(),
db: gormDB, db: gormDB,
anthrovePostID: "", anthrovePost: invalidPost,
anthroveRating: "a4dsa4d",
}, },
wantErr: true, wantErr: true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := CreateAnthrovePostNode(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.anthroveRating); (err != nil) != tt.wantErr { if err := CreatePost(tt.args.ctx, tt.args.db, tt.args.anthrovePost); (err != nil) != tt.wantErr {
t.Errorf("CreateAnthrovePostNode() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CreatePost() error = %v, wantErr %v", err, tt.wantErr)
} }
}) })
} }
} }
func TestCheckIfAnthrovePostNodeExistsByAnthroveID(t *testing.T) { func TestGetPostByAnthroveID(t *testing.T) {
// Setup trow away container // Setup trow away container
ctx := context.Background() ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx) container, gormDB, err := test.StartPostgresContainer(ctx)
@ -69,68 +81,284 @@ func TestCheckIfAnthrovePostNodeExistsByAnthroveID(t *testing.T) {
} }
defer container.Terminate(ctx) defer container.Terminate(ctx)
// Setup Test // Setup Tests
err = CreateAnthrovePostNode(ctx, gormDB, "1234", "safe") post := &models.Post{
BaseModel: models.BaseModel{
ID: fmt.Sprintf("%025s", "1"),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal("Could not create post", err)
} }
// Test // Test
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthrovePostID models.AnthrovePostID anthrovePostID string
} }
tests := []struct { tests := []struct {
name string name string
args args args args
want bool want *models.Post
wantErr bool wantErr bool
}{ }{
{ {
name: "Test 1: Valid AnthroveID", name: "Test 1: Valid anthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: post.ID,
},
want: post,
wantErr: false,
},
{
name: "Test 2: Invalid anthrovePostID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: "1234", anthrovePostID: "1234",
}, },
want: true, want: nil,
wantErr: false, wantErr: true,
}, },
{ {
name: "Test 2: Invalid AnthroveID", name: "Test 3: No anthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: "123456",
},
want: false,
wantErr: false,
},
{
name: "Test 3: No AnthroveID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: "", anthrovePostID: "",
}, },
want: false, want: nil,
wantErr: true, wantErr: true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := CheckIfAnthrovePostNodeExistsByAnthroveID(tt.args.ctx, tt.args.db, tt.args.anthrovePostID) got, err := GetPostByAnthroveID(tt.args.ctx, tt.args.db, tt.args.anthrovePostID)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("CheckIfAnthrovePostNodeExistsByAnthroveID() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("GetPostByAnthroveID() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if got != tt.want { if !checkPost(got, tt.want) {
t.Errorf("CheckIfAnthrovePostNodeExistsByAnthroveID() got = %v, want %v", got, tt.want) t.Errorf("GetPostByAnthroveID() got = %v, want %v", got, tt.want)
} }
}) })
} }
} }
func TestGetPostBySourceURL(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 Tests
post := &models.Post{
BaseModel: models.BaseModel{
ID: fmt.Sprintf("%025s", "1"),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil {
t.Fatal("Could not create post", err)
}
source := models.Source{
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")},
DisplayName: "e621",
Domain: "e621.net",
Icon: "https://e621.net/icon.ico",
}
err = CreateSourceNode(ctx, gormDB, &source)
if err != nil {
t.Fatal("Could not create source", err)
}
err = EstablishAnthrovePostToSourceLink(ctx, gormDB, post.ID, source.Domain)
if err != nil {
t.Fatal("Could not create source reference", err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
sourceURL string
}
tests := []struct {
name string
args args
want *models.Post
wantErr bool
}{
{
name: "Test 1: Valid sourceURL",
args: args{
ctx: ctx,
db: gormDB,
sourceURL: source.Domain,
},
want: post,
wantErr: false,
},
{
name: "Test 2: Invalid sourceURL",
args: args{
ctx: ctx,
db: gormDB,
sourceURL: "1234",
},
want: nil,
wantErr: false,
},
{
name: "Test 3: No sourceURL",
args: args{
ctx: ctx,
db: gormDB,
sourceURL: "",
},
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetPostBySourceURL(tt.args.ctx, tt.args.db, tt.args.sourceURL)
if (err != nil) != tt.wantErr {
t.Errorf("GetPostBySourceURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkPost(got, tt.want) {
t.Errorf("GetPostBySourceURL() got = %v, want %v", got, tt.want)
}
})
}
}
func TestGetPostBySourceID(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 Tests
post := &models.Post{
BaseModel: models.BaseModel{
ID: fmt.Sprintf("%025s", "1"),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil {
t.Fatal("Could not create post", err)
}
source := models.Source{
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")},
DisplayName: "e621",
Domain: "e621.net",
Icon: "https://e621.net/icon.ico",
}
err = CreateSourceNode(ctx, gormDB, &source)
if err != nil {
t.Fatal("Could not create source", err)
}
err = EstablishAnthrovePostToSourceLink(ctx, gormDB, post.ID, source.Domain)
if err != nil {
t.Fatal("Could not create source reference", err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
sourceID string
}
tests := []struct {
name string
args args
want *models.Post
wantErr bool
}{
{
name: "Test 1: Valid sourceID",
args: args{
ctx: ctx,
db: gormDB,
sourceID: source.ID,
},
want: post,
wantErr: false,
},
{
name: "Test 2: Invalid sourceID",
args: args{
ctx: ctx,
db: gormDB,
sourceID: "1234",
},
want: nil,
wantErr: false,
},
{
name: "Test 3: No sourceID",
args: args{
ctx: ctx,
db: gormDB,
sourceID: "",
},
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetPostBySourceID(tt.args.ctx, tt.args.db, tt.args.sourceID)
if (err != nil) != tt.wantErr {
t.Errorf("GetPostBySourceID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkPost(got, tt.want) {
t.Errorf("GetPostBySourceID() got = %v, want %v", got, tt.want)
}
})
}
}
func checkPost(got *models.Post, want *models.Post) bool {
if got == nil {
return true
}
if got.ID != want.ID {
return false
}
if got.Rating != want.Rating {
return false
}
return true
}

View File

@ -7,7 +7,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, sourceDomain string, anthrovePostRelationship *models.PostReference) error { func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrovePostID string, sourceDomain string) error {
var source models.Source var source models.Source
var err error var err error
@ -19,9 +19,9 @@ func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrov
// Establish the relationship // Establish the relationship
err = db.WithContext(ctx).Create(models.PostReference{ err = db.WithContext(ctx).Create(models.PostReference{
PostID: string(anthrovePostID), PostID: anthrovePostID,
SourceID: source.ID, SourceID: source.ID,
URL: anthrovePostRelationship.URL, URL: sourceDomain,
}).Error }).Error
if err != nil { if err != nil {
@ -55,7 +55,7 @@ func EstablishUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID st
return nil return nil
} }
func CheckUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) { func CheckUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID string, anthrovePostID string) (bool, error) {
var count int64 var count int64
err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error
if err != nil { if err != nil {

View File

@ -2,13 +2,14 @@ package postgres
import ( import (
"context" "context"
"fmt"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test" "git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm" "gorm.io/gorm"
"testing" "testing"
) )
func TestEstablishAnthrovePostToSourceLink(t *testing.T) { func TestCheckUserToPostLink(t *testing.T) {
// Setup trow away container // Setup trow away container
ctx := context.Background() ctx := context.Background()
@ -19,8 +20,118 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
defer container.Terminate(ctx) defer container.Terminate(ctx)
// Setup Test // Setup Test
err = CreateUser(ctx, gormDB, "1")
if err != nil {
t.Fatal(err)
}
err = CreateAnthrovePostNode(ctx, gormDB, "1234", "safe") post := &models.Post{
BaseModel: models.BaseModel{
ID: fmt.Sprintf("%025s", "1"),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil {
t.Fatal(err)
}
err = EstablishUserToPostLink(ctx, gormDB, "1", post.ID)
if err != nil {
t.Fatal(err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthroveUserID string
anthrovePostID string
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
name: "Test 1: Valid AnthroveUserID and AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthrovePostID: post.ID,
},
want: true,
wantErr: false,
},
{
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthrovePostID: "qadw",
},
want: false,
wantErr: false,
},
{
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "123",
anthrovePostID: post.ID,
},
want: false,
wantErr: false,
},
{
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "123",
anthrovePostID: "123456",
},
want: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CheckUserToPostLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID)
if (err != nil) != tt.wantErr {
t.Errorf("CheckUserToPostLink() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CheckUserToPostLink() got = %v, want %v", got, tt.want)
}
})
}
}
func TestEstablishAnthrovePostToSourceLink(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
post := &models.Post{
BaseModel: models.BaseModel{
ID: fmt.Sprintf("%025s", "1"),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -36,12 +147,12 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
} }
// Test // Test
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthrovePostID models.AnthrovePostID anthrovePostID string
anthroveSourceDomain string sourceDomain string
anthrovePostRelationship *models.PostReference
} }
tests := []struct { tests := []struct {
name string name string
@ -51,47 +162,47 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
{ {
name: "Test 1: Valid AnthrovePostID and anthroveSourceDomain", name: "Test 1: Valid AnthrovePostID and anthroveSourceDomain",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: "1234", anthrovePostID: post.ID,
anthroveSourceDomain: "e621.net", sourceDomain: "e621.net",
}, },
wantErr: false, wantErr: false,
}, },
{ {
name: "Test 2: Invalid AnthrovePostID and Valid anthroveSourceDomain", name: "Test 2: Invalid AnthrovePostID and Valid anthroveSourceDomain",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: "123456", anthrovePostID: "123456",
anthroveSourceDomain: "e621.net", sourceDomain: "e621.net",
}, },
wantErr: true, wantErr: true,
}, },
{ {
name: "Test 3: Invalid anthroveSourceDomain and Valid AnthrovePostID", name: "Test 3: Invalid anthroveSourceDomain and Valid AnthrovePostID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: "1234", anthrovePostID: "1234",
anthroveSourceDomain: "fa.banana", sourceDomain: "fa.banana",
}, },
wantErr: true, wantErr: true,
}, },
{ {
name: "Test 4: Invalid anthroveSourceDomain and Invalid AnthrovePostID", name: "Test 4: Invalid anthroveSourceDomain and Invalid AnthrovePostID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: "696969", anthrovePostID: "696969",
anthroveSourceDomain: "hehe.funny.number", sourceDomain: "hehe.funny.number",
}, },
wantErr: true, wantErr: true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := EstablishAnthrovePostToSourceLink(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.anthroveSourceDomain); (err != nil) != tt.wantErr { if err := EstablishAnthrovePostToSourceLink(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.sourceDomain); (err != nil) != tt.wantErr {
t.Errorf("EstablishAnthrovePostToSourceLink() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("EstablishAnthrovePostToSourceLink() error = %v, wantErr %v", err, tt.wantErr)
} }
}) })
@ -113,7 +224,14 @@ func TestEstablishUserToPostLink(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = CreateAnthrovePostNode(ctx, gormDB, "1234", "safe") post := &models.Post{
BaseModel: models.BaseModel{
ID: fmt.Sprintf("%025s", "1"),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -122,8 +240,8 @@ func TestEstablishUserToPostLink(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthroveUserID models.AnthroveUserID anthroveUserID string
anthrovePostID models.AnthrovePostID anthrovePostID string
} }
tests := []struct { tests := []struct {
name string name string
@ -136,7 +254,7 @@ func TestEstablishUserToPostLink(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
anthrovePostID: "1234", anthrovePostID: post.ID,
}, },
wantErr: false, wantErr: false,
}, },
@ -179,101 +297,3 @@ func TestEstablishUserToPostLink(t *testing.T) {
}) })
} }
} }
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
err = CreateUser(ctx, gormDB, "1")
if err != nil {
t.Fatal(err)
}
err = CreateAnthrovePostNode(ctx, gormDB, "1234", "safe")
if err != nil {
t.Fatal(err)
}
err = EstablishUserToPostLink(ctx, gormDB, "1", "1234")
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: "1",
anthrovePostID: "1234",
},
want: true,
wantErr: false,
},
{
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthrovePostID: "123456",
},
want: false,
wantErr: false,
},
{
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "123",
anthrovePostID: "1234",
},
want: false,
wantErr: false,
},
{
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "123",
anthrovePostID: "123456",
},
want: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CheckUserToPostLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID)
if (err != nil) != tt.wantErr {
t.Errorf("CheckUserToPostLink() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CheckUserToPostLink() got = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -19,13 +19,13 @@ func TestCreateSourceNode(t *testing.T) {
// Setup Test // Setup Test
validAnthroveSource := &models.Source{ validSource := &models.Source{
DisplayName: "e621", DisplayName: "e621",
Domain: "e621.net", Domain: "e621.net",
Icon: "icon.e621.net", Icon: "icon.e621.net",
} }
invalidAnthroveSource := &models.Source{ invalidSource := &models.Source{
Domain: "", Domain: "",
} }
@ -45,7 +45,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveSource: validAnthroveSource, anthroveSource: validSource,
}, },
wantErr: false, wantErr: false,
}, },
@ -54,7 +54,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveSource: invalidAnthroveSource, anthroveSource: invalidSource,
}, },
wantErr: true, wantErr: true,
}, },
@ -63,7 +63,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveSource: validAnthroveSource, anthroveSource: validSource,
}, },
wantErr: false, wantErr: false,
}, },

View File

@ -8,7 +8,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error { func createTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
resultTag := db.WithContext(ctx).Where(tag).Create(tag) resultTag := db.WithContext(ctx).Where(tag).Create(tag)
@ -24,12 +24,16 @@ func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
return nil return nil
} }
func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.AnthrovePostID, tag *models.Tag) error { func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID string, tag *models.Tag) error {
if PostID == "" { if PostID == "" {
return fmt.Errorf("PostID is empty") return fmt.Errorf("PostID is empty")
} }
if tag == nil {
return fmt.Errorf("tag is nill")
}
pgPost := models.Post{ pgPost := models.Post{
BaseModel: models.BaseModel{ BaseModel: models.BaseModel{
ID: string(PostID), ID: string(PostID),

View File

@ -2,6 +2,7 @@ package postgres
import ( import (
"context" "context"
"fmt"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test" "git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm" "gorm.io/gorm"
@ -19,7 +20,14 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
// Setup Test // Setup Test
err = CreateAnthrovePostNode(ctx, gormDB, "1234", "safe") post := &models.Post{
BaseModel: models.BaseModel{
ID: fmt.Sprintf("%025s", "1"),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -33,7 +41,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
PostID models.AnthrovePostID PostID string
tag *models.Tag tag *models.Tag
} }
tests := []struct { tests := []struct {
@ -46,17 +54,17 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
PostID: "1234", PostID: post.ID,
tag: tag, tag: tag,
}, },
wantErr: false, wantErr: false,
}, },
{ {
name: "Test 2: Valid PostID and invalid Tag", name: "Test 2: Valid PostID and no Tag",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
PostID: "1234", PostID: post.ID,
tag: nil, tag: nil,
}, },
wantErr: true, wantErr: true,
@ -118,7 +126,7 @@ func TestGetTags(t *testing.T) {
} }
for _, tag := range tags { for _, tag := range tags {
err = CreateTag(ctx, gormDB, &tag) err = createTag(ctx, gormDB, &tag)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -8,7 +8,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error { func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID string) error {
if anthroveUserID == "" { if anthroveUserID == "" {
return fmt.Errorf("anthroveUserID cannot be empty") return fmt.Errorf("anthroveUserID cannot be empty")
@ -20,7 +20,7 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove
}, },
} }
err := db.WithContext(ctx).Create(&user).Error err := db.WithContext(ctx).FirstOrCreate(&user).Error
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,
@ -31,12 +31,17 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove
return nil return nil
} }
func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error { func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthroveUserID string, sourceDomain string, userID string, username string) error {
if anthroveUserID == "" || username == "" || userID == "" { if anthroveUserID == "" || username == "" || userID == "" {
return fmt.Errorf("anthroveUserID cannot be empty") return fmt.Errorf("anthroveUserID cannot be empty")
} }
err := CreateUser(ctx, db, anthroveUserID)
if err != nil {
return err
}
source := models.Source{ source := models.Source{
Domain: sourceDomain, Domain: sourceDomain,
} }
@ -49,10 +54,11 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthrove
} }
userSource := models.UserSource{ userSource := models.UserSource{
User: models.User{BaseModel: models.BaseModel{ID: string(anthroveUserID)}}, User: models.User{BaseModel: models.BaseModel{ID: anthroveUserID}},
SourceID: source.ID, SourceID: source.ID,
AccountUsername: username, AccountUsername: username,
AccountID: userID, AccountID: userID,
UserID: anthroveUserID,
} }
if err := db.WithContext(ctx).FirstOrCreate(&userSource).Error; err != nil { if err := db.WithContext(ctx).FirstOrCreate(&userSource).Error; err != nil {
@ -183,9 +189,9 @@ func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID
return userSourceMap, nil return userSourceMap, nil
} }
func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveUserID, error) { func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]string, error) {
var users []models.User var users []models.User
var userIDs []models.AnthroveUserID var userIDs []string
err := db.WithContext(ctx).Model(&models.User{}).Find(&users).Error err := db.WithContext(ctx).Model(&models.User{}).Find(&users).Error
if err != nil { if err != nil {
@ -194,7 +200,7 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveU
} }
for _, user := range users { for _, user := range users {
userIDs = append(userIDs, models.AnthroveUserID(user.ID)) userIDs = append(userIDs, user.ID)
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -204,7 +210,7 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveU
return userIDs, nil return userIDs, nil
} }
func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) { func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID string, skip int, limit int) (*models.FavoriteList, error) {
var userFavorites []models.UserFavorite var userFavorites []models.UserFavorite
var favoritePosts []models.Post var favoritePosts []models.Post
@ -257,6 +263,7 @@ func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, ant
name string name string
typeName string typeName string
}]int) }]int)
for _, userFavorite := range userFavorites { for _, userFavorite := range userFavorites {
var post models.Post var post models.Post
err = db.WithContext(ctx).Preload("Tags").First(&post, "id = ?", userFavorite.PostID).Error err = db.WithContext(ctx).Preload("Tags").First(&post, "id = ?", userFavorite.PostID).Error

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "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/test" "git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm" "gorm.io/gorm"
"reflect" "reflect"
@ -26,7 +25,7 @@ func TestCreateUser(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthroveUserID models.AnthroveUserID anthroveUserID string
} }
tests := []struct { tests := []struct {
name string name string
@ -86,7 +85,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthroveUserID models.AnthroveUserID anthroveUserID string
sourceDomain string sourceDomain string
userID string userID string
username string username string
@ -102,7 +101,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceDomain: "e621.net", sourceDomain: source.Domain,
userID: "e1", userID: "e1",
username: "marius", username: "marius",
}, },
@ -114,7 +113,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "2", anthroveUserID: "2",
sourceDomain: "e621.net", sourceDomain: source.Domain,
userID: "e1", userID: "e1",
username: "marius", username: "marius",
}, },
@ -126,7 +125,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "", anthroveUserID: "",
sourceDomain: "e621.net", sourceDomain: source.Domain,
userID: "e1", userID: "e1",
username: "marius", username: "marius",
}, },
@ -150,7 +149,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceDomain: "e621.net", sourceDomain: source.Domain,
userID: "", userID: "",
username: "marius", username: "marius",
}, },
@ -162,7 +161,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceDomain: "e621.net", sourceDomain: source.Domain,
userID: "aa", userID: "aa",
username: "", username: "",
}, },
@ -189,7 +188,7 @@ func TestGetAllAnthroveUserIDs(t *testing.T) {
// Setup Test // Setup Test
users := []models.AnthroveUserID{"1", "2", "3"} users := []string{"1", "2", "3"}
for _, user := range users { for _, user := range users {
err = CreateUser(ctx, gormDB, user) err = CreateUser(ctx, gormDB, user)
@ -206,7 +205,7 @@ func TestGetAllAnthroveUserIDs(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
args args args args
want []models.AnthroveUserID want []string
wantErr bool wantErr bool
}{ }{
{ {
@ -233,82 +232,6 @@ func TestGetAllAnthroveUserIDs(t *testing.T) {
} }
} }
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) { func TestGetSpecifiedUserSourceLink(t *testing.T) {
// Setup trow away container // Setup trow away container
ctx := context.Background() ctx := context.Background()
@ -319,31 +242,33 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) {
defer container.Terminate(ctx) defer container.Terminate(ctx)
// Setup Test // Setup Test
source := &models.Source{
DisplayName: "e621", expectedResult := make(map[string]models.UserSource)
Domain: "e621.net", expectedResult["e621"] = models.UserSource{
UserID: "e1",
AccountUsername: "euser",
Source: models.Source{
DisplayName: "e621",
Domain: "e621.net",
},
} }
source := &models.Source{
DisplayName: expectedResult["e621"].Source.DisplayName,
Domain: expectedResult["e621"].Source.Domain,
}
err = CreateSourceNode(ctx, gormDB, source) err = CreateSourceNode(ctx, gormDB, source)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
expectedResult := make(map[string]graphModels.AnthroveUserRelationship) err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", source.Domain, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Test
// Test
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
@ -353,7 +278,7 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
args args args args
want map[string]graphModels.AnthroveUserRelationship want map[string]models.UserSource
wantErr bool wantErr bool
}{ }{
{ {
@ -431,7 +356,7 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) {
return return
} }
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetSpecifiedUserSourceLink() got = %v, expectedResult %v", got, tt.want) t.Errorf("GetSpecifiedUserSourceLink() got = %v, want %v", got, tt.want)
} }
}) })
} }
@ -448,52 +373,41 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
// Setup Test // Setup Test
expectedResultPostsGraph := []graphModels.FavoritePost{ expectedResultPosts := []models.Post{
{ {
AnthrovePost: graphModels.AnthrovePost{ BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post1")},
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post1")), Rating: "safe",
Rating: "safe",
},
}, },
{ {
AnthrovePost: graphModels.AnthrovePost{
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post2")), BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post2")},
Rating: "safe", Rating: "safe",
},
}, },
{ {
AnthrovePost: graphModels.AnthrovePost{ BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post3")},
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post3")), Rating: "explicit",
Rating: "explicit",
},
}, },
{ {
AnthrovePost: graphModels.AnthrovePost{ BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post4")},
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post4")), Rating: "explicit",
Rating: "explicit",
},
}, },
{ {
AnthrovePost: graphModels.AnthrovePost{ BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post5")},
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post5")), Rating: "questionable",
Rating: "questionable",
},
}, },
{ {
AnthrovePost: graphModels.AnthrovePost{ BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post6")},
PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post6")), Rating: "safe",
Rating: "safe",
},
}, },
} }
expectedResult := &graphModels.FavoriteList{ expectedResult := &models.FavoriteList{
Posts: expectedResultPostsGraph, Posts: expectedResultPosts,
} }
expectedResult2 := &graphModels.FavoriteList{ expectedResult2 := &models.FavoriteList{
Posts: expectedResultPostsGraph[2:], Posts: expectedResultPosts[2:],
} }
expectedResult3 := &graphModels.FavoriteList{ expectedResult3 := &models.FavoriteList{
Posts: expectedResultPostsGraph[:3], Posts: expectedResultPosts[:3],
} }
err = CreateUser(ctx, gormDB, "1") err = CreateUser(ctx, gormDB, "1")
@ -501,39 +415,12 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
expectedResultPostsPg := []models.Post{ for _, expectedResultPost := range expectedResultPosts {
{ err = CreatePost(ctx, gormDB, &expectedResultPost)
BaseModel: models.BaseModel{ID: "Post1"},
Rating: "safe",
},
{
BaseModel: models.BaseModel{ID: "Post2"},
Rating: "safe",
},
{
BaseModel: models.BaseModel{ID: "Post3"},
Rating: "explicit",
},
{
BaseModel: models.BaseModel{ID: "Post4"},
Rating: "explicit",
},
{
BaseModel: models.BaseModel{ID: "Post5"},
Rating: "questionable",
},
{
BaseModel: models.BaseModel{ID: "Post6"},
Rating: "safe",
},
}
for _, expectedResultPost := range expectedResultPostsPg {
err = CreateAnthrovePostNode(ctx, gormDB, models.AnthrovePostID(expectedResultPost.ID), expectedResultPost.Rating)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = EstablishUserToPostLink(ctx, gormDB, "1", models.AnthrovePostID(expectedResultPost.ID)) err = EstablishUserToPostLink(ctx, gormDB, "1", expectedResultPost.ID)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -543,14 +430,14 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthroveUserID models.AnthroveUserID anthroveUserID string
skip int skip int
limit int limit int
} }
tests := []struct { tests := []struct {
name string name string
args args args args
want *graphModels.FavoriteList want *models.FavoriteList
wantErr bool wantErr bool
}{ }{
{ {
@ -620,7 +507,7 @@ func TestGetUserFavoritesCount(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
expectedResultPostsPg := []models.Post{ expectedResultPosts := []models.Post{
{ {
BaseModel: models.BaseModel{ID: "Post1"}, BaseModel: models.BaseModel{ID: "Post1"},
Rating: "safe", Rating: "safe",
@ -647,12 +534,12 @@ func TestGetUserFavoritesCount(t *testing.T) {
}, },
} }
for _, expectedResultPost := range expectedResultPostsPg { for _, post := range expectedResultPosts {
err = CreateAnthrovePostNode(ctx, gormDB, models.AnthrovePostID(expectedResultPost.ID), expectedResultPost.Rating) err = CreatePost(ctx, gormDB, &post)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = EstablishUserToPostLink(ctx, gormDB, "1", models.AnthrovePostID(expectedResultPost.ID)) err = EstablishUserToPostLink(ctx, gormDB, "1", post.ID)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -715,7 +602,7 @@ func TestGetUserFavoritesCount(t *testing.T) {
} }
} }
func TestGetUserSourceLink(t *testing.T) { func TestGetUserSourceLinks(t *testing.T) {
// Setup trow away containert // Setup trow away containert
ctx := context.Background() ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx) container, gormDB, err := test.StartPostgresContainer(ctx)
@ -725,51 +612,51 @@ func TestGetUserSourceLink(t *testing.T) {
defer container.Terminate(ctx) defer container.Terminate(ctx)
// Setup Test // Setup Test
eSource := &models.Source{
esource := &models.Source{
DisplayName: "e621", DisplayName: "e621",
Domain: "e621.net", Domain: "e621.net",
} }
err = CreateSourceNode(ctx, gormDB, esource) err = CreateSourceNode(ctx, gormDB, eSource)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fasource := &models.Source{ faSource := &models.Source{
DisplayName: "fa", DisplayName: "fa",
Domain: "fa.net", Domain: "fa.net",
} }
err = CreateSourceNode(ctx, gormDB, fasource) err = CreateSourceNode(ctx, gormDB, faSource)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
expectedResult := make(map[string]graphModels.AnthroveUserRelationship) expectedResult := make(map[string]models.UserSource)
expectedResult["e621"] = graphModels.AnthroveUserRelationship{ expectedResult["e621"] = models.UserSource{
UserID: "e1", UserID: "e1",
Username: "euser", AccountUsername: "e621-user",
Source: graphModels.AnthroveSource{ Source: models.Source{
DisplayName: esource.DisplayName, DisplayName: eSource.DisplayName,
Domain: esource.Domain, Domain: eSource.Domain,
}, },
} }
expectedResult["fa"] = graphModels.AnthroveUserRelationship{ expectedResult["fa"] = models.UserSource{
UserID: "fa1", UserID: "fa1",
Username: "fauser", AccountUsername: "fa-user",
Source: graphModels.AnthroveSource{ Source: models.Source{
DisplayName: fasource.DisplayName, DisplayName: faSource.DisplayName,
Domain: fasource.Domain, Domain: faSource.Domain,
}, },
} }
err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", esource.Domain, expectedResult["e621"].UserID, expectedResult["e621"].Username) err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", eSource.Domain, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", fasource.Domain, expectedResult["fa"].UserID, expectedResult["fa"].Username) err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", faSource.Domain, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Test // Test
type args struct { type args struct {
ctx context.Context ctx context.Context
@ -779,7 +666,7 @@ func TestGetUserSourceLink(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
args args args args
want map[string]graphModels.AnthroveUserRelationship want map[string]models.UserSource
wantErr bool wantErr bool
}{ }{
{ {
@ -829,11 +716,11 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
} }
for _, post := range posts { for _, post := range posts {
err = CreateAnthrovePostNode(ctx, gormDB, models.AnthrovePostID(post.ID), post.Rating) err = CreatePost(ctx, gormDB, &post)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = EstablishUserToPostLink(ctx, gormDB, "1", models.AnthrovePostID(post.ID)) err = EstablishUserToPostLink(ctx, gormDB, "1", post.ID)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -846,32 +733,32 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
} }
for i, tag := range tags { for i, tag := range tags {
err = CreateTagNodeWitRelation(ctx, gormDB, models.AnthrovePostID(posts[i].ID), &tag) err = CreateTagNodeWitRelation(ctx, gormDB, posts[i].ID, &tag)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
expectedResult := []graphModels.TagsWithFrequency{ expectedResult := []models.TagsWithFrequency{
{ {
Frequency: 1, Frequency: 1,
Tags: graphModels.AnthroveTag{ Tags: models.Tag{
Name: tags[0].Name, Name: tags[0].Name,
Type: string(tags[0].Type), Type: tags[0].Type,
}, },
}, },
{ {
Frequency: 1, Frequency: 1,
Tags: graphModels.AnthroveTag{ Tags: models.Tag{
Name: tags[1].Name, Name: tags[1].Name,
Type: string(tags[1].Type), Type: tags[1].Type,
}, },
}, },
{ {
Frequency: 1, Frequency: 1,
Tags: graphModels.AnthroveTag{ Tags: models.Tag{
Name: tags[2].Name, Name: tags[2].Name,
Type: string(tags[2].Type), Type: tags[2].Type,
}, },
}, },
} }
@ -885,11 +772,11 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
args args args args
want []graphModels.TagsWithFrequency want []models.TagsWithFrequency
wantErr bool wantErr bool
}{ }{
{ {
name: "", name: "Test 1: Get Data",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,

View File

@ -91,7 +91,8 @@ CREATE TABLE "UserSource"
( (
user_id TEXT REFERENCES "User" (id), user_id TEXT REFERENCES "User" (id),
source_id TEXT REFERENCES "Source" (id), source_id TEXT REFERENCES "Source" (id),
scrape_time_interval TEXT account_username TEXT, scrape_time_interval TEXT,
account_username TEXT,
account_id TEXT, account_id TEXT,
PRIMARY KEY (user_id, source_id), PRIMARY KEY (user_id, source_id),
UNIQUE (account_username, account_id) UNIQUE (account_username, account_id)

View File

@ -73,7 +73,7 @@ func (p *postgresqlConnection) AddSource(ctx context.Context, anthroveSource *mo
} }
func (p *postgresqlConnection) AddPost(ctx context.Context, anthrovePost *models.Post) error { func (p *postgresqlConnection) AddPost(ctx context.Context, anthrovePost *models.Post) error {
return postgres.CreateAnthrovePostNode(ctx, p.db, anthrovePost) return postgres.CreatePost(ctx, p.db, anthrovePost)
} }
func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error { func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error {