Fix Flask test and refactor raw sql tests