Add deep search for filter taking in properties #1749

This commit is contained in:
Anders Søgaard
2024-12-03 14:11:15 +01:00
committed by Guilherme Carreiro
parent 4b01977067
commit 1058a7768f
2 changed files with 365 additions and 18 deletions
+293
View File
@@ -286,6 +286,60 @@ class StandardFiltersTest < Minitest::Test
assert_equal([{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], @filters.sort([{ "a" => 4 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a"))
end
def test_sort_deep_default_separator
input = [
{ "foo" => { "price" => 4, "handle" => "alpha" } },
{ "foo" => { "handle" => "beta" } },
{ "foo" => { "price" => 1, "handle" => "gamma" } },
{ "foo" => { "handle" => "delta" } },
{ "foo" => { "price" => 2, "handle" => "epsilon" } },
]
expectation = [
{ "foo" => { "price" => 1, "handle" => "gamma" } },
{ "foo" => { "price" => 2, "handle" => "epsilon" } },
{ "foo" => { "price" => 4, "handle" => "alpha" } },
{ "foo" => { "handle" => "beta" } },
{ "foo" => { "handle" => "delta" } },
]
assert_equal(expectation, @filters.sort(input, "foo.price", { "deep" => true }))
end
def test_sort_deep_custom_separator
input = [
{ "foo" => { "price" => 4, "handle" => "alpha" } },
{ "foo" => { "handle" => "beta" } },
{ "foo" => { "price" => 1, "handle" => "gamma" } },
{ "foo" => { "handle" => "delta" } },
{ "foo" => { "price" => 2, "handle" => "epsilon" } },
]
expectation = [
{ "foo" => { "price" => 1, "handle" => "gamma" } },
{ "foo" => { "price" => 2, "handle" => "epsilon" } },
{ "foo" => { "price" => 4, "handle" => "alpha" } },
{ "foo" => { "handle" => "beta" } },
{ "foo" => { "handle" => "delta" } },
]
assert_equal(expectation, @filters.sort(input, "foo_price", { "deep" => "_" }))
end
def test_sort_deep_off_by_default
input = [
{ "foo.price" => 4, "handle" => "alpha" },
{ "handle" => "beta" },
{ "foo.price" => 1, "handle" => "gamma" },
{ "handle" => "delta" },
{ "foo.price" => 2, "handle" => "epsilon" },
]
expectation = [
{ "foo.price" => 1, "handle" => "gamma" },
{ "foo.price" => 2, "handle" => "epsilon" },
{ "foo.price" => 4, "handle" => "alpha" },
{ "handle" => "beta" },
{ "handle" => "delta" },
]
assert_equal(expectation, @filters.sort(input, "foo.price"))
end
def test_sort_with_nils
assert_equal([1, 2, 3, 4, nil], @filters.sort([nil, 4, 3, 2, 1]))
assert_equal([{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }, {}], @filters.sort([{ "a" => 4 }, { "a" => 3 }, {}, { "a" => 1 }, { "a" => 2 }], "a"))
@@ -360,6 +414,72 @@ class StandardFiltersTest < Minitest::Test
assert_equal(["a", "b", "c", "X", "Y", "Z"], @filters.sort_natural(["X", "Y", "Z", "a", "b", "c"]))
end
def test_sort_natural_deep_default_separator
input = [
{ "foo" => { "key" => "X" } },
{ "foo" => { "key" => "Y" } },
{ "foo" => { "key" => "Z" } },
{ "foo" => { "fake" => "t" } },
{ "foo" => { "key" => "a" } },
{ "foo" => { "key" => "b" } },
{ "foo" => { "key" => "c" } },
]
expectation = [
{ "foo" => { "key" => "a" } },
{ "foo" => { "key" => "b" } },
{ "foo" => { "key" => "c" } },
{ "foo" => { "key" => "X" } },
{ "foo" => { "key" => "Y" } },
{ "foo" => { "key" => "Z" } },
{ "foo" => { "fake" => "t" } },
]
assert_equal(expectation, @filters.sort_natural(input, "foo.key", { "deep" => true }))
end
def test_sort_natural_deep_custom_separator
input = [
{ "foo" => { "key" => "X" } },
{ "foo" => { "key" => "Y" } },
{ "foo" => { "key" => "Z" } },
{ "foo" => { "fake" => "t" } },
{ "foo" => { "key" => "a" } },
{ "foo" => { "key" => "b" } },
{ "foo" => { "key" => "c" } },
]
expectation = [
{ "foo" => { "key" => "a" } },
{ "foo" => { "key" => "b" } },
{ "foo" => { "key" => "c" } },
{ "foo" => { "key" => "X" } },
{ "foo" => { "key" => "Y" } },
{ "foo" => { "key" => "Z" } },
{ "foo" => { "fake" => "t" } },
]
assert_equal(expectation, @filters.sort_natural(input, "foo_key", { "deep" => "_" }))
end
def test_sort_natural_deep_off_by_default
input = [
{ "foo.key" => "X" },
{ "foo.key" => "Y" },
{ "foo.key" => "Z" },
{ "foo.fake" => "t" },
{ "foo.key" => "a" },
{ "foo.key" => "b" },
{ "foo.key" => "c" },
]
expectation = [
{ "foo.key" => "a" },
{ "foo.key" => "b" },
{ "foo.key" => "c" },
{ "foo.key" => "X" },
{ "foo.key" => "Y" },
{ "foo.key" => "Z" },
{ "foo.fake" => "t" },
]
assert_equal(expectation, @filters.sort_natural(input, "foo.key"))
end
def test_sort_empty_array
assert_equal([], @filters.sort([], "a"))
end
@@ -428,6 +548,45 @@ class StandardFiltersTest < Minitest::Test
end
end
def test_uniq_deep_default_separator
input = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "bar" => "baz", "handle" => "beta" } },
{ "foo" => { "bar" => "qux", "handle" => "charlie" } },
]
expectation = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "bar" => "qux", "handle" => "charlie" } },
]
assert_equal(expectation, @filters.uniq(input, "foo.bar", { "deep" => true }))
end
def test_uniq_deep_custom_separator
input = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "bar" => "baz", "handle" => "beta" } },
{ "foo" => { "bar" => "qux", "handle" => "charlie" } },
]
expectation = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "bar" => "qux", "handle" => "charlie" } },
]
assert_equal(expectation, @filters.uniq(input, "foo_bar", { "deep" => "_" }))
end
def test_uniq_deep_off_by_default
input = [
{ "foo.bar" => "baz", "handle" => "alpha" },
{ "foo.bar" => "baz", "handle" => "beta" },
{ "foo.bar" => "qux", "handle" => "charlie" },
]
expectation = [
{ "foo.bar" => "baz", "handle" => "alpha" },
{ "foo.bar" => "qux", "handle" => "charlie" },
]
assert_equal(expectation, @filters.uniq(input, "foo.bar"))
end
def test_compact_empty_array
assert_equal([], @filters.compact([], "a"))
end
@@ -444,6 +603,45 @@ class StandardFiltersTest < Minitest::Test
end
end
def test_compact_deep_default_separator
input = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "handle" => "beta" } },
{ "foo" => { "bar" => "qux", "handle" => "charlie" } },
]
expectation = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "bar" => "qux", "handle" => "charlie" } },
]
assert_equal(expectation, @filters.compact(input, "foo.bar", { "deep" => true }))
end
def test_compact_deep_custom_separator
input = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "handle" => "beta" } },
{ "foo" => { "bar" => "qux", "handle" => "charlie" } },
]
expectation = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "bar" => "qux", "handle" => "charlie" } },
]
assert_equal(expectation, @filters.compact(input, "foo_bar", { "deep" => "_" }))
end
def test_compact_deep_off_by_default
input = [
{ "foo.bar" => "baz", "handle" => "alpha" },
{ "foo.handle" => "beta" },
{ "foo.bar" => "qux", "handle" => "charlie" },
]
expectation = [
{ "foo.bar" => "baz", "handle" => "alpha" },
{ "foo.bar" => "qux", "handle" => "charlie" },
]
assert_equal(expectation, @filters.compact(input, "foo.bar"))
end
def test_reverse
assert_equal([4, 3, 2, 1], @filters.reverse([1, 2, 3, 4]))
end
@@ -461,6 +659,30 @@ class StandardFiltersTest < Minitest::Test
)
end
def test_map_deep_default_separator
assert_template_result(
'abc',
"{{ ary | map: 'foo.bar', deep: true }}",
{ 'ary' => [{ 'foo' => { 'bar' => 'a' } }, { 'foo' => { 'bar' => 'b' } }, { 'foo' => { 'bar' => 'c' } }] },
)
end
def test_map_deep_custom_separator
assert_template_result(
'abc',
"{{ ary | map: 'foo_bar', deep: '_' }}",
{ 'ary' => [{ 'foo' => { 'bar' => 'a' } }, { 'foo' => { 'bar' => 'b' } }, { 'foo' => { 'bar' => 'c' } }] },
)
end
def test_map_deep_off_by_default
assert_template_result(
'abc',
"{{ ary | map: 'foo.bar' }}",
{ 'ary' => [{ 'foo.bar' => 'a' }, { 'foo.bar' => 'b' }, { 'foo.bar' => 'c' }] },
)
end
def test_map_doesnt_call_arbitrary_stuff
assert_template_result("", '{{ "foo" | map: "__id__" }}')
assert_template_result("", '{{ "foo" | map: "inspect" }}')
@@ -972,6 +1194,7 @@ class StandardFiltersTest < Minitest::Test
assert_nil(@filters.where([nil], "ok"))
end
<<<<<<< HEAD
def test_reject
input = [
{ "handle" => "alpha", "ok" => true },
@@ -1045,6 +1268,45 @@ class StandardFiltersTest < Minitest::Test
assert_equal(expectation, @filters.reject(input, "item.ok", false))
assert_equal(expectation, @filters.reject(input, "item.ok"))
=======
def test_where_deep_default_separator
input = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "bar" => "baz", "handle" => "beta" } },
{ "foo" => { "bar" => "qux", "handle" => "charlie" } },
]
expectation = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "bar" => "baz", "handle" => "beta" } },
]
assert_equal(expectation, @filters.where(input, "foo.bar", "baz", { "deep" => true }))
end
def test_where_deep_custom_separator
input = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "bar" => "baz", "handle" => "beta" } },
{ "foo" => { "bar" => "qux", "handle" => "charlie" } },
]
expectation = [
{ "foo" => { "bar" => "baz", "handle" => "alpha" } },
{ "foo" => { "bar" => "baz", "handle" => "beta" } },
]
assert_equal(expectation, @filters.where(input, "foo_bar", "baz", { "deep" => "_" }))
end
def test_where_deep_off_by_default
input = [
{ "foo.bar" => "baz", "handle" => "alpha" },
{ "foo.bar" => "baz", "handle" => "beta" },
{ "foo.bar" => "qux", "handle" => "charlie" },
]
expectation = [
{ "foo.bar" => "baz", "handle" => "alpha" },
{ "foo.bar" => "baz", "handle" => "beta" },
]
assert_equal(expectation, @filters.where(input, "foo.bar", "baz"))
>>>>>>> 2800d8dc (Add missing tests)
end
def test_all_filters_never_raise_non_liquid_exception
@@ -1182,6 +1444,7 @@ class StandardFiltersTest < Minitest::Test
assert(t.foo > 0)
end
<<<<<<< HEAD
def test_sum_of_floats
input = [0.1, 0.2, 0.3]
assert_equal(0.6, @filters.sum(input))
@@ -1216,6 +1479,36 @@ class StandardFiltersTest < Minitest::Test
assert_template_result("1.2", "{{ input | sum: 'quantity' }}", { "input" => input })
assert_template_result("0.1", "{{ input | sum: 'weight' }}", { "input" => input })
assert_template_result("0", "{{ input | sum: 'subtotal' }}", { "input" => input })
=======
def test_sum_deep_default_separator
input = [
{ "foo" => { "quantity" => 1 } },
{ "foo" => { "quantity" => 2 } },
{ "foo" => { "quantity" => 3 } },
{ "foo" => { "quantity" => 4 } },
]
assert_equal(10, @filters.sum(input, "foo.quantity", { "deep" => true }))
end
def test_sum_deep_custom_separator
input = [
{ "foo" => { "quantity" => 1 } },
{ "foo" => { "quantity" => 2 } },
{ "foo" => { "quantity" => 3 } },
{ "foo" => { "quantity" => 4 } },
]
assert_equal(10, @filters.sum(input, "foo_quantity", { "deep" => "_" }))
end
def test_sum_deep_off_by_default
input = [
{ "foo.quantity" => 1 },
{ "foo.quantity" => 2 },
{ "foo.quantity" => 3 },
{ "foo.quantity" => 4 },
]
assert_equal(10, @filters.sum(input, "foo.quantity"))
>>>>>>> 858428f2 (Initial new tests)
end
private