def test_sqs_apply_custom_facets(self, mock_active): """ Tests that correct query strings are added to search query sets for different combinations of custom facets and exclude facets This is a regression test. Exclude facets weren't being applied when no custom facets were being passed in Since this is the first place we're using mock, here's a brief explanation. patch.object is a decorator that passes in an object (in this case CustomFacet.active_site_facet) to our function's local environment (where we've named it mock_active). We then override that objects behavior (it's return value in this case) to avoid having to set up a chain of sites and configurations just to get settings.SITE_ID set so that active_site_facet works correctly. """ terms = [unicode(i) for i in range(10)] facets = CustomFacet.objects.all() self.assertEqual(len(facets), 0) facet_ids = [ factories.CustomFacetFactory(title=term).id for term in terms ] facets = CustomFacet.objects.filter(id__in=facet_ids).order_by('title') [factories.SeoSiteFacetFactory(customfacet=facet) for facet in facets] site_facet = factories.SeoSiteFacetFactory() mock_active.return_value = site_facet for split in range(len(terms)): cf = facets.filter(title__in=terms[0:split]).order_by('title') ef = facets.filter(title__in=terms[split:]).order_by('title') with self.assertNumQueries(FuzzyInt(1, 10)): sqs = helpers.sqs_apply_custom_facets(custom_facets=cf, exclude_facets=ef) # Narrow_queries is a set of querystrings. Our current # backend should build 1 string starting with NOT for exclude facets # and another string for custom facets queries = sqs.query.narrow_queries self.assertTrue(len(queries) > 0) for query in queries: if query.find(u'NOT') == -1: present_terms = terms[0:split] missing_terms = terms[split:] else: present_terms = terms[split:] missing_terms = terms[0:split] for term in present_terms: self.assertNotEqual(query.find(term), -1) for term in missing_terms: self.assertEqual(query.find(term), -1)
def test_sqs_apply_custom_facets(self, mock_active): """ Tests that correct query strings are added to search query sets for different combinations of custom facets and exclude facets This is a regression test. Exclude facets weren't being applied when no custom facets were being passed in Since this is the first place we're using mock, here's a brief explanation. patch.object is a decorator that passes in an object (in this case CustomFacet.active_site_facet) to our function's local environment (where we've named it mock_active). We then override that objects behavior (it's return value in this case) to avoid having to set up a chain of sites and configurations just to get settings.SITE_ID set so that active_site_facet works correctly. """ terms = [unicode(i) for i in range(10)] facets = CustomFacet.objects.all() self.assertEqual(len(facets), 0) facet_ids = [factories.CustomFacetFactory(title=term).id for term in terms] facets = CustomFacet.objects.filter(id__in=facet_ids).order_by('title') [factories.SeoSiteFacetFactory(customfacet=facet) for facet in facets] site_facet = factories.SeoSiteFacetFactory() mock_active.return_value = site_facet for split in range(len(terms)): cf = facets.filter(title__in=terms[0:split]).order_by('title') ef = facets.filter(title__in=terms[split:]).order_by('title') with self.assertNumQueries(FuzzyInt(1, 10)): sqs = helpers.sqs_apply_custom_facets(custom_facets=cf, exclude_facets=ef) # Narrow_queries is a set of querystrings. Our current # backend should build 1 string starting with NOT for exclude facets # and another string for custom facets queries = sqs.query.narrow_queries self.assertTrue(len(queries) > 0) for query in queries: if query.find(u'NOT') == -1: present_terms = terms[0:split] missing_terms = terms[split:] else: present_terms = terms[split:] missing_terms = terms[0:split] for term in present_terms: self.assertNotEqual(query.find(term), -1) for term in missing_terms: self.assertEqual(query.find(term), -1)
def _sqs(self): sqs = super(DESolrSitemap, self)._sqs()._clone() if self.buids: sqs = sqs.narrow("buid:(%s)" % self.buid_str) sqs = sqs_apply_custom_facets(settings.DEFAULT_FACET, sqs) if self.fields: sqs = sqs.fields(self.fields) return sqs
def _sqs(self): sqs = super(DESolrSitemap, self)._sqs()._clone() if self.buids: sqs = sqs.narrow("buid:(%s)" % self.buid_str) sqs = sqs_apply_custom_facets(settings.DEFAULT_FACET, sqs) if self.fields: sqs = sqs.fields(self.fields) return sqs