Skip to content

Commit

Permalink
Tests covering PartnerFilter
Browse files Browse the repository at this point in the history
And some minor code fixes
  • Loading branch information
ivankocienski committed Oct 22, 2024
1 parent cf65617 commit 93356e5
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/filters/partners_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def name_value
@name_value ||= @params[:name].to_s.strip
end

#def link_to(title, options={})
#end

def keyword_value
@keyword_value ||=
begin
Expand Down
2 changes: 1 addition & 1 deletion app/models/partner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def search_data
optional: true

scope :with_fuzzy_string, lambda { |string_value|
string_value = string_value.to_s.gsub(/\s+/, '') # remove whitespace
string_value = string_value.to_s.strip
return none if string_value.empty?

# uses searchkick
Expand Down
2 changes: 1 addition & 1 deletion app/views/partners/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
= @partner_filter.title_part(self)
%br

- if @partner_filter.result.empty?
- if @partner_filter.empty?
%h2 No results found :(

- @partner_filter.result.each do |partner|
Expand Down
173 changes: 173 additions & 0 deletions spec/filters/partners_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@

require 'rails_helper'

RSpec.describe PartnersFilter, type: :unit do

let(:geo_enclosure) do
GeoEnclosure.create!(
name: 'beta enclosure',
ons_id: 'abc123',
ons_version: '100', ons_type: 'ward'
)
end

let(:keyword) { Keyword.create! name: 'alpha' }

describe 'basic value function' do
describe '#name_value' do
it 'is normalized' do
filter = PartnersFilter.new(name: ' Hello ')
expect(filter.name_value).to eq 'Hello'
end
end

describe '#keyword_value' do
it 'is blank if not set' do
filter = PartnersFilter.new({})
expect(filter.keyword_value).to be nil
end

it 'is blank if not found' do
filter = PartnersFilter.new({ keyword: 'alpha'})
expect(filter.keyword_value).to be nil
end

it 'returns keyword object if it exists' do
keyword # must exist
filter = PartnersFilter.new({ keyword: 'alpha' })
expect(filter.keyword_value).to eq keyword
end
end

describe '#geo_value' do
it 'is blank if not set' do
filter = PartnersFilter.new({})
expect(filter.geo_value).to be nil
end

it 'is blank if not found' do
filter = PartnersFilter.new({ geo: 123456 })
expect(filter.geo_value).to be nil
end

it 'returns GeoEnclosure object if it exists' do
filter = PartnersFilter.new({ geo: geo_enclosure.id })
expect(filter.geo_value).to eq geo_enclosure
end
end

describe '#active?' do
it 'is false when no params are present' do
filter = PartnersFilter.new({})
expect(filter.active?).to be false

# names of just whitespace are ignored
filter = PartnersFilter.new({ name: " \t\n\r " })
expect(filter.active?).to be false
end

it 'is true if any param value is set (and valid)' do
filter = PartnersFilter.new({ name: 'hello' })
expect(filter.active?).to be true

keyword # must exist
filter = PartnersFilter.new({ keyword: 'alpha' })
expect(filter.active?).to be true

filter = PartnersFilter.new({ geo: geo_enclosure.id })
expect(filter.active?).to be true
end
end

describe '#empty?' do
it 'is false if no params are set' do
filter = PartnersFilter.new({})
expect(filter.empty?).to be false
end

it 'is true if param set but there are no results' do
filter = PartnersFilter.new({ name: 'alpha' })
expect(filter.empty?).to be true
end

it 'is false if param set and a result is present' do
partner = Partner.create(
name: 'Zulu partner',
placecal_id: 123,
address_ward: geo_enclosure
)

filter = PartnersFilter.new({ geo: geo_enclosure.id })
expect(filter.empty?).to be false
end
end
end

describe '#link_to' # TODO

describe '#search_form' # TODO

describe '#title_part' # TODO

describe '#results' do
let!(:partner_by_name) do
Partner.create!(
name: 'Zulu Partner Alpha',
placecal_id: 100,
)
end

let!(:partner_by_keyword) do
partner = Partner.create!(
name: 'Zulu partner Beta',
placecal_id: 200,
)
partner.keywords << keyword
partner
end

let!(:partner_by_geo) do
Partner.create(
name: 'Zulu partner Cappa',
placecal_id: 300,
address_ward: geo_enclosure
)
end

before :each do
Partner.reindex
end

it 'returns all partners when no params given' do
filter = PartnersFilter.new({ })
results = filter.result

expect(results.count).to eq 3
end

it 'filters on name' do
filter = PartnersFilter.new({ name: 'zulu alpha' })
results = filter.result

expect(results.count).to eq 1
expect(results.first).to eq partner_by_name
end

it 'filters on keyword' do
filter = PartnersFilter.new({ keyword: 'alpha' })
results = filter.result

expect(results.count).to eq 1
expect(results.first).to eq partner_by_keyword
end

it 'filters on geo' do
filter = PartnersFilter.new({ geo: geo_enclosure.id })
results = filter.result

expect(results.count).to eq 1
expect(results.first).to eq partner_by_geo
end
end

end
2 changes: 1 addition & 1 deletion spec/models/partner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

it 'finds names with a fuzzy string match' do
# finds stemmed word (burger in burgers)
result = Partner.with_fuzzy_string('bob burger').first
result = Partner.with_fuzzy_string('bob burg').first
expect(result.placecal_id).to be 101

# case insensitive
Expand Down

0 comments on commit 93356e5

Please sign in to comment.