Add fancy special cases for postgres exists in db
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
kolaente 2020-09-09 10:55:24 +02:00
parent 6bef7896e9
commit ce18ff7f6d
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 21 additions and 8 deletions

View File

@ -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))
}

View File

@ -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) {