if you can decompose the query, you can unit test the logic against inmemory sets(or language specific equivalent).