def test_filter__by_boolean__invalid(self): url = filter_reverse('crime', [('by-arrests', 'yes', 'no'),]) response = self.client.get(url) self.assertEqual(response.status_code, 404) url = filter_reverse('crime', [('by-arrests', 'maybe'),]) response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_filter_by_street__bad_block(self): url = filter_reverse('crime', [('streets', 'bogus',)]) response = self.client.get(url) self.assertEqual(response.status_code, 404) url = filter_reverse('crime', [('streets', 'bogus street', 'bogus block', '8-blocks')]) response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_make_url__both_args_and_query(self): url = filter_reverse('crime', [('by-date', '2011-04-05', '2011-04-06')]) url += '?textsearch=status&q=bar' chain = self._make_chain(url) expected = filter_reverse('crime', [('by-date', '2011-04-05', '2011-04-06'), ('by-status', 'bar')]) self.assertEqual(chain.make_url(), expected)
def test_filter__invalid_daterange(self): url = filter_reverse('crime', [('start_date', 'whoops'), ('end_date', 'ouchie')]) response = self.client.get(url) self.assertEqual(response.status_code, 404) url = filter_reverse('crime', [('start_date', '2006-11-30'), ('end_date', 'ouchie')]) response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_filter_by_block__no_radius(self, mock_proper_city): # We just fall back to the default radius. mock_proper_city.return_value = 'chicago' url = filter_reverse('crime', [('streets', 'wabash-ave', '216-299n-s')]) response = self.client.get(url) self.assertEqual(response.status_code, 302) fixed_url = filter_reverse('crime', [ ('streets', 'wabash-ave', '216-299n-s', '8-blocks')]) self.assertEqual(response['location'], 'http://testserver' + fixed_url)
def test_make_url__preserves_other_query_params_sorted(self): url = filter_reverse("crime", [("by-date", "2011-04-05", "2011-04-06")]) url += "?textsearch=status&q=bar" # Add some params that we don't know about. url += "&B=no&A=yes" chain = self._make_chain(url) expected = filter_reverse("crime", [("by-date", "2011-04-05", "2011-04-06"), ("by-status", "bar")]) expected += "?A=yes&B=no" self.assertEqual(chain.make_url(), expected)
def test_filter__only_one_location_allowed(self, mock_proper_city): mock_proper_city.return_value = 'chicago' url = filter_reverse('crime', [('streets', 'wabash-ave', '216-299n-s', '8-blocks'), ('locations', 'neighborhoods', 'hood-1'), ]) response = self.client.get(url) url = filter_reverse('crime', [('locations', 'neighborhoods', 'hood-1'), ('streets', 'wabash-ave', '216-299n-s', '8') ]) response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_make_url__preserves_other_query_params_sorted(self): url = filter_reverse('crime', [('by-date', '2011-04-05', '2011-04-06')]) url += '?textsearch=status&q=bar' # Add some params that we don't know about. url += '&B=no&A=yes' chain = self._make_chain(url) expected = filter_reverse('crime', [('by-date', '2011-04-05', '2011-04-06'), ('by-status', 'bar')]) expected += '?A=yes&B=no' self.assertEqual(chain.make_url(), expected)
def test_make_url__preserves_other_query_params_sorted(self): url = filter_reverse("crime", [("start_date", "2011-04-05"), ("end_date", "2011-04-06")]) url += "&textsearch=status&q=bar" # Add the extra params. url += "&zzz=yes&A=no" chain = self._make_chain(url) expected = filter_reverse( "crime", [("start_date", "2011-04-05"), ("end_date", "2011-04-06"), ("by-status", "bar")] ) # The extra params should end up sorted alphanumerically. expected = expected.replace("?", "?A=no&") + "&zzz=yes" self.assertEqual(chain.make_url(), expected)
def test_make_url__preserves_other_query_params_sorted(self): url = filter_reverse('crime', [('start_date', '2011-04-05'), ('end_date', '2011-04-06')]) url += '&textsearch=status&q=bar' # Add the extra params. url += '&zzz=yes&A=no' chain = self._make_chain(url) expected = filter_reverse('crime', [('start_date', '2011-04-05'), ('end_date', '2011-04-06'), ('by-status', 'bar')]) # The extra params should end up sorted alphanumerically. expected = expected.replace('?', '?A=no&') + '&zzz=yes' self.assertEqual(chain.make_url(), expected)
def test_filter__bad_date(self, mock_update): from ebpub.db.views import BadDateException mock_update.side_effect = BadDateException("oh no") url = filter_reverse('crime', [('by-date', '2006-11-01', '2006-11-30')]) response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_filter_by_location(self): url = filter_reverse('crime', [ ('locations', 'neighborhoods', 'hood-1'), ]) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Hood 1')
def test_filter_by_street__missing_block(self): url = filter_reverse('crime', [( 'streets', 'wabash-ave', )]) response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_filter_by_location_detail(self): url = filter_reverse('crime', [('locations', 'neighborhoods', 'hood-1')]) response = self.client.get(url) self.assertContains(response, 'Hood 1') self.assertNotContains(response, 'Hood 2') self.assertContains(response, 'Remove this filter')
def test_filter_by_pubdate_daterange(self): url = filter_reverse('crime', [('by-pub-date', '2006-11-01', '2006-11-30')]) response = self.client.get(url) self.assertContains(response, 'Clear') self.assertNotContains(response, "crime title 1") self.assertContains(response, "crime title 2") self.assertContains(response, "crime title 3")
def test_make_url__date_query(self): url = urlresolvers.reverse('ebpub-schema-filter', args=['crime']) url += '?start_date=2010/12/01&end_date=2011/01/01' chain = self._make_chain(url) expected = filter_reverse('crime', [('start_date', '2010-12-01'), ('end_date', '2011-01-01')]) self.assertEqual(chain.make_url(), expected)
def test_filter__pagination__has_more(self, mock_chain): url = filter_reverse('crime', [('by-status', 'status 9-19')]) url += '&page=2' # We can mock the FilterChain to get a very long list of NewsItems # without actually creating them in the db, but it means # also mocking a ton of methods used by schema_filter or filter.html. # (We can't just patch text_search() anymore because now there's more # filtering after that.) # TODO: this is pretty brittle. Worth it? mock_qs = mock.Mock() mock_qs.filter.return_value = mock_qs newsitem = models.NewsItem.objects.all()[0] mock_qs.order_by.return_value = [newsitem] * 999 mock_chain.return_value = mock_chain mock_chain.apply.return_value = mock_qs mock_chain.__contains__ = lambda self, other: False mock_chain.get.return_value = None mock_chain.validate.return_value = {} mock_chain.make_breadcrumbs.return_value = [] mock_chain.values = [] mock_chain.lookup_descriptions = [] mock_chain.make_url.return_value = urllib.unquote(url) # Avoid redirect. response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['has_next'], True) self.assertEqual(response.context['has_previous'], True)
def test_make_url__textsearch_query(self): url = urlresolvers.reverse('ebpub-schema-filter', args=['crime', 'filter']) url += '?textsearch=status&q=hello+goodbye' chain = self._make_chain(url) expected = filter_reverse('crime', [('by-status', 'hello goodbye')]) self.assertEqual(chain.make_url(), expected)
def test_filter_by_day(self): url = filter_reverse('crime', [('start_date', '2006-09-26'), ('end_date', '2006-09-26')]) response = self.client.get(url) self.assertContains(response, "crime title 1") self.assertNotContains(response, "crime title 2") self.assertNotContains(response, "crime title 3")
def test_filter_by_attributes__text(self): url = filter_reverse('crime', [('by-status', 'status 9-19')]) response = self.client.get(url) self.assertEqual(len(response.context['newsitem_list']), 1) self.assertContains(response, "crime title 1") self.assertNotContains(response, "crime title 2") self.assertNotContains(response, "crime title 3")
def test_filter__by_boolean_attr__false(self): url = filter_reverse('crime', [('by-arrests', 'no',),]) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertNotContains(response, 'crime title 2') self.assertContains(response, 'crime title 1') self.assertContains(response, 'crime title 3')
def test_filter_by_pubdate_daterange(self): url = filter_reverse('crime', [('start_pubdate', '2006-11-01'), ('end_pubdate', '2006-11-30')]) response = self.client.get(url) self.assertContains(response, 'Clear') self.assertNotContains(response, "crime title 1") self.assertContains(response, "crime title 2") self.assertContains(response, "crime title 3")
def test_filter__normalized_redirect(self, mock_make_url, mock_from_request): from ebpub.db import schemafilters mock_make_url.return_value = '/whee/' mock_from_request.return_value = schemafilters.FilterChain() url = filter_reverse('crime', [('by-foo', 'bar')]) response = self.client.get(url) self.assertEqual(response.status_code, 302) self.assertEqual(response['location'], 'http://testserver/whee/')
def test_filter__by_date__legacy_redirects(self): base = 'http://testserver' + filter_reverse('crime', []) expected_to_actual = ( (filter_reverse('crime', [('by-date', '2011-09-25', '2012-10-31')]), base + '?end_date=2012-10-31&start_date=2011-09-25' ), (filter_reverse('crime', [('by-pubdate', '2011-09-25', '2012-10-31')]), base + '?end_pubdate=2012-10-31&start_pubdate=2011-09-25' ), ) for expected, actual in expected_to_actual: response = self.client.get(expected) self.assertEqual(response.status_code, 302) self.assertEqual(response['location'], actual) response2 = self.client.get(actual) self.assertEqual(response2.status_code, 200)
def test_filter__by_lookup__not_specified(self): url = filter_reverse('crime', [ ('by-beat', ''), ]) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.template[0].name, 'db/filter_lookup_list.html')
def _make_filter(self, *url_args): crime = mock.Mock(spec=models.Schema) from ebpub.db.schemafilters import BlockFilter reverse_args = ('streets', ) + url_args url = filter_reverse('crime', [reverse_args]) req = RequestFactory().get(url) context = {'schema': crime} filt = BlockFilter(req, context, None, *reverse_args[1:]) return filt
def test_filter__by_m2m_lookup_attr(self): url = filter_reverse('crime', [('by-tag', 'tag-1', 'tag-2',),]) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Tag 1') self.assertContains(response, 'Tag 2') self.assertNotContains(response, 'Tag 999') self.assertContains(response, 'crime title 1') self.assertContains(response, 'crime title 2')
def _make_filter(self, *url_args): crime = mock.Mock() from ebpub.db.schemafilters import DateFilter url = filter_reverse('crime', [url_args]) req = RequestFactory().get(url) context = {'schema': crime} self.mock_qs = mock.Mock() filt = DateFilter(req, context, self.mock_qs, *url_args[1:]) return filt
def test_filter__charting_disallowed_redirect(self): # could make a different fixture, but, meh. crime = models.Schema.objects.get(slug='crime') crime.allow_charting = False crime.save() url = filter_reverse('crime', [('by-foo', 'bar')]) response = self.client.get(url) self.assertEqual(response.status_code, 301) self.assertEqual(response['location'], 'http://testserver/crime/')
def test_filter__by_bad_lookup_attr(self): url = filter_reverse('crime', [ ( 'by-fleezleglop', '214', ), ]) response = self.client.get(url) self.assertEqual(response.status_code, 404)
def _make_filter(self, *url_args): crime = mock.Mock(spec=models.Schema) from ebpub.db.schemafilters import LocationFilter reverse_args = ('locations',) + url_args url = filter_reverse('crime', [reverse_args]) req = RequestFactory().get(url) context = {'schema': crime} filt = LocationFilter(req, context, None, *reverse_args[1:]) return filt
def test_filter__by_lookup_attr(self): url = filter_reverse('crime', [ ( 'by-beat', 'beat-214', ), ]) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Police Beat 214')
def _make_filter(self, *url_args): crime = mock.Mock(spec=models.Schema) from ebpub.db.schemafilters import BlockFilter reverse_args = ("streets",) + url_args url = filter_reverse("crime", [reverse_args]) req = RequestFactory().get(url) context = {"schema": crime} filt = BlockFilter(req, context, None, *reverse_args[1:]) return filt
def _make_filter(self, *url_args): crime = models.Schema.objects.get(slug='crime') from ebpub.db.schemafilters import TextSearchFilter url = filter_reverse('crime', [url_args]) req = RequestFactory().get(url) context = {'schema': crime} sf_name = url_args[0][3:] # 'by-foo' -> 'foo' sf = models.SchemaField.objects.get(name=sf_name, schema=crime) self.mock_qs = mock.Mock() filt = TextSearchFilter(req, context, self.mock_qs, *url_args[1:], schemafield=sf) return filt
def test_filter_by_ambiguous_address(self, mock_from_request, mock_logger): # Using Mocks here causes eb_filter to call FilterChain.make_url # with additions that it doesn't understand. That's fine for this test, # but causes logging spew, hence we mock the logger too. url = filter_reverse('crime', [('by-foo', 'bar')]) + '?address=foofoo' mock_result = {'address': 'foofoo', 'block': mock.Mock()} mock_result['block'].url = '/foofoo/' mock_from_request.side_effect = BadAddressException('123 somewhere', 3, [mock_result, mock_result]) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.template[0].name, 'db/filter_bad_address.html')
def _make_filter(self, *url_args): crime = mock.Mock() from ebpub.db.schemafilters import LookupFilter url = filter_reverse('crime', [url_args]) req = RequestFactory().get(url) context = {'schema': crime} sf_name, lookups = url_args[0], url_args[1:] sf_name = sf_name[3:] # 'by-foo' -> 'foo' sf = models.SchemaField.objects.get(name=sf_name) self.mock_qs = mock.Mock() filt = LookupFilter(req, context, self.mock_qs, *lookups, schemafield=sf) return filt
def test_urls__intersection(self): url = urlresolvers.reverse("ebpub-schema-filter", args=["crime", "filter"]) + "?address=foo+and+bar" from ebpub.streets.models import Block, Intersection, BlockIntersection intersection = mock.Mock(spec=Intersection) blockintersection = mock.Mock(spec=BlockIntersection) blockintersection.block = Block.objects.get(street_slug="wabash-ave", from_num=216) intersection.blockintersection_set.all.return_value = [blockintersection] self.mock_geocode.return_value = {"intersection": intersection, "block": None} chain = self._make_chain(url) expected = filter_reverse("crime", [("streets", "wabash-ave", "216-299n-s", "8-blocks")]) result = chain.make_url() self.assertEqual(result, expected)
def test_urls__intersection(self): url = urlresolvers.reverse( 'ebpub-schema-filter', args=['crime']) + '?address=foo+and+bar' from ebpub.streets.models import Block, Intersection, BlockIntersection intersection = mock.Mock(spec=Intersection) blockintersection = mock.Mock(spec=BlockIntersection) blockintersection.block = Block.objects.get(street_slug='wabash-ave', from_num=216) intersection.blockintersection_set.all.return_value = [blockintersection] self.mock_geocode.return_value = {'intersection': intersection, 'block': None} chain = self._make_chain(url) expected = filter_reverse('crime', [('streets', 'wabash-ave', '216-299n-s', '8-blocks'),]) result = chain.make_url() self.assertEqual(result, expected)
def test_make_url__address_query(self): from ebpub.streets.models import Block block = Block.objects.get(street_slug='wabash-ave', from_num=216) self.mock_geocode.return_value = { 'city': block.left_city.title(), 'zip': block.left_zip, 'point': block.geom.centroid, 'state': block.left_state, 'intersection_id': None, 'address': u'216 N Wabash Ave', 'intersection': None, 'block': block, } url = urlresolvers.reverse('ebpub-schema-filter', args=['crime']) url += '?address=216+n+wabash+st&radius=8' # geocode result is mocked anyway. expected_url = filter_reverse('crime', [('streets', 'wabash-ave', '216-299n-s', '8-blocks'),]) chain = self._make_chain(url) self.assertEqual(expected_url, chain.make_url())
def test_make_url__address_query(self): from ebpub.streets.models import Block block = Block.objects.get(street_slug="wabash-ave", from_num=216) self.mock_geocode.return_value = { "city": block.left_city.title(), "zip": block.left_zip, "point": block.geom.centroid, "state": block.left_state, "intersection_id": None, "address": u"216 N Wabash Ave", "intersection": None, "block": block, } url = urlresolvers.reverse("ebpub-schema-filter", args=["crime", "filter"]) url += "?address=216+n+wabash+st&radius=8" # geocode result is mocked anyway. expected_url = filter_reverse("crime", [("streets", "wabash-ave", "216-299n-s", "8-blocks")]) chain = self._make_chain(url) self.assertEqual(expected_url, chain.make_url())
def test_filter__pagination__empty_page(self): url = filter_reverse('crime', [('by-status', 'status 9-19')]) url += '&page=99' response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_filter__invalid_argname(self): # These are ignored. url = filter_reverse('crime', [('bogus-key', 'bogus-value')]) response = self.client.get(url) self.assertEqual(response.status_code, 200)