diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
commit | 0653e08efd039a5905f3fa4f6e9cef9f5d2f799c (patch) | |
tree | 4dcc884cf6d81db44adae4aa99f8ec1233a41f55 /spec/presenters | |
parent | 744144d28e3e7fddc117924fef88de5d9674fe4c (diff) |
Add latest changes from gitlab-org/gitlab@14-3-stable-eev14.3.0-rc42
Diffstat (limited to 'spec/presenters')
-rw-r--r-- | spec/presenters/packages/helm/index_presenter_spec.rb | 80 | ||||
-rw-r--r-- | spec/presenters/packages/npm/package_presenter_spec.rb | 65 | ||||
-rw-r--r-- | spec/presenters/project_presenter_spec.rb | 38 | ||||
-rw-r--r-- | spec/presenters/snippet_blob_presenter_spec.rb | 3 |
4 files changed, 145 insertions, 41 deletions
diff --git a/spec/presenters/packages/helm/index_presenter_spec.rb b/spec/presenters/packages/helm/index_presenter_spec.rb new file mode 100644 index 00000000000..38e1dc17f49 --- /dev/null +++ b/spec/presenters/packages/helm/index_presenter_spec.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Packages::Helm::IndexPresenter do + include_context 'with expected presenters dependency groups' + + let_it_be(:project) { create(:project) } + let_it_be(:packages) { create_list(:helm_package, 5, project: project) } + let_it_be(:package_files3_1) { create(:helm_package_file, package: packages[2], file_sha256: '3_1', file_name: 'file3_1') } + let_it_be(:package_files3_2) { create(:helm_package_file, package: packages[2], file_sha256: '3_2', file_name: 'file3_2') } + let_it_be(:package_files4_1) { create(:helm_package_file, package: packages[3], file_sha256: '4_1', file_name: 'file4_1') } + let_it_be(:package_files4_2) { create(:helm_package_file, package: packages[3], file_sha256: '4_2', file_name: 'file4_2') } + let_it_be(:package_files4_3) { create(:helm_package_file, package: packages[3], file_sha256: '4_3', file_name: 'file4_3') } + + let(:project_id_param) { project.id } + let(:channel) { 'stable' } + let(:presenter) { described_class.new(project_id_param, channel, ::Packages::Package.id_in(packages.map(&:id))) } + + describe('#entries') do + subject { presenter.entries } + + it 'returns the correct hash' do + expect(subject.size).to eq(5) + expect(subject.keys).to eq(packages.map(&:name)) + subject.values.zip(packages) do |raws, pkg| + expect(raws.size).to eq(1) + + file = pkg.package_files.recent.first + raw = raws.first + expect(raw['name']).to eq(pkg.name) + expect(raw['version']).to eq(pkg.version) + expect(raw['apiVersion']).to eq("v2") + expect(raw['created']).to eq(file.created_at.utc.strftime('%Y-%m-%dT%H:%M:%S.%NZ')) + expect(raw['digest']).to eq(file.file_sha256) + expect(raw['urls']).to eq(["charts/#{file.file_name}"]) + end + end + + context 'with an unknown channel' do + let(:channel) { 'unknown' } + + it { is_expected.to be_empty } + end + + context 'with a nil channel' do + let(:channel) { nil } + + it { is_expected.to be_empty } + end + end + + describe('#api_version') do + subject { presenter.api_version } + + it { is_expected.to eq(described_class::API_VERSION) } + end + + describe('#generated') do + subject { presenter.generated } + + it 'returns the expected format' do + freeze_time do + expect(subject).to eq(Time.zone.now.utc.strftime('%Y-%m-%dT%H:%M:%S.%NZ')) + end + end + end + + describe('#server_info') do + subject { presenter.server_info } + + it { is_expected.to eq({ 'contextPath' => "/api/v4/projects/#{project.id}/packages/helm" }) } + + context 'with url encoded project id param' do + let_it_be(:project_id_param) { 'foo/bar' } + + it { is_expected.to eq({ 'contextPath' => '/api/v4/projects/foo%2Fbar/packages/helm' }) } + end + end +end diff --git a/spec/presenters/packages/npm/package_presenter_spec.rb b/spec/presenters/packages/npm/package_presenter_spec.rb index e524edaadc6..65f69d4056b 100644 --- a/spec/presenters/packages/npm/package_presenter_spec.rb +++ b/spec/presenters/packages/npm/package_presenter_spec.rb @@ -5,10 +5,10 @@ require 'spec_helper' RSpec.describe ::Packages::Npm::PackagePresenter do let_it_be(:project) { create(:project) } let_it_be(:package_name) { "@#{project.root_namespace.path}/test" } + let_it_be(:package1) { create(:npm_package, version: '2.0.4', project: project, name: package_name) } + let_it_be(:package2) { create(:npm_package, version: '2.0.6', project: project, name: package_name) } + let_it_be(:latest_package) { create(:npm_package, version: '2.0.11', project: project, name: package_name) } - let!(:package1) { create(:npm_package, version: '1.0.4', project: project, name: package_name) } - let!(:package2) { create(:npm_package, version: '1.0.6', project: project, name: package_name) } - let!(:latest_package) { create(:npm_package, version: '1.0.11', project: project, name: package_name) } let(:packages) { project.packages.npm.with_name(package_name).last_of_each_version } let(:presenter) { described_class.new(package_name, packages) } @@ -20,23 +20,39 @@ RSpec.describe ::Packages::Npm::PackagePresenter do it { expect(subject[package1.version].with_indifferent_access).to match_schema('public_api/v4/packages/npm_package_version') } it { expect(subject[package2.version].with_indifferent_access).to match_schema('public_api/v4/packages/npm_package_version') } - described_class::NPM_VALID_DEPENDENCY_TYPES.each do |dependency_type| + ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type| it { expect(subject.dig(package1.version, dependency_type)).to be nil } it { expect(subject.dig(package2.version, dependency_type)).to be nil } end + + it 'avoids N+1 database queries' do + check_n_plus_one(:versions) do + create_list(:npm_package, 5, project: project, name: package_name) + end + end end context 'for packages with dependencies' do - described_class::NPM_VALID_DEPENDENCY_TYPES.each do |dependency_type| - let!("package_dependency_link_for_#{dependency_type}") { create(:packages_dependency_link, package: package1, dependency_type: dependency_type) } + ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type| + let_it_be("package_dependency_link_for_#{dependency_type}") { create(:packages_dependency_link, package: package1, dependency_type: dependency_type) } end it { is_expected.to be_a(Hash) } it { expect(subject[package1.version].with_indifferent_access).to match_schema('public_api/v4/packages/npm_package_version') } it { expect(subject[package2.version].with_indifferent_access).to match_schema('public_api/v4/packages/npm_package_version') } - described_class::NPM_VALID_DEPENDENCY_TYPES.each do |dependency_type| + ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type| it { expect(subject.dig(package1.version, dependency_type.to_s)).to be_any } end + + it 'avoids N+1 database queries' do + check_n_plus_one(:versions) do + create_list(:npm_package, 5, project: project, name: package_name).each do |npm_package| + ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type| + create(:packages_dependency_link, package: npm_package, dependency_type: dependency_type) + end + end + end + end end end @@ -46,14 +62,20 @@ RSpec.describe ::Packages::Npm::PackagePresenter do context 'for packages without tags' do it { is_expected.to be_a(Hash) } it { expect(subject["latest"]).to eq(latest_package.version) } + + it 'avoids N+1 database queries' do + check_n_plus_one(:dist_tags) do + create_list(:npm_package, 5, project: project, name: package_name) + end + end end context 'for packages with tags' do - let!(:package_tag1) { create(:packages_tag, package: package1, name: 'release_a') } - let!(:package_tag2) { create(:packages_tag, package: package1, name: 'test_release') } - let!(:package_tag3) { create(:packages_tag, package: package2, name: 'release_b') } - let!(:package_tag4) { create(:packages_tag, package: latest_package, name: 'release_c') } - let!(:package_tag5) { create(:packages_tag, package: latest_package, name: 'latest') } + let_it_be(:package_tag1) { create(:packages_tag, package: package1, name: 'release_a') } + let_it_be(:package_tag2) { create(:packages_tag, package: package1, name: 'test_release') } + let_it_be(:package_tag3) { create(:packages_tag, package: package2, name: 'release_b') } + let_it_be(:package_tag4) { create(:packages_tag, package: latest_package, name: 'release_c') } + let_it_be(:package_tag5) { create(:packages_tag, package: latest_package, name: 'latest') } it { is_expected.to be_a(Hash) } it { expect(subject[package_tag1.name]).to eq(package1.version) } @@ -61,6 +83,25 @@ RSpec.describe ::Packages::Npm::PackagePresenter do it { expect(subject[package_tag3.name]).to eq(package2.version) } it { expect(subject[package_tag4.name]).to eq(latest_package.version) } it { expect(subject[package_tag5.name]).to eq(latest_package.version) } + + it 'avoids N+1 database queries' do + check_n_plus_one(:dist_tags) do + create_list(:npm_package, 5, project: project, name: package_name).each_with_index do |npm_package, index| + create(:packages_tag, package: npm_package, name: "tag_#{index}") + end + end + end end end + + def check_n_plus_one(field) + pkgs = project.packages.npm.with_name(package_name).last_of_each_version.preload_files + control = ActiveRecord::QueryRecorder.new { described_class.new(package_name, pkgs).public_send(field) } + + yield + + pkgs = project.packages.npm.with_name(package_name).last_of_each_version.preload_files + + expect { described_class.new(package_name, pkgs).public_send(field) }.not_to exceed_query_limit(control) + end end diff --git a/spec/presenters/project_presenter_spec.rb b/spec/presenters/project_presenter_spec.rb index fd75c8411d5..5f789f59908 100644 --- a/spec/presenters/project_presenter_spec.rb +++ b/spec/presenters/project_presenter_spec.rb @@ -649,36 +649,18 @@ RSpec.describe ProjectPresenter do end end - describe 'experiment(:repo_integrations_link)' do - context 'when enabled' do - before do - stub_experiments(repo_integrations_link: :candidate) - end - - it 'includes a button to configure integrations for maintainers' do - project.add_maintainer(user) - - expect(empty_repo_statistics_buttons.map(&:label)).to include( - a_string_including('Configure Integration') - ) - end - - it 'does not include a button if not a maintainer' do - expect(empty_repo_statistics_buttons.map(&:label)).not_to include( - a_string_including('Configure Integration') - ) - end - end + it 'includes a button to configure integrations for maintainers' do + project.add_maintainer(user) - context 'when disabled' do - it 'does not include a button' do - project.add_maintainer(user) + expect(empty_repo_statistics_buttons.map(&:label)).to include( + a_string_including('Configure Integration') + ) + end - expect(empty_repo_statistics_buttons.map(&:label)).not_to include( - a_string_including('Configure Integration') - ) - end - end + it 'does not include a button if not a maintainer' do + expect(empty_repo_statistics_buttons.map(&:label)).not_to include( + a_string_including('Configure Integration') + ) end context 'for a developer' do diff --git a/spec/presenters/snippet_blob_presenter_spec.rb b/spec/presenters/snippet_blob_presenter_spec.rb index 1a5130dcdf6..d7f56c30b5e 100644 --- a/spec/presenters/snippet_blob_presenter_spec.rb +++ b/spec/presenters/snippet_blob_presenter_spec.rb @@ -10,6 +10,7 @@ RSpec.describe SnippetBlobPresenter do describe '#rich_data' do let(:data_endpoint_url) { "/-/snippets/#{snippet.id}/raw/#{branch}/#{file}" } + let(:data_raw_dir) { "/-/snippets/#{snippet.id}/raw/#{branch}/" } before do allow_next_instance_of(described_class) do |instance| @@ -45,7 +46,7 @@ RSpec.describe SnippetBlobPresenter do let(:file) { 'test.ipynb' } it 'returns rich notebook content' do - expect(subject.strip).to eq %Q(<div class="file-content" data-endpoint="#{data_endpoint_url}" id="js-notebook-viewer"></div>) + expect(subject.strip).to eq %Q(<div class="file-content" data-endpoint="#{data_endpoint_url}" data-relative-raw-path="#{data_raw_dir}" id="js-notebook-viewer"></div>) end end |