I am using query classes to abstract building complex queries. These class usually takes a lot of different options to customized final query like search word, some filters, page number or sorting field.
I am wondering now what are the best practices to test these kind of classes. Mainly I am interested how to test sorting capabilities when there are many fields that sort can be applied on.
One solution can look something like this:
%w{user_id email full_name description amount country}.each do |sort_column|
it "sorts by #{sort_column} in ascending order" do
results = Query::Bonus.new(
sort_column: sort_column,
sort_direction: :asc
).results.to_a
expect(results).to eq(bonuses_list.sort_by { |b| b.send(sort_column) })
end
end
bonuses_list is array of bonuses created before tests.
This works, but I think this is an overkill when there is a lot of attributes and is also not very clear. There also a problem when sorting is possible on joined tables, then sort_by won’t work that easy.
So what are your thoughts on this? I am thinking about testing only one attribute to see if sorting basically works and skip the rest.