package postgresflexalpha import ( "context" "testing" "github.com/google/go-cmp/cmp" "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api" ) func TestGetDatabase(t *testing.T) { mockResp := func(page int32) (*v3alpha1api.ListDatabasesResponse, error) { if page == 1 { return &v3alpha1api.ListDatabasesResponse{ Databases: []v3alpha1api.ListDatabase{ {Id: int32(1), Name: "first"}, {Id: int32(2), Name: "second"}, }, Pagination: v3alpha1api.Pagination{ Page: int32(1), TotalPages: int32(2), Size: int32(3), }, }, nil } if page == 2 { return &v3alpha1api.ListDatabasesResponse{ Databases: []v3alpha1api.ListDatabase{{Id: int32(3), Name: "three"}}, Pagination: v3alpha1api.Pagination{ Page: int32(2), TotalPages: int32(2), Size: int32(3), }, }, nil } return &v3alpha1api.ListDatabasesResponse{ Databases: []v3alpha1api.ListDatabase{}, Pagination: v3alpha1api.Pagination{ Page: int32(3), TotalPages: int32(2), Size: int32(3), }, }, nil } tests := []struct { description string projectID string region string instanceID string wantErr bool wantDbName string wantDbID int32 }{ { description: "Success - Found by name on first page", projectID: "pid", region: "reg", instanceID: "inst", wantErr: false, wantDbName: "second", }, { description: "Success - Found by id on first page", projectID: "pid", region: "reg", instanceID: "inst", wantErr: false, wantDbID: 2, }, { description: "Success - Found by name on second page", projectID: "pid", region: "reg", instanceID: "inst", wantErr: false, wantDbName: "three", }, { description: "Success - Found by id on second page", projectID: "pid", region: "reg", instanceID: "inst", wantErr: false, wantDbID: 1, }, { description: "Error - API failure", projectID: "pid", region: "reg", instanceID: "inst", wantErr: true, }, { description: "Error - Missing parameters", projectID: "", region: "reg", instanceID: "inst", wantErr: true, }, { description: "Error - Search by name not found after all pages", projectID: "pid", region: "reg", instanceID: "inst", wantDbName: "non-existent", wantErr: true, }, { description: "Error - Search by id not found after all pages", projectID: "pid", region: "reg", instanceID: "inst", wantDbID: 999999, wantErr: true, }, } for _, tt := range tests { t.Run( tt.description, func(t *testing.T) { var currentPage int32 mockCall := func(_ v3alpha1api.ApiListDatabasesRequestRequest) (*v3alpha1api.ListDatabasesResponse, error) { currentPage++ return mockResp(currentPage) } client := &v3alpha1api.DefaultAPIServiceMock{ ListDatabasesRequestExecuteMock: &mockCall, } var actual *v3alpha1api.ListDatabase var errDB error if tt.wantDbName != "" { actual, errDB = getDatabaseByName( t.Context(), client, tt.projectID, tt.region, tt.instanceID, tt.wantDbName, ) } else if tt.wantDbID != 0 { actual, errDB = getDatabaseById( t.Context(), client, tt.projectID, tt.region, tt.instanceID, int64(tt.wantDbID), ) } else { actual, errDB = getDatabase( context.Background(), client, tt.projectID, tt.region, tt.instanceID, func(_ v3alpha1api.ListDatabase) bool { return false }, ) } if (errDB != nil) != tt.wantErr { t.Errorf("getDatabaseByNameOrID() error = %v, wantErr %v", errDB, tt.wantErr) return } if !tt.wantErr && tt.wantDbName != "" && actual != nil { if actual.Name != tt.wantDbName { t.Errorf("getDatabaseByNameOrID() got name = %v, want %v", actual.Name, tt.wantDbName) } } if !tt.wantErr && tt.wantDbID != 0 && actual != nil { if actual.Id != tt.wantDbID { t.Errorf("getDatabaseByNameOrID() got id = %v, want %v", actual.Id, tt.wantDbID) } } }, ) } } func TestCleanString(t *testing.T) { testcases := []struct { name string given string expected string }{ { name: "should remove quotes", given: "\"quoted\"", expected: "quoted", }, { name: "should not change unquoted string", given: "unquoted", expected: "unquoted", }, } for _, tc := range testcases { t.Run( tc.name, func(t *testing.T) { actual := cleanString(tc.given) if diff := cmp.Diff(tc.expected, actual); diff != "" { t.Errorf("string mismatch (-want +got):\n%s", diff) } }, ) } }