remove hardcoded SQL queries in tests

There are several occurrences of hardcoded SQL queries in tests.
Due to variations in databases these queries might not work all
the time, and should be replaced by queries built with sqlalchemy.

Change-Id: Id0198ea03087ab4acc7eebf0e4d12a37e3437382
Closes-Bug: #1287637
Merge "remove hardcoded SQL queries in tests"