package database import ( "context" "fmt" "testing" "git.dragse.it/anthrove/otter-space-sdk/internal/postgres" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/test" ) func TestNewPostgresqlConnection(t *testing.T) { // Test tests := []struct { name string want OtterSpace }{ { 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) } }) } } 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 source := &models.Source{ BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1"))}, 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, anthroveUserID: "1", sourceID: source.ID, accountId: "e1", accountUsername: "marius", }, wantErr: false, }, { name: "Test 2: Invalid anthroveUserID, valid sourceID, accountId, accountUsername", args: args{ ctx: ctx, anthroveUserID: "2", sourceID: source.ID, accountId: "e1", accountUsername: "marius", }, wantErr: true, }, { name: "Test 3: Empty anthroveUserID", args: args{ ctx: ctx, anthroveUserID: "", sourceID: source.ID, accountId: "e1", accountUsername: "marius", }, wantErr: true, }, { name: "Test 4: invalid sourceID", args: args{ ctx: ctx, anthroveUserID: "1", sourceID: "fa.net", accountId: "e1", accountUsername: "marius", }, wantErr: true, }, { name: "Test 5: no accountId", args: args{ ctx: ctx, anthroveUserID: "1", sourceID: source.ID, accountId: "", accountUsername: "marius", }, wantErr: true, }, { name: "Test 6: no accountUsername", args: args{ ctx: ctx, anthroveUserID: "1", sourceID: source.ID, 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, }, wantErr: false, }, } 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 validPost := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, 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 post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, 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, anthrovePostID: post.ID, anthroveTag: tag, }, wantErr: false, }, { name: "Test 2: Valid PostID and no Tag", args: args{ ctx: ctx, anthrovePostID: post.ID, 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, } if err := p.CreateTagAndReferenceToPost(tt.args.ctx, tt.args.anthrovePostID, tt.args.anthroveTag); (err != nil) != tt.wantErr { t.Errorf("CreateTagAndReferenceToPost() error = %v, wantErr %v", err, tt.wantErr) } }) } } 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 post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, Rating: "safe", } err = postgres.CreatePost(ctx, gormDB, post) if err != nil { t.Fatal(err) } 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 anthrovePostID models.AnthrovePostID sourceDomain models.AnthroveSourceDomain } tests := []struct { name string args args wantErr bool }{ { name: "Test 1: Valid AnthrovePostID and anthroveSourceDomain", args: args{ ctx: ctx, anthrovePostID: post.ID, sourceDomain: "e621.net", }, wantErr: false, }, { name: "Test 2: Invalid AnthrovePostID and Valid anthroveSourceDomain", args: args{ ctx: ctx, anthrovePostID: "123456", sourceDomain: "e621.net", }, wantErr: true, }, { name: "Test 3: Invalid anthroveSourceDomain and Valid AnthrovePostID", args: args{ ctx: ctx, anthrovePostID: "1234", sourceDomain: "fa.banana", }, wantErr: true, }, { name: "Test 4: Invalid anthroveSourceDomain and Invalid AnthrovePostID", args: args{ ctx: ctx, anthrovePostID: "696969", sourceDomain: "hehe.funny.number", }, wantErr: true, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := &postgresqlConnection{ db: gormDB, debug: true, } if err := p.CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain); (err != nil) != tt.wantErr { t.Errorf("CreateReferenceBetweenPostAndSource() error = %v, wantErr %v", err, tt.wantErr) } }) } } 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 err = postgres.CreateUser(ctx, gormDB, "1") if err != nil { t.Fatal(err) } post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, 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, anthroveUserID: "1", anthrovePostID: post.ID, }, wantErr: false, }, { name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", args: args{ ctx: ctx, anthroveUserID: "1", 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 err = postgres.CreateUser(ctx, gormDB, "1") if err != nil { t.Fatal(err) } post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, Rating: "safe", } err = postgres.CreatePost(ctx, gormDB, post) if err != nil { t.Fatal(err) } err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", post.ID) 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, anthroveUserID: "1", anthrovePostID: post.ID, }, want: true, wantErr: false, }, { name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", args: args{ ctx: ctx, anthroveUserID: "1", anthrovePostID: "qadw", }, want: false, wantErr: false, }, { name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID", args: args{ ctx: ctx, anthroveUserID: "123", anthrovePostID: post.ID, }, want: false, wantErr: false, }, { name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID", args: args{ ctx: ctx, anthroveUserID: "123", anthrovePostID: "123456", }, want: false, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := &postgresqlConnection{ db: gormDB, debug: true, } got, err := p.CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.anthroveUserID, tt.args.anthrovePostID) if (err != nil) != tt.wantErr { t.Errorf("CheckReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr) return } if got != tt.want { t.Errorf("CheckReferenceBetweenUserAndPost() got = %v, want %v", got, tt.want) } }) } } 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 post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, 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, anthrovePost: post.ID, }, 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) } }) } } 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 }