Commit 4092721b authored by Jan Provaznik's avatar Jan Provaznik Committed by Imre Farkas

Use separate source type for ProjectNamespace

parent 24946c7e
...@@ -7,5 +7,9 @@ module Namespaces ...@@ -7,5 +7,9 @@ module Namespaces
def self.sti_name def self.sti_name
'Project' 'Project'
end end
def self.polymorphic_name
'Namespaces::ProjectNamespace'
end
end end
end end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.shared_examples '.find_by_full_path' do RSpec.shared_examples 'routable resource' do
describe '.find_by_full_path', :aggregate_failures do describe '.find_by_full_path', :aggregate_failures do
it 'finds records by their full path' do it 'finds records by their full path' do
expect(described_class.find_by_full_path(record.full_path)).to eq(record) expect(described_class.find_by_full_path(record.full_path)).to eq(record)
...@@ -52,13 +52,27 @@ RSpec.shared_examples '.find_by_full_path' do ...@@ -52,13 +52,27 @@ RSpec.shared_examples '.find_by_full_path' do
end end
end end
RSpec.describe Routable do RSpec.shared_examples 'routable resource with parent' do
it_behaves_like '.find_by_full_path' do it_behaves_like 'routable resource'
let_it_be(:record) { create(:group) }
describe '#full_path' do
it { expect(record.full_path).to eq "#{record.parent.full_path}/#{record.path}" }
it 'hits the cache when not preloaded' do
forcibly_hit_cached_lookup(record, :full_path)
expect(record.full_path).to eq("#{record.parent.full_path}/#{record.path}")
end
end end
it_behaves_like '.find_by_full_path' do describe '#full_name' do
let_it_be(:record) { create(:project) } it { expect(record.full_name).to eq "#{record.parent.human_name} / #{record.name}" }
it 'hits the cache when not preloaded' do
forcibly_hit_cached_lookup(record, :full_name)
expect(record.full_name).to eq("#{record.parent.human_name} / #{record.name}")
end
end end
end end
...@@ -66,6 +80,14 @@ RSpec.describe Group, 'Routable', :with_clean_rails_cache do ...@@ -66,6 +80,14 @@ RSpec.describe Group, 'Routable', :with_clean_rails_cache do
let_it_be_with_reload(:group) { create(:group, name: 'foo') } let_it_be_with_reload(:group) { create(:group, name: 'foo') }
let_it_be(:nested_group) { create(:group, parent: group) } let_it_be(:nested_group) { create(:group, parent: group) }
it_behaves_like 'routable resource' do
let_it_be(:record) { group }
end
it_behaves_like 'routable resource with parent' do
let_it_be(:record) { nested_group }
end
describe 'Validations' do describe 'Validations' do
it { is_expected.to validate_presence_of(:route) } it { is_expected.to validate_presence_of(:route) }
end end
...@@ -119,24 +141,6 @@ RSpec.describe Group, 'Routable', :with_clean_rails_cache do ...@@ -119,24 +141,6 @@ RSpec.describe Group, 'Routable', :with_clean_rails_cache do
end end
end end
describe '.find_by_full_path' do
it_behaves_like '.find_by_full_path' do
let_it_be(:record) { group }
end
it_behaves_like '.find_by_full_path' do
let_it_be(:record) { nested_group }
end
it 'does not find projects with a matching path' do
project = create(:project)
redirect_route = create(:redirect_route, source: project)
expect(described_class.find_by_full_path(project.full_path)).to be_nil
expect(described_class.find_by_full_path(redirect_route.path, follow_redirects: true)).to be_nil
end
end
describe '.where_full_path_in' do describe '.where_full_path_in' do
context 'without any paths' do context 'without any paths' do
it 'returns an empty relation' do it 'returns an empty relation' do
...@@ -195,64 +199,39 @@ RSpec.describe Group, 'Routable', :with_clean_rails_cache do ...@@ -195,64 +199,39 @@ RSpec.describe Group, 'Routable', :with_clean_rails_cache do
expect(group.route_loaded?).to be_truthy expect(group.route_loaded?).to be_truthy
end end
end end
describe '#full_path' do
it { expect(group.full_path).to eq(group.path) }
it { expect(nested_group.full_path).to eq("#{group.full_path}/#{nested_group.path}") }
it 'hits the cache when not preloaded' do
forcibly_hit_cached_lookup(nested_group, :full_path)
expect(nested_group.full_path).to eq("#{group.full_path}/#{nested_group.path}")
end
end
describe '#full_name' do
it { expect(group.full_name).to eq(group.name) }
it { expect(nested_group.full_name).to eq("#{group.name} / #{nested_group.name}") }
it 'hits the cache when not preloaded' do
forcibly_hit_cached_lookup(nested_group, :full_name)
expect(nested_group.full_name).to eq("#{group.name} / #{nested_group.name}")
end
end
end end
RSpec.describe Project, 'Routable', :with_clean_rails_cache do RSpec.describe Project, 'Routable', :with_clean_rails_cache do
let_it_be(:namespace) { create(:namespace) } let_it_be(:namespace) { create(:namespace) }
let_it_be(:project) { create(:project, namespace: namespace) } let_it_be(:project) { create(:project, namespace: namespace) }
it_behaves_like '.find_by_full_path' do it_behaves_like 'routable resource with parent' do
let_it_be(:record) { project } let_it_be(:record) { project }
end end
end
it 'does not find groups with a matching path' do RSpec.describe Namespaces::ProjectNamespace, 'Routable', :with_clean_rails_cache do
group = create(:group) let_it_be(:group) { create(:group) }
redirect_route = create(:redirect_route, source: group) let_it_be(:project_namespace) do
# For now we create only project namespace w/o project, otherwise same path
expect(described_class.find_by_full_path(group.full_path)).to be_nil # would be used for project and project namespace.
expect(described_class.find_by_full_path(redirect_route.path, follow_redirects: true)).to be_nil # This can be removed when route is created automatically for project namespaces.
end # https://gitlab.com/gitlab-org/gitlab/-/issues/346448
create(:project_namespace, project: nil, parent: group,
describe '#full_path' do visibility_level: Gitlab::VisibilityLevel::PUBLIC,
it { expect(project.full_path).to eq "#{namespace.full_path}/#{project.path}" } path: 'foo', name: 'foo').tap do |project_namespace|
Route.create!(source: project_namespace, path: project_namespace.full_path,
it 'hits the cache when not preloaded' do name: project_namespace.full_name)
forcibly_hit_cached_lookup(project, :full_path)
expect(project.full_path).to eq("#{namespace.full_path}/#{project.path}")
end end
end end
describe '#full_name' do # we have couple of places where we use generic Namespace, in that case
it { expect(project.full_name).to eq "#{namespace.human_name} / #{project.name}" } # we don't want to include ProjectNamespace routes yet
it 'ignores project namespace when searching for generic namespace' do
it 'hits the cache when not preloaded' do redirect_route = create(:redirect_route, source: project_namespace)
forcibly_hit_cached_lookup(project, :full_name)
expect(project.full_name).to eq("#{namespace.human_name} / #{project.name}") expect(Namespace.find_by_full_path(project_namespace.full_path)).to be_nil
end expect(Namespace.find_by_full_path(redirect_route.path, follow_redirects: true)).to be_nil
end end
end end
......
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