Add spec for group_by notification sql output

pull/9656/head
Oliver Günther 3 years ago
parent 4e2dc171db
commit 5d39c59d35
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  1. 15
      app/models/queries/base_query.rb
  2. 51
      spec/models/queries/notifications/notification_query_spec.rb

@ -69,6 +69,11 @@ class Queries::BaseQuery
return empty_scope unless valid?
apply_group_by(apply_filters(default_scope))
.select(group_by.name, Arel.sql('COUNT(*)'))
end
def group_values
groups
.pluck(group_by.name, Arel.sql('COUNT(*)'))
.to_h
end
@ -145,11 +150,11 @@ class Queries::BaseQuery
def add_error(local_attribute, attribute_name, object)
messages = object
.errors
.messages
.values
.flatten
.join(" #{I18n.t('support.array.sentence_connector')} ")
.errors
.messages
.values
.flatten
.join(" #{I18n.t('support.array.sentence_connector')} ")
errors.add local_attribute, errors.full_message(attribute_name, messages)
end

@ -114,7 +114,11 @@ describe Queries::Notifications::NotificationQuery, type: :model do
describe '#results' do
it 'is the same as handwriting the query' do
expected = "SELECT \"notifications\".* FROM \"notifications\" WHERE \"notifications\".\"recipient_id\" = #{recipient.id} ORDER BY \"notifications\".\"read_ian\" DESC, \"notifications\".\"id\" DESC"
expected = <<~SQL.squish
SELECT "notifications".* FROM "notifications"
WHERE "notifications"."recipient_id" = #{recipient.id}
ORDER BY "notifications"."read_ian" DESC, "notifications"."id" DESC
SQL
expect(instance.results.to_sql).to eql expected
end
@ -128,7 +132,11 @@ describe Queries::Notifications::NotificationQuery, type: :model do
describe '#results' do
it 'is the same as handwriting the query' do
expected = "SELECT \"notifications\".* FROM \"notifications\" WHERE \"notifications\".\"recipient_id\" = #{recipient.id} ORDER BY \"reason\" DESC, \"notifications\".\"id\" DESC"
expected = <<~SQL.squish
SELECT "notifications".* FROM "notifications"
WHERE "notifications"."recipient_id" = #{recipient.id}
ORDER BY "notifications"."reason_ian" DESC, "notifications"."id" DESC
SQL
expect(instance.results.to_sql).to eql expected
end
@ -154,4 +162,43 @@ describe Queries::Notifications::NotificationQuery, type: :model do
end
end
end
context 'with a reason group_by' do
before do
instance.group(:reason)
end
describe '#results' do
it 'is the same as handwriting the query' do
expected = <<~SQL.squish
SELECT "notifications"."reason_ian", COUNT(*) FROM "notifications"
WHERE "notifications"."recipient_id" = #{recipient.id}
GROUP BY "notifications"."reason_ian"
ORDER BY "notifications"."reason_ian" ASC
SQL
expect(instance.groups.to_sql).to eql expected
end
end
end
context 'with a non existing group_by' do
before do
instance.group(:does_not_exist)
end
describe '#results' do
it 'returns a query not returning anything' do
expected = Notification.where(Arel::Nodes::Equality.new(1, 0))
expect(instance.results.to_sql).to eql expected.to_sql
end
end
describe 'valid?' do
it 'is false' do
expect(instance).to be_invalid
end
end
end
end

Loading…
Cancel
Save