Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add spec for group_by notification sql output

Oliver Günther5d39c59d352d
diff --git a/app/models/queries/base_query.rb b/app/models/queries/base_query.rb
index cab98823f24..de7e658bf02 100644
--- a/app/models/queries/base_query.rb
+++ b/app/models/queries/base_query.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
diff --git a/spec/models/queries/notifications/notification_query_spec.rb b/spec/models/queries/notifications/notification_query_spec.rb
index 68d7cb95f5c..93c70e1ae9f 100644
--- a/spec/models/queries/notifications/notification_query_spec.rb
+++ b/spec/models/queries/notifications/notification_query_spec.rb
@@ -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