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_constructor_ne(disjunction_node): or_node = query.FilterNode("a", "!=", 2.5) assert or_node is disjunction_node.return_value filter_node1 = query.FilterNode("a", "<", 2.5) filter_node2 = query.FilterNode("a", ">", 2.5) disjunction_node.assert_called_once_with(filter_node1, filter_node2)
def test_add_node_or_with_disjunction(): clauses = query._BooleanClauses("name", True) node1 = query.FilterNode("a", "=", 7) node2 = query.FilterNode("b", ">", 7.5) node3 = query.DisjunctionNode(node1, node2) clauses.add_node(node3) assert clauses.or_parts == [node1, 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_constructor_in(): or_node = query.FilterNode("a", "in", ("x", "y", "z")) filter_node1 = query.FilterNode("a", "=", "x") filter_node2 = query.FilterNode("a", "=", "y") filter_node3 = query.FilterNode("a", "=", "z") assert or_node == query.DisjunctionNode(filter_node1, filter_node2, filter_node3)
def test_pickling(): node1 = query.FilterNode("a", "=", 7) node2 = query.FilterNode("b", ">", 7.5) or_node = query.DisjunctionNode(node1, node2) pickled = pickle.dumps(or_node) unpickled = pickle.loads(pickled) assert or_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.DisjunctionNode(node1, node2, node3, node4) assert isinstance(result_node, query.DisjunctionNode) assert result_node._nodes == [node1, node2, node3, node4]
def test_constructor_in(disjunction_node): or_node = query.FilterNode("a", "in", ("x", "y", "z")) assert or_node is disjunction_node.return_value filter_node1 = query.FilterNode("a", "=", "x") filter_node2 = query.FilterNode("a", "=", "y") filter_node3 = query.FilterNode("a", "=", "z") disjunction_node.assert_called_once_with( filter_node1, filter_node2, filter_node3 )
def test___eq__(): filter_node1 = query.FilterNode("speed", ">=", 88) filter_node2 = query.FilterNode("slow", ">=", 88) filter_node3 = query.FilterNode("speed", "<=", 88) filter_node4 = query.FilterNode("speed", ">=", 188) filter_node5 = unittest.mock.sentinel.filter_node assert filter_node1 == filter_node1 assert not filter_node1 == filter_node2 assert not filter_node1 == filter_node3 assert not filter_node1 == filter_node4 assert not filter_node1 == filter_node5
def test__IN(): prop = model.Property("name", indexed=True) or_node = prop._IN(["a", None, "xy"]) expected = query.DisjunctionNode( query.FilterNode(b"name", "=", "a"), query.FilterNode(b"name", "=", None), query.FilterNode(b"name", "=", "xy"), ) assert or_node == expected # Also verify the alias assert or_node == prop.IN(["a", None, "xy"])
def test_get_query_in(): class SomeKind(model.Model): prop1 = model.IntegerProperty() gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 IN (1, 2, 3)") query = gql.get_query() assert query.filters == query_module.OR( query_module.FilterNode("prop1", "=", 1), query_module.FilterNode("prop1", "=", 2), query_module.FilterNode("prop1", "=", 3), )
def test_resolve(): node1 = query.FilterNode("a", "=", 7) node2 = query.FilterNode("b", ">", 77) or_node = query.DisjunctionNode(node1, node2) bindings = {} used = {} resolved_node = or_node.resolve(bindings, used) assert resolved_node is or_node assert bindings == {} assert used == {}
def test___ne__(): prop = model.Property("name", indexed=True) value = 7.0 expected = query.DisjunctionNode( query.FilterNode(b"name", "<", value), query.FilterNode(b"name", ">", value), ) or_node_left = prop != value assert or_node_left == expected or_node_right = value != prop assert or_node_right == expected
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") or_node1 = query.DisjunctionNode(filter_node1, filter_node2) or_node2 = query.DisjunctionNode(filter_node2, filter_node1) or_node3 = query.DisjunctionNode(filter_node1, filter_node3) or_node4 = unittest.mock.sentinel.or_node assert or_node1 == or_node1 assert not or_node1 == or_node2 assert not or_node1 == or_node3 assert not or_node1 == or_node4
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___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_add_node_and_with_simple(): clauses = query._BooleanClauses("name", False) node1 = query.FilterNode("a", "=", 7) node2 = query.FilterNode("b", ">", 7.5) node3 = query.FilterNode("c", "<", "now") # Modify to see the "broadcast" clauses.or_parts = [[node1], [node2], [node3]] node4 = query.FilterNode("d", ">=", 80) clauses.add_node(node4) assert clauses.or_parts == [ [node1, node4], [node2, node4], [node3, node4], ]
def test_resolve_with_in(): prop = model.Property(name="val") param = query.Parameter("replace") parameter_node = query.ParameterNode(prop, "in", param) value = (19, 20, 28) bindings = {"replace": value} used = {} resolved_node = parameter_node.resolve(bindings, used) assert resolved_node == query.DisjunctionNode( query.FilterNode("val", "=", 19), query.FilterNode("val", "=", 20), query.FilterNode("val", "=", 28), ) assert used == {"replace": True}
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 _comparison(self, op, value): """Internal helper for comparison operators. Args: op (str): The comparison operator. One of ``=``, ``!=``, ``<``, ``<=``, ``>``, ``>=`` or ``in``. Returns: FilterNode: A FilterNode instance representing the requested comparison. Raises: BadFilterError: If the current property is not indexed. """ # Import late to avoid circular imports. from google.cloud.ndb import query if not self._indexed: raise exceptions.BadFilterError( "Cannot query for unindexed property {}".format(self._name) ) if value is not None: value = self._do_validate(value) value = self._call_to_base_type(value) value = self._datastore_type(value) return query.FilterNode(self._name, op, value)
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 or_node = query.DisjunctionNode(node1, node2) bindings = {} used = {} resolved_node = or_node.resolve(bindings, used) assert isinstance(resolved_node, query.DisjunctionNode) assert resolved_node._nodes == [node3, node2] assert bindings == {} assert used == {} node1.resolve.assert_called_once_with(bindings, used)
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_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)])
def _IN(self, value): """For the ``in`` comparison operator. The ``in`` operator cannot be overloaded in the way we want to, so we define a method. For example: .. code-block:: python Employee.query(Employee.rank.IN([4, 5, 6])) Note that the method is called ``_IN()`` but may normally be invoked as ``IN()``; ``_IN()`` is provided for the case that a :class:`.StructuredProperty` refers to a model that has a property named ``IN``. Args: value (Iterable[Any]): The set of values that the property value must be contained in. Returns: Union[~google.cloud.ndb.query.DisjunctionNode, \ ~google.cloud.ndb.query.FilterNode, \ ~google.cloud.ndb.query.FalseNode]: A node corresponding to the desired in filter. * If ``value`` is empty, this will return a :class:`.FalseNode` * If ``len(value) == 1``, this will return a :class:`.FilterNode` * Otherwise, this will return a :class:`.DisjunctionNode` Raises: ~google.cloud.ndb.exceptions.BadFilterError: If the current property is not indexed. ~google.cloud.ndb.exceptions.BadArgumentError: If ``value`` is not a basic container (:class:`list`, :class:`tuple`, :class:`set` or :class:`frozenset`). """ # Import late to avoid circular imports. from google.cloud.ndb import query if not self._indexed: raise exceptions.BadFilterError( "Cannot query for unindexed property {}".format(self._name) ) if not isinstance(value, (list, tuple, set, frozenset)): raise exceptions.BadArgumentError( "Expected list, tuple or set, got {!r}".format(value) ) values = [] for sub_value in value: if sub_value is not None: sub_value = self._do_validate(sub_value) sub_value = self._call_to_base_type(sub_value) sub_value = self._datastore_type(sub_value) values.append(sub_value) return query.FilterNode(self._name, "in", values)
def test___ge__(): prop = model.Property("name", indexed=True) value = "old" expected = query.FilterNode(b"name", ">=", value) filter_node_left = prop >= value assert filter_node_left == expected filter_node_right = value <= prop assert filter_node_right == expected
def test_get_query_geopt(): class SomeKind(model.Model): prop1 = model.GeoPtProperty() gql = gql_module.GQL( "SELECT prop1 FROM SomeKind WHERE prop1 = GeoPt(20.67, -100.32)") query = gql.get_query() assert query.filters == query_module.FilterNode( "prop1", "=", model.GeoPt(20.67, -100.32))
def test_get_query_time_one_parameter_int(): class SomeKind(model.Model): prop1 = model.TimeProperty() gql = gql_module.GQL( "SELECT prop1 FROM SomeKind WHERE prop1 = Time(12)") query = gql.get_query() assert query.filters == query_module.FilterNode( "prop1", "=", datetime.datetime(1970, 1, 1, 12))
def test_get_query_datetime_ome_parameter(): class SomeKind(model.Model): prop1 = model.DateTimeProperty() gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 = " "DateTime('2020-03-26 12:45:05')") query = gql.get_query() assert query.filters == query_module.FilterNode( "prop1", "=", datetime.datetime(2020, 3, 26, 12, 45, 5))
def test_get_query_date(): class SomeKind(model.Model): prop1 = model.DateProperty() gql = gql_module.GQL( "SELECT prop1 FROM SomeKind WHERE prop1 = Date(2020, 3, 26)") query = gql.get_query() assert query.filters == query_module.FilterNode( "prop1", "=", datetime.datetime(2020, 3, 26, 0, 0, 0))