Add fancy special cases for postgres exists in db
continuous-integration/drone/pr Build is passing
Details
continuous-integration/drone/pr Build is passing
Details
This commit is contained in:
parent
6bef7896e9
commit
ce18ff7f6d
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue