class TestUpdateChangedEntities(CommonTest): def setUp(self): self.account = Account() self.client = factory.create_client(budgetname='budgetname', nynabconnection=MockConnection(), sync=False) self.client.budget.be_accounts = [self.account] self.account2 = self.account.copy() self.client.session.commit() def testupdateChangedEntities_add(self): new_account = Account() changed_entities = dict(be_accounts=[new_account]) self.client.budgetClient.update_from_changed_entities(changed_entities) self.assertEqual(len(self.client.budget.be_accounts), 2) self.assertIn(new_account, self.client.budget.be_accounts) def testupdateChangedEntities_delete(self): self.account2.is_tombstone = True changed_entities = dict(be_accounts=[self.account2]) self.client.budgetClient.update_from_changed_entities(changed_entities) self.assertEqual(len(self.client.budget.be_accounts), 0) def testupdateChangedEntities_modify(self): self.account2 = self.account.copy() self.account2.note = 'note' changed_entities = dict(be_accounts=[self.account2]) self.client.budgetClient.update_from_changed_entities(changed_entities) self.assertEqual(len(self.client.budget.be_accounts), 1) acc = self.client.budget.be_accounts[0] self.assertEqual(acc, self.account2)
def setUp(self): super(TestUpdateChangedEntities, self).setUp() self.account = Account() self.client = nYnabClient_(budgetname='Mock Budget') self.client.budget.be_accounts = [self.account] self.account2 = self.account.copy() self.client.session.commit()
def setUp(self): super(TestGetChangedEntities, self).setUp() self.obj = Budget() self.account = Account() self.obj.be_accounts = [self.account] self.obj.clear_changed_entities() self.account2 = Account(id=self.account)
class TestUpdateChangedEntities(CommonTest): def setUp(self): super(TestUpdateChangedEntities, self).setUp() self.account = Account() self.client = nYnabClient_(budgetname='Mock Budget') self.client.budget.be_accounts = [self.account] self.account2 = self.account.copy() self.client.session.commit() def testupdateChangedEntities_add(self): new_account = Account() changed_entities = dict(be_accounts=[new_account]) self.client.budgetClient.update_from_changed_entities(changed_entities) self.assertEqual(len(self.client.budget.be_accounts), 2) self.assertIn(new_account, self.client.budget.be_accounts) def testupdateChangedEntities_delete(self): self.account2.is_tombstone = True changed_entities = dict(be_accounts=[self.account2]) self.client.budgetClient.update_from_changed_entities(changed_entities) self.assertEqual(len(self.client.budget.be_accounts), 0) def testupdateChangedEntities_modify(self): self.account2 = self.account.copy() self.account2.note = 'note' changed_entities = dict(be_accounts=[self.account2]) self.client.budgetClient.update_from_changed_entities(changed_entities) self.assertEqual(len(self.client.budget.be_accounts), 1) acc = self.client.budget.be_accounts[0] self.assertEqual(acc, self.account2)
def setUp(self): self.account = Account() self.client = factory.create_client(budgetname='budgetname', nynabconnection=MockConnection(), sync=False) self.client.budget.be_accounts = [self.account] self.account2 = self.account.copy() self.client.session.commit()
def testCE_simplechange(self): obj = Budget() account1 = Account() obj.be_accounts.append(account1) account2 = Account(id=account1.id, account_name='BLA') obj.be_accounts.changed = [] obj.be_accounts.modify(account2) self.assertEqual(obj.get_changed_entities(), {'be_accounts': [account2]})
def setUp(self): super(TestGetChangedEntities, self).setUp() self.obj = Budget() self.account = Account() self.obj.be_accounts = [self.account] self.obj.clear_changed_entities() self.account2 = Account(id=self.account) self.client = factory.create_client(budget_name='budgetname', nynabconnection=MockConnection(), sync=False)
def get_nubank_account(self): try: logging.info('Searching for Nubank account') return next(acc for acc in self.client.budget.be_accounts if acc.account_name == 'Nubank') except StopIteration: logging.info('Nubank account not found, creating a new one') account = Account() account.account_name = 'Nubank' self.client.budget.be_accounts.append(account) self.delta += 1 return account
def get_account(self, account_name): try: logging.info('Searching for ' + account_name + ' account') return next(acc for acc in self.client.budget.be_accounts if acc.account_name == account_name) except StopIteration: logging.info(account_name + ' account not found, creating a new one') account = Account() account.account_name = account_name self.client.budget.be_accounts.append(account) self.delta += 1 return account
class TestGetChangedEntities(CommonTest): def setUp(self): super(TestGetChangedEntities, self).setUp() self.obj = Budget() self.account = Account() self.obj.be_accounts = [self.account] self.obj.clear_changed_entities() self.account2 = Account(id=self.account) def testgetChangedEntities_add(self): added_account = Account() self.obj.be_accounts.append(added_account) changed_entities = self.obj.get_changed_entities() self.assertEqual(changed_entities, {'be_accounts': [added_account]}) def testgetChangedEntities_delete(self): self.obj.be_accounts.remove(self.account) changed_entities = self.obj.get_changed_entities() deleted = self.account.copy() deleted.is_tombstone = True self.assertEqual(changed_entities, {'be_accounts': [deleted]}) def testgetChangedEntities_modify(self): self.account.account_name = 'BLA' changed_entities = self.obj.get_changed_entities() self.assertEqual(changed_entities, {'be_accounts': [self.account]})
def testappend(self): obj = Budget() account = Account(None) obj.be_accounts.append(account) assert (len(obj.be_accounts) == 1) assert (list(obj.be_accounts)[-1] == account)
def test_add_delete_account(self): account_type = AccountTypes.Checking account_name = KeyGenerator.generateuuid() budget = self.client.budget for account in budget.be_accounts: if account.account_name == account_name: return if len(budget.be_accounts) > 0: sortable_index = max(account.sortable_index for account in budget.be_accounts) else: sortable_index = 0 account = Account( account_type=account_type, account_name=account_name, sortable_index=sortable_index, ) self.client.add_account(account, balance=random.randint(-10, 10), balance_date=datetime.now()) self.reload() self.assertIn(account, self.client.budget.be_accounts) self.client.delete_account(account) self.reload() result = self.client.budget.be_transactions.get(account.id) self.assertTrue(result is None)
def migrate_main(): print('migrate YNAB4 to pynYNAB') """Migrate a YNAB4 budget transaction history to nYNAB, using pyynab""" parser = configargparse.getArgumentParser('pynYNAB') parser.description = inspect.getdoc(migrate_main) parser.add_argument('budget', metavar='BudgetPath', type=str, help='The budget .ynab4 directory') args = parser.parse_args() test_common_args(args) budget_base_name = os.path.basename(args.budget) budget_path = os.path.dirname(args.budget) budget_name = re.match(r"(?P<budget_name>.*)~[A-Z0-9]{8}\.ynab4", budget_base_name).groupdict().get('budget_name') if args.budgetname is not None: budget_name = args.budgetname thisynab = YNAB(budget_path, budget_name) client = clientfromargs(args, reset=True) for ynab4_account in thisynab.accounts: account = Account( name=ynab4_account.name, account_type=ynab4_account.type.value, on_budget=ynab4_account.on_budget, sortable_index=random.randint(-50000, 50000), ) mindate = min([ ynab4transaction.date for ynab4transaction in thisynab.transactions if ynab4transaction.account == ynab4_account ]) client.add_account(account, 0, mindate) for master_category in thisynab.master_categories: master_entity = MasterCategory(name=master_category.name, sortable_index=random.randint( -50000, 50000)) client.budget.be_master_categories.append(master_entity) for category in master_category.categories: entity = Subcategory(name=category.name, entities_master_category_id=master_entity.id, sortable_index=random.randint(-50000, 50000)) client.budget.be_subcategories.append(entity) client.sync() for ynab4_payee in thisynab.payees: payee = Payee(name=ynab4_payee.name) client.budget.be_payees.append(payee) client.sync() for ynab4transaction in thisynab.transactions: transaction = Transaction() pass
def test_get_ce_add(obj, objclient): added_account = Account() obj.be_accounts.append(added_account) changed_entities = objclient.get_changed_entities() assert changed_entities == { 'be_accounts': { added_account.id: added_account } }
def util_add_account(client, account_name=None): if account_name is None: account_name = str(KeyGenerator.generateuuid()) account = Account(account_type=random.choice(list(AccountTypes)), account_name=account_name) client.add_account(account, balance=random.randint(-10, 10), balance_date=datetime.now()) return account
def testupdatechangedentities(self): obj = Budget() assert (obj.be_accounts.__class__ == ListofEntities) assert (obj.be_accounts.typeItems == Account) assert (len(obj.be_accounts) == 0) account = Account() changed_entities = dict(be_accounts=[account]) obj.update_from_changed_entities(changed_entities) assert (len(obj.be_accounts) == 1) assert (next(obj.be_accounts.__iter__()) == account)
def test_get_ce_replace(obj, objclient, account): added_account = Account() obj.be_accounts = [added_account] changed_entities = objclient.get_changed_entities() removed_account = account.copy() removed_account.is_tombstone = True assert changed_entities == { 'be_accounts': { added_account.id: added_account, removed_account.id: removed_account } }
def test_add_deleteaccount(live_client): account = Account(account_type=random.choice(list(AccountTypes)), account_name=KeyGenerator.generateuuid()) live_client.add_account(account, balance=random.randint(-10, 10), balance_date=datetime.now()) live_client = reload() assert account in live_client.budget.be_accounts live_client.delete_account(account) live_client = reload() assert account not in live_client.budget.be_accounts
def get_or_create_account(client, name): accounts = {a.account_name: a for a in client.budget.be_accounts if a.account_name == name} if name in accounts: return accounts[name] account = Account( account_type=AccountTypes.Checking, account_name=name ) client.add_account(account, balance=random.randint(-10, 10), balance_date=datetime.now()) return account
def util_get_empty_account_by_name_if_doesnt_exist(client, name): accounts = { a.account_name: a for a in client.budget.be_accounts if a.account_name == name } if name in accounts: account = accounts[name] client.delete_account(account) account = Account(account_type=AccountTypes.Checking, account_name=name) client.add_account(account, balance=random.randint(-10, 10), balance_date=datetime.now()) client.reload() return account
def testgetChangedEntities_add(self): added_account = Account() self.obj.be_accounts.append(added_account) changed_entities = self.obj.get_changed_entities() self.assertEqual(changed_entities, {'be_accounts': [added_account]})
def test_get_ce_add_delete(obj, objclient): added_account = Account() obj.be_accounts.append(added_account) obj.be_accounts.remove(added_account) changed_entities = objclient.get_changed_entities() assert changed_entities == {}
class TestGetChangedEntities(CommonTest): def setUp(self): super(TestGetChangedEntities, self).setUp() self.obj = Budget() self.account = Account() self.obj.be_accounts = [self.account] self.obj.clear_changed_entities() self.account2 = Account(id=self.account) self.client = factory.create_client(budget_name='budgetname', nynabconnection=MockConnection(), sync=False) def testgetChangedEntities_add(self): added_account = Account() self.obj.be_accounts.append(added_account) changed_entities = self.obj.get_changed_entities() self.assertEqual(changed_entities, {'be_accounts': [added_account]}) def testgetChangedEntities_addtransactionsubtransaction(self): added_transaction = Transaction() subtransaction1 = Subtransaction( entities_transaction=added_transaction) subtransaction2 = Subtransaction( entities_transaction=added_transaction) self.client.budget.be_transactions.append(added_transaction) self.client.budget.be_subtransactions.append(subtransaction1) self.client.budget.be_subtransactions.append(subtransaction2) self.client.session.commit() changed_entities = self.client.budget.get_changed_entities() self.assertIsInstance(changed_entities, dict) self.assertEqual(1, len(changed_entities.keys())) self.assertEqual('be_transaction_groups', list(changed_entities.keys())[0]) transaction_groups = changed_entities['be_transaction_groups'] self.assertEqual(1, len(transaction_groups)) self.assertEqual(added_transaction, transaction_groups[0]['be_transaction']) self.assertIsNotNone(transaction_groups[0]['be_subtransactions']) try: self.assertItemsEqual( [subtransaction1, subtransaction2], set(transaction_groups[0]['be_subtransactions'])) except AttributeError: self.assertCountEqual( [subtransaction1, subtransaction2], set(transaction_groups[0]['be_subtransactions'])) def testgetChangedEntities_delete(self): self.obj.be_accounts.remove(self.account) changed_entities = self.obj.get_changed_entities() deleted = self.account.copy() deleted.is_tombstone = True self.assertEqual(changed_entities, {'be_accounts': [deleted]}) def testgetChangedEntities_modify(self): self.account.account_name = 'BLA' changed_entities = self.obj.get_changed_entities() self.assertEqual(changed_entities, {'be_accounts': [self.account]}) def test_arraytype(self): user = User() user.feature_flags = ['featureA', 'feature1'] self.session.add(user) self.session.commit() fetched_user = self.session.query(User).first() self.assertEqual(user, fetched_user)
def test_copy(self): obj = Account() objc = obj.copy() self.assertEqual(obj.id, objc.id) self.assertEqual(obj.get_dict(), objc.get_dict())
def testappend(self): obj = Budget() account = Account() obj.be_accounts.append(account) self.assertEqual(len(obj.be_accounts), 1) self.assertEqual(list(obj.be_accounts)[-1], account)
def testupdateChangedEntities_add(self): new_account = Account() changed_entities = dict(be_accounts=[new_account]) self.client.budgetClient.update_from_changed_entities(changed_entities) self.assertEqual(len(self.client.budget.be_accounts), 2) self.assertIn(new_account, self.client.budget.be_accounts)
def test_append(): obj = Budget() account = Account() obj.be_accounts.append(account) assert len(obj.be_accounts) == 1 assert list(obj.be_accounts)[-1] == account
def test_copy(): obj = Account() objc = obj.copy() assert obj.id == objc.id assert obj.get_dict() == objc.get_dict()
def account(): return Account()
def testCE_simpledelete(self): obj = Budget() account = Account() obj.be_accounts.delete(account) self.assertEqual(obj.get_changed_entities(), {'be_accounts': [account]})