def test_unexpected_number_of_columns_in_csv_file(self): prop_parser = PropertyDataParser('./test_data/test_data_5.csv') try: with self.assertRaises(ValueError): prop_parser._read_csv_file() finally: prop_parser.file_handle.close()
def main(): parser = argparse.ArgumentParser( description='Parses property data from a CSV file') parser.add_argument('-f', dest='file', type=str, required=True, help="specify path to csv file") args = parser.parse_args() parser = PropertyDataParser(args.file) mean_property_price_w1f = parser.mean_price_in_postcode_w1f() print(">> Mean property prices in postcode outward W1F: £{:,.2f}\n".format( mean_property_price_w1f)) avg_diff_detached_vs_flats = \ parser.average_price_difference_between_detached_houses_and_flats() print(">> Average property price difference between detached houses " "and flats: £{:,.2f}\n".format(avg_diff_detached_vs_flats)) print(">> Top 10% most expensive properties ...") top_expensive_properties = parser.top_10_percent_expensive_properties() print("{0:18} {1:18} {2:18} {3:18} {4:18} {5:18} {6:18} {7:18} " "{8:18}".format('PROPERTY_REFERENCE', 'BEDROOMS', 'PRICE', 'BATHROOMS', 'HOUSE_NUMBER', 'ADDRESS', 'REGION', 'POSTCODE', 'PROPERTY_TYPE')) for i in top_expensive_properties: print("{0:18} {1:18} {2:18} {3:18} {4:18} {5:18} {6:18} {7:18} " "{8:18}".format(i['PROPERTY_REFERENCE'], i['BEDROOMS'], i['PRICE'], i['BATHROOMS'], i['HOUSE_NUMBER'], i['ADDRESS'], i['REGION'], i['POSTCODE'], i['PROPERTY_TYPE']))
def test_csv_reader_object_with_no_data_rows(self): prop_parser = PropertyDataParser('./test_data/test_data_4.csv') try: with self.assertRaises(ValueError): prop_parser._read_csv_file() finally: prop_parser.file_handle.close()
def test_file_exists(self): prop_parser = PropertyDataParser('./test_data/test_data_1.csv') self.assertTrue(prop_parser._check_file_exists())
def test_top_10_percent_expensive_properties(self): prop_parser = PropertyDataParser('./test_data/test_data_3.csv') top_expensive = prop_parser.top_10_percent_expensive_properties() self.assertEqual(len(top_expensive), 1) self.assertEqual(top_expensive[0]['PROPERTY_REFERENCE'], '1') self.assertEqual(top_expensive[0]['PRICE'], '1000000')
def test_get_average_diff_between_detached_house_and_flat(self): prop_parser = PropertyDataParser('./test_data/test_data_2.csv') self.assertEqual( prop_parser. average_price_difference_between_detached_houses_and_flats(), 88750)
def test_get_mean_price_in_w1f(self): prop_parser = PropertyDataParser('./test_data/test_data_1.csv') self.assertEqual(prop_parser.mean_price_in_postcode_w1f(), 1750000)
def test_csv_reader_object(self): prop_parser = PropertyDataParser('./test_data/test_data_1.csv') reader = prop_parser._read_csv_file() data_rows = sum(1 for row in reader) self.assertEqual(data_rows, 2) prop_parser.file_handle.close()
def test_file_not_found(self): prop_parser = PropertyDataParser('./test_data/foo_bar.csv') with self.assertRaises(FileNotFoundError): prop_parser._check_file_exists()