示例#1
0
 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))
示例#2
0
 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'))")
示例#3
0
 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)
示例#4
0
 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'))")
示例#5
0
    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, [])
示例#6
0
 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}])
示例#7
0
    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"))
示例#8
0
    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)",
        )
示例#9
0
 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"))
示例#10
0
 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")
示例#11
0
 def test_find_with_exception(self):
     expr = Expression(Child(Root(), Fields("data")), "==", 2)
     assert_with_error(self, Filter(expr), JPathNodeError)
示例#12
0
 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"])
示例#13
0
 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}])
示例#14
0
 def p_index_fields(p):
     """index : fields"""
     p[0] = Fields(*p[1])
示例#15
0
 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")
示例#16
0
 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)
示例#17
0
 def test_find_negative_cases(self):
     extra = ([1, 3, 4], "the word", "")
     assert_with_error(self, Fields("*"), JPathNodeError, extra_cases=extra)
示例#18
0
 def test_equal(self):
     assert Descendants(Root(), Fields("name")) == Descendants(
         Root(), Fields("name"))
示例#19
0
 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}])
示例#20
0
 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}), [])
示例#21
0
 def test_find_with_no_list(self):
     expr = Expression(Child(Root(), Fields("data")), "==", 2)
     self.assertEqual(find(Filter(expr), {}), [])
示例#22
0
 def p_json_path_fields(p):
     """json_path : fields"""
     p[0] = Fields(*p[1])