Use ActiveRecord for comparison

This commit is contained in:
Shinya Maeda 2017-03-08 02:44:38 +09:00
parent 658f2cb148
commit 44ae99399f

View file

@ -1,22 +1,20 @@
require 'spec_helper' require 'spec_helper'
describe PipelinesFinder do describe PipelinesFinder do
let!(:user1) { create(:user) } let(:user1) { create(:user) }
let!(:user2) { create(:user) } let(:user2) { create(:user) }
let!(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let!(:dummy_pipelines) do before do
{ create(:ci_pipeline, project: project, user: user1, ref: 'v1.0.0', tag: true)
tag: create(:ci_pipeline, project: project, user: user1, created_at: 9.minutes.ago, ref: 'v1.0.0', tag: true), create(:ci_pipeline, project: project, user: user1, status: 'created')
created: create(:ci_pipeline, project: project, user: user1, created_at: 8.minutes.ago, status: 'created'), create(:ci_pipeline, project: project, user: user1, status: 'pending')
pending: create(:ci_pipeline, project: project, user: user1, created_at: 7.minutes.ago, status: 'pending'), create(:ci_pipeline, project: project, user: user1, status: 'running')
running: create(:ci_pipeline, project: project, user: user1, created_at: 6.minutes.ago, status: 'running'), create(:ci_pipeline, project: project, user: user1, status: 'success')
success: create(:ci_pipeline, project: project, user: user1, created_at: 5.minutes.ago, status: 'success'), create(:ci_pipeline, project: project, user: user2, status: 'failed')
failed: create(:ci_pipeline, project: project, user: user2, created_at: 4.minutes.ago, status: 'failed'), create(:ci_pipeline, project: project, user: user2, status: 'canceled')
canceled: create(:ci_pipeline, project: project, user: user2, created_at: 3.minutes.ago, status: 'canceled'), create(:ci_pipeline, project: project, user: user2, status: 'skipped')
skipped: create(:ci_pipeline, project: project, user: user2, created_at: 2.minutes.ago, status: 'skipped'), create(:ci_pipeline, project: project, user: user2, yaml_errors: 'Syntax error')
yaml_errors: create(:ci_pipeline, project: project, user: user2, created_at: 1.minute.ago, yaml_errors: 'Syntax error'),
}
end end
subject { described_class.new(project, params).execute } subject { described_class.new(project, params).execute }
@ -26,12 +24,11 @@ describe PipelinesFinder do
let(:params) { {} } let(:params) { {} }
it 'selects all pipelines' do it 'selects all pipelines' do
expect(subject.count).to be dummy_pipelines.count expect(subject).to match_array(Ci::Pipeline.all)
expect(subject).to match_array dummy_pipelines.values
end end
it 'orders in descending order on ID' do it 'orders in descending order on ID' do
expect(subject.map(&:id)).to eq dummy_pipelines.values.map(&:id).sort.reverse expect(subject).to match_array(Ci::Pipeline.order(id: :desc))
end end
end end
@ -40,7 +37,7 @@ describe PipelinesFinder do
let(:params) { { scope: 'running' } } let(:params) { { scope: 'running' } }
it 'has only running status' do it 'has only running status' do
expect(subject.map(&:status)).to all(eq('running')) expect(subject).to match_array(Ci::Pipeline.running)
end end
end end
@ -48,7 +45,7 @@ describe PipelinesFinder do
let(:params) { { scope: 'pending' } } let(:params) { { scope: 'pending' } }
it 'has only pending status' do it 'has only pending status' do
expect(subject.map(&:status)).to all(eq('pending')) expect(subject).to match_array(Ci::Pipeline.pending)
end end
end end
@ -56,7 +53,7 @@ describe PipelinesFinder do
let(:params) { { scope: 'finished' } } let(:params) { { scope: 'finished' } }
it 'has only finished status' do it 'has only finished status' do
expect(subject.map(&:status)).to match_array %w[success canceled failed] expect(subject).to match_array(Ci::Pipeline.finished)
end end
end end
@ -64,9 +61,7 @@ describe PipelinesFinder do
let(:params) { { scope: 'branches' } } let(:params) { { scope: 'branches' } }
it 'excludes tags' do it 'excludes tags' do
expect(subject.count).to be 1 expect(subject).to eq([Ci::Pipeline.where(tag: false).last])
expect(subject).not_to include dummy_pipelines[:tag]
expect(subject.map(&:ref)).to all(eq('master'))
end end
end end
@ -74,9 +69,7 @@ describe PipelinesFinder do
let(:params) { { scope: 'tags' } } let(:params) { { scope: 'tags' } }
it 'excludes branches' do it 'excludes branches' do
expect(subject.count).to be 1 expect(subject).to eq([Ci::Pipeline.where(tag: true).last])
expect(subject).to include dummy_pipelines[:tag]
expect(subject.map(&:ref)).not_to include('master')
end end
end end
end end
@ -86,7 +79,7 @@ describe PipelinesFinder do
let(:params) { { status: 'running' } } let(:params) { { status: 'running' } }
it 'has only running status' do it 'has only running status' do
expect(subject.map(&:status)).to all(eq('running')) expect(subject).to match_array(Ci::Pipeline.running)
end end
end end
@ -94,7 +87,7 @@ describe PipelinesFinder do
let(:params) { { status: 'pending' } } let(:params) { { status: 'pending' } }
it 'has only pending status' do it 'has only pending status' do
expect(subject.map(&:status)).to all(eq('pending')) expect(subject).to match_array(Ci::Pipeline.pending)
end end
end end
@ -102,7 +95,7 @@ describe PipelinesFinder do
let(:params) { { status: 'success' } } let(:params) { { status: 'success' } }
it 'has only success status' do it 'has only success status' do
expect(subject.map(&:status)).to all(eq('success')) expect(subject).to match_array(Ci::Pipeline.success)
end end
end end
@ -110,7 +103,7 @@ describe PipelinesFinder do
let(:params) { { status: 'failed' } } let(:params) { { status: 'failed' } }
it 'has only failed status' do it 'has only failed status' do
expect(subject.map(&:status)).to all(eq('failed')) expect(subject).to match_array(Ci::Pipeline.failed)
end end
end end
@ -118,7 +111,7 @@ describe PipelinesFinder do
let(:params) { { status: 'canceled' } } let(:params) { { status: 'canceled' } }
it 'has only canceled status' do it 'has only canceled status' do
expect(subject.map(&:status)).to all(eq('canceled')) expect(subject).to match_array(Ci::Pipeline.canceled)
end end
end end
@ -126,7 +119,7 @@ describe PipelinesFinder do
let(:params) { { status: 'skipped' } } let(:params) { { status: 'skipped' } }
it 'has only skipped status' do it 'has only skipped status' do
expect(subject.map(&:status)).to all(eq('skipped')) expect(subject).to match_array(Ci::Pipeline.skipped)
end end
end end
end end
@ -136,8 +129,7 @@ describe PipelinesFinder do
let(:params) { { ref: 'master' } } let(:params) { { ref: 'master' } }
it 'selects all pipelines which belong to the ref' do it 'selects all pipelines which belong to the ref' do
expected = dummy_pipelines.except(:tag) expect(subject).to match_array(Ci::Pipeline.where(ref: 'master'))
expect(subject).to match_array expected.values
end end
end end
@ -155,8 +147,7 @@ describe PipelinesFinder do
let(:params) { { username: user1.name } } let(:params) { { username: user1.name } }
it 'selects all pipelines which belong to the username' do it 'selects all pipelines which belong to the username' do
expected = dummy_pipelines.except(:failed).except(:canceled).except(:skipped).except(:yaml_errors) expect(subject).to match_array(Ci::Pipeline.where(user: user1))
expect(subject).to match_array expected.values
end end
end end
@ -174,8 +165,7 @@ describe PipelinesFinder do
let(:params) { { yaml_errors: true } } let(:params) { { yaml_errors: true } }
it 'selects only pipelines have yaml_errors' do it 'selects only pipelines have yaml_errors' do
expect(subject.count).to be 1 expect(subject).to match_array(Ci::Pipeline.where("yaml_errors IS NOT NULL"))
expect(subject.first).to eq dummy_pipelines[:yaml_errors]
end end
end end
@ -183,9 +173,7 @@ describe PipelinesFinder do
let(:params) { { yaml_errors: false } } let(:params) { { yaml_errors: false } }
it 'selects only pipelines do not have yaml_errors' do it 'selects only pipelines do not have yaml_errors' do
expected = dummy_pipelines.except(:yaml_errors) expect(subject).to match_array(Ci::Pipeline.where("yaml_errors IS NULL"))
expect(subject.count).to be expected.count
expect(subject).to match_array expected.values
end end
end end
@ -193,7 +181,7 @@ describe PipelinesFinder do
let(:params) { { yaml_errors: "UnexpectedValue" } } let(:params) { { yaml_errors: "UnexpectedValue" } }
it 'selects all pipelines' do it 'selects all pipelines' do
expect(subject).to match_array dummy_pipelines.values expect(subject).to match_array(Ci::Pipeline.all)
end end
end end
end end
@ -203,8 +191,7 @@ describe PipelinesFinder do
let(:params) { { order_by: 'created_at', sort: 'asc' } } let(:params) { { order_by: 'created_at', sort: 'asc' } }
it 'sorts by created_at asc' do it 'sorts by created_at asc' do
expect(subject.first).to eq dummy_pipelines[:tag] expect(subject).to match_array(Ci::Pipeline.order(created_at: :asc))
expect(subject.last).to eq dummy_pipelines[:yaml_errors]
end end
end end
@ -212,8 +199,7 @@ describe PipelinesFinder do
let(:params) { { order_by: 'created_at', sort: 'desc' } } let(:params) { { order_by: 'created_at', sort: 'desc' } }
it 'sorts by created_at desc' do it 'sorts by created_at desc' do
expect(subject.first).to eq dummy_pipelines[:yaml_errors] expect(subject).to match_array(Ci::Pipeline.order(created_at: :desc))
expect(subject.last).to eq dummy_pipelines[:tag]
end end
end end
@ -221,7 +207,7 @@ describe PipelinesFinder do
let(:params) { { order_by: 'abnormal_column', sort: 'desc' } } let(:params) { { order_by: 'abnormal_column', sort: 'desc' } }
it 'sorts by default' do it 'sorts by default' do
expect(subject.map(&:id)).to eq dummy_pipelines.values.map(&:id).sort.reverse expect(subject).to match_array(Ci::Pipeline.order(id: :desc))
end end
end end
@ -229,7 +215,7 @@ describe PipelinesFinder do
let(:params) { { order_by: 'created_at', sort: 'abnormal_sort' } } let(:params) { { order_by: 'created_at', sort: 'abnormal_sort' } }
it 'sorts by default' do it 'sorts by default' do
expect(subject.map(&:id)).to eq dummy_pipelines.values.map(&:id).sort.reverse expect(subject).to match_array(Ci::Pipeline.order(id: :desc))
end end
end end
end end