Skip to content

Commit

Permalink
Add tests for integer infinite range filter
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Jan 30, 2024
1 parent eb1c4a1 commit 844a36b
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions spec/datagrid/filters/integer_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,55 @@

describe Datagrid::Filters::IntegerFilter do

let(:entry1) { Entry.create!(:group_id => 1) }
let(:entry2) { Entry.create!(:group_id => 2) }
let(:entry3) { Entry.create!(:group_id => 3) }
let(:entry4) { Entry.create!(:group_id => 4) }
let(:entry5) { Entry.create!(:group_id => 5) }
let(:entry7) { Entry.create!(:group_id => 7) }
let(:entry1) { Entry.create!(group_id: 1) }
let(:entry2) { Entry.create!(group_id: 2) }
let(:entry3) { Entry.create!(group_id: 3) }
let(:entry4) { Entry.create!(group_id: 4) }
let(:entry5) { Entry.create!(group_id: 5) }
let(:entry7) { Entry.create!(group_id: 7) }

it "should support integer range argument" do
report = test_report(:group_id => 3..5) do
report = test_report(group_id: 3..5) do
scope { Entry }
filter(:group_id, :integer)
end
expect(report.assets).not_to include(entry1)
expect(report.assets).to include(entry4)
expect(report.assets).not_to include(entry7)
report.group_id = (4..)
expect(report.assets).to include(entry4)
expect(report.assets).to include(entry5)
expect(report.assets).not_to include(entry3)
report.group_id = (..2)
expect(report.assets).to include(entry1)
expect(report.assets).to include(entry2)
expect(report.assets).not_to include(entry3)
end

it "should support integer range given as array argument" do
report = test_report(:group_id => [3.to_s, 5.to_s]) do
report = test_report(group_id: [3.to_s, 5.to_s]) do
scope { Entry }
filter(:group_id, :integer, :range => true)
filter(:group_id, :integer, range: true)
end
expect(report.assets).not_to include(entry7)
expect(report.assets).to include(entry4)
expect(report.assets).not_to include(entry1)
end

it "should support minimum integer argument" do
report = test_report(:group_id => [5.to_s, nil]) do
report = test_report(group_id: [5.to_s, nil]) do
scope { Entry }
filter(:group_id, :integer, :range => true)
filter(:group_id, :integer, range: true)
end
expect(report.assets).not_to include(entry1)
expect(report.assets).not_to include(entry4)
expect(report.assets).to include(entry7)
end

it "should support maximum integer argument" do
report = test_report(:group_id => [nil, 5.to_s]) do
report = test_report(group_id: [nil, 5.to_s]) do
scope { Entry }
filter(:group_id, :integer, :range => true)
filter(:group_id, :integer, range: true)
end
expect(report.assets).to include(entry1)
expect(report.assets).to include(entry4)
Expand All @@ -51,9 +59,9 @@

it "should find something in one integer interval" do

report = test_report(:group_id => (4..4)) do
report = test_report(group_id: (4..4)) do
scope { Entry }
filter(:group_id, :integer, :range => true)
filter(:group_id, :integer, range: true)
end
expect(report.assets).not_to include(entry7)
expect(report.assets).to include(entry4)
Expand All @@ -62,9 +70,9 @@

it "should support invalid range" do

report = test_report(:group_id => (7..1)) do
report = test_report(group_id: (7..1)) do
scope { Entry }
filter(:group_id, :integer, :range => true)
filter(:group_id, :integer, range: true)
end
expect(report.group_id).to eq([1,7])
expect(report.assets).to include(entry7)
Expand All @@ -74,9 +82,9 @@


it "should support block" do
report = test_report(:group_id => 5) do
report = test_report(group_id: 5) do
scope { Entry }
filter(:group_id, :integer, :range => true) do |value|
filter(:group_id, :integer, range: true) do |value|
where("group_id >= ?", value)
end
end
Expand All @@ -86,29 +94,29 @@


it "should not prefix table name if column is joined" do
report = test_report(:rating => [4,nil]) do
report = test_report(rating: [4,nil]) do
scope { Entry.joins(:group) }
filter(:rating, :integer, :range => true)
filter(:rating, :integer, range: true)
end
expect(report.rating).to eq([4,nil])
expect(report.assets).not_to include(Entry.create!(:group => Group.create!(:rating => 3)))
expect(report.assets).to include(Entry.create!(:group => Group.create!(:rating => 5)))
expect(report.assets).not_to include(Entry.create!(group: Group.create!(rating: 3)))
expect(report.assets).to include(Entry.create!(group: Group.create!(rating: 5)))
end

it "should support multiple values" do
report = test_report(:group_id => "1,2") do
report = test_report(group_id: "1,2") do
scope {Entry}
filter(:group_id, :integer, :multiple => true)
filter(:group_id, :integer, multiple: true)
end
expect(report.group_id).to eq([1,2])
expect(report.assets).to include(entry1)
expect(report.assets).to include(entry2)
expect(report.assets).not_to include(entry3)
end
it "should support custom separator multiple values" do
report = test_report(:group_id => "1|2") do
report = test_report(group_id: "1|2") do
scope {Entry}
filter(:group_id, :integer, :multiple => '|')
filter(:group_id, :integer, multiple: '|')
end
expect(report.group_id).to eq([1,2])
expect(report.assets).to include(entry1)
Expand All @@ -119,7 +127,7 @@
it "should support multiple with allow_blank allow_nil options" do
report = test_report do
scope {Entry}
filter(:group_id, :integer, :multiple => true, :allow_nil => false, :allow_blank => true )
filter(:group_id, :integer, multiple: true, allow_nil: false, allow_blank: true )
end
report.group_id = []
expect(report.assets).to_not include(entry1)
Expand Down

0 comments on commit 844a36b

Please sign in to comment.