def test_data_creation_from_base_row(self, mock_read_csv): """Confirm creation of a CountyMortgageData object from a CSV row.""" f = StringIO(self.data_header + self.data_row) reader = csv.DictReader(f) mock_read_csv.return_value = reader load_values() self.assertEqual(CountyMortgageData.objects.count(), 1) county = CountyMortgageData.objects.first() fields = reader.fieldnames fields.pop(fields.index('fips')) # test string separately fields.pop(fields.index('open')) # 'open' is stored as 'total' fields.pop(fields.index('date')) # date must be parsed before testing self.assertEqual(county.fips, self.data_row_dict.get('fips')) open_value = int(self.data_row_dict.get('open')) self.assertEqual(county.total, open_value) target_date = parser.parse(self.data_row_dict['date']).date() self.assertEqual(county.date, target_date) for field in fields: # remaining fields can be tested in a loop self.assertEqual(getattr(county, field), int(self.data_row_dict.get(field))) # test computed values self.assertEqual(county.epoch, int(target_date.strftime('%s')) * 1000) self.assertEqual( county.percent_90, int(self.data_row_dict.get('ninety')) * 1.0 / open_value) self.assertEqual(county.percent_30_60, (int(self.data_row_dict.get('thirty')) + int(self.data_row_dict.get('sixty'))) * 1.0 / open_value)
def test_load_values(self, mock_read_in): mock_read_in.return_value = [{ 'thirty': '4', 'month': '1', 'current': '262', 'sixty': '1', 'ninety': '0', 'date': '01/01/2008', 'open': '270', 'other': '3', 'fips': '12081'}] load_values() self.assertEqual(mock_read_in.call_count, 1) self.assertEqual(CountyMortgageData.objects.count(), 1)
def test_load_values(self, mock_read_in): mock_read_in.return_value = [{ 'thirty': '4', 'month': '1', 'current': '262', 'sixty': '1', 'ninety': '0', 'date': '01/01/2008', 'open': '270', 'other': '3', 'fips': '12081'}] load_values() self.assertEqual(mock_read_in.call_count, 1) self.assertEqual(CountyMortgageData.objects.count(), 1)
def test_load_values_return_fips(self, mock_read_in): mock_read_in.return_value = [{ 'thirty': '4', 'month': '1', 'current': '262', 'sixty': '1', 'ninety': '0', 'date': '01/01/2008', 'open': '270', 'other': '3', 'fips': '12081'}] fips_list = load_values(return_fips=True) self.assertEqual(mock_read_in.call_count, 1) self.assertEqual(fips_list, ['12081'])
def test_load_values_return_fips(self, mock_read_in): mock_read_in.return_value = [{ 'thirty': '4', 'month': '1', 'current': '262', 'sixty': '1', 'ninety': '0', 'date': '01/01/2008', 'open': '270', 'other': '3', 'fips': '12081'}] fips_list = load_values(return_fips=True) self.assertEqual(mock_read_in.call_count, 1) self.assertEqual(fips_list, ['12081'])
def test_data_creation_from_base_row( self, mock_read_csv): """ Confirm that loading a single row of real base data creates a CountyMortgageData object with the base row's values, and that the object's calculated API values are correct. """ f = StringIO.StringIO(self.data_header + self.data_row) reader = unicodecsv.DictReader(f) mock_read_csv.return_value = reader load_values() self.assertEqual(CountyMortgageData.objects.count(), 1) county = CountyMortgageData.objects.first() fields = reader.fieldnames fields.pop(fields.index('fips')) # test string separately fields.pop(fields.index('open')) # 'open' is stored as 'total' fields.pop(fields.index('date')) # date must be parsed before testing self.assertEqual(county.fips, self.data_row_dict.get('fips')) open_value = int(self.data_row_dict.get('open')) self.assertEqual(county.total, open_value) target_date = parser.parse(self.data_row_dict['date']).date() self.assertEqual(county.date, target_date) for field in fields: # remaining fields can be tested in a loop self.assertEqual( getattr(county, field), int(self.data_row_dict.get(field))) # test computed values self.assertEqual( county.epoch, int(target_date.strftime('%s')) * 1000) self.assertEqual( county.percent_90, int(self.data_row_dict.get('ninety')) * 1.0 / open_value) self.assertEqual( county.percent_30_60, (int(self.data_row_dict.get('thirty')) + int(self.data_row_dict.get('sixty'))) * 1.0 / open_value)