Commit 95b92ddc authored by Giorgenes Gelatti's avatar Giorgenes Gelatti

Add specs for Package.sort_by_attribute

parent a195c841
......@@ -13,6 +13,61 @@ RSpec.describe Packages::Package, type: :model do
it { is_expected.to have_one(:maven_metadatum).inverse_of(:package) }
end
describe '.sort_by_attribute' do
let_it_be(:group) { create(:group, :public) }
let_it_be(:project) { create(:project, :public, namespace: group, name: 'project A') }
let!(:package1) { create(:npm_package, project: project, version: '3.1.0', name: "@#{project.root_namespace.path}/foo1") }
let!(:package2) { create(:nuget_package, project: project, version: '2.0.4') }
let(:package3) { create(:maven_package, project: project, version: '1.1.1', name: 'zzz') }
before do
travel_to(1.day.ago) do
package3
end
end
RSpec.shared_examples 'package sorting by attribute' do |order_by|
subject { described_class.where(id: packages.map(&:id)).sort_by_attribute("#{order_by}_#{sort}").to_a }
context "sorting by #{order_by}" do
context 'ascending order' do
let(:sort) { 'asc' }
it { is_expected.to eq packages }
end
context 'descending order' do
let(:sort) { 'desc' }
it { is_expected.to eq packages.reverse }
end
end
end
it_behaves_like 'package sorting by attribute', 'name' do
let(:packages) { [package1, package2, package3] }
end
it_behaves_like 'package sorting by attribute', 'created_at' do
let(:packages) { [package3, package1, package2] }
end
it_behaves_like 'package sorting by attribute', 'version' do
let(:packages) { [package3, package2, package1] }
end
it_behaves_like 'package sorting by attribute', 'type' do
let(:packages) { [package3, package1, package2] }
end
it_behaves_like 'package sorting by attribute', 'project_path' do
let(:another_project) { create(:project, :public, namespace: group, name: 'project B') }
let!(:package4) { create(:npm_package, project: another_project, version: '3.1.0', name: "@#{project.root_namespace.path}/bar") }
let(:packages) { [package1, package2, package3, package4] }
end
end
describe 'validations' do
subject { create(:package) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment