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