def test_conform_actions(self): data = self.get_data() actions = self.get_conform_actions() result = sdt.conform(data, actions=actions) self.assertEqual(result.loc[0, 'description'], '123') self.assertEqual(result.loc[1, 'description'], 'tacoBar') self.assertEqual(result.loc[1, 'amount'], 0.0) self.assertEqual(result.loc[2, 'amount'], 11.11)
def test_to_records(self): data = self.get_data() data = sdt.conform(data) result = db.Database._to_records(data) expected = data.copy() expected.date = expected.date.apply(lambda x: x.isoformat()) expected = expected.replace({np.nan: None}).to_dict(orient='records') self.assertEqual(result, expected)
def test_conform_columns(self): data = self.get_data() result = sdt.conform(data).columns.tolist() expected = [ 'date', 'description', 'original_description', 'amount', 'type', 'category', 'account', 'labels', 'notes', ] self.assertEqual(result, expected) expected = expected[:-2] result = sdt.conform(data, columns=expected).columns.tolist() self.assertEqual(result, expected)
def test_update(self): with TemporaryDirectory() as root: config, _ = self.get_config(root) result = db.Database(config).update()._data expected = pd.read_csv(config['data_path'], index_col=None) expected = sdt.conform(expected, actions=config['conform'], columns=config['columns']) eft.enforce_dataframes_are_equal(result, expected)
def test_conform_actions_errors(self): data = self.get_data() ow, sub, _ = self.get_conform_actions() bad = deepcopy(sub) bad['mapping'] = {'a': ['b']} with self.assertRaises(DataError): sdt.conform(data, actions=[ow, sub, bad]) # bad source column cols = [ 'date', 'description', 'original_description', 'amount', 'type', 'category', 'account', 'labels', 'notes' ] cols = "'" + "', '".join(cols) + "'" cols = cols.lower() expected = 'Source column bagel not found in columns. ' expected += f"Legal columns include: \\[{cols}\\]\\." bad = deepcopy(sub) bad['source_column'] = 'bagel' with self.assertRaisesRegexp(ValueError, expected): sdt.conform(data, actions=[ow, sub, bad])
def test_conform_actions_target(self): data = self.get_data() actions = [{ 'action': 'overwrite', 'source_column': 'description', 'target_column': 'new_col', 'mapping': { 'kiwi': 123, } }] result = sdt.conform(data, actions=actions) self.assertEqual(result.loc[0, 'description'], 'Kiwi') self.assertEqual(result.loc[0, 'new_col'], 123)
def update(self): # type: () -> Database ''' Loads CSV found in config's data_path into self._data. Returns: Database: self. ''' data = pd.read_csv(self._config['data_path'], index_col=None) self._data = sdt.conform( data, actions=self._config['conform'], columns=self._config['columns'], ) return self
def test_to_records_no_mutation(self): result = self.get_data() result = sdt.conform(result) expected = result.copy() db.Database._to_records(result) eft.enforce_dataframes_are_equal(result, expected)
def test_conform_category(self): data = self.get_data() result = sdt.conform(data).category self.assertEqual(result[0], 'food_and_drug') self.assertEqual(result[1], 'fancy_banking')
def test_conform_account(self): data = self.get_data() result = sdt.conform(data).account self.assertEqual(result[0], 'visa_credit_card') self.assertEqual(result[1], 'amex')