summaryrefslogtreecommitdiff
path: root/spec/models
diff options
context:
space:
mode:
authorRobert Speicher <robert@gitlab.com>2016-06-10 17:17:19 +0000
committerRobert Speicher <robert@gitlab.com>2016-06-10 17:17:19 +0000
commitb4e84809e8170a43126507da0bc6a3b94c33804b (patch)
treedc318db3813c5e4597a6eed9793d66f4561e7e1c /spec/models
parent9c96074c0e277489637d7d27e681515edd2714ea (diff)
parent24920bc52a5658dd1d16d38ba3dc46f92dfe7675 (diff)
downloadgitlab-ce-b4e84809e8170a43126507da0bc6a3b94c33804b.tar.gz
Merge branch 'finding-multiple-projects-by-paths' into 'master'
Add Project.where_paths_in In https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/4410 I'm working on reducing the SQL queries needed to render Markdown. One reason for the large amount of queries is executing a large number of queries needed to find projects and related data. Basically `Project.find_with_namespace` is called in a loop and then any relations have to be retrieved separately. By using `Project.where_paths_in` we can work around this by doing something like: ```ruby project_paths = [...] # populated by some method projects = Project.where_paths_in(project_paths).includes(:namespace, ...) ``` Ref: https://gitlab.com/gitlab-org/gitlab-ce/issues/18042 See merge request !4535
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/project_spec.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index b6f36d3481e..f3590f72cfe 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -922,4 +922,37 @@ describe Project, models: true do
it { is_expected.to be_falsey }
end
end
+
+ describe '.where_paths_in' do
+ context 'without any paths' do
+ it 'returns an empty relation' do
+ expect(Project.where_paths_in([])).to eq([])
+ end
+ end
+
+ context 'without any valid paths' do
+ it 'returns an empty relation' do
+ expect(Project.where_paths_in(%w[foo])).to eq([])
+ end
+ end
+
+ context 'with valid paths' do
+ let!(:project1) { create(:project) }
+ let!(:project2) { create(:project) }
+
+ it 'returns the projects matching the paths' do
+ projects = Project.where_paths_in([project1.path_with_namespace,
+ project2.path_with_namespace])
+
+ expect(projects).to contain_exactly(project1, project2)
+ end
+
+ it 'returns projects regardless of the casing of paths' do
+ projects = Project.where_paths_in([project1.path_with_namespace.upcase,
+ project2.path_with_namespace.upcase])
+
+ expect(projects).to contain_exactly(project1, project2)
+ end
+ end
+ end
end