Skip to content

Commit

Permalink
4700: handle nil filters (#11874)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpethtel authored Feb 23, 2023
1 parent 0ab37b9 commit e962553
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
2 changes: 2 additions & 0 deletions modules/mobile/app/helpers/mobile/list_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def self.matches(list, filter_params)
end

def result
return [@list, nil] if @filter_params.nil?

validate!
[matches, nil]
rescue => e
Expand Down
50 changes: 28 additions & 22 deletions modules/mobile/spec/helpers/mobile/list_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ def paramiterize(params)
expect(error).to be_nil
end

it 'returns the list when filters are nil' do
result, error = Mobile::ListFilter.matches(list, nil)
expect(result).to eq(list)
expect(error).to be_nil
end

it 'returns the list when empty filters are provided' do
params = paramiterize({})

Expand All @@ -91,6 +97,28 @@ def paramiterize(params)
expect(error).to be_nil
end

it 'filters an array of Common::Resource objects' do
filters = { species: { eq: 'cat' } }
params = paramiterize(filters)

results, error = Mobile::ListFilter.matches(list, params)
expect(results).to eq([cat])
expect(error).to be_nil
end

it 'filters an array of Common::Base objects' do
filters = { species: { eq: 'dog' } }
params = paramiterize(filters)
base_pup = PetBase.new(species: 'dog', age: 1, fully_vaccinated: false)
base_dog = PetBase.new(species: 'dog', age: 5, fully_vaccinated: true)
base_cat = PetBase.new(species: 'cat', age: 12, fully_vaccinated: nil)
base_list = [base_pup, base_dog, base_cat]

results, error = Mobile::ListFilter.matches(base_list, params)
expect(results).to eq([base_pup, base_dog])
expect(error).to be_nil
end

describe 'data validation and error handling' do
before do
Settings.sentry.dsn = 'asdf'
Expand All @@ -100,28 +128,6 @@ def paramiterize(params)
Settings.sentry.dsn = nil
end

it 'works with an array of Common::Resource objects' do
filters = { species: { eq: 'dog' } }
params = paramiterize(filters)

results, error = Mobile::ListFilter.matches(list, params)
expect(results).to eq([dog, puppy])
expect(error).to be_nil
end

it 'works with an array of Common::Base objects' do
filters = { species: { eq: 'dog' } }
params = paramiterize(filters)
base_pup = PetBase.new(species: 'dog', age: 1, fully_vaccinated: false)
base_dog = PetBase.new(species: 'dog', age: 5, fully_vaccinated: true)
base_cat = PetBase.new(species: 'cat', age: 12, fully_vaccinated: nil)
base_list = [base_pup, base_dog, base_cat]

results, error = Mobile::ListFilter.matches(base_list, params)
expect(results).to eq([base_pup, base_dog])
expect(error).to be_nil
end

it 'logs an error and returns original list when list is not an array' do
params = paramiterize({})

Expand Down

0 comments on commit e962553

Please sign in to comment.