2024-06-28 12:16:28 +00:00
|
|
|
package database
|
2024-06-24 12:28:09 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-24 20:07:34 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-06-26 07:10:04 +00:00
|
|
|
"reflect"
|
2024-06-25 12:50:56 +00:00
|
|
|
"testing"
|
2024-07-01 11:42:53 +00:00
|
|
|
"time"
|
2024-06-25 12:50:56 +00:00
|
|
|
|
2024-07-02 08:11:42 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/v2/internal/postgres"
|
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/v2/pkg/models"
|
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/v2/test"
|
2024-06-26 07:10:04 +00:00
|
|
|
"gorm.io/gorm"
|
2024-06-24 12:28:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewPostgresqlConnection(t *testing.T) {
|
|
|
|
// Test
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2024-06-28 12:16:28 +00:00
|
|
|
want OtterSpace
|
2024-06-24 12:28:09 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Create new postgresql connection",
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.want != NewPostgresqlConnection() {
|
|
|
|
} else {
|
|
|
|
t.Errorf("NewPostgresqlConnection() = %s", tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-24 20:07:34 +00:00
|
|
|
|
|
|
|
func Test_postgresqlConnection_Connect(t *testing.T) {
|
|
|
|
|
|
|
|
// Setup trow away container
|
|
|
|
ctx := context.Background()
|
|
|
|
container, _, err := test.StartPostgresContainer(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start PostgreSQL container: %v", err)
|
|
|
|
}
|
|
|
|
defer container.Terminate(ctx)
|
|
|
|
|
|
|
|
// Setup Tests
|
|
|
|
|
|
|
|
dbConfig, err := test.DatabaseModesFromConnectionString(ctx, container)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not parse database config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
in0 context.Context
|
|
|
|
config models.DatabaseConfig
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Connect to postgresql connection",
|
|
|
|
args: args{
|
|
|
|
in0: ctx,
|
|
|
|
config: *dbConfig,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 1: Empty connection config",
|
|
|
|
args: args{
|
|
|
|
in0: ctx,
|
|
|
|
config: models.DatabaseConfig{},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{}
|
|
|
|
if err := p.Connect(tt.args.in0, tt.args.config); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Connect() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_CreateUserWithRelationToSource(t *testing.T) {
|
|
|
|
// Setup trow away container
|
|
|
|
ctx := context.Background()
|
|
|
|
container, gormDB, err := test.StartPostgresContainer(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start PostgreSQL container: %v", err)
|
|
|
|
}
|
|
|
|
defer container.Terminate(ctx)
|
|
|
|
|
|
|
|
// Setup Test
|
2024-06-26 07:10:04 +00:00
|
|
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
|
2024-06-24 20:07:34 +00:00
|
|
|
|
|
|
|
source := &models.Source{
|
2024-06-26 07:10:04 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1},
|
2024-06-24 20:07:34 +00:00
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "icon.e621.net",
|
|
|
|
}
|
|
|
|
err = postgres.CreateSource(ctx, gormDB, source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
sourceID models.AnthroveSourceID
|
|
|
|
accountId string
|
|
|
|
accountUsername string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid anthroveUserID, sourceID, accountId, accountUsername",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-26 07:10:04 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-25 12:50:56 +00:00
|
|
|
sourceID: source.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
accountId: "e1",
|
|
|
|
accountUsername: "marius",
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid anthroveUserID, valid sourceID, accountId, accountUsername",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "2",
|
2024-06-25 12:50:56 +00:00
|
|
|
sourceID: source.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
accountId: "e1",
|
|
|
|
accountUsername: "marius",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Empty anthroveUserID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "",
|
2024-06-25 12:50:56 +00:00
|
|
|
sourceID: source.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
accountId: "e1",
|
|
|
|
accountUsername: "marius",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: invalid sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-26 07:10:04 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-24 20:07:34 +00:00
|
|
|
sourceID: "fa.net",
|
|
|
|
accountId: "e1",
|
|
|
|
accountUsername: "marius",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 5: no accountId",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-26 07:10:04 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-25 12:50:56 +00:00
|
|
|
sourceID: source.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
accountId: "",
|
|
|
|
accountUsername: "marius",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 6: no accountUsername",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-26 07:10:04 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-25 12:50:56 +00:00
|
|
|
sourceID: source.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
accountId: "aa",
|
|
|
|
accountUsername: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.CreateUserWithRelationToSource(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.accountId, tt.args.accountUsername); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateUserWithRelationToSource() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_CreateSource(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
|
|
|
|
|
|
|
|
validSource := &models.Source{
|
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "icon.e621.net",
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidSource := &models.Source{
|
|
|
|
Domain: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthroveSource *models.Source
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid anthroveSource",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveSource: validSource,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: inValid anthroveSource",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveSource: invalidSource,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: unique anthroveSource",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveSource: validSource,
|
|
|
|
},
|
2024-06-26 07:10:04 +00:00
|
|
|
wantErr: true,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.CreateSource(tt.args.ctx, tt.args.anthroveSource); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateSource() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_CreatePost(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
|
|
|
|
|
2024-06-26 07:10:04 +00:00
|
|
|
validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
|
2024-06-24 20:07:34 +00:00
|
|
|
validPost := &models.Post{
|
2024-06-25 12:50:56 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
2024-06-26 07:10:04 +00:00
|
|
|
ID: validPostID1,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
Rating: "safe",
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidPost := &models.Post{
|
|
|
|
Rating: "error",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthrovePost *models.Post
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid AnthrovePostID and Rating",
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
anthrovePost: validPost,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid Rating",
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
anthrovePost: invalidPost,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.CreatePost(tt.args.ctx, tt.args.anthrovePost); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreatePost() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_CreateTagAndReferenceToPost(t *testing.T) {
|
|
|
|
// Setup trow away container
|
|
|
|
ctx := context.Background()
|
|
|
|
container, gormDB, err := test.StartPostgresContainer(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start PostgreSQL container: %v", err)
|
|
|
|
}
|
|
|
|
defer container.Terminate(ctx)
|
|
|
|
|
|
|
|
// Setup Test
|
2024-06-26 07:10:04 +00:00
|
|
|
validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
2024-06-24 20:07:34 +00:00
|
|
|
|
|
|
|
post := &models.Post{
|
2024-06-25 12:50:56 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
2024-06-26 07:10:04 +00:00
|
|
|
ID: validPostID1,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
Rating: "safe",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreatePost(ctx, gormDB, post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tag := &models.Tag{
|
|
|
|
Name: "JayTheFerret",
|
|
|
|
Type: "artist",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthrovePostID models.AnthrovePostID
|
|
|
|
anthroveTag *models.Tag
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid PostID and Tag",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-25 12:50:56 +00:00
|
|
|
anthrovePostID: post.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
anthroveTag: tag,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Valid PostID and no Tag",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-25 12:50:56 +00:00
|
|
|
anthrovePostID: post.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
anthroveTag: nil,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Invalid PostID and valid Tag",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthrovePostID: "123456",
|
|
|
|
anthroveTag: tag,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: No PostID and valid Tag",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthrovePostID: "",
|
|
|
|
anthroveTag: tag,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
2024-06-28 12:16:28 +00:00
|
|
|
if err := p.CreatePostWithReferenceToTagAnd(tt.args.ctx, tt.args.anthrovePostID, tt.args.anthroveTag); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreatePostWithReferenceToTagAnd() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-24 20:07:34 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T) {
|
|
|
|
// Setup trow away container
|
|
|
|
ctx := context.Background()
|
|
|
|
container, gormDB, err := test.StartPostgresContainer(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start PostgreSQL container: %v", err)
|
|
|
|
}
|
|
|
|
defer container.Terminate(ctx)
|
|
|
|
|
|
|
|
// Setup Test
|
2024-06-26 07:10:04 +00:00
|
|
|
validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
|
|
|
|
|
2024-06-24 20:07:34 +00:00
|
|
|
post := &models.Post{
|
2024-06-25 12:50:56 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
2024-06-26 07:10:04 +00:00
|
|
|
ID: validPostID1,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
Rating: "safe",
|
|
|
|
}
|
|
|
|
|
|
|
|
source := &models.Source{
|
2024-06-26 07:10:04 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1},
|
2024-06-24 20:07:34 +00:00
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "icon.e621.net",
|
|
|
|
}
|
2024-06-26 07:10:04 +00:00
|
|
|
|
|
|
|
err = postgres.CreatePost(ctx, gormDB, post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-24 20:07:34 +00:00
|
|
|
err = postgres.CreateSource(ctx, gormDB, source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthrovePostID models.AnthrovePostID
|
|
|
|
sourceDomain models.AnthroveSourceDomain
|
2024-06-27 19:37:29 +00:00
|
|
|
postURl models.AnthrovePostURL
|
2024-06-24 20:07:34 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid AnthrovePostID and anthroveSourceDomain",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-25 12:50:56 +00:00
|
|
|
anthrovePostID: post.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
sourceDomain: "e621.net",
|
2024-06-27 19:37:29 +00:00
|
|
|
postURl: "http://e621.net/post/eeasd",
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid AnthrovePostID and Valid anthroveSourceDomain",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthrovePostID: "123456",
|
|
|
|
sourceDomain: "e621.net",
|
2024-06-27 19:37:29 +00:00
|
|
|
postURl: "",
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Invalid anthroveSourceDomain and Valid AnthrovePostID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthrovePostID: "1234",
|
|
|
|
sourceDomain: "fa.banana",
|
2024-06-27 19:37:29 +00:00
|
|
|
postURl: "",
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: Invalid anthroveSourceDomain and Invalid AnthrovePostID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthrovePostID: "696969",
|
2024-06-27 19:37:29 +00:00
|
|
|
postURl: "",
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
2024-06-28 12:16:28 +00:00
|
|
|
if err := p.CreatePostReference(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain, tt.args.postURl, models.PostReferenceConfig{}); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreatePostReference() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-24 20:07:34 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_CreateReferenceBetweenUserAndPost(t *testing.T) {
|
|
|
|
// Setup trow away container
|
|
|
|
ctx := context.Background()
|
|
|
|
container, gormDB, err := test.StartPostgresContainer(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start PostgreSQL container: %v", err)
|
|
|
|
}
|
|
|
|
defer container.Terminate(ctx)
|
|
|
|
|
|
|
|
// Setup Test
|
2024-06-26 07:10:04 +00:00
|
|
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
|
|
|
|
err = postgres.CreateUser(ctx, gormDB, validUserID)
|
2024-06-24 20:07:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
post := &models.Post{
|
2024-06-25 12:50:56 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
2024-06-26 07:10:04 +00:00
|
|
|
ID: validPostID1,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
Rating: "safe",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreatePost(ctx, gormDB, post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
anthrovePostID models.AnthrovePostID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid AnthroveUserID and AnthrovePostID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-26 07:10:04 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-25 12:50:56 +00:00
|
|
|
anthrovePostID: post.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-26 07:10:04 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-24 20:07:34 +00:00
|
|
|
anthrovePostID: "123456",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "123",
|
|
|
|
anthrovePostID: "1234",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "123",
|
|
|
|
anthrovePostID: "123456",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.CreateReferenceBetweenUserAndPost(tt.args.ctx, tt.args.anthroveUserID, tt.args.anthrovePostID); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_CheckReferenceBetweenUserAndPost(t *testing.T) {
|
|
|
|
// Setup trow away container
|
|
|
|
ctx := context.Background()
|
|
|
|
container, gormDB, err := test.StartPostgresContainer(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start PostgreSQL container: %v", err)
|
|
|
|
}
|
|
|
|
defer container.Terminate(ctx)
|
|
|
|
|
|
|
|
// Setup Test
|
2024-06-26 07:10:04 +00:00
|
|
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
|
|
|
|
err = postgres.CreateUser(ctx, gormDB, validUserID)
|
2024-06-24 20:07:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
post := &models.Post{
|
2024-06-25 12:50:56 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
2024-06-26 07:10:04 +00:00
|
|
|
ID: validPostID1,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
Rating: "safe",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreatePost(ctx, gormDB, post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-26 07:10:04 +00:00
|
|
|
err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID)
|
2024-06-24 20:07:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
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,
|
2024-06-26 07:10:04 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-25 12:50:56 +00:00
|
|
|
anthrovePostID: post.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-26 07:10:04 +00:00
|
|
|
anthroveUserID: validUserID,
|
2024-06-24 20:07:34 +00:00
|
|
|
anthrovePostID: "qadw",
|
|
|
|
},
|
|
|
|
want: false,
|
2024-06-26 07:10:04 +00:00
|
|
|
wantErr: true,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "123",
|
2024-06-25 12:50:56 +00:00
|
|
|
anthrovePostID: post.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
want: false,
|
2024-06-26 07:10:04 +00:00
|
|
|
wantErr: true,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "123",
|
|
|
|
anthrovePostID: "123456",
|
|
|
|
},
|
|
|
|
want: false,
|
2024-06-26 07:10:04 +00:00
|
|
|
wantErr: true,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
2024-06-28 12:16:28 +00:00
|
|
|
got, err := p.CheckIfUserHasPostAsFavorite(tt.args.ctx, tt.args.anthroveUserID, tt.args.anthrovePostID)
|
2024-06-24 20:07:34 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-28 10:15:51 +00:00
|
|
|
t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-24 20:07:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
2024-06-28 10:15:51 +00:00
|
|
|
t.Errorf("CheckIfUserHasPostAsFavorite() got = %v, want %v", got, tt.want)
|
2024-06-24 20:07:34 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetPostByAnthroveID(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
|
2024-06-26 07:10:04 +00:00
|
|
|
validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
2024-06-24 20:07:34 +00:00
|
|
|
|
|
|
|
post := &models.Post{
|
2024-06-25 12:50:56 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
2024-06-26 07:10:04 +00:00
|
|
|
ID: validPostID1,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
Rating: "safe",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreatePost(ctx, gormDB, post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create post", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthrovePost models.AnthrovePostID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *models.Post
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid anthrovePostID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-25 12:50:56 +00:00
|
|
|
anthrovePost: post.ID,
|
2024-06-24 20:07:34 +00:00
|
|
|
},
|
|
|
|
want: post,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: No anthrovePostID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthrovePost: "nil",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetPostByAnthroveID(tt.args.ctx, tt.args.anthrovePost)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetPostByAnthroveID() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !checkPost(got, tt.want) {
|
|
|
|
t.Errorf("GetPostByAnthroveID() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 07:10:04 +00:00
|
|
|
func Test_postgresqlConnection_GetPostByURL(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)
|
2024-06-24 20:07:34 +00:00
|
|
|
|
2024-06-26 07:10:04 +00:00
|
|
|
// Setup Tests
|
|
|
|
validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
|
|
|
|
post := &models.Post{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
|
|
|
ID: validPostID1,
|
|
|
|
},
|
|
|
|
Rating: "safe",
|
2024-06-24 20:07:34 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 07:10:04 +00:00
|
|
|
err = postgres.CreatePost(ctx, gormDB, post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create post", err)
|
2024-06-24 20:07:34 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 07:10:04 +00:00
|
|
|
source := models.Source{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
|
|
|
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
|
|
|
|
},
|
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "https://e621.net/icon.ico",
|
2024-06-24 20:07:34 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 07:10:04 +00:00
|
|
|
err = postgres.CreateSource(ctx, gormDB, &source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create source", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:05:21 +00:00
|
|
|
err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://e62asdwad.com/asdas", models.PostReferenceConfig{})
|
2024-06-26 07:10:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create source reference", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
sourceUrl string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *models.Post
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid sourceUrl",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-06-27 19:37:29 +00:00
|
|
|
sourceUrl: "https://e62asdwad.com/asdas",
|
2024-06-26 07:10:04 +00:00
|
|
|
},
|
|
|
|
want: post,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid sourceUrl",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
sourceUrl: "1234",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: No sourceUrl",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
sourceUrl: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetPostByURL(tt.args.ctx, tt.args.sourceUrl)
|
|
|
|
if (err != nil) != tt.wantErr {
|
2024-07-01 19:38:09 +00:00
|
|
|
t.Errorf("GetPostBySourceURL() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-26 07:10:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !checkPost(got, tt.want) {
|
2024-07-01 19:38:09 +00:00
|
|
|
t.Errorf("GetPostBySourceURL() got = %v, want %v", got, tt.want)
|
2024-06-26 07:10:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetPostBySourceID(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
|
|
|
|
validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
|
|
|
|
|
|
|
|
post := &models.Post{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
|
|
|
ID: validPostID1,
|
|
|
|
},
|
|
|
|
Rating: "safe",
|
|
|
|
}
|
|
|
|
|
|
|
|
source := models.Source{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
|
|
|
ID: validSourceID1,
|
|
|
|
},
|
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "https://e621.net/icon.ico",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreatePost(ctx, gormDB, post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create post", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateSource(ctx, gormDB, &source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create source", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:05:21 +00:00
|
|
|
err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://easd15aed.de/asd", models.PostReferenceConfig{})
|
2024-06-26 07:10:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create source reference", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
sourceID models.AnthroveSourceID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *models.Post
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
sourceID: source.ID,
|
|
|
|
},
|
|
|
|
want: post,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
sourceID: "1234",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: No sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
sourceID: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetPostBySourceID(tt.args.ctx, 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 Test_postgresqlConnection_GetUserFavoritesCount(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
|
|
|
|
|
|
|
|
validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
|
|
|
|
validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2"))
|
|
|
|
validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3"))
|
|
|
|
validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4"))
|
|
|
|
validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5"))
|
|
|
|
validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6"))
|
|
|
|
|
|
|
|
expectedResultPosts := []models.Post{
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1},
|
|
|
|
Rating: "safe",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2},
|
|
|
|
Rating: "safe",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3},
|
|
|
|
Rating: "explicit",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4},
|
|
|
|
Rating: "explicit",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5},
|
|
|
|
Rating: "questionable",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6},
|
|
|
|
Rating: "safe",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, post := range expectedResultPosts {
|
|
|
|
err = postgres.CreatePost(ctx, gormDB, &post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, post.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
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,
|
|
|
|
anthroveUserID: validAnthroveUserID,
|
|
|
|
},
|
|
|
|
want: 6,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid anthroveUserID and 6 favorite posts",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "2",
|
|
|
|
},
|
|
|
|
want: 0,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: no anthroveUserID and 6 favorite posts",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "",
|
|
|
|
},
|
|
|
|
want: 0,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetUserFavoritesCount(tt.args.ctx, 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 Test_postgresqlConnection_GetUserSourceLinks(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
|
|
|
|
validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
|
|
|
|
validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
|
|
|
|
validSourceID2 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source2"))
|
|
|
|
|
|
|
|
eSource := &models.Source{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1},
|
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
}
|
|
|
|
err = postgres.CreateSource(ctx, gormDB, eSource)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
faSource := &models.Source{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID2},
|
|
|
|
DisplayName: "fa",
|
|
|
|
Domain: "fa.net",
|
|
|
|
}
|
|
|
|
err = postgres.CreateSource(ctx, gormDB, faSource)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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"] = models.UserSource{
|
|
|
|
UserID: "fa1",
|
|
|
|
AccountUsername: "fa-user",
|
|
|
|
Source: models.Source{
|
|
|
|
DisplayName: faSource.DisplayName,
|
|
|
|
Domain: faSource.Domain,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want map[string]models.UserSource
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Get Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validAnthroveUserID,
|
|
|
|
},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
2024-06-28 10:15:51 +00:00
|
|
|
got, err := p.GetAllUserSources(tt.args.ctx, tt.args.anthroveUserID)
|
2024-06-26 07:10:04 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-28 10:15:51 +00:00
|
|
|
t.Errorf("GetAllUserSources() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-26 07:10:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
2024-06-28 10:15:51 +00:00
|
|
|
t.Errorf("GetAllUserSources() got = %v, want %v", got, tt.want)
|
2024-06-26 07:10:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetUserSourceBySourceID(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
|
|
|
|
|
|
|
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
invalidUserID := models.AnthroveUserID("XXX")
|
|
|
|
|
2024-06-27 19:37:29 +00:00
|
|
|
validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
|
|
|
|
|
|
|
|
source := &models.Source{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
|
|
|
ID: validSourceID,
|
|
|
|
},
|
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
2024-06-28 12:16:28 +00:00
|
|
|
Icon: "https://e621.icon",
|
2024-06-27 19:37:29 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 12:16:28 +00:00
|
|
|
expectedResult := &models.UserSource{
|
|
|
|
UserID: string(validUserID),
|
2024-06-26 07:10:04 +00:00
|
|
|
AccountUsername: "euser",
|
|
|
|
Source: models.Source{
|
2024-06-28 12:16:28 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: source.ID},
|
2024-06-27 19:37:29 +00:00
|
|
|
DisplayName: source.DisplayName,
|
|
|
|
Domain: source.Domain,
|
|
|
|
Icon: source.Icon,
|
2024-06-26 07:10:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateSource(ctx, gormDB, source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-28 12:16:28 +00:00
|
|
|
err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult.UserID, expectedResult.AccountUsername)
|
2024-06-26 07:10:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
sourceID models.AnthroveSourceID
|
|
|
|
}
|
2024-06-28 12:16:28 +00:00
|
|
|
|
2024-06-26 07:10:04 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2024-06-28 12:16:28 +00:00
|
|
|
want *models.UserSource
|
2024-06-26 07:10:04 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid AnthroveUserID and sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: source.ID,
|
|
|
|
},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid AnthroveUserID and valid sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: invalidUserID,
|
|
|
|
sourceID: source.ID,
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Valid AnthroveUserID and invalid sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: "fa",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: No AnthroveUserID and Valid sourceID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "",
|
|
|
|
sourceID: source.ID,
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 5: Valid AnthroveUserID and No SourceDisplayName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "1",
|
|
|
|
sourceID: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 6: No AnthroveUserID and No SourceDisplayName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "",
|
|
|
|
sourceID: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetUserSourceBySourceID(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
2024-06-28 12:16:28 +00:00
|
|
|
if !checkUserSource(got, tt.want) {
|
2024-06-26 07:10:04 +00:00
|
|
|
t.Errorf("GetUserSourceBySourceID() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-28 12:16:28 +00:00
|
|
|
func Test_postgresqlConnection_GetAllUsers(t *testing.T) {
|
2024-06-26 07:10:04 +00:00
|
|
|
// Setup trow away container
|
|
|
|
ctx := context.Background()
|
|
|
|
container, gormDB, err := test.StartPostgresContainer(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start PostgreSQL container: %v", err)
|
|
|
|
}
|
|
|
|
defer container.Terminate(ctx)
|
|
|
|
|
|
|
|
// Setup Test
|
|
|
|
validUserID01 := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
validUserID02 := models.AnthroveUserID(fmt.Sprintf("%025s", "User2"))
|
|
|
|
validUserID03 := models.AnthroveUserID(fmt.Sprintf("%025s", "User3"))
|
|
|
|
|
2024-06-28 12:16:28 +00:00
|
|
|
users := []models.User{
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID01},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID02},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID03},
|
|
|
|
},
|
|
|
|
}
|
2024-06-26 07:10:04 +00:00
|
|
|
|
|
|
|
for _, user := range users {
|
2024-06-28 12:16:28 +00:00
|
|
|
err = postgres.CreateUser(ctx, gormDB, user.ID)
|
2024-06-26 07:10:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2024-06-28 12:16:28 +00:00
|
|
|
want []models.User
|
2024-06-26 07:10:04 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Get Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
},
|
|
|
|
want: users,
|
|
|
|
wantErr: false,
|
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
2024-06-28 12:16:28 +00:00
|
|
|
got, err := p.GetAllUsers(tt.args.ctx)
|
2024-06-26 07:10:04 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-28 10:15:51 +00:00
|
|
|
t.Errorf("GetAllUsers() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-26 07:10:04 +00:00
|
|
|
return
|
|
|
|
}
|
2024-06-28 12:16:28 +00:00
|
|
|
if !checkUser(got, tt.want) {
|
2024-06-28 10:15:51 +00:00
|
|
|
t.Errorf("GetAllUsers() got = %v, want %v", got, tt.want)
|
2024-06-26 07:10:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetUserFavoriteWithPagination(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
|
|
|
|
validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
|
|
|
|
validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2"))
|
|
|
|
validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3"))
|
|
|
|
validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4"))
|
|
|
|
validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5"))
|
|
|
|
validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6"))
|
|
|
|
|
|
|
|
expectedResultPosts := []models.Post{
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1},
|
|
|
|
Rating: "safe",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2},
|
|
|
|
Rating: "safe",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3},
|
|
|
|
Rating: "explicit",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4},
|
|
|
|
Rating: "explicit",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5},
|
|
|
|
Rating: "questionable",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6},
|
|
|
|
Rating: "safe",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expectedResult := &models.FavoriteList{
|
|
|
|
Posts: expectedResultPosts,
|
|
|
|
}
|
|
|
|
expectedResult2 := &models.FavoriteList{
|
|
|
|
Posts: expectedResultPosts[2:],
|
|
|
|
}
|
|
|
|
expectedResult3 := &models.FavoriteList{
|
|
|
|
Posts: expectedResultPosts[:3],
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, expectedResultPost := range expectedResultPosts {
|
|
|
|
err = postgres.CreatePost(ctx, gormDB, &expectedResultPost)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, expectedResultPost.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
skip int
|
|
|
|
limit int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *models.FavoriteList
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid AnthroveUserID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validAnthroveUserID,
|
|
|
|
skip: 0,
|
|
|
|
limit: 2000,
|
|
|
|
},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Skip first two",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validAnthroveUserID,
|
|
|
|
skip: 2,
|
|
|
|
limit: 2000,
|
|
|
|
},
|
|
|
|
want: expectedResult2,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Limit of 3",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validAnthroveUserID,
|
|
|
|
skip: 0,
|
|
|
|
limit: 3,
|
|
|
|
},
|
|
|
|
want: expectedResult3,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
2024-06-28 10:15:51 +00:00
|
|
|
got, err := p.GetAllUserFavoritesWithPagination(tt.args.ctx, tt.args.anthroveUserID, tt.args.skip, tt.args.limit)
|
2024-06-26 07:10:04 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-28 10:15:51 +00:00
|
|
|
t.Errorf("GetAllUserFavoritesWithPagination() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-26 07:10:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
2024-06-28 10:15:51 +00:00
|
|
|
t.Errorf("GetAllUserFavoritesWithPagination() got = %v, want %v", got, tt.want)
|
2024-06-26 07:10:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetUserTagWitRelationToFavedPosts(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
|
|
|
|
validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
|
|
|
|
validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
|
|
|
|
validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2"))
|
|
|
|
validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3"))
|
|
|
|
|
|
|
|
err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
posts := []models.Post{
|
|
|
|
{BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, Rating: "safe"},
|
|
|
|
{BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, Rating: "safe"},
|
|
|
|
{BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, Rating: "explicit"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, post := range posts {
|
|
|
|
err = postgres.CreatePost(ctx, gormDB, &post)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, models.AnthrovePostID(post.ID))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := []models.Tag{
|
|
|
|
{Name: "JayTheFerret", Type: "artist"},
|
|
|
|
{Name: "Ferret", Type: "species"},
|
|
|
|
{Name: "Jay", Type: "character"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tag := range tags {
|
|
|
|
err = postgres.CreateTagAndReferenceToPost(ctx, gormDB, posts[i].ID, &tag)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedResult := []models.TagsWithFrequency{
|
|
|
|
{
|
|
|
|
Frequency: 1,
|
|
|
|
Tags: models.Tag{
|
|
|
|
Name: tags[0].Name,
|
|
|
|
Type: tags[0].Type,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Frequency: 1,
|
|
|
|
Tags: models.Tag{
|
2024-06-27 19:37:29 +00:00
|
|
|
Name: tags[2].Name,
|
|
|
|
Type: tags[2].Type,
|
2024-06-26 07:10:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Frequency: 1,
|
|
|
|
Tags: models.Tag{
|
2024-06-27 19:37:29 +00:00
|
|
|
Name: tags[1].Name,
|
|
|
|
Type: tags[1].Type,
|
2024-06-26 07:10:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []models.TagsWithFrequency
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Get Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validAnthroveUserID,
|
|
|
|
},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
2024-06-28 10:15:51 +00:00
|
|
|
got, err := p.GetAllTagsFromUser(tt.args.ctx, tt.args.anthroveUserID)
|
2024-06-26 07:10:04 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-06-28 10:15:51 +00:00
|
|
|
t.Errorf("GetAllTagsFromUser() error = %v, wantErr %v", err, tt.wantErr)
|
2024-06-26 07:10:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
2024-06-28 10:15:51 +00:00
|
|
|
t.Errorf("GetAllTagsFromUser() got = %v, want %v", got, tt.want)
|
2024-06-26 07:10:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetAllTags(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
|
|
|
|
|
|
|
|
tags := []models.Tag{
|
|
|
|
{
|
|
|
|
Name: "JayTheFerret",
|
|
|
|
Type: "artist",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "anthro",
|
|
|
|
Type: "general",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "soxx",
|
|
|
|
Type: "character",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
2024-07-01 20:06:36 +00:00
|
|
|
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type)
|
2024-06-26 07:10:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []models.Tag
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Get Tags",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
},
|
|
|
|
want: tags,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetAllTags(tt.args.ctx)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetAllTags() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("GetAllTags() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetAllSources(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
|
|
|
|
|
|
|
|
sources := []models.Source{
|
|
|
|
{
|
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "icon.e621.net",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DisplayName: "furaffinity",
|
|
|
|
Domain: "furaffinity.net",
|
|
|
|
Icon: "icon.furaffinity.net",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DisplayName: "fenpaws",
|
|
|
|
Domain: "fenpa.ws",
|
|
|
|
Icon: "icon.fenpa.ws",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, source := range sources {
|
|
|
|
err = postgres.CreateSource(ctx, gormDB, &source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []models.Source
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Get all entries",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
},
|
|
|
|
want: sources,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetAllSources(tt.args.ctx)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetAllSources() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !checkSources(got, tt.want) {
|
|
|
|
t.Errorf("GetAllSources() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetSourceByDomain(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 := &models.Source{
|
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "icon.e621.net",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateSource(ctx, gormDB, source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
sourceDomain models.AnthroveSourceDomain
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *models.Source
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid URL",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
sourceDomain: "e621.net",
|
|
|
|
},
|
|
|
|
want: source,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid URL",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
sourceDomain: "eeeee.net",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: No URL",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
sourceDomain: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetSourceByDomain(tt.args.ctx, tt.args.sourceDomain)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !checkSource(got, tt.want) {
|
|
|
|
t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
func Test_postgresqlConnection_migrateDatabase(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 {
|
|
|
|
dbPool *gorm.DB
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Migrate Databases",
|
|
|
|
args: args{dbPool: gormDB},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.migrateDatabase(tt.args.dbPool); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("migrateDatabase() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions for validation purposes
|
2024-06-26 07:10:04 +00:00
|
|
|
func checkSource(got *models.Source, want *models.Source) bool {
|
|
|
|
|
|
|
|
if want == nil && got == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.Domain != want.Domain {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
func checkPost(got *models.Post, want *models.Post) bool {
|
|
|
|
|
|
|
|
if got == nil && want == nil {
|
|
|
|
return true
|
|
|
|
} else if got == nil || want == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.ID != want.ID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.Rating != want.Rating {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
func checkSources(got []models.Source, want []models.Source) bool {
|
|
|
|
for i, source := range want {
|
|
|
|
if source.DisplayName != got[i].DisplayName {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if source.Domain != got[i].Domain {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if source.Icon != got[i].Icon {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2024-06-28 12:16:28 +00:00
|
|
|
func checkUser(got []models.User, want []models.User) bool {
|
|
|
|
for i, user := range want {
|
|
|
|
if user.ID != got[i].ID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
func checkUserSource(got *models.UserSource, want *models.UserSource) bool {
|
|
|
|
|
|
|
|
if got == nil && want == nil {
|
|
|
|
return true
|
|
|
|
} else if got == nil || want == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.UserID != want.UserID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if got.AccountUsername != want.AccountUsername {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if got.Source.DisplayName != want.Source.DisplayName {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if got.Source.Domain != want.Source.Domain {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if got.Source.Icon != want.Source.Icon {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2024-06-28 13:13:40 +00:00
|
|
|
|
|
|
|
//-------------------------
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_CreateTagAlias(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
|
|
|
|
|
|
|
|
validTagAliasName01 := models.AnthroveTagAliasName("httyd")
|
|
|
|
validTagAliasName02 := models.AnthroveTagAliasName("dragon")
|
|
|
|
|
|
|
|
validTagID := models.AnthroveTagID("toothless")
|
|
|
|
|
|
|
|
validTag := &models.Tag{
|
|
|
|
Name: string(validTagID),
|
|
|
|
Type: models.Character,
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:06:36 +00:00
|
|
|
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
|
2024-06-28 13:13:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
tagAliasName models.AnthroveTagAliasName
|
|
|
|
tagID models.AnthroveTagID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliasName: validTagAliasName01,
|
|
|
|
tagID: validTagID,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: No TagAliasName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliasName: "",
|
|
|
|
tagID: validTagID,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: No tagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliasName: validTagAliasName01,
|
|
|
|
tagID: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 5: Duplicate tagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliasName: validTagAliasName01,
|
|
|
|
tagID: validTagID,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 6: Invalide tagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliasName: validTagAliasName02,
|
|
|
|
tagID: "aaa",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.CreateTagAlias(tt.args.ctx, tt.args.tagAliasName, tt.args.tagID); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateTagAlias() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-28 20:14:27 +00:00
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetAllTagAlias(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
|
|
|
|
validTagID := models.AnthroveTagID("toothless")
|
|
|
|
validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"}
|
|
|
|
|
|
|
|
validTag := &models.Tag{
|
|
|
|
Name: string(validTagID),
|
|
|
|
Type: models.Character,
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedResult := []models.TagAlias{
|
|
|
|
{
|
|
|
|
Name: string(validTagAliases[0]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: string(validTagAliases[1]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: string(validTagAliases[2]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:06:36 +00:00
|
|
|
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
|
2024-06-28 20:14:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tagAliasName := range validTagAliases {
|
|
|
|
err = postgres.CreateTagAlias(ctx, gormDB, tagAliasName, validTagID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []models.TagAlias
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Get Data",
|
|
|
|
args: args{ctx: ctx},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetAllTagAlias(tt.args.ctx)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetAllTagAlias() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("GetAllTagAlias() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-28 20:27:31 +00:00
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetAllTagAliasByTag(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
|
|
|
|
validTagID := models.AnthroveTagID("toothless")
|
|
|
|
validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"}
|
|
|
|
|
|
|
|
validTag := &models.Tag{
|
|
|
|
Name: string(validTagID),
|
|
|
|
Type: models.Character,
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedResult := []models.TagAlias{
|
|
|
|
{
|
|
|
|
Name: string(validTagAliases[0]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: string(validTagAliases[1]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: string(validTagAliases[2]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:06:36 +00:00
|
|
|
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
|
2024-06-28 20:27:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tagAliasName := range validTagAliases {
|
|
|
|
err = postgres.CreateTagAlias(ctx, gormDB, tagAliasName, validTagID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
tagID models.AnthroveTagID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []models.TagAlias
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid TagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagID: validTagID,
|
|
|
|
},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: No TagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagID: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Invalid TagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagID: "adads",
|
|
|
|
},
|
|
|
|
want: []models.TagAlias{},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetAllTagAliasByTag(tt.args.ctx, tt.args.tagID)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetAllTagAliasByTag() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("GetAllTagAliasByTag() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_DeleteTagAlias(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
|
|
|
|
validTagID := models.AnthroveTagID("toothless")
|
|
|
|
validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"}
|
|
|
|
|
|
|
|
validTag := &models.Tag{
|
|
|
|
Name: string(validTagID),
|
|
|
|
Type: models.Character,
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:06:36 +00:00
|
|
|
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
|
2024-06-28 20:27:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tagAliasName := range validTagAliases {
|
|
|
|
err = postgres.CreateTagAlias(ctx, gormDB, tagAliasName, validTagID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
tagAliasName models.AnthroveTagAliasName
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid AnthroveTagAliasName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliasName: validTagAliases[0],
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid AnthroveTagAliasName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliasName: "asdad",
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: No AnthroveTagAliasName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliasName: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.DeleteTagAlias(tt.args.ctx, tt.args.tagAliasName); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-28 20:39:12 +00:00
|
|
|
|
|
|
|
//--------------------------
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_CreateTagGroup(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
|
|
|
|
|
|
|
|
validTagGroupName01 := models.AnthroveTagGroupName("httyd")
|
|
|
|
validTagGroupName02 := models.AnthroveTagGroupName("dragon")
|
|
|
|
|
|
|
|
validTagID := models.AnthroveTagID("toothless")
|
|
|
|
|
|
|
|
validTag := &models.Tag{
|
|
|
|
Name: string(validTagID),
|
|
|
|
Type: models.Character,
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:06:36 +00:00
|
|
|
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
|
2024-06-28 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
tagGroupName models.AnthroveTagGroupName
|
|
|
|
tagID models.AnthroveTagID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroupName: validTagGroupName01,
|
|
|
|
tagID: validTagID,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: No TagGroupName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroupName: "",
|
|
|
|
tagID: validTagID,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: No tagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroupName: validTagGroupName01,
|
|
|
|
tagID: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 5: Duplicate tagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroupName: validTagGroupName01,
|
|
|
|
tagID: validTagID,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 6: Invalide tagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroupName: validTagGroupName02,
|
|
|
|
tagID: "aaa",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.CreateTagGroup(tt.args.ctx, tt.args.tagGroupName, tt.args.tagID); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateTagGroup() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetAllTagGroup(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
|
|
|
|
validTagID := models.AnthroveTagID("toothless")
|
|
|
|
validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"}
|
|
|
|
|
|
|
|
validTag := &models.Tag{
|
|
|
|
Name: string(validTagID),
|
|
|
|
Type: models.Character,
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedResult := []models.TagGroup{
|
|
|
|
{
|
|
|
|
Name: string(validTagGroupes[0]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: string(validTagGroupes[1]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: string(validTagGroupes[2]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:06:36 +00:00
|
|
|
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
|
2024-06-28 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tagGroupName := range validTagGroupes {
|
|
|
|
err = postgres.CreateTagGroup(ctx, gormDB, tagGroupName, validTagID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []models.TagGroup
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Get Data",
|
|
|
|
args: args{ctx: ctx},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetAllTagGroup(tt.args.ctx)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetAllTagGroup() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("GetAllTagGroup() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetAllTagGroupByTag(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
|
|
|
|
validTagID := models.AnthroveTagID("toothless")
|
|
|
|
validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"}
|
|
|
|
|
|
|
|
validTag := &models.Tag{
|
|
|
|
Name: string(validTagID),
|
|
|
|
Type: models.Character,
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedResult := []models.TagGroup{
|
|
|
|
{
|
|
|
|
Name: string(validTagGroupes[0]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: string(validTagGroupes[1]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: string(validTagGroupes[2]),
|
|
|
|
TagID: string(validTagID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:06:36 +00:00
|
|
|
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
|
2024-06-28 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tagGroupName := range validTagGroupes {
|
|
|
|
err = postgres.CreateTagGroup(ctx, gormDB, tagGroupName, validTagID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
tagID models.AnthroveTagID
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []models.TagGroup
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid TagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagID: validTagID,
|
|
|
|
},
|
|
|
|
want: expectedResult,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: No TagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagID: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Invalid TagID",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagID: "adads",
|
|
|
|
},
|
|
|
|
want: []models.TagGroup{},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetAllTagGroupByTag(tt.args.ctx, tt.args.tagID)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetAllTagGroupByTag() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("GetAllTagGroupByTag() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_DeleteTagGroup(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
|
|
|
|
validTagID := models.AnthroveTagID("toothless")
|
|
|
|
validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"}
|
|
|
|
|
|
|
|
validTag := &models.Tag{
|
|
|
|
Name: string(validTagID),
|
|
|
|
Type: models.Character,
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:06:36 +00:00
|
|
|
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
|
2024-06-28 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tagGroupName := range validTagGroupes {
|
|
|
|
err = postgres.CreateTagGroup(ctx, gormDB, tagGroupName, validTagID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
tagGroupName models.AnthroveTagGroupName
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid AnthroveTagGroupName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroupName: validTagGroupes[0],
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid AnthroveTagGroupName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroupName: "asdad",
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: No AnthroveTagGroupName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroupName: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.DeleteTagGroup(tt.args.ctx, tt.args.tagGroupName); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-07-01 11:42:53 +00:00
|
|
|
|
|
|
|
func Test_postgresqlConnection_UpdateUserSourceScrapeTimeInterval(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
|
|
|
|
|
|
|
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
invalidUserID := models.AnthroveUserID("XXX")
|
|
|
|
|
|
|
|
validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
|
|
|
|
|
|
|
|
source := &models.Source{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
|
|
|
ID: validSourceID,
|
|
|
|
},
|
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "https://e621.icon",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateSource(ctx, gormDB, source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
sourceID models.AnthroveSourceID
|
|
|
|
scrapeTime models.AnthroveScrapeTimeInterval
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: validSourceID,
|
|
|
|
scrapeTime: 10,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: anthroveUserID to short",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: invalidUserID,
|
|
|
|
sourceID: validSourceID,
|
|
|
|
scrapeTime: 10,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: anthroveUserID is empty",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "",
|
|
|
|
sourceID: validSourceID,
|
|
|
|
scrapeTime: 10,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: anthroveUserID to short",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: "111",
|
|
|
|
scrapeTime: 10,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 5: anthroveUserID is empty",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: "",
|
|
|
|
scrapeTime: 10,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 5: scrapeTime is empty",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: validSourceID,
|
|
|
|
scrapeTime: 0,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.UpdateUserSourceScrapeTimeInterval(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.scrapeTime); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("UpdateUserSourceScrapeTimeInterval() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_UpdateUserSourceLastScrapeTime(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
|
|
|
|
|
|
|
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
invalidUserID := models.AnthroveUserID("XXX")
|
|
|
|
|
|
|
|
validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
|
|
|
|
|
|
|
|
validScrapeTime := models.AnthroveUserLastScrapeTime(time.Now())
|
|
|
|
inValidScrapeTime := models.AnthroveUserLastScrapeTime{}
|
|
|
|
|
|
|
|
source := &models.Source{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
|
|
|
ID: validSourceID,
|
|
|
|
},
|
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "https://e621.icon",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateSource(ctx, gormDB, source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
sourceID models.AnthroveSourceID
|
|
|
|
lastScrapeTime models.AnthroveUserLastScrapeTime
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: validSourceID,
|
|
|
|
lastScrapeTime: validScrapeTime,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: anthroveUserID to short",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: invalidUserID,
|
|
|
|
sourceID: validSourceID,
|
|
|
|
lastScrapeTime: validScrapeTime,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: anthroveUserID is empty",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "",
|
|
|
|
sourceID: validSourceID,
|
|
|
|
lastScrapeTime: validScrapeTime,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: anthroveUserID to short",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: "111",
|
|
|
|
lastScrapeTime: validScrapeTime,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 5: anthroveUserID is empty",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: "",
|
|
|
|
lastScrapeTime: validScrapeTime,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 5: scrapeTime is empty",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: validSourceID,
|
|
|
|
lastScrapeTime: inValidScrapeTime,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.UpdateUserSourceLastScrapeTime(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.lastScrapeTime); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("UpdateUserSourceLastScrapeTime() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_UpdateUserSourceValidation(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
|
|
|
|
|
|
|
|
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
|
|
|
invalidUserID := models.AnthroveUserID("XXX")
|
|
|
|
|
|
|
|
validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
|
|
|
|
|
|
|
|
source := &models.Source{
|
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
|
|
|
ID: validSourceID,
|
|
|
|
},
|
|
|
|
DisplayName: "e621",
|
|
|
|
Domain: "e621.net",
|
|
|
|
Icon: "https://e621.icon",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateSource(ctx, gormDB, source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
anthroveUserID models.AnthroveUserID
|
|
|
|
sourceID models.AnthroveSourceID
|
|
|
|
valid bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: validSourceID,
|
|
|
|
valid: true,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: anthroveUserID to short",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: invalidUserID,
|
|
|
|
sourceID: validSourceID,
|
|
|
|
valid: true,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: anthroveUserID is empty",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: "",
|
|
|
|
sourceID: validSourceID,
|
|
|
|
valid: true,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: anthroveUserID to short",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: "111",
|
|
|
|
valid: true,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 5: anthroveUserID is empty",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
anthroveUserID: validUserID,
|
|
|
|
sourceID: "",
|
|
|
|
valid: true,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.UpdateUserSourceValidation(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.valid); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("UpdateUserSourceValidation() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-07-01 20:15:43 +00:00
|
|
|
|
|
|
|
func Test_postgresqlConnection_DeleteTag(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
|
|
|
|
validTagID := models.AnthroveTagID("toothless")
|
|
|
|
|
|
|
|
validTag := &models.Tag{
|
|
|
|
Name: string(validTagID),
|
|
|
|
Type: models.Character,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
tagName models.AnthroveTagName
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid TagName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagName: models.AnthroveTagName(validTagID),
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Invalid TagName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagName: models.AnthroveTagName("aaa"),
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: No TagName",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagName: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.DeleteTag(tt.args.ctx, tt.args.tagName); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-07-01 20:29:37 +00:00
|
|
|
|
|
|
|
func Test_postgresqlConnection_GetAllTagsByTagType(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
|
|
|
|
|
|
|
|
validTags := []models.Tag{
|
|
|
|
{
|
|
|
|
Name: "JayTheFerret",
|
|
|
|
Type: models.Character,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "SoXX",
|
|
|
|
Type: models.Character,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Alphyron",
|
|
|
|
Type: models.Character,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Dragon",
|
|
|
|
Type: models.Species,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:40:53 +00:00
|
|
|
expectetResult := []models.Tag{
|
|
|
|
{
|
|
|
|
Name: "JayTheFerret",
|
|
|
|
Type: models.Character,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "SoXX",
|
|
|
|
Type: models.Character,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Alphyron",
|
|
|
|
Type: models.Character,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:29:37 +00:00
|
|
|
for _, tag := range validTags {
|
|
|
|
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
tagType models.TagType
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []models.Tag
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Get Data",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagType: models.Character,
|
|
|
|
},
|
2024-07-01 20:40:53 +00:00
|
|
|
want: expectetResult,
|
2024-07-01 20:29:37 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: invalid Tag Type",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagType: "aa",
|
|
|
|
},
|
2024-07-02 06:46:47 +00:00
|
|
|
want: nil,
|
2024-07-02 06:33:53 +00:00
|
|
|
wantErr: true,
|
2024-07-01 20:29:37 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: No Tag Type",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagType: "",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
got, err := p.GetAllTagsByTagType(tt.args.ctx, tt.args.tagType)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetAllTagsByTagType() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("GetAllTagsByTagType() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-07-02 20:35:42 +00:00
|
|
|
|
|
|
|
func Test_postgresqlConnection_CreateTagInBatchAndUpdate(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
|
|
|
|
tags := []models.Tag{
|
|
|
|
{
|
|
|
|
Name: "JayTheFerret",
|
|
|
|
Type: models.Artist,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "SoXX",
|
|
|
|
Type: models.Character,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Dragon",
|
|
|
|
Type: models.Species,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Fennec",
|
|
|
|
Type: models.Species,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
emptyTags := []models.Tag{}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
tags []models.Tag
|
|
|
|
batchSize int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid Tags",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tags: tags,
|
|
|
|
batchSize: 10,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Empty Tags",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tags: emptyTags,
|
|
|
|
batchSize: 10,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Nil Tags",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tags: nil,
|
|
|
|
batchSize: 10,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: No batchSize",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tags: nil,
|
|
|
|
batchSize: 0,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.CreateTagInBatchAndUpdate(tt.args.ctx, tt.args.tags, tt.args.batchSize); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateTagInBatchAndUpdate() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_CreateTagAliasInBatch(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
|
|
|
|
tags := []models.Tag{
|
|
|
|
{
|
|
|
|
Name: "JayTheFerret",
|
|
|
|
Type: models.Artist,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "SoXX",
|
|
|
|
Type: models.Character,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Dragon",
|
|
|
|
Type: models.Species,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Fennec",
|
|
|
|
Type: models.Species,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = postgres.CreateTagInBatchAndUpdate(ctx, gormDB, tags, len(tags))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tagAlias := []models.TagAlias{
|
|
|
|
{
|
|
|
|
Name: "test1",
|
|
|
|
TagID: tags[0].Name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test2",
|
|
|
|
TagID: tags[1].Name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test3",
|
|
|
|
TagID: tags[2].Name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test4",
|
|
|
|
TagID: tags[3].Name,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
emptyTagAlias := []models.TagAlias{}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
tagAliases []models.TagAlias
|
|
|
|
batchSize int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid Tags",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliases: tagAlias,
|
|
|
|
batchSize: 10,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Empty Tags",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliases: emptyTagAlias,
|
|
|
|
batchSize: 10,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Nil Tags",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliases: nil,
|
|
|
|
batchSize: 10,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: No batchSize",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagAliases: tagAlias,
|
|
|
|
batchSize: 0,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.CreateTagAliasInBatch(tt.args.ctx, tt.args.tagAliases, tt.args.batchSize); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateTagAliasInBatch() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_postgresqlConnection_CreateTagGroupInBatch(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
|
|
|
|
tags := []models.Tag{
|
|
|
|
{
|
|
|
|
Name: "JayTheFerret",
|
|
|
|
Type: models.Artist,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "SoXX",
|
|
|
|
Type: models.Character,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Dragon",
|
|
|
|
Type: models.Species,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Fennec",
|
|
|
|
Type: models.Species,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = postgres.CreateTagInBatchAndUpdate(ctx, gormDB, tags, len(tags))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tagGroup := []models.TagGroup{
|
|
|
|
{
|
|
|
|
Name: "test1",
|
|
|
|
TagID: tags[0].Name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test2",
|
|
|
|
TagID: tags[1].Name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test3",
|
|
|
|
TagID: tags[2].Name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test4",
|
|
|
|
TagID: tags[3].Name,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
emptyTagGroup := []models.TagGroup{}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
tagGroups []models.TagGroup
|
|
|
|
batchSize int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Valid Tags",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroups: tagGroup,
|
|
|
|
batchSize: 10,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 2: Empty Tags",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroups: emptyTagGroup,
|
|
|
|
batchSize: 10,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 3: Nil Tags",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroups: nil,
|
|
|
|
batchSize: 10,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 4: No batchSize",
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
tagGroups: tagGroup,
|
|
|
|
batchSize: 0,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &postgresqlConnection{
|
|
|
|
db: gormDB,
|
|
|
|
debug: true,
|
|
|
|
}
|
|
|
|
if err := p.CreateTagGroupInBatch(tt.args.ctx, tt.args.tagGroups, tt.args.batchSize); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateTagGroupInBatch() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|