Fix test and refactor toarray for query