def setup_method(self, method): self.user, self.username, self.password = setup_user() self.app = app.test_client() self.account_type = AccountType('Income') db.session.add(self.account_type) db.session.commit() self.account1 = Account('TRX_Salary', self.account_type, "Show me the money") self.account2 = Account('TRX_Checking', self.account_type, "Mine mine mine") db.session.add(self.account1) db.session.add(self.account2) db.session.commit() self.transaction = Transaction( self.account1, self.account2, 10000, 'Employer', datetime.date.today(), 'January' ) db.session.add(self.transaction) db.session.commit()
def setup_method(self, method): self.account_type = AccountType('Income') db.session.add(self.account_type) db.session.commit() self.account1 = Account("Test1", self.account_type) self.account2 = Account("Test2", self.account_type) db.session.add(self.account1) db.session.add(self.account2) db.session.commit()
def setup_method(self, method): self.user, self.username, self.password = setup_user() self.app = app.test_client() self.account_type = AccountType('Expense') db.session.add(self.account_type) db.session.commit() self.account = Account('Insurance', self.account_type, "Safety Net") db.session.add(self.account) db.session.commit()
class TestTransactionModel(): @classmethod def setup_method(self, method): self.account_type = AccountType('Income') db.session.add(self.account_type) db.session.commit() self.account1 = Account("Test1", self.account_type) self.account2 = Account("Test2", self.account_type) db.session.add(self.account1) db.session.add(self.account2) db.session.commit() @classmethod def teardown_method(self, method): db.session.delete(self.account_type) db.session.delete(self.account1) db.session.delete(self.account2) db.session.commit() db.session.remove() def test_user_repr(self): """Ensure __repr__ function works""" t = Transaction( self.account1, self.account2, 1, "ACME, Inc.", datetime.date.today(), "January's Salary" ) assert repr(t) == "<Transaction: ACME, Inc. 1>" def test_transaction_add(self): """Test adding a transaction normaly""" t = Transaction( self.account1, self.account2, 1, "ACME, Inc.", datetime.date.today(), "January's Salary" ) db.session.add(t) db.session.commit() t2 = Transaction.query.filter(Transaction.amount == t.amount).first() assert t2.account_debit_id == t.account_debit_id assert t2.account_credit_id == t.account_credit_id assert t2.amount == t.amount assert t2.summary == t.summary assert t2.description == t.description assert t2.date == t.date assert bool(t2.transaction_id) is True def test_account_balance(self): """Test that balance on account calculates correctly""" t1 = Transaction( self.account1, self.account2, 10, 'ACME, Inc.', datetime.date.today(), "January's Salary'" ) db.session.add(t1) t2 = Transaction( self.account2, self.account1, 5, 'IRS', datetime.date.today(), "Taxes for January'" ) db.session.add(t2) db.session.commit() assert self.account1.get_balance() == t1.amount - t2.amount assert self.account2.get_balance() == t2.amount - t1.amount def test_account_balance_onesided(self): """Test that balance on account calculates correctly""" t1 = Transaction( self.account1, self.account2, 10, 'ACME, Inc.', datetime.date.today(), "January's Salary'" ) db.session.add(t1) t2 = Transaction( self.account1, self.account2, 10, 'ACME, Inc.', datetime.date.today(), "February's Salary'" ) db.session.add(t2) db.session.commit() assert self.account1.get_balance() == t1.amount + t2.amount assert self.account2.get_balance() == -t1.amount - t2.amount def test_account_transactions(self): """Test getting a list of transactions for an account""" acct1_trx_count = len(self.account1.transactions()) acct2_trx_count = len(self.account2.transactions()) t1 = Transaction( self.account1, self.account2, 10, 'ACME, Inc.', datetime.date.today(), "January's Salary'" ) db.session.add(t1) t2 = Transaction( self.account2, self.account1, 5, 'IRS', datetime.date.today(), "Taxes for January'" ) db.session.add(t2) db.session.commit() assert t1 in self.account1.transactions() assert t2 in self.account1.transactions() assert acct1_trx_count + 2 == len(self.account1.transactions()) assert t1 in self.account2.transactions() assert t2 in self.account2.transactions() assert acct2_trx_count + 2 == len(self.account2.transactions()) def test_transaction_jsonify(self): """Test the jsonify method""" t = Transaction( self.account1, self.account2, 10, 'ACME, Inc.', datetime.date.today(), "January's Salary'" ) assert dict == type(t.jsonify()) assert bool(json.dumps(t.jsonify())) is True db.session.add(t) db.session.commit() t_json = t.jsonify() assert dict == type(t_json) assert bool(json.dumps(t_json)) is True assert self.account1.jsonify() == t_json.get('debit') assert self.account2.jsonify() == t_json.get('credit')
class TestTransactionView(BaseViewTestCase): def setup_method(self, method): self.user, self.username, self.password = setup_user() self.app = app.test_client() self.account_type = AccountType('Income') db.session.add(self.account_type) db.session.commit() self.account1 = Account('TRX_Salary', self.account_type, "Show me the money") self.account2 = Account('TRX_Checking', self.account_type, "Mine mine mine") db.session.add(self.account1) db.session.add(self.account2) db.session.commit() self.transaction = Transaction( self.account1, self.account2, 10000, 'Employer', datetime.date.today(), 'January' ) db.session.add(self.transaction) db.session.commit() def teardown_method(self, method): db.session.delete(self.transaction) db.session.delete(self.account1) db.session.delete(self.account2) db.session.delete(self.account_type) db.session.commit() delete_user(self.user) def test_view_auth_required(self): """Test that authentication is required""" rv = self.app.get("/transactions") assert 401 == rv.status_code def test_view_all(self): """Test viewing all transactions""" trx = self.transaction.jsonify() rv = self.open_with_auth( "/transactions", 'GET', self.username, self.password ) assert 200 == rv.status_code data = json.loads(rv.data) assert trx in data def test_view_transaction(self): """Test viewing a single transaction""" trx = self.transaction.jsonify() rv = self.open_with_auth( "/transactions/%s" % self.transaction.transaction_id, "GET", self.username, self.password ) assert 200 == rv.status_code assert trx == json.loads(rv.data) def test_view_transaction_404(self): """Test viewing a non-existant transaction""" rv = self.open_with_auth( "/transaction/999", "GET", self.username, self.password ) assert 404 == rv.status_code assert "404 Not Found" in rv.data def test_view_add(self): """Test adding a transaction""" summary = 'Supplies' amount = 100.00 date = datetime.date.today().strftime("%Y-%m-%d") description = 'Getting things done' debit_json = self.account1.jsonify() credit_json = self.account2.jsonify() rv = self.open_with_auth( "/transactions", "POST", self.username, self.password, data=dict( debit=debit_json.get('account_id'), credit=credit_json.get('account_id'), amount=amount, summary=summary, date=date, description=description ) ) assert 200 == rv.status_code assert 'Success' in rv.data trx = json.loads(rv.data) rv = self.open_with_auth( "/transactions/%s" % trx.get('transaction_id'), "GET", self.username, self.password, ) assert 200 == rv.status_code trx_get = json.loads(rv.data) # need to update account balances with this trx debit_json['balance'] += amount credit_json['balance'] -= amount assert debit_json == trx_get.get('debit') assert debit_json.get('account_id') == trx_get.get('account_debit_id') assert credit_json == trx_get.get('credit') assert credit_json.get('account_id') == trx_get.get( 'account_credit_id' ) assert summary == trx_get.get('summary') assert amount == trx_get.get('amount') assert date == trx_get.get('date') assert description == trx_get.get('description') assert trx.get('transaction_id') == trx_get.get('transaction_id') def test_view_add_locale_date(self): """Test adding a transaction using a locale date value""" summary = 'Supplies' amount = 200.00 date = '2013-01-15T05:00:00.000Z' description = 'Getting things done' debit_json = self.account1.jsonify() credit_json = self.account2.jsonify() rv = self.open_with_auth( "/transactions", "POST", self.username, self.password, data=dict( debit=debit_json.get('account_id'), credit=credit_json.get('account_id'), amount=amount, summary=summary, date=date, description=description ) ) assert 200 == rv.status_code assert 'Success' in rv.data trx = json.loads(rv.data) rv = self.open_with_auth( "/transactions/%s" % trx.get('transaction_id'), "GET", self.username, self.password, ) assert 200 == rv.status_code trx_get = json.loads(rv.data) # need to update account balances with this trx debit_json['balance'] += amount credit_json['balance'] -= amount assert debit_json == trx_get.get('debit') assert debit_json.get('account_id') == trx_get.get('account_debit_id') assert credit_json == trx_get.get('credit') assert credit_json.get('account_id') == trx_get.get( 'account_credit_id' ) assert summary == trx_get.get('summary') assert amount == trx_get.get('amount') assert date[:10] == trx_get.get('date') assert description == trx_get.get('description') assert trx.get('transaction_id') == trx_get.get('transaction_id') def test_view_add_fail(self): """Test adding an invalid transaction""" summary = 'Supplies' amount = -100.00 date = datetime.date.today().strftime("%Y-%m-%d") description = 'Getting things done' debit_json = self.account1.jsonify() credit_json = self.account2.jsonify() rv = self.open_with_auth( "/transactions", "POST", self.username, self.password, data=dict( debit=debit_json.get('account_id'), credit=credit_json.get('account_id'), amount=amount, summary=summary, date=date, description=description ) ) assert 400 == rv.status_code assert 'errors' in rv.data def test_view_account_transactions(self): """Test viewing transactions for an account""" trxs = self.account1.transactions()[0].jsonify() rv = self.open_with_auth( '/accounts/transactions/%s' % self.account1.account_id, 'GET', self.username, self.password ) acct_trx_list = json.loads(rv.data) assert len(acct_trx_list) == 1 assert trxs in acct_trx_list def test_view_account_transactions_invalid_account(self): """Test viewing transactions for an invalid account""" rv = self.open_with_auth( '/accounts/transactions/99', 'GET', self.username, self.password ) assert rv.status_code == 404 def test_view_delete(self): """Test deleting transaction""" transaction1 = Transaction( self.account1, self.account2, 100.00, 'Bonus', datetime.date.today() ) db.session.add(transaction1) db.session.commit() rv = self.open_with_auth( "/transactions/%s" % transaction1.transaction_id, "DELETE", self.username, self.password ) assert 200 == rv.status_code # attempt to get the transaction rv = self.open_with_auth( "/transactions/%s" % transaction1.transaction_id, "GET", self.username, self.password ) assert 404 == rv.status_code def test_view_delete_fail(self): """Test deleting a non existant transaction""" rv = self.open_with_auth( "/transactions/999", "DELETE", self.username, self.password ) assert 404 == rv.status_code def test_view_update(self): """Test updating an transaction""" description = 'Something witty here' transaction_id = self.transaction.transaction_id rv = self.open_with_auth( "/transactions/%s" % transaction_id, "PUT", self.username, self.password, data=dict( debit=self.transaction.account_debit_id, credit=self.transaction.account_credit_id, amount=self.transaction.amount, summary=self.transaction.summary, date=self.transaction.date.strftime("%Y-%m-%d"), description=description, ) ) assert 200 == rv.status_code assert 'Success' in rv.data rv = self.open_with_auth( "/transactions/%s" % transaction_id, "GET", self.username, self.password, ) assert 200 == rv.status_code trx_get = json.loads(rv.data) assert description == trx_get.get('description') def test_view_update_invalid(self): """Test updating an transaction with invalid data""" description = 'Something witty here' transaction_id = self.transaction.transaction_id rv = self.open_with_auth( "/transactions/%s" % transaction_id, "PUT", self.username, self.password, data=dict( credit=self.transaction.account_credit_id, amount=self.transaction.amount, summary=self.transaction.summary, date=self.transaction.date.strftime("%Y-%m-%d"), description=description, ) ) assert 400 == rv.status_code assert 'error' in rv.data def test_view_update_nochange(self): """Test updating an transaction with same values""" transaction_id = self.transaction.transaction_id rv = self.open_with_auth( "/transactions/%s" % transaction_id, "PUT", self.username, self.password, data=dict( debit=self.transaction.account_debit_id, credit=self.transaction.account_credit_id, amount=self.transaction.amount, summary=self.transaction.summary, date=self.transaction.date.strftime("%Y-%m-%d"), description=self.transaction.description, ) ) assert 200 == rv.status_code assert 'Success' in rv.data def test_view_update_fail_invalid_id(self): """Test updating an transaction with invalid id""" transaction_id = '999' rv = self.open_with_auth( "/transactions/%s" % transaction_id, "PUT", self.username, self.password, data=dict( debit=self.transaction.account_debit_id, credit=self.transaction.account_credit_id, amount=self.transaction.amount, summary=self.transaction.summary, date=self.transaction.date.strftime("%Y-%m-%d"), description=self.transaction.description, ) ) assert 404 == rv.status_code
def test_account_jsonify(self, account_type): """Test the jsonify method""" a = Account("Interest", account_type, "Compound it baby") assert dict == type(a.jsonify()) assert bool(json.dumps(a.jsonify())) is True
class TestAccountView(BaseViewTestCase): def setup_method(self, method): self.user, self.username, self.password = setup_user() self.app = app.test_client() self.account_type = AccountType('Expense') db.session.add(self.account_type) db.session.commit() self.account = Account('Insurance', self.account_type, "Safety Net") db.session.add(self.account) db.session.commit() def teardown_method(self, method): db.session.delete(self.account_type) db.session.delete(self.account) db.session.commit() delete_user(self.user) def test_view_auth_required(self): """Test that authentication is required""" rv = self.app.get("/accounts") assert 401 == rv.status_code def test_view_all(self): """Test viewing all accounts""" rv = self.open_with_auth( "/accounts", 'GET', self.username, self.password ) assert 200 == rv.status_code data = json.loads(rv.data) acct = self.account.jsonify() assert acct in data def test_view_account(self): """Test viewing a single account""" rv = self.open_with_auth( "/accounts/%s" % self.account.account_id, "GET", self.username, self.password ) assert 200 == rv.status_code assert self.account.jsonify() == json.loads(rv.data) def test_view_account_404(self): """Test viewing a non-existant account""" rv = self.open_with_auth( "/accounts/999", "GET", self.username, self.password ) assert 404 == rv.status_code assert "404 Not Found" in rv.data def test_view_add(self): """Test adding an account""" name = 'Supplies' account_type_json = self.account_type.jsonify() description = 'Getting things done' rv = self.open_with_auth( "/accounts", "POST", self.username, self.password, data=dict( name=name, account_type_id=account_type_json.get('account_type_id'), description=description ) ) assert 200 == rv.status_code assert 'Success' in rv.data acct = json.loads(rv.data) rv = self.open_with_auth( "/accounts/%s" % acct.get('account_id'), "GET", self.username, self.password, ) assert 200 == rv.status_code acct_get = json.loads(rv.data) assert name == acct_get.get('name') assert account_type_json == acct_get.get('account_type') assert account_type_json.get('account_type_id') == \ acct_get.get('account_type_id') assert description == acct_get.get('description') assert acct.get('account_id') == acct_get.get('account_id') def test_view_add_fail(self): """Test adding an invalid account""" name = 'Supplies' account_type = 'Exp' description = 'Getting things done' rv = self.open_with_auth( "/accounts", "POST", self.username, self.password, data=dict( name=name, account_type=account_type, description=description ) ) assert 400 == rv.status_code assert 'errors' in rv.data def test_view_delete(self): """Test deleting account""" account1 = Account( 'Entertainment', self.account_type, "Party like it's 1999'" ) db.session.add(account1) db.session.commit() rv = self.open_with_auth( "/accounts/%s" % account1.account_id, "DELETE", self.username, self.password ) assert 200 == rv.status_code # attempt to get the account rv = self.open_with_auth( "/accounts/%s" % account1.account_id, "GET", self.username, self.password ) assert 404 == rv.status_code def test_view_delete_fail(self): """Test deleting a non existant account""" rv = self.open_with_auth( "/accounts/999", "DELETE", self.username, self.password ) assert 404 == rv.status_code def test_view_update(self): """Test updating an account""" name = 'Entertainment' account_id = self.account.account_id rv = self.open_with_auth( "/accounts/%s" % account_id, "PUT", self.username, self.password, data=dict( name=name, account_type_id=self.account.account_type_id, description=self.account.description ) ) assert 200 == rv.status_code assert 'Success' in rv.data rv = self.open_with_auth( "/accounts/%s" % account_id, "GET", self.username, self.password, ) assert 200 == rv.status_code acct_get = json.loads(rv.data) assert name == acct_get.get('name') def test_view_update_nonvalid(self): """Test updating an account with invalid form data""" name = "" account_id = self.account.account_id rv = self.open_with_auth( "/accounts/%s" % account_id, 'PUT', self.username, self.password, data=dict( name=name, account_type_id=self.account.account_type_id, description=self.account.description, ) ) assert 400 == rv.status_code assert 'errors' in rv.data def test_view_update_nochange(self): """Test updating an account with same values""" account_id = self.account.account_id rv = self.open_with_auth( "/accounts/%s" % account_id, "PUT", self.username, self.password, data=dict( name=self.account.name, account_type_id=self.account.account_type_id, description=self.account.description ) ) assert 200 == rv.status_code assert 'Success' in rv.data def test_view_update_fail_invalid_id(self): """Test updating an account with invalid id""" account_id = '999' rv = self.open_with_auth( "/accounts/%s" % account_id, "PUT", self.username, self.password, data=dict( name=self.account.name, account_type_id=self.account.account_type_id, description=self.account.description ) ) assert 404 == rv.status_code