def __make_specification(self): ref_attr, backref_attr = self._get_specification_attributes() #: Builds the filter specification. spec_fac = get_filter_specification_factory() crd = self.descriptor.cardinality if backref_attr is None: relatee = get_nested_attribute(self.relator, ref_attr) if crd.relatee == CARDINALITY_CONSTANTS.MANY: # This is potentially expensive as we may need to iterate over # a large collection to create the "contained" specification. if len(relatee) > 0: ids = [related.id for related in relatee] spec = spec_fac.create_contained('id', ids) else: # Create impossible search criterion. spec = spec_fac.create_equal_to('id', -1) else: if not relatee is None: spec = spec_fac.create_equal_to('id', relatee.id) else: # Create impossible search criterion. spec = spec_fac.create_equal_to('id', -1) else: if crd.relator == CARDINALITY_CONSTANTS.MANY: spec = spec_fac.create_contains(backref_attr, self.relator) else: spec = spec_fac.create_equal_to(backref_attr, self.relator) return spec
def test_resource_to_url_with_filter(self): flt_spec_fac = get_filter_specification_factory() flt_spec = flt_spec_fac.create_equal_to('id', 0) self.coll.filter = flt_spec self.__check_url(resource_to_url(self.coll), schema='http', path='/my-entities/', params='', query='q=id:equal-to:0')
def test_filter_nested(self): coll = create_collection() children = coll['1'].children spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('id', 0) children.filter = spec self.assert_true(isinstance(children.filter, ConjunctionFilterSpecification)) self.assert_equal(len(children), 0)
def test_filter_nested(self): coll = create_collection() children = iter(coll).next().children spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('id', 1) children.filter = spec self.assert_true(isinstance(children.filter, ValueEqualToFilterSpecification)) self.assert_equal(len(children), 0)
def test_filter_nested(self): coll = create_collection() children = coll['1'].children spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('id', 0) children.filter = spec self.assert_true( isinstance(children.filter, ConjunctionFilterSpecification)) self.assert_equal(len(children), 0)
def _get_filter(self): # Overwrite to prepend relationship specification to filter spec. rel_spec = self._relationship.specification if not self._filter_spec is None: spec_fac = get_filter_specification_factory() filter_spec = spec_fac.create_conjunction(rel_spec, self._filter_spec) else: filter_spec = rel_spec return filter_spec
def _test_with_filter(self, agg_children): spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('id', 0) agg_children.filter = spec self.assert_true(agg_children.filter is spec) self.assert_equal(len(list(agg_children.iterator())), 1) spec1 = spec_fac.create_equal_to('id', 1) agg_children.filter = spec1 self.assert_equal(len(list(agg_children.iterator())), 0) self.assert_is_none(agg_children.get_by_id(0)) self.assert_is_none(agg_children.get_by_slug('0'))
def test_atom_collection(self, collection, representer): rpr_str1 = representer.to_string(collection) assert rpr_str1.find('<feed xmlns:') != -1 collection.slice = slice(0, 1) filter_spec_fac = get_filter_specification_factory() filter_spec = filter_spec_fac.create_equal_to('id', 0) collection.filter = filter_spec order_spec_fac = get_order_specification_factory() order_spec = order_spec_fac.create_ascending('id') collection.order = order_spec rpr_str2 = representer.to_string(collection) assert rpr_str2.find('<feed xmlns:') != -1
def _get_filter(self): if self._relationship is None: filter_spec = self._filter_spec else: rel_spec = self._relationship.specification if self._filter_spec is None: filter_spec = rel_spec else: spec_fac = get_filter_specification_factory() filter_spec = spec_fac.create_conjunction( rel_spec, self._filter_spec) return filter_spec
def _get_filter(self): if self._relationship is None: filter_spec = self._filter_spec else: rel_spec = self._relationship.specification if self._filter_spec is None: filter_spec = rel_spec else: spec_fac = get_filter_specification_factory() filter_spec = spec_fac.create_conjunction(rel_spec, self._filter_spec) return filter_spec
def test_filter_backref_only_raises_error(self): # We can not use resource attributes that do not have a corresponding # entity attribute (such as backref only collections) for filtering. coll = create_collection() children = iter(coll).next().children grandchildren = iter(children).next().children grandchild = iter(grandchildren).next() spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('backref_only_children', grandchild) with self.assert_raises(ValueError) as cm: children.filter = spec exc_msg = 'does not have a corresponding entity attribute.' self.assert_true(cm.exception.message.endswith(exc_msg))
def _get_filter(self): if self.__relationship is None: filter_spec = self._filter_spec else: # Prepend the relationship specification to the current filter # specification. if self._filter_spec is None: filter_spec = self.__relationship.specification else: spec_fac = get_filter_specification_factory() filter_spec = spec_fac.create_conjunction( self.__relationship.specification, self._filter_spec) return filter_spec
def test_filter_not_nested(self): # The grand children are not nested, so the filter spec has to be # a ConjunctionFilterSpecification. coll = create_collection() grand_children = coll['0'].children['0'].children spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('id', 1) grand_children.filter = spec self.assert_equal(len(grand_children), 0) self.assert_true( isinstance(grand_children.filter, ConjunctionFilterSpecification)) grand_children.filter = None self.assert_equal(len(grand_children), 1) self.assert_true( isinstance(grand_children.filter, ValueEqualToFilterSpecification))
def test_filter_not_nested(self): # The grand children are not nested, so the filter spec has to be # a ConjunctionFilterSpecification. coll = create_collection() grand_children = iter(iter(coll).next().children).next().children spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('id', 1) grand_children.filter = spec self.assert_equal(len(grand_children), 0) self.assert_true(isinstance(grand_children.filter, ConjunctionFilterSpecification)) grand_children.filter = None self.assert_equal(len(grand_children), 1) self.assert_true(isinstance(grand_children.filter, ValueEqualToFilterSpecification))
def test_atom_collection(self): def _test(rc): rpr = as_representer(rc, AtomMime) rpr_str = rpr.to_string(rc) self.assert_not_equal(rpr_str.find('<feed xmlns:'), -1) coll = create_collection() _test(coll) coll.slice = slice(0, 1) filter_spec_fac = get_filter_specification_factory() filter_spec = filter_spec_fac.create_equal_to('id', 0) coll.filter = filter_spec order_spec_fac = get_order_specification_factory() order_spec = order_spec_fac.create_ascending('id') coll.order = order_spec _test(coll)
def test_filter_backref_only_raises_error(self): # We can not use resource attributes that do not have a corresponding # entity attribute (such as backref only collections) for filtering. coll = create_collection() children = next(iter(coll)).children grandchildren = next(iter(children)).children grandchild = next(iter(grandchildren)) spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('children', grandchild) with patch('%s.resources.MyEntityChildMember.children.entity_attr' % self.package_name, None): with self.assert_raises(ValueError) as cm: children.filter = spec exc_msg = 'does not have a corresponding entity attribute.' self.assert_true(str(cm.exception).endswith(exc_msg))
def test_filter_backref_only_raises_error(self): # We can not use resource attributes that do not have a corresponding # entity attribute (such as backref only collections) for filtering. coll = create_collection() children = next(iter(coll)).children grandchildren = next(iter(children)).children grandchild = next(iter(grandchildren)) spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('children', grandchild) with patch( '%s.resources.MyEntityChildMember.children.entity_attr' % self.package_name, None): with self.assert_raises(ValueError) as cm: children.filter = spec exc_msg = 'does not have a corresponding entity attribute.' self.assert_true(str(cm.exception).endswith(exc_msg))
def test_atom_collection(self): def _test(rc): rpr = as_representer(rc, AtomMime) rpr_str = rpr.to_string(rc) self.assert_not_equal( rpr_str.find('<feed xmlns:ent="http://xml.test.org/tests"'), -1) coll = create_collection() _test(coll) coll.slice = slice(0, 1) filter_spec_fac = get_filter_specification_factory() filter_spec = filter_spec_fac.create_equal_to('id', 0) coll.filter = filter_spec order_spec_fac = get_order_specification_factory() order_spec = order_spec_fac.create_ascending('id') coll.order = order_spec _test(coll)
def specification(self): spec_fac = get_filter_specification_factory() if not self.backref is None: # Simple case: We have an attribute in the child that references # the parent and we can identify all elements of the child # collection with an "equal_to" specification. rel_spec = spec_fac.create_equal_to(self.backref, self.parent) else: # Complex case: We use the IDs of the elements of the child # collection to form a "contained" specification. This is slow # because we need to iterate over the whole collection. elems = self.children if len(elems) > 0: ids = [elem.id for elem in elems] rel_spec = spec_fac.create_contained('id', ids) else: # Create impossible search criterion for empty collection. rel_spec = spec_fac.create_equal_to('id', None) return rel_spec
def test_instantiating_generator(self): gen = FilterSpecificationGenerator(get_filter_specification_factory()) spec = gen.lt(number_attr=1) & gen.gt(number_attr=-1) self.assert_true(spec.is_satisfied_by(self.candidate))
def test_instantiating_generator(self): gen = FilterSpecificationGenerator(get_filter_specification_factory()) spec = gen.lt(number_attr=1) & gen.gt(number_attr= -1) self.assert_true(spec.is_satisfied_by(self.candidate))