def test_equal(self): self.assertEqual(Expression("", "", ""), Expression("", "", "")) self.assertEqual( Expression(Child(This(), Fields("name")), "==", 4), Expression(Child(This(), Fields("name")), "==", 4), ) self.assertNotEqual(Expression("", "", ""), Expression(None, None, None))
def test_repr_all_nodes(self): self.assertEqual(repr(Node(None, None)), "") self.assertEqual(repr(Child(Root(), Fields("name"))), "Child(Root(), Fields('name'))") self.assertEqual( repr(Descendants(Root(), Fields("name"))), "Descendants(Root(), Fields('name'))", ) self.assertEqual(repr(Union(Root(), Fields("name"))), "Union(Root()|Fields('name'))")
def test_find(self): data = {"value": 2, "other": 3} self.assertEqual( Expression(Child(This(), Fields("value")), "==", 2).find(data), True) self.assertEqual( Expression(Child(This(), Fields("other")), "<", 6).find(data), True) self.assertEqual( Expression(Child(This(), Fields("value")), None, 2).find(data), True) self.assertEqual( Expression(Child(This(), Fields("val")), ">=", 4).find({}), False)
def test_repr_all_leaf(self): self.assertEqual(repr(Root()), "Root()") self.assertEqual(repr(This()), "This()") self.assertEqual(repr(Index(2)), "Index(2)") self.assertEqual(repr(AllIndex()), "AllIndex()") self.assertEqual(repr(Fields("name", "other")), "Fields('name','other')") self.assertEqual(repr(Slice(0, 3, 2)), "Slice(start=0,end=3,step=2)") self.assertEqual(repr(Len()), "Len()") self.assertEqual(repr(Sum()), "Sum()") self.assertEqual(repr(Avg()), "Avg()") self.assertEqual(repr(Sorted()), "Sorted()") self.assertEqual(repr(And(Fields("name"), Fields("id"))), "And(Fields('name'),Fields('id'))") self.assertEqual(repr(Or(Fields("name"), Fields("id"))), "Or(Fields('name'),Fields('id'))")
def test_find_in_dict(self): result = [] Recursive.find_in_dict(self._data, Fields("name"), result) self.assertEqual(result, ["dishes", "spoon"]) result = [] Recursive.find_in_dict(self._data, Fields("other"), result) self.assertEqual(result, []) result = [] Recursive.find_in_dict(self._data, Fields("items"), result) self.assertEqual(result, [self._data["items"]]) result = [] Recursive.find_in_dict({}, Fields("items"), result) self.assertEqual(result, [])
def test_find_with_none_expressions(self): data = {"data": 2} data_2 = [{"data": 2}, {"data": 4}] self.assertEqual(find(Filter(None), data), [data]) self.assertEqual(find(Filter([]), data), [data]) self.assertEqual(find(Filter({}), data), [data]) expr = Expression(Child(Root(), Fields("data")), "==", 2) self.assertEqual(find(Filter(expr), data_2), [{"data": 2}])
def test_node_equals(self): assert Root() == Root() assert This() == This() assert Index(2) == Index(2) assert Len() == Len() assert Sum() == Sum() assert Avg() == Avg() assert Sorted() == Sorted() assert And(Root(), Fields("name")) == And(Root(), Fields("name")) assert Or(Root(), Fields("name")) == Or(Root(), Fields("name")) assert Child(Root(), Fields("name")) == Child(Root(), Fields("name")) assert Where(Root(), Fields("name")) == Where(Root(), Fields("name"))
def test_str_and_repr(self): self.assertEqual(str(Expression("", "", "")), "") self.assertEqual( str(Expression(Child(This(), Fields("name")), ">", 10)), "@.name>10") self.assertEqual(repr(Expression("", "", "")), "Expression(target='',op='',value='')") self.assertEqual( repr(Expression("name", ">", 10)), "Expression(target='name',op='>',value=10)", )
def test_node_not_equals(self): assert not Root() == This() assert not This() == Index(2) assert not Index(2) == Root() assert not Len() == Avg() assert not Sum() == Len() assert not Avg() == Sorted() assert not Sorted() == Sum() assert not And(Root(), Fields("name")) == And(Root(), Fields("other")) assert not Or(Root(), Fields("name")) == Or(Root(), Fields("other")) assert not Or(Root(), Fields("name")) == And(Root(), Fields("name"))
def test_str_all_leaf(self): self.assertEqual(str(Root()), "$") self.assertEqual(str(This()), "@") self.assertEqual(str(Index(1)), "[1]") self.assertEqual(str(Index("*")), "[*]") self.assertEqual(str(AllIndex()), "[*]") self.assertEqual(str(Fields("name", "other")), "name,other") self.assertEqual(str(Fields("name")), "name") self.assertEqual(str(Slice(0, 3, 2)), "[0:3:2]") self.assertEqual(str(Slice()), "[::]") self.assertEqual(str(Len()), "`len`") self.assertEqual(str(Sum()), "`sum`") self.assertEqual(str(Avg()), "`avg`") self.assertEqual(str(Sorted()), "`sorted`") self.assertEqual(str(And(Fields("name"), Fields("id"))), "name&id") self.assertEqual(str(Or(Fields("name"), Fields("id"))), "name|id")
def test_find_with_exception(self): expr = Expression(Child(Root(), Fields("data")), "==", 2) assert_with_error(self, Filter(expr), JPathNodeError)
def test_find_recursive(self): self.assertEqual(Recursive.find(self._data, Fields("price")), [12, 32]) self.assertEqual(Recursive.find(self._data, Fields("name")), ["dishes", "spoon"])
def test_find(self): data = [{"value": 2}, {"other": 3}, {"value": 1}] exp = Expression(Child(This(), Fields("value")), "<=", 2) self.assertEqual(find(Filter(exp), data), [{"value": 2}, {"value": 1}])
def p_index_fields(p): """index : fields""" p[0] = Fields(*p[1])
def test_str_all_nodes(self): self.assertEqual(str(Node(None, None)), "") self.assertEqual(str(Child(Root(), Fields("name"))), "$.name") self.assertEqual(str(Where(Root(), Fields("name"))), "$ where name") self.assertEqual(str(Descendants(Root(), Fields("name"))), "$..name") self.assertEqual(str(Union(Root(), Fields("name"))), "$|name")
def test_find_positive_cases(self): data = {"data": "value", "is_valid": True, 2: 34, True: False} cases = (("data", ["value"]), ("is_valid", [True]), (2, [34]), (True, [False])) for key, expected in cases: self.assertEqual(Fields(key).find(data), expected)
def test_find_negative_cases(self): extra = ([1, 3, 4], "the word", "") assert_with_error(self, Fields("*"), JPathNodeError, extra_cases=extra)
def test_equal(self): assert Descendants(Root(), Fields("name")) == Descendants( Root(), Fields("name"))
def test_find(self): data = {"data": [{"value": 2}, {"other": 3}]} child = Child(Child(Root(), Fields("data")), AllIndex()) where = Where(child, Fields("value")) self.assertEqual(find(where, data), [{"value": 2}])
def test_find(self): child = Child(Root(), Fields("value")) self.assertEqual(find(child, {"value": 23}), [23]) self.assertEqual(find(child, dict()), []) self.assertEqual(find(child, {"other": 23}), [])
def test_find_with_no_list(self): expr = Expression(Child(Root(), Fields("data")), "==", 2) self.assertEqual(find(Filter(expr), {}), [])
def p_json_path_fields(p): """json_path : fields""" p[0] = Fields(*p[1])