From ce18ff7f6d9d3682b30687ef035fb8cbe79edda3 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 9 Sep 2020 10:55:24 +0200 Subject: [PATCH] Add fancy special cases for postgres exists in db --- pkg/db/test.go | 17 +++++++++++++++-- pkg/models/saved_filters_test.go | 12 ++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/pkg/db/test.go b/pkg/db/test.go index 2c0beb7b1..b0bb980f2 100644 --- a/pkg/db/test.go +++ b/pkg/db/test.go @@ -74,9 +74,22 @@ func InitTestFixtures(tablenames ...string) (err error) { } // AssertDBExists checks and asserts the existence of certain entries in the db -func AssertDBExists(t *testing.T, table string, values map[string]interface{}) { +func AssertDBExists(t *testing.T, table string, values map[string]interface{}, custom bool) { + var exists bool + var err error v := make(map[string]interface{}) - exists, err := x.Table(table).Where(values).Get(&v) + // Postgres sometimes needs to build raw sql. Because is won't always need to do this and this isn't fun, it's a flag. + if custom { + //#nosec + sql := "SELECT * FROM " + table + " WHERE " + for col, val := range values { + sql += col + "=" + fmt.Sprintf("%v", val) + " AND " + } + sql = sql[:len(sql)-5] + exists, err = x.SQL(sql).Get(&v) + } else { + exists, err = x.Table(table).Where(values).Get(&v) + } assert.NoError(t, err, fmt.Sprintf("Failed to assert entries exist in db, error was: %s", err)) assert.True(t, exists, fmt.Sprintf("Entries %v do not exist in table %s", values, table)) } diff --git a/pkg/models/saved_filters_test.go b/pkg/models/saved_filters_test.go index 413903048..4656f09b7 100644 --- a/pkg/models/saved_filters_test.go +++ b/pkg/models/saved_filters_test.go @@ -55,17 +55,17 @@ func TestSavedFilter_Create(t *testing.T) { assert.NoError(t, err) assert.Equal(t, u.ID, sf.OwnerID) vals := map[string]interface{}{ - "title": "test", - "description": "Lorem Ipsum dolor sit amet", - "filters": "{\"sort_by\":null,\"order_by\":null,\"filter_by\":null,\"filter_value\":null,\"filter_comparator\":null,\"filter_concat\":\"\",\"filter_include_nulls\":false}", + "title": "'test'", + "description": "'Lorem Ipsum dolor sit amet'", + "filters": "'{\"sort_by\":null,\"order_by\":null,\"filter_by\":null,\"filter_value\":null,\"filter_comparator\":null,\"filter_concat\":\"\",\"filter_include_nulls\":false}'", "owner_id": 1, } // Postgres can't compare json values directly, see https://dba.stackexchange.com/a/106290/210721 if x.Dialect().URI().DBType == schemas.POSTGRES { + vals["filters::jsonb"] = vals["filters"].(string) + "::jsonb" delete(vals, "filters") - vals["filters::jsonb"] = "{\"sort_by\":null,\"order_by\":null,\"filter_by\":null,\"filter_value\":null,\"filter_comparator\":null,\"filter_concat\":\"\",\"filter_include_nulls\":false}::jsonb" } - db.AssertDBExists(t, "saved_filters", vals) + db.AssertDBExists(t, "saved_filters", vals, true) } func TestSavedFilter_ReadOne(t *testing.T) { @@ -96,7 +96,7 @@ func TestSavedFilter_Update(t *testing.T) { "id": 1, "title": "NewTitle", "description": "", - }) + }, false) } func TestSavedFilter_Delete(t *testing.T) {