Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

326 provide basic search functionality #459

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ GEM
amq-protocol (~> 2.3.0)
bunny-mock (1.7.0)
bunny (>= 1.7)
byebug (10.0.1)
byebug (10.0.2)
charlock_holmes (0.7.6)
chewy (5.0.0)
activesupport (>= 4.0)
Expand Down Expand Up @@ -126,8 +126,8 @@ GEM
responders
warden (~> 1.2.3)
diff-lcs (1.3)
docile (1.3.0)
dotenv (2.2.1)
docile (1.3.1)
dotenv (2.4.0)
dry-configurable (0.7.0)
concurrent-ruby (~> 1.0)
dry-container (0.6.0)
Expand Down Expand Up @@ -182,7 +182,7 @@ GEM
fasterer (0.4.1)
colorize (~> 0.7)
ruby_parser (~> 3.11.0)
ffi (1.9.23)
ffi (1.9.25)
filelock (1.1.1)
formatador (0.2.5)
fuubar (2.3.1)
Expand Down Expand Up @@ -365,14 +365,14 @@ GEM
activemodel (>= 4.0.0)
railties (>= 4.0.0)
sequel (>= 3.28, < 6.0)
sequel_pg (1.8.1)
sequel_pg (1.8.2)
pg (>= 0.18.0)
sequel (>= 4.34.0)
sequel_postgresql_triggers (1.4.0)
sequel
serverengine (2.0.6)
sigdump (~> 0.2.2)
sexp_processor (4.10.1)
sexp_processor (4.11.0)
sigdump (0.2.4)
simplecov (0.16.1)
docile (~> 1.1)
Expand Down Expand Up @@ -403,7 +403,7 @@ GEM
thread_safe (~> 0.1)
unicode-display_width (1.4.0)
url (0.3.2)
uuid (2.3.8)
uuid (2.3.9)
macaddr (~> 1.0)
warden (1.2.7)
rack (>= 1.0)
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
description 'The query string'
end

resolve(->(_root, _arguments, _context) { :ok })
resolve ->(_root, arguments, _context) { arguments }
end

field :version, !Types::VersionType do
Expand Down
4 changes: 1 addition & 3 deletions app/graphql/types/search_result_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
description 'Limit search to certain categories'
end

resolve(lambda do |_root, arguments, _context|
SearchResult.new(categories: arguments['categories'])
end)
resolve SearchResolver.new
end
end
86 changes: 86 additions & 0 deletions lib/search_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# frozen_string_literal: true

require 'ostruct'

# Returns a search result to the GraphQL API
class SearchResolver
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E: class definition in method body (Using Ruby 2.5 parser; configure using TargetRubyVersion parameter, under AllCops)

def call(root, arguments, _context)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Assignment Branch Condition size for call is too high. [17.26/15]
C: Method has too many lines. [38/10]

query = root[:query]
categories = arguments[:categories]
if categories.blank?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Method has too many lines. [17/10]

indices = [::Index::RepositoryIndex::Repository,
::Index::OrganizationIndex::Organization,
::Index::UserIndex::User]
else
indices = categories.map do |category|
Copy link
Contributor

@phyrog phyrog Jun 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out Enumerable#reduce:

indices = categories.reduce([]) do |indices, category|
  ...
    when 'repositories'
      indices + [::Index::RepositoryIndex::Repository]
  ...
end

that way you don't need to flatten the array at the end, which might make it faster.

case category
when 'organizationalUnits'
[::Index::OrganizationIndex::Organization, ::Index::UserIndex::User]
when 'repositories'
[::Index::RepositoryIndex::Repository]
else
[]
end
end
indices = indices.flatten
end
result = indices.map do |index|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be done using reduce as well, but I think here flatten is fine.

index.query(multi_match: {query: query,
fuzziness: 'auto',
fields: [
:display_name,
:slug,
:name,
:description]}).entries
end
result = result.flatten
graphQLResult = OpenStruct.new(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W: Useless assignment to variable - graphQLResult.
C: Use snake_case for variable names.

entries: createEntries(result),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W: Useless assignment to variable - graphQLResult.
C: Use snake_case for variable names.

count: OpenStruct.new(
all: result.size,
organizational_units: allOrganizationalUnits(result),
repositories: allRepositories(result)
)
)
end

private
def allOrganizationalUnits(result)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Use snake_case for method names.

search_result = 0
result.each do |element|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Use snake_case for method names.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out Enumerable#count

elem = element._data["_index"]
if elem == 'user' || elem == 'organization'
search_result+=1
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Avoid comparing a variable with multiple items in a conditional, use Array#include? instead.

end
search_result
end

private
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W: Useless private access modifier.

def allRepositories(result)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Use snake_case for method names.

search_result = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Use snake_case for method names.

result.each do |element|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out Enumerable#count

elem = element._data["_index"]
if elem == 'repository'
search_result+=1
end
end
search_result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Assignment Branch Condition size for create_entries is too high. [19.1/15]
C: Method has too many lines. [13/10]

end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E: unexpected token kEND (Using Ruby 2.5 parser; configure using TargetRubyVersion parameter, under AllCops)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W: Useless private access modifier.

private
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W: Useless private access modifier.

def createEntries(result)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Assignment Branch Condition size for createEntries is too high. [19.1/15]
C: Method has too many lines. [12/10]
C: Use snake_case for method names.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Assignment Branch Condition size for createEntries is too high. [19.1/15]
C: Method has too many lines. [12/10]
C: Use snake_case for method names.

result.map do |element|
OpenStruct.new(
ranking: element._data["_score"],
entry: if element._data["_type"] == 'user'
User.first(slug: element.attributes["slug"])
elsif element._data["_type"] == 'repository'
Repository.first(slug: element.attributes["slug"])
else
Organization.first(slug: element.attributes["slug"])
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W: end at 82, 8 is not aligned with if at 76, 15.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W: end at 82, 8 is not aligned with if at 76, 15.

)
end
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E: unexpected token kEND (Using Ruby 2.5 parser; configure using TargetRubyVersion parameter, under AllCops)

76 changes: 45 additions & 31 deletions spec/graphql/queries/search_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,26 @@
end

RSpec.describe 'Search query' do
let(:user) { create :user }
before do
create :user
3.times { create :organization }
5.times { create :repository, owner: user }
::Index::UserIndex.purge
::Index::OrganizationIndex.purge
::Index::RepositoryIndex.purge

ada = create :user, display_name: 'Ada'
::Index::UserIndex.import(ada)
adc = create :user, display_name: 'Adc'
::Index::UserIndex.import(adc)
bob = create :user, display_name: 'Bob'
::Index::UserIndex.import(bob)

::Index::OrganizationIndex.import(create :organization, display_name: 'Ada')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [81/80]

::Index::OrganizationIndex.import(create :organization, display_name: 'Bda Organization')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [94/80]

::Index::OrganizationIndex.import(create :organization, display_name: 'Abc_Organization')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [94/80]


::Index::RepositoryIndex.import(create :repository, name: 'Ada/repository', owner: ada)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [92/80]

::Index::RepositoryIndex.import(create :repository, name: 'Bob/repository', owner: bob)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [92/80]

::Index::RepositoryIndex.import(create :repository, name: 'Adc/repository', owner: adc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [92/80]

::Index::RepositoryIndex.import(create :repository, name: 'Bob/AdaRepository', owner: bob)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [95/80]

end

let(:context) { {} }
Expand Down Expand Up @@ -71,88 +86,87 @@
context 'with global scope' do
let(:scope) { 'global' }
context 'no categories' do
let(:variables) { {'query' => ''} }
let(:expected_num_entries) { 10 }
let(:expected_count_all) { 10 }
let(:expected_count_organizational_units) { 5 }
let(:expected_count_repositories) { 5 }
let(:variables) { {'query' => 'Ada'} }
let(:expected_num_entries) { 6 }
let(:expected_count_all) { 6 }
let(:expected_count_organizational_units) { 4 }
let(:expected_count_repositories) { 2 }

include_examples 'number of entries'

it 'returns the repositories' do
repositories = search_result['entries'].select do |e|
e['entry']['__typename'] == 'Repository'
end
expect(repositories.length).to eq(5)
expect(repositories.length).to eq(expected_count_repositories)
end

it 'returns the organizational units' do
organizational_units = search_result['entries'].select do |e|
%w(User Organization).include?(e['entry']['__typename'])
end
expect(organizational_units.length).to eq(5)
expect(organizational_units.length).to eq(expected_count_organizational_units)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [86/80]

end
end

context 'category: all' do
let(:variables) do
{'query' => '', 'categories' => %w(repositories organizationalUnits)}
{'query' => 'Ada', 'categories' => %w(repositories organizationalUnits)}
end
let(:expected_num_entries) { 10 }
let(:expected_count_all) { 10 }
let(:expected_count_organizational_units) { 5 }
let(:expected_count_repositories) { 5 }
let(:expected_num_entries) { 6 }
let(:expected_count_all) { 6 }
let(:expected_count_organizational_units) { 4 }
let(:expected_count_repositories) { 2 }

include_examples 'number of entries'

it 'returns the repositories' do
repositories = search_result['entries'].select do |e|
e['entry']['__typename'] == 'Repository'
end
expect(repositories.length).to eq(5)
expect(repositories.length).to eq(expected_count_repositories)
end

it 'returns the organizational units' do
organizational_units = search_result['entries'].select do |e|
%w(User Organization).include?(e['entry']['__typename'])
end
expect(organizational_units.length).to eq(5)
expect(organizational_units.length).to eq(expected_count_organizational_units)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [86/80]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [86/80]

end
end

context 'category: repositories' do
let(:variables) { {'query' => '', 'categories' => %w(repositories)} }
let(:expected_num_entries) { 5 }
let(:expected_count_all) { 10 }
let(:expected_count_organizational_units) { 5 }
let(:expected_count_repositories) { 5 }
let(:variables) { {'query' => 'Ada', 'categories' => %w(repositories)} }
let(:expected_num_entries) { 2 }
let(:expected_count_all) { 2 }
let(:expected_count_organizational_units) { 0 }
let(:expected_count_repositories) { 2 }

include_examples 'number of entries'

it 'returns the repositories' do
repositories = search_result['entries'].select do |e|
e['entry']['__typename'] == 'Repository'
end
expect(repositories.length).to eq(5)
expect(repositories.length).to eq(expected_count_repositories)
end
end

context 'category: organizationalUnits' do
let(:variables) do
{'query' => '', 'categories' => %w(organizationalUnits)}
{'query' => 'Ada', 'categories' => %w(organizationalUnits)}
end
let(:expected_num_entries) { 5 }
let(:expected_count_all) { 10 }
let(:expected_count_organizational_units) { 5 }
let(:expected_count_repositories) { 5 }
let(:expected_num_entries) { 4 }
let(:expected_count_all) { 4 }
let(:expected_count_organizational_units) { 4 }
let(:expected_count_repositories) { 0 }

include_examples 'number of entries'

it 'returns the organizational units' do
organizational_units = search_result['entries'].select do |e|
%w(User Organization).include?(e['entry']['__typename'])
end
expect(organizational_units.length).to eq(5)
expect(organizational_units.length).to eq(expected_count_organizational_units)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [86/80]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C: Line is too long. [86/80]

end
end
end
Expand Down