Commit e8c1d3e7 authored by Matija Čupić's avatar Matija Čupić

Move offset and limit params to BranchNamesFinder

Moves the offset and limit parameters to the BranchNamesFinder.
parent f5910a20
......@@ -10,9 +10,9 @@ module Repositories
end
def execute
return unless search
return unless search && offset && limit
repository.search_branch_names(search)
repository.search_branch_names(search).lazy.drop(offset).take(limit) # rubocop:disable CodeReuse/ActiveRecord
end
private
......@@ -20,5 +20,13 @@ module Repositories
def search
@params[:search].presence
end
def offset
@params[:offset]
end
def limit
@params[:limit]
end
end
end
......@@ -19,16 +19,7 @@ module Resolvers
description: 'The number of branch names to return.'
def resolve(search_pattern:, offset:, limit:)
names = branch_names(search_pattern)
return unless names
names.lazy.drop(offset).take(limit) # rubocop:disable CodeReuse/ActiveRecord
end
private
def branch_names(search_pattern)
Repositories::BranchNamesFinder.new(object, search: search_pattern).execute
Repositories::BranchNamesFinder.new(object, offset: offset, limit: limit, search: search_pattern).execute
end
end
end
......@@ -5,21 +5,34 @@ require 'spec_helper'
RSpec.describe Repositories::BranchNamesFinder do
let(:project) { create(:project, :repository) }
let(:branch_names_finder) { described_class.new(project.repository, search: 'conflict-*') }
describe '#execute' do
subject(:execute) { branch_names_finder.execute }
it 'filters branch names' do
expect(execute).to contain_exactly(
'conflict-binary-file',
'conflict-resolvable',
'conflict-contains-conflict-markers',
'conflict-missing-side',
'conflict-start',
'conflict-non-utf8',
'conflict-too-large'
it 'returns all filtered branch names' do
expect(create_branch_names_finder(0, 100).execute).to contain_exactly(
'snippet/edit-file',
'snippet/multiple-files',
'snippet/no-files',
'snippet/rename-and-edit-file',
'snippet/single-file'
)
end
it 'returns a limited number of offset filtered branch names' do
starting_names = create_branch_names_finder(0, 3).execute
offset_names = create_branch_names_finder(3, 2).execute
expect(starting_names.count).to eq(3)
expect(offset_names.count).to eq(2)
expect(offset_names).not_to include(*starting_names)
all_names = create_branch_names_finder(0, 100).execute
expect(all_names).to contain_exactly(*starting_names, *offset_names)
end
private
def create_branch_names_finder(offset, limit)
described_class.new(project.repository, search: 'snippet/*', offset: offset, limit: limit)
end
end
end
......@@ -8,23 +8,11 @@ RSpec.describe Resolvers::RepositoryBranchNamesResolver do
let(:project) { create(:project, :repository) }
describe '#resolve' do
subject(:resolve_branch_names) do
resolve(
described_class,
obj: project.repository,
args: { search_pattern: pattern, offset: offset, limit: 1 },
ctx: { current_user: project.creator }
)
end
context 'with zero offset' do
let(:offset) { 0 }
context 'with empty search pattern' do
let(:pattern) { '' }
it 'returns nil' do
expect(resolve_branch_names).to eq(nil)
expect(resolve_branch_names(pattern, 0, 100)).to eq(nil)
end
end
......@@ -32,18 +20,38 @@ RSpec.describe Resolvers::RepositoryBranchNamesResolver do
let(:pattern) { 'snippet/*' }
it 'returns matching branches' do
expect(resolve_branch_names).to contain_exactly('snippet/rename-and-edit-file')
end
end
expect(resolve_branch_names(pattern, 0, 100)).to contain_exactly(
'snippet/edit-file',
'snippet/multiple-files',
'snippet/no-files',
'snippet/rename-and-edit-file',
'snippet/single-file'
)
end
context 'with offset' do
let(:pattern) { 'snippet/*' }
let(:offset) { 1 }
it 'properly offsets and limits branch name results' do
starting_names = resolve_branch_names(pattern, 0, 3)
offset_names = resolve_branch_names(pattern, 3, 2)
expect(starting_names.count).to eq(3)
expect(offset_names.count).to eq(2)
expect(offset_names).not_to include(*starting_names)
it 'skips first match' do
expect(resolve_branch_names).to contain_exactly('snippet/edit-file')
all_names = resolve_branch_names(pattern, 0, 100)
expect(all_names).to contain_exactly(*starting_names, *offset_names)
end
end
end
private
def resolve_branch_names(pattern, offset, limit)
resolve(
described_class,
obj: project.repository,
args: { search_pattern: pattern, offset: offset, limit: limit },
ctx: { current_user: project.creator }
)
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