def test_load_data_from_csv_without_postcodes(self): collection_type = BinCollectionType.objects.get(friendly_id='D') # sample_domestic_no_postcodes.csv is the first few lines from a Barnet spreadsheet, exported to CSV domestic_sample_file = os.path.join(os.path.dirname(binalerts.__file__), 'fixtures/sample_domestic_no_postcodes.csv') DataImport.load_from_csv_file(open(domestic_sample_file, 'r'), collection_type=collection_type, guess_postcodes=True) response = self.client.post('/', { 'query': 'Amber Grove' }) self.assertContains(response, "No street found with that name. Try typing a smaller part of it") response = self.client.get('/street/juniper_close') self.assertContains(response, "No street found with that name. Try typing a smaller part of it")
def test_load_data_from_native_csv(self): collection_type = BinCollectionType.objects.get(friendly_id='D') # sample_ideal.csv is in the nativce CSV format, rather than a proprietary one sample_file = os.path.join(os.path.dirname(binalerts.__file__), 'fixtures/sample_native.csv') DataImport.load_from_csv_file(open(sample_file, 'r'), collection_type=collection_type, guess_postcodes=False) response = self.client.get('/street/test_road') self.assertRedirects(response, '/street/test_road_ab1') response = self.client.get('/street/test_road_ab1') self.assertContains(response, 'Garden') self.assertContains(response, 'Domestic') self.assertContains(response, 'Friday') self.assertNotContains(response, 'Recycl')
def test_load_data_from_csv_then_pdf(self): Street.objects.all().delete() # not using fixtures here garden_sample_file = os.path.join(os.path.dirname(binalerts.__file__), 'fixtures/sample_garden_from_pdf.xml') DataImport.load_from_pdf_xml(garden_sample_file, collection_type=BinCollectionType.objects.get(friendly_id='G')) domestic_sample_file = os.path.join(os.path.dirname(binalerts.__file__), 'fixtures/sample_domestic_no_postcodes.csv') DataImport.load_from_csv_file(open(domestic_sample_file, 'r'), collection_type=BinCollectionType.objects.get(friendly_id='D')) # Juniper Close in csv doesn't have postcode... should snap to the (only) Juniper Close (with postcode) and # not create a record -- instead, redirecting to it response = self.client.get('/street/juniper_close') self.assertRedirects(response, '/street/juniper_close_en5') response = self.client.get('/street/juniper_close_en5') self.assertContains(response, 'Garden') # from xml import self.assertNotContains(response, 'Domestic') # from the csv import, which had no postcode
def test_load_data_from_pdf_xml_with_multiple_days(self): old_BINS_ALLOW_MULTIPLE_COLLECTIONS_PER_WEEK = settings.BINS_ALLOW_MULTIPLE_COLLECTIONS_PER_WEEK settings.BINS_ALLOW_MULTIPLE_COLLECTIONS_PER_WEEK = True try: collection_type = BinCollectionType.objects.get(friendly_id='G') garden_sample_file = os.path.join(os.path.dirname(binalerts.__file__), 'fixtures/sample_garden_from_pdf.xml') DataImport.load_from_pdf_xml(garden_sample_file, collection_type=collection_type) # multiple days of week e.g Tuesday/Thursday are handled OK response = self.client.get('/street/athenaeum_road_n20') #import pdb; pdb.set_trace() self.assertContains(response, "Kitchen Waste</strong> collection days are <strong>Tuesday & Thursday") finally: settings.BINS_ALLOW_MULTIPLE_COLLECTIONS_PER_WEEK = old_BINS_ALLOW_MULTIPLE_COLLECTIONS_PER_WEEK
def test_load_data_from_pdf_xml(self): # garden_sample_pdf.xml was converted with "pdftohtml -xml" from this file: # http://www.barnet.gov.uk/garden-and-kitchen-waste-collection-streets.pdf old_BINS_ALLOW_MULTIPLE_COLLECTIONS_PER_WEEK = settings.BINS_ALLOW_MULTIPLE_COLLECTIONS_PER_WEEK settings.BINS_ALLOW_MULTIPLE_COLLECTIONS_PER_WEEK = False try: LoadDataTest.old_stdout = sys.stdout sys.stdout = StringIO() collection_type = BinCollectionType.objects.get(friendly_id='G') garden_sample_file = os.path.join(os.path.dirname(binalerts.__file__), 'fixtures/sample_garden_from_pdf.xml') DataImport.load_from_pdf_xml(garden_sample_file, collection_type=collection_type) # first item in sample file response = self.client.post('/', { 'query': 'Ibsley Way' }) self.assertRedirects(response, '/street/ibsley_way_en4') # one in the middle response = self.client.post('/', { 'query': 'Jade Close' }) self.assertRedirects(response, '/street/jade_close_nw2') # last item in sample file response = self.client.post('/', { 'query': 'Juniper Close' }) self.assertRedirects(response, '/street/juniper_close_en5') # check that postcodes are indeed being loaded (and not simply being put into the url_name) a_street = Street.objects.get(name="Juniper Close") self.assertEquals(a_street.partial_postcode, 'EN5') # multiple partial postcodes e.g. EN5/N20 are ignored for now response = self.client.post('/', { 'query': 'Barnet Lane' }) self.assertContains(response, "No street found with that name.") # multiple days of week e.g Tuesday/Thursday are ignored for now # would be better to test the output (stdout?) from the import task, but this is its consequence response = self.client.post('/', { 'query': 'Athenaeum Road' }) self.assertContains(response, "No street found with that name.") # they are green/garden waste (the default for xml import) response = self.client.get('/street/juniper_close_en5') self.assertContains(response, 'Green Garden') finally: settings.BINS_ALLOW_MULTIPLE_COLLECTIONS_PER_WEEK = old_BINS_ALLOW_MULTIPLE_COLLECTIONS_PER_WEEK
def test_load_data_from_pdf_then_csv(self): Street.objects.all().delete() # not using fixtures here collection_type = BinCollectionType.objects.get(friendly_id='G') garden_sample_file = os.path.join(os.path.dirname(binalerts.__file__), 'fixtures/sample_garden_from_pdf.xml') DataImport.load_from_pdf_xml(garden_sample_file, collection_type=collection_type) collection_type = BinCollectionType.objects.get(friendly_id='D') domestic_sample_file = os.path.join(os.path.dirname(binalerts.__file__), 'fixtures/sample_domestic_no_postcodes.csv') DataImport.load_from_csv_file(open(domestic_sample_file, 'r'), collection_type=collection_type, guess_postcodes=True) # Should snap to the (only) Juniper Close (with postcode) response = self.client.get('/street/juniper_close') self.assertRedirects(response, '/street/juniper_close_en5') response = self.client.get('/street/juniper_close_en5') self.assertContains(response, 'Domestic') # from csv import, because postcode was guessed OK self.assertContains(response, 'Garden') # from xml import response = self.client.post('/', { 'query': 'Amber Grove' }) self.assertContains(response, "No street found with that name. Try typing a smaller part of it") # not imported from csv, had no postcode