def test__post_filters_multiple():
        node1 = query.FilterNode("a", "=", 7)
        node2 = query.PostFilterNode("predicate2")
        node3 = query.PostFilterNode("predicate3")
        and_node = query.ConjunctionNode(node1, node2, node3)

        post_filters_node = and_node._post_filters()
        assert post_filters_node == query.ConjunctionNode(node2, node3)
    def test_constructor_convert_or():
        node1 = query.FilterNode("a", "=", 7)
        node2 = query.FilterNode("b", ">", 7.5)
        node3 = query.DisjunctionNode(node1, node2)
        node4 = query.FilterNode("d", ">=", 80)

        result_node = query.ConjunctionNode(node3, node4)
        assert isinstance(result_node, query.DisjunctionNode)
        assert result_node._nodes == [
            query.ConjunctionNode(node1, node4),
            query.ConjunctionNode(node2, node4),
        ]
    def test___eq__():
        filter_node1 = query.FilterNode("a", "=", 7)
        filter_node2 = query.FilterNode("b", ">", 7.5)
        filter_node3 = query.FilterNode("c", "<", "now")

        and_node1 = query.ConjunctionNode(filter_node1, filter_node2)
        and_node2 = query.ConjunctionNode(filter_node2, filter_node1)
        and_node3 = query.ConjunctionNode(filter_node1, filter_node3)
        and_node4 = unittest.mock.sentinel.and_node

        assert and_node1 == and_node1
        assert not and_node1 == and_node2
        assert not and_node1 == and_node3
        assert not and_node1 == and_node4
    def test__post_filters_empty():
        node1 = query.FilterNode("a", "=", 7)
        node2 = query.FilterNode("b", ">", 77)
        and_node = query.ConjunctionNode(node1, node2)

        post_filters_node = and_node._post_filters()
        assert post_filters_node is None
    def test__to_filter_multiple():
        node1 = query.PostFilterNode("predicate1")
        node2 = query.PostFilterNode("predicate2")
        and_node = query.ConjunctionNode(node1, node2)

        with pytest.raises(NotImplementedError):
            and_node._to_filter(post=True)
    def test__post_filters_same():
        node1 = query.PostFilterNode("predicate1")
        node2 = query.PostFilterNode("predicate2")
        and_node = query.ConjunctionNode(node1, node2)

        post_filters_node = and_node._post_filters()
        assert post_filters_node is and_node
    def test__post_filters_single():
        node1 = query.FilterNode("a", "=", 7)
        node2 = query.PostFilterNode("predicate2")
        and_node = query.ConjunctionNode(node1, node2)

        post_filters_node = and_node._post_filters()
        assert post_filters_node is node2
    def test__to_filter_empty():
        node1 = query.FilterNode("a", "=", 7)
        node2 = query.FilterNode("b", "<", 6)
        and_node = query.ConjunctionNode(node1, node2)

        as_filter = and_node._to_filter(post=True)
        assert as_filter is None
    def test_pickling():
        node1 = query.FilterNode("a", "=", 7)
        node2 = query.FilterNode("b", ">", 7.5)
        and_node = query.ConjunctionNode(node1, node2)

        pickled = pickle.dumps(and_node)
        unpickled = pickle.loads(pickled)
        assert and_node == unpickled
    def test_constructor_many_nodes():
        node1 = query.FilterNode("a", "=", 7)
        node2 = query.FilterNode("b", ">", 7.5)
        node3 = query.FilterNode("c", "<", "now")
        node4 = query.FilterNode("d", ">=", 80)

        result_node = query.ConjunctionNode(node1, node2, node3, node4)
        assert isinstance(result_node, query.ConjunctionNode)
        assert result_node._nodes == [node1, node2, node3, node4]
    def test__to_filter_single():
        node1 = unittest.mock.Mock(spec=query.FilterNode)
        node2 = query.PostFilterNode("predicate")
        node3 = unittest.mock.Mock(spec=query.FilterNode)
        node3._to_filter.return_value = False
        and_node = query.ConjunctionNode(node1, node2, node3)

        as_filter = and_node._to_filter()
        assert as_filter is node1._to_filter.return_value

        node1._to_filter.assert_called_once_with(post=False)
    def test_resolve():
        node1 = query.FilterNode("a", "=", 7)
        node2 = query.FilterNode("b", ">", 77)
        and_node = query.ConjunctionNode(node1, node2)

        bindings = {}
        used = {}
        resolved_node = and_node.resolve(bindings, used)

        assert resolved_node is and_node
        assert bindings == {}
        assert used == {}
    def test_add_node_and_with_conjunction():
        clauses = query._BooleanClauses("name", False)
        node1 = query.FilterNode("a", "=", 7)
        node2 = query.FilterNode("b", ">", 7.5)
        clauses.or_parts = [[node1], [node2]]  # Modify to see the "broadcast"

        node3 = query.FilterNode("c", "<", "now")
        node4 = query.FilterNode("d", ">=", 80)
        node5 = query.ConjunctionNode(node3, node4)
        clauses.add_node(node5)
        assert clauses.or_parts == [
            [node1, node3, node4],
            [node2, node3, node4],
        ]
    def test_resolve_changed():
        node1 = unittest.mock.Mock(spec=query.FilterNode)
        node2 = query.FilterNode("b", ">", 77)
        node3 = query.FilterNode("c", "=", 7)
        node1.resolve.return_value = node3
        and_node = query.ConjunctionNode(node1, node2)

        bindings = {}
        used = {}
        resolved_node = and_node.resolve(bindings, used)

        assert isinstance(resolved_node, query.ConjunctionNode)
        assert resolved_node._nodes == [node3, node2]
        assert bindings == {}
        assert used == {}
        node1.resolve.assert_called_once_with(bindings, used)
    def test_constructor_unreachable(boolean_clauses):
        clauses = unittest.mock.Mock(or_parts=[],
                                     spec=("add_node", "or_parts"))
        boolean_clauses.return_value = clauses

        node1 = query.FilterNode("a", "=", 7)
        node2 = query.FilterNode("b", ">", 7.5)

        with pytest.raises(RuntimeError):
            query.ConjunctionNode(node1, node2)

        boolean_clauses.assert_called_once_with("ConjunctionNode",
                                                combine_or=False)
        assert clauses.add_node.call_count == 2
        clauses.add_node.assert_has_calls(
            [unittest.mock.call(node1),
             unittest.mock.call(node2)])
Beispiel #16
0
 def query_filters(self, model_class, filters):
     """Get the filters in a format compatible with the Query constructor"""
     gql_filters = self.filters()
     for name_op in sorted(gql_filters):
         name, op = name_op
         values = gql_filters[name_op]
         op = op.lower()
         for (func, args) in values:
             prop = model_class._properties.get(name)
             val = self._args_to_val(func, args)
             if isinstance(val, query_module.ParameterizedThing):
                 node = query_module.ParameterNode(prop, op, val)
             else:
                 node = prop._comparison(op, val)
             filters.append(node)
     if filters:
         filters = query_module.ConjunctionNode(*filters)
     else:
         filters = None
     return filters
Beispiel #17
0
 def test_constructor():
     with pytest.raises(NotImplementedError):
         query.ConjunctionNode()
 def test_constructor_no_nodes():
     with pytest.raises(TypeError):
         query.ConjunctionNode()
 def test_constructor_one_node():
     node = query.FilterNode("a", "=", 7)
     result_node = query.ConjunctionNode(node)
     assert result_node is node
    def test___iter__():
        node1 = query.FilterNode("a", "=", 7)
        node2 = query.FilterNode("b", ">", 7.5)
        and_node = query.ConjunctionNode(node1, node2)

        assert list(and_node) == and_node._nodes
 def test___repr__():
     node1 = query.FilterNode("a", "=", 7)
     node2 = query.FilterNode("b", ">", 7.5)
     and_node = query.ConjunctionNode(node1, node2)
     expected = "AND(FilterNode('a', '=', 7), FilterNode('b', '>', 7.5))"
     assert repr(and_node) == expected