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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex cab98823f24..de7e658bf02 100644--- a/app/models/queries/base_query.rb+++ b/app/models/queries/base_query.rb@@ -69,6 +69,11 @@ class Queries::BaseQueryreturn 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_hend@@ -145,11 +150,11 @@ class Queries::BaseQuerydef 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)enddiff --git a/spec/models/queries/notifications/notification_query_spec.rb b/spec/models/queries/notifications/notification_query_spec.rbindex 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 dodescribe '#results' doit '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+ SQLexpect(instance.results.to_sql).to eql expectedend@@ -128,7 +132,11 @@ describe Queries::Notifications::NotificationQuery, type: :model dodescribe '#results' doit '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+ SQLexpect(instance.results.to_sql).to eql expectedend@@ -154,4 +162,43 @@ describe Queries::Notifications::NotificationQuery, type: :model doendendend++ 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+ endend