Refactor date testing query