From 1f14e689e579163222fa0a410d3ab4ca672d465e Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 6 Oct 2015 13:52:11 +0200 Subject: Added specs for TrendingProjectsFinder --- spec/finders/trending_projects_finder_spec.rb | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 spec/finders/trending_projects_finder_spec.rb (limited to 'spec/finders') diff --git a/spec/finders/trending_projects_finder_spec.rb b/spec/finders/trending_projects_finder_spec.rb new file mode 100644 index 00000000000..1ad2e23f1f1 --- /dev/null +++ b/spec/finders/trending_projects_finder_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +describe TrendingProjectsFinder do + let(:user) { create(:user) } + let(:group) { create(:group) } + + let(:project1) { create(:empty_project, :public, group: group) } + let(:project2) { create(:empty_project, :public, group: group) } + + before do + 2.times do + create(:note_on_commit, project: project1) + end + + create(:note_on_commit, project: project2) + end + + describe '#execute' do + describe 'without an explicit start date' do + subject { described_class.new.execute(user).to_a } + + it 'sorts Projects by the amount of notes in descending order' do + expect(subject).to eq([project1, project2]) + end + end + + describe 'with an explicit start date' do + let(:date) { 2.months.ago } + + subject { described_class.new.execute(user, date).to_a } + + before do + 2.times do + create(:note_on_commit, project: project2, created_at: date) + end + end + + it 'sorts Projects by the amount of notes in descending order' do + expect(subject).to eq([project2, project1]) + end + end + end +end -- cgit v1.2.1 From b7abba0ca0a4629a854eee0488f94f160452e2f6 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 6 Oct 2015 16:35:51 +0200 Subject: Revamp trending projects query This changes the query to use a COUNT nested in an INNER JOIN, instead of a COUNT plus a GROUP BY. There are two reasons for this: 1. Using a COUNT in an INNER JOIN can be quite a bit faster. 2. The use of a GROUP BY means that method calls such as "any?" (and everything else that calls "count") operate on a Hash that counts the amount of notes on a per project basis, instead of just counting the total amount of projects. The query has been moved into Project.trending as its logic is simple enough. As a result of this testing the TrendingProjectsFinder class simply involves testing if the right methods are called, removing the need for setting up database records. --- spec/finders/trending_projects_finder_spec.rb | 44 ++++++++++++--------------- 1 file changed, 20 insertions(+), 24 deletions(-) (limited to 'spec/finders') diff --git a/spec/finders/trending_projects_finder_spec.rb b/spec/finders/trending_projects_finder_spec.rb index 1ad2e23f1f1..a49cbfd5160 100644 --- a/spec/finders/trending_projects_finder_spec.rb +++ b/spec/finders/trending_projects_finder_spec.rb @@ -1,42 +1,38 @@ require 'spec_helper' describe TrendingProjectsFinder do - let(:user) { create(:user) } - let(:group) { create(:group) } - - let(:project1) { create(:empty_project, :public, group: group) } - let(:project2) { create(:empty_project, :public, group: group) } - - before do - 2.times do - create(:note_on_commit, project: project1) - end - - create(:note_on_commit, project: project2) - end + let(:user) { build(:user) } describe '#execute' do describe 'without an explicit start date' do - subject { described_class.new.execute(user).to_a } + subject { described_class.new } - it 'sorts Projects by the amount of notes in descending order' do - expect(subject).to eq([project1, project2]) + it 'returns the trending projects' do + relation = double(:ar_relation) + + allow(subject).to receive(:projects_for) + .with(user) + .and_return(relation) + + allow(relation).to receive(:trending) + .with(an_instance_of(ActiveSupport::TimeWithZone)) end end describe 'with an explicit start date' do let(:date) { 2.months.ago } - subject { described_class.new.execute(user, date).to_a } + subject { described_class.new } - before do - 2.times do - create(:note_on_commit, project: project2, created_at: date) - end - end + it 'returns the trending projects' do + relation = double(:ar_relation) + + allow(subject).to receive(:projects_for) + .with(user) + .and_return(relation) - it 'sorts Projects by the amount of notes in descending order' do - expect(subject).to eq([project2, project1]) + allow(relation).to receive(:trending) + .with(date) end end end -- cgit v1.2.1 From dfbbc80611fbdafe6f5ed809f98fc63987d104a6 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Tue, 6 Oct 2015 09:57:13 -0700 Subject: Support filtering by "Any" milestone or issue and fix "No Milestone" and "No Label" filters Closes #2619 Closes https://github.com/gitlabhq/gitlabhq/issues/9631 --- spec/finders/issues_finder_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'spec/finders') diff --git a/spec/finders/issues_finder_spec.rb b/spec/finders/issues_finder_spec.rb index db20b23f87d..b1648055462 100644 --- a/spec/finders/issues_finder_spec.rb +++ b/spec/finders/issues_finder_spec.rb @@ -6,9 +6,11 @@ describe IssuesFinder do let(:project1) { create(:project) } let(:project2) { create(:project) } let(:milestone) { create(:milestone, project: project1) } + let(:label) { create(:label, project: project2) } let(:issue1) { create(:issue, author: user, assignee: user, project: project1, milestone: milestone) } let(:issue2) { create(:issue, author: user, assignee: user, project: project2) } let(:issue3) { create(:issue, author: user2, assignee: user2, project: project2) } + let!(:label_link) { create(:label_link, label: label, target: issue2) } before do project1.team << [user, :master] @@ -48,6 +50,24 @@ describe IssuesFinder do expect(issues).to eq([issue1]) end + it 'should filter by no milestone id' do + params = { scope: "all", milestone_title: Milestone::None.title, state: 'opened' } + issues = IssuesFinder.new(user, params).execute + expect(issues).to match_array([issue2, issue3]) + end + + it 'should filter by label name' do + params = { scope: "all", label_name: label.title, state: 'opened' } + issues = IssuesFinder.new(user, params).execute + expect(issues).to eq([issue2]) + end + + it 'should filter by no label name' do + params = { scope: "all", label_name: Label::None.title, state: 'opened' } + issues = IssuesFinder.new(user, params).execute + expect(issues).to match_array([issue1, issue3]) + end + it 'should be empty for unauthorized user' do params = { scope: "all", state: 'opened' } issues = IssuesFinder.new(nil, params).execute -- cgit v1.2.1