Ejemplo n.º 1
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'))")
Ejemplo n.º 2
0
class TestRoot(TestCase):
    def setUp(self):
        self._root = Root()
        self._data = {"data": [{"name": "jorge"}, {"name": "james"}]}

    def test_find(self):
        self.assertEqual(list(self._root.find({})), [{}])
        self.assertEqual(list(self._root.find(None)), [None])
        self.assertEqual(list(self._root.find(self._data)), [self._data])
        self.assertEqual(list(self._root.find(34)), [34])
        self.assertEqual(list(self._root.find("")), [""])
Ejemplo n.º 3
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"))
Ejemplo n.º 4
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}])
Ejemplo n.º 5
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"))
Ejemplo n.º 6
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'))")
Ejemplo n.º 7
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")
Ejemplo n.º 8
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")
Ejemplo n.º 9
0
 def test_find_with_no_list(self):
     expr = Expression(Child(Root(), Fields("data")), "==", 2)
     self.assertEqual(find(Filter(expr), {}), [])
Ejemplo n.º 10
0
 def test_find_with_exception(self):
     expr = Expression(Child(Root(), Fields("data")), "==", 2)
     assert_with_error(self, Filter(expr), JPathNodeError)
Ejemplo n.º 11
0
 def test_equal(self):
     assert Descendants(Root(), Fields("name")) == Descendants(
         Root(), Fields("name"))
Ejemplo n.º 12
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}])
Ejemplo n.º 13
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}), [])
Ejemplo n.º 14
0
 def setUp(self):
     self._root = Root()
     self._data = {"data": [{"name": "jorge"}, {"name": "james"}]}