BREAKING CHANGE: V2 of thr SDK #12

Merged
fenpaws merged 124 commits from develop/postgresql into main 2024-07-01 20:46:28 +00:00
11 changed files with 556 additions and 408 deletions
Showing only changes of commit 7842976f4b - Show all commits

View File

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

View File

@ -2,6 +2,7 @@ package postgres
import (
"context"
"fmt"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test"
_ "github.com/lib/pq"
@ -19,11 +20,24 @@ func TestCreateAnthrovePostNode(t *testing.T) {
}
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 {
ctx context.Context
db *gorm.DB
anthrovePostID models.AnthrovePostID
anthroveRating models.Rating
ctx context.Context
db *gorm.DB
anthrovePost *models.Post
}
tests := []struct {
name string
@ -33,34 +47,32 @@ func TestCreateAnthrovePostNode(t *testing.T) {
{
name: "Test 1: Valid AnthrovePostID and Rating",
args: args{
ctx: context.Background(),
db: gormDB,
anthrovePostID: "1234",
anthroveRating: models.Rating("safe"),
ctx: context.Background(),
db: gormDB,
anthrovePost: validPost,
},
wantErr: false,
},
{
name: "Test 2: Invalid AnthrovePostID and Rating",
name: "Test 2: Invalid Rating",
args: args{
ctx: context.Background(),
db: gormDB,
anthrovePostID: "",
anthroveRating: "a4dsa4d",
ctx: context.Background(),
db: gormDB,
anthrovePost: invalidPost,
},
wantErr: true,
},
}
for _, tt := range tests {
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 {
t.Errorf("CreateAnthrovePostNode() error = %v, wantErr %v", err, tt.wantErr)
if err := CreatePost(tt.args.ctx, tt.args.db, tt.args.anthrovePost); (err != nil) != 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
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
@ -69,68 +81,284 @@ func TestCheckIfAnthrovePostNodeExistsByAnthroveID(t *testing.T) {
}
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 {
t.Fatal(err)
t.Fatal("Could not create post", err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthrovePostID models.AnthrovePostID
anthrovePostID string
}
tests := []struct {
name string
args args
want bool
want *models.Post
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{
ctx: ctx,
db: gormDB,
anthrovePostID: "1234",
},
want: true,
wantErr: false,
want: nil,
wantErr: true,
},
{
name: "Test 2: Invalid AnthroveID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: "123456",
},
want: false,
wantErr: false,
},
{
name: "Test 3: No AnthroveID",
name: "Test 3: No anthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: "",
},
want: false,
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
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 {
t.Errorf("CheckIfAnthrovePostNodeExistsByAnthroveID() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetPostByAnthroveID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CheckIfAnthrovePostNodeExistsByAnthroveID() got = %v, want %v", got, tt.want)
if !checkPost(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"
)
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 err error
@ -19,9 +19,9 @@ func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrov
// Establish the relationship
err = db.WithContext(ctx).Create(models.PostReference{
PostID: string(anthrovePostID),
PostID: anthrovePostID,
SourceID: source.ID,
URL: anthrovePostRelationship.URL,
URL: sourceDomain,
}).Error
if err != nil {
@ -55,7 +55,7 @@ func EstablishUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID st
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
err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error
if err != nil {

View File

@ -2,13 +2,14 @@ package postgres
import (
"context"
"fmt"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm"
"testing"
)
func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
func TestCheckUserToPostLink(t *testing.T) {
// Setup trow away container
ctx := context.Background()
@ -19,8 +20,118 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
defer container.Terminate(ctx)
// 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 {
t.Fatal(err)
}
@ -36,12 +147,12 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthrovePostID models.AnthrovePostID
anthroveSourceDomain string
ctx context.Context
db *gorm.DB
anthrovePostID string
sourceDomain string
anthrovePostRelationship *models.PostReference
}
tests := []struct {
name string
@ -51,47 +162,47 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
{
name: "Test 1: Valid AnthrovePostID and anthroveSourceDomain",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: "1234",
anthroveSourceDomain: "e621.net",
ctx: ctx,
db: gormDB,
anthrovePostID: post.ID,
sourceDomain: "e621.net",
},
wantErr: false,
},
{
name: "Test 2: Invalid AnthrovePostID and Valid anthroveSourceDomain",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: "123456",
anthroveSourceDomain: "e621.net",
ctx: ctx,
db: gormDB,
anthrovePostID: "123456",
sourceDomain: "e621.net",
},
wantErr: true,
},
{
name: "Test 3: Invalid anthroveSourceDomain and Valid AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: "1234",
anthroveSourceDomain: "fa.banana",
ctx: ctx,
db: gormDB,
anthrovePostID: "1234",
sourceDomain: "fa.banana",
},
wantErr: true,
},
{
name: "Test 4: Invalid anthroveSourceDomain and Invalid AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: "696969",
anthroveSourceDomain: "hehe.funny.number",
ctx: ctx,
db: gormDB,
anthrovePostID: "696969",
sourceDomain: "hehe.funny.number",
},
wantErr: true,
},
}
for _, tt := range tests {
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)
}
})
@ -113,7 +224,14 @@ func TestEstablishUserToPostLink(t *testing.T) {
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)
}
@ -122,8 +240,8 @@ func TestEstablishUserToPostLink(t *testing.T) {
type args struct {
ctx context.Context
db *gorm.DB
anthroveUserID models.AnthroveUserID
anthrovePostID models.AnthrovePostID
anthroveUserID string
anthrovePostID string
}
tests := []struct {
name string
@ -136,7 +254,7 @@ func TestEstablishUserToPostLink(t *testing.T) {
ctx: ctx,
db: gormDB,
anthroveUserID: "1",
anthrovePostID: "1234",
anthrovePostID: post.ID,
},
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
validAnthroveSource := &models.Source{
validSource := &models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "icon.e621.net",
}
invalidAnthroveSource := &models.Source{
invalidSource := &models.Source{
Domain: "",
}
@ -45,7 +45,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveSource: validAnthroveSource,
anthroveSource: validSource,
},
wantErr: false,
},
@ -54,7 +54,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveSource: invalidAnthroveSource,
anthroveSource: invalidSource,
},
wantErr: true,
},
@ -63,7 +63,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveSource: validAnthroveSource,
anthroveSource: validSource,
},
wantErr: false,
},

View File

@ -8,7 +8,7 @@ import (
"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)
@ -24,12 +24,16 @@ func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
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 == "" {
return fmt.Errorf("PostID is empty")
}
if tag == nil {
return fmt.Errorf("tag is nill")
}
pgPost := models.Post{
BaseModel: models.BaseModel{
ID: string(PostID),

View File

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

View File

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

View File

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

View File

@ -91,7 +91,8 @@ CREATE TABLE "UserSource"
(
user_id TEXT REFERENCES "User" (id),
source_id TEXT REFERENCES "Source" (id),
scrape_time_interval TEXT account_username TEXT,
scrape_time_interval TEXT,
account_username TEXT,
account_id TEXT,
PRIMARY KEY (user_id, source_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 {
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 {