QueryWrapper tests and refactoring