Esempio n. 1
0
 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)
Esempio n. 2
0
 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)
Esempio n. 3
0
    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)
Esempio n. 4
0
    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)
Esempio n. 5
0
    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])
Esempio n. 6
0
 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)
Esempio n. 7
0
    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
Esempio n. 8
0
 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)
Esempio n. 9
0
 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')
Esempio n. 10
0
 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')