コード例 #1
0
class TestRouteExceptions(unittest.TestCase):
    session = None
    def call_test_route(self):
        return self.client.get('/test')
  
    def delete_all_rows(self):
        self.session.query(User).delete()
        self.session.commit()

    def setUp(self):
        self.session = SessionLocal()
        self.delete_all_rows()
        self.client = app.test_client()

    def tearDown(self):
        self.delete_all_rows()
        self.session.close()

    # we expect the session to rollback on exception call
    def test_exception_rollback(self):
        response = self.call_test_route()
        
        # try to commit any changes made in /test route
        app.session.commit()
        self.assertEqual(response.status_code, 500)

        # verify the session was rolled back after exception was caught
        self.assertEqual(len(self.session.query(User).all()), 0)
        
        
コード例 #2
0
 def setUp(self):
     self.session = SessionLocal()
     self.delete_all_rows()
     self.create_test_user()
     self.create_account()
     self.client = app.test_client()
     self.post_login(self.test_email, self.test_pass)
コード例 #3
0
class TestRouteUser(unittest.TestCase):
    test_email = '*****@*****.**'
    test_pass = '******'
    test_user = None
    session = None

    def create_test_user(self):
        self.test_user = create_user(session=self.session,
                                     email=self.test_email,
                                     password=self.test_pass)
        self.session.commit()

    def post_login(self, email: str, password: str):
        test_login = {'email': email, 'password': password}
        return self.client.post('/login',
                                data=json.dumps(test_login),
                                content_type='application/json')

    def get_logout(self):
        return self.client.get('/logout')

    def get_profile(self):
        return self.client.get('/profile')

    def delete_all_rows(self):
        self.session.query(User).delete()
        self.session.commit()

    def setUp(self):
        self.session = SessionLocal()
        self.delete_all_rows()
        self.create_test_user()
        self.client = app.test_client()

    def tearDown(self):
        self.delete_all_rows()
        self.session.close()

    def test_profile_path(self):
        response = self.post_login(self.test_email, self.test_pass)
        self.assertEqual(response.status_code, 200)

        response = self.get_profile()
        self.assertEqual(response.status_code, 200)

        user = json.loads(response.data)
        self.assertEqual(user['email'], self.test_email)

    def test_profile_path_401_on_not_logged_in(self):
        response = self.get_profile()
        self.assertEqual(response.status_code, 401)
コード例 #4
0
 def setUp(self):
     self.session = SessionLocal()
     self.delete_all_rows()
     self.test_user = create_user(self.session, self.test_email,
                                  self.test_password)
     self.test_user_2 = create_user(self.session, self.test_email_2,
                                    self.test_password)
     self.test_account_1 = create_account(self.test_account_name,
                                          self.test_user,
                                          self.test_balance_1)
     self.test_account_2 = create_account(self.test_account_name,
                                          self.test_user_2,
                                          self.test_balance_2)
     self.session.commit()
コード例 #5
0
class TestORMRecurringTransactions(unittest.TestCase):
    session = None
    # User info
    test_email = '*****@*****.**'
    test_email_2 = '*****@*****.**'
    test_password = '******'
    test_user = None
    test_user_2 = None

    # Account Info
    test_account_name = 'test_account'
    test_balance_1 = 100.50
    test_balance_2 = 205.85
    test_account_1 = None
    test_account_2 = None

    # Transaction Info
    test_transaction_1 = 'account_1_trans'
    test_transaction_2 = 'account_2_trans'
    test_date_1 = date.today() + timedelta(days=1)
    test_date_2 = date.today() + timedelta(days=2)
    test_amount_1 = 500
    test_amount_2 = -200
    test_amount_3 = -1000
    biweekly = Timespan(0, 2, 0, 0)
    monthly = Timespan(0, 0, 1, 0)
    yearly = Timespan(0, 0, 0, 1)

    def delete_all_rows(self):
        self.session.query(RecurringTransaction).delete()
        self.session.query(Account).delete()
        self.session.query(User).delete()
        self.session.commit()

    def setUp(self):
        self.session = SessionLocal()
        self.delete_all_rows()
        self.test_user = create_user(self.session, self.test_email,
                                     self.test_password)
        self.test_user_2 = create_user(self.session, self.test_email_2,
                                       self.test_password)
        self.test_account_1 = create_account(self.test_account_name,
                                             self.test_user,
                                             self.test_balance_1)
        self.test_account_2 = create_account(self.test_account_name,
                                             self.test_user_2,
                                             self.test_balance_2)
        self.session.commit()

    def tearDown(self):
        self.delete_all_rows()
        self.session.close()

    def test_create_recurring_transaction(self):
        raises = False
        try:
            trans = create_recurring_transaction(account=self.test_account_1,
                                                 name=self.test_transaction_1,
                                                 start_date=self.test_date_1,
                                                 timespan=self.biweekly,
                                                 amount=self.test_amount_1)
            self.session.commit()
        except:
            raises = True

        self.assertFalse(raises)
        self.assertEqual(len(self.session.query(RecurringTransaction).all()),
                         1)
        self.assertEqual(self.test_account_1.recurring_transactions.count(), 1)
        self.assertEqual(self.test_account_2.recurring_transactions.count(), 0)
        self.assertEqual(self.test_account_1.recurring_transactions.first(),
                         trans)
        self.assertEqual(trans.account, self.test_account_1)
        self.assertEqual(trans.name, self.test_transaction_1)
        self.assertEqual(trans.start_date.date(), self.test_date_1)
        self.assertEqual(trans.timespan, self.biweekly.to_timespan_str())
        self.assertEqual(trans.amount, self.test_amount_1)

    def test_get_all_recurring_transactions_on_date(self):
        raises = False
        try:
            create_recurring_transaction(account=self.test_account_1,
                                         name=self.test_transaction_1,
                                         start_date=self.test_date_1,
                                         timespan=self.biweekly,
                                         amount=self.test_amount_1)
            create_recurring_transaction(account=self.test_account_1,
                                         name=self.test_transaction_1,
                                         start_date=self.test_date_2,
                                         timespan=self.biweekly,
                                         amount=self.test_amount_2)
            self.session.commit()
            transactions_1 = get_all_recurring_transactions_on_date(
                self.test_account_1, self.test_date_1)
            transactions_2 = get_all_recurring_transactions_on_date(
                self.test_account_1, self.test_date_2)
            transactions_3 = get_all_recurring_transactions_on_date(
                self.test_account_2, self.test_date_2)
        except:
            raises = True

        self.assertFalse(raises)
        self.assertEqual(self.test_account_1.recurring_transactions.count(), 2)
        self.assertEqual(len(transactions_1), 1)
        self.assertEqual(len(transactions_2), 2)
        self.assertEqual(len(transactions_3), 0)

    def test_get_all_recurring_transactions_on_date(self):
        start_date = date.today()
        raises = False
        try:
            create_recurring_transaction(account=self.test_account_1,
                                         name=self.test_transaction_1,
                                         start_date=start_date,
                                         timespan=self.biweekly,
                                         amount=self.test_amount_1)
            create_recurring_transaction(account=self.test_account_1,
                                         name=self.test_transaction_1,
                                         start_date=start_date,
                                         timespan=self.monthly,
                                         amount=self.test_amount_2)
            create_recurring_transaction(account=self.test_account_1,
                                         name=self.test_transaction_1,
                                         start_date=start_date,
                                         timespan=self.yearly,
                                         amount=self.test_amount_3)
            self.session.commit()

            # recurring transactions charge on the date of creation
            # base_sum is the sum of all transactions for their initial state
            base_sum = self.test_amount_1 + self.test_amount_2 + self.test_amount_3
            # in 3 weeks we expect the biweekly payout to be 1x
            expected_sum_1 = base_sum + self.test_amount_1
            # in 1 month we expect
            #   monthly payout to be 1x
            #   biweekly payout to be 2x
            expected_sum_2 = base_sum + (self.test_amount_1 *
                                         2) + self.test_amount_2
            # in 1 year we expect
            #   monthly payout to be 12x
            #   biweekly payout to be 26
            #   yearly payout to be 1
            expected_sum_3 = base_sum + (self.test_amount_1 * 26) + \
                (self.test_amount_2 * 12) + self.test_amount_3

            date_1 = start_date + relativedelta(weeks=+3)
            date_2 = start_date + relativedelta(months=+1)
            date_3 = start_date + relativedelta(years=+1)
            sum_1 = get_recurring_transaction_sum_on_date(
                self.test_account_1, date_1)
            sum_2 = get_recurring_transaction_sum_on_date(
                self.test_account_1, date_2)
            sum_3 = get_recurring_transaction_sum_on_date(
                self.test_account_1, date_3)
        except:
            raises = True

        self.assertFalse(raises)
        self.assertEqual(self.test_account_1.recurring_transactions.count(), 3)
        self.assertEqual(expected_sum_1, sum_1)
        self.assertEqual(expected_sum_2, sum_2)
        self.assertEqual(expected_sum_3, sum_3)
コード例 #6
0
 def setUp(self):
     self.session = SessionLocal()
     self.delete_all_rows()
     self.create_test_user()
     self.client = app.test_client()
コード例 #7
0
class TestRouteAccount(unittest.TestCase):
    test_email = '*****@*****.**'
    test_pass = '******'
    test_user = None
    session = None

    def create_account(self, name: str, balance: float):
        account = create_account(name=name,
                                 user=self.test_user,
                                 balance=balance)
        self.session.commit()
        return account

    def create_test_user(self):
        self.test_user = create_user(session=self.session,
                                     email=self.test_email,
                                     password=self.test_pass)
        self.session.commit()

    def get_accounts(self):
        return self.client.get('/accounts')

    def post_create_account(self, name, balance):
        data = {'name': name, 'balance': balance}
        return self.client.post('/accounts',
                                data=json.dumps(data),
                                mimetype='application/json')

    def post_login(self, email: str, password: str):
        test_login = {'email': email, 'password': password}
        return self.client.post('/login',
                                data=json.dumps(test_login),
                                content_type='application/json')

    def get_logout(self):
        return self.client.get('/logout')

    def get_account(self, account_id):
        return self.client.get('/account/%s' % account_id)

    def get_account_balance(self, account_id, date):
        return self.client.get('/account/%s/balance/%s' % (account_id, date))

    def delete_all_rows(self):
        self.session.query(SingleTransaction).delete()
        self.session.query(RecurringTransaction).delete()
        self.session.query(Account).delete()
        self.session.query(User).delete()
        self.session.commit()

    def setUp(self):
        self.session = SessionLocal()
        self.delete_all_rows()
        self.create_test_user()
        self.client = app.test_client()
        self.post_login(self.test_email, self.test_pass)

    def tearDown(self):
        self.delete_all_rows()
        self.session.close()

    def test_get_accounts_empty(self):
        response = self.get_accounts()

        accounts = json.loads(response.data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(accounts), 0)

    def test_get_accounts(self):
        account_name = 'test_account'
        balance = 95.50
        self.create_account(account_name, balance)

        response = self.get_accounts()

        accounts = json.loads(response.data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(accounts), 1)

    def test_create_account(self):
        account_name = 'test_account'
        balance = 95.50

        response = self.post_create_account(account_name, balance)

        self.assertEqual(response.status_code, 200)

        account = json.loads(response.data)
        self.assertEqual(account['name'], account_name)
        self.assertEqual(account['balance'], balance)

        response = self.get_accounts()

        accounts = json.loads(response.data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(accounts), 1)

    def test_create_account_400_missing_name(self):
        balance = 95.50

        data = {'balance': balance}
        return self.client.post('/accounts',
                                data=json.dumps(data),
                                mimetype='application/json')

        self.assertEqual(response.status_code, 400)

    def test_create_account_400_missing_balance(self):
        data = {'name': 'test_name'}
        return self.client.post('/accounts',
                                data=json.dumps(data),
                                mimetype='application/json')

        self.assertEqual(response.status_code, 400)

    def test_get_account(self):
        name = 'test_name'
        balance = 50.65
        account = self.create_account(name, balance)

        response = self.get_account(account.account_id)

        self.assertEqual(response.status_code, 200)

        account_dict = json.loads(response.data)
        self.assertEqual(account_dict['name'], name)
        self.assertEqual(account_dict['balance'], balance)

    def test_get_account_404_path_not_found(self):
        response = self.get_account('not_a_uuid')

        self.assertEqual(response.status_code, 404)

    def test_get_account_404_account_not_found(self):
        response = self.get_account(uuid4())

        self.assertEqual(response.status_code, 404)

    def test_get_account_balance(self):
        name = 'test_name'
        balance = 50.65
        account = self.create_account(name, balance)
        today = date.today()

        response = self.get_account_balance(account.account_id, today)

        self.assertEqual(response.status_code, 200)

        account_dict = json.loads(response.data)
        self.assertEqual(account_dict['balance'], balance)

    def test_get_account_balance_404_path_not_found(self):
        today = date.today()

        response = self.get_account_balance('not_uuid', today)

        self.assertEqual(response.status_code, 404)

    def test_get_account_balance_404_account_not_found(self):
        today = date.today()

        response = self.get_account_balance(uuid4(), today)

        self.assertEqual(response.status_code, 404)

    def test_get_account_balance_with_transactions(self):
        name = 'test_name'
        balance = 50.65
        account = self.create_account(name, balance)

        single = 'test_single'
        single_amount = 100.75
        single_date = date.today() + timedelta(days=14)
        create_single_transaction(account, single, single_date, single_amount)
        self.session.commit()

        recurring = 'test_recurring'
        recurring_amount = 50.50
        recurring_date = date.today()
        recurring_timespan = Timespan(0, 2, 0, 0)
        create_recurring_transaction(account, recurring, recurring_date,
                                     recurring_timespan, recurring_amount)
        self.session.commit()

        one_week = date.today() + timedelta(days=7)
        four_weeks = date.today() + timedelta(days=28)

        first_expected = float(account.balance) + recurring_amount
        second_expected = float(
            account.balance) + single_amount + (recurring_amount * 3)

        response_first = self.get_account_balance(account.account_id, one_week)
        self.assertEqual(response_first.status_code, 200)

        response_second = self.get_account_balance(account.account_id,
                                                   four_weeks)
        self.assertEqual(response_second.status_code, 200)

        first_balance = json.loads(response_first.data)
        second_balance = json.loads(response_second.data)
        self.assertEqual(first_balance['balance'], first_expected)
        self.assertEqual(second_balance['balance'], second_expected)

    def test_get_account_balance_401_on_not_logged_in(self):
        name = 'test_name'
        balance = 50.65
        account = self.create_account(name, balance)
        today = date.today()

        self.get_logout()
        response = self.get_account_balance(account.account_id, today)

        self.assertEqual(response.status_code, 401)
コード例 #8
0
ファイル: test_route_auth.py プロジェクト: tkardach/Finance
class TestRouteAuth(unittest.TestCase):
    test_email = '*****@*****.**'
    test_pass = '******'
    test_user = None
    session = None

    def create_test_user(self):
        self.test_user = create_user(session=self.session,
                                     email=self.test_email,
                                     password=self.test_pass)
        self.session.commit()

    def post_login(self, email: str, password: str):
        test_login = {'email': email, 'password': password}
        return self.client.post('/login',
                                data=json.dumps(test_login),
                                content_type='application/json')

    def get_logout(self):
        return self.client.get('/logout')

    def post_signup(self, email: str, password: str):
        test_signup = {'email': email, 'password': password}
        return self.client.post('/signup',
                                data=json.dumps(test_signup),
                                content_type='application/json')

    def delete_all_rows(self):
        self.session.query(User).delete()
        self.session.commit()

    def setUp(self):
        self.session = SessionLocal()
        self.delete_all_rows()
        self.client = app.test_client()

    def tearDown(self):
        self.delete_all_rows()
        self.session.close()

    def test_login_path(self):
        self.create_test_user()
        response = self.post_login(self.test_email, self.test_pass)
        self.assertEqual(response.status_code, 200)

    def test_login_400_on_missing_email(self):
        test_login = {'password': self.test_pass}
        response = self.client.post('/login',
                                    data=json.dumps(test_login),
                                    content_type='application/json')

        self.assertEqual(response.status_code, 400)

    def test_login_400_on_missing_password(self):
        test_login = {'email': self.test_email}
        response = self.client.post('/login',
                                    data=json.dumps(test_login),
                                    content_type='application/json')

        self.assertEqual(response.status_code, 400)

    def test_login_path_400_on_invalid_email(self):
        response = self.post_login('invalid.com', 'wrong')

        self.assertEqual(response.status_code, 400)

    def test_login_path_401_on_bad_password(self):
        self.create_test_user()
        response = self.post_login(self.test_email, 'wrong')

        self.assertEqual(response.status_code, 401)

    def test_login_path_401_on_wrong_email(self):
        self.create_test_user()
        response = self.post_login('*****@*****.**', self.test_pass)

        self.assertEqual(response.status_code, 401)

    def test_login_path_401_on_no_user(self):
        response = self.post_login(self.test_email, self.test_pass)

        self.assertEqual(response.status_code, 401)

    def test_login_path_400_on_bad_mimetype(self):
        response = self.client.post('/login',
                                    data='{"email":"%s","password":"******"}' %
                                    (self.test_email, self.test_pass),
                                    content_type='text/plain')

        self.assertEqual(response.status_code, 400)

    def test_signup(self):
        response = self.post_signup(self.test_email, self.test_pass)

        users = self.session.query(User).all()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(users), 1)

        user = users[0]
        self.assertEqual(user.email, self.test_email)

    def test_signup_400_on_user_exists(self):
        self.create_test_user()
        response = self.post_signup(self.test_email, self.test_pass)

        self.assertEqual(response.status_code, 400)

    def test_signup_400_on_missing_email(self):
        test_signup = {'password': self.test_pass}
        response = self.client.post('/signup',
                                    data=json.dumps(test_signup),
                                    content_type='application/json')

        self.assertEqual(response.status_code, 400)

    def test_signup_400_on_missing_password(self):
        test_signup = {'email': self.test_email}
        response = self.client.post('/signup',
                                    data=json.dumps(test_signup),
                                    content_type='application/json')

        self.assertEqual(response.status_code, 400)

    def test_signup_400_on_invalid_email(self):
        response = self.post_signup('email.com', self.test_pass)

        self.assertEqual(response.status_code, 400)

    def test_logout(self):
        self.create_test_user()
        response = self.post_login(self.test_email, self.test_pass)
        self.assertEqual(response.status_code, 200)

        response = self.get_logout()
        self.assertEqual(response.status_code, 200)

    def test_logout_401_on_user_not_logged_in(self):
        response = self.get_logout()
        self.assertEqual(response.status_code, 401)
コード例 #9
0
class TestORMTransactions(unittest.TestCase):
    session = None
    # User info
    test_email = '*****@*****.**'
    test_email_2 = '*****@*****.**'
    test_password = '******'
    test_user = None
    test_user_2 = None

    # Account Info
    test_account_name = 'test_account'
    test_balance_1 = 100.50
    test_balance_2 = 205.85
    test_account_1 = None
    test_account_2 = None

    # Transaction Info
    test_transaction_1 = 'account_1_trans'
    test_transaction_2 = 'account_2_trans'
    test_date_1 = date.today() + relativedelta(weeks=+1)
    test_date_2 = date.today() + relativedelta(weeks=+3)
    test_date_3 = date.today() + relativedelta(months=+6)
    test_amount_1 = 500.33
    test_amount_2 = -200.23
    test_amount_3 = -1000.50
    test_amount_single = 35.75
    biweekly = Timespan(0, 2, 0, 0)
    monthly = Timespan(0, 0, 1, 0)
    yearly = Timespan(0, 0, 0, 1)

    def delete_all_rows(self):
        self.session.query(SingleTransaction).delete()
        self.session.query(RecurringTransaction).delete()
        self.session.query(Account).delete()
        self.session.query(User).delete()
        self.session.commit()

    def setUp(self):
        self.session = SessionLocal()
        self.delete_all_rows()
        self.test_user = create_user(self.session, self.test_email,
                                     self.test_password)
        self.test_user_2 = create_user(self.session, self.test_email_2,
                                       self.test_password)
        self.test_account_1 = create_account(self.test_account_name,
                                             self.test_user,
                                             self.test_balance_1)
        self.test_account_2 = create_account(self.test_account_name,
                                             self.test_user_2,
                                             self.test_balance_2)
        self.session.commit()

    def tearDown(self):
        self.delete_all_rows()
        self.session.close()

    def test_get_account_balance_on_date(self):
        start_date = date.today()
        raises = False
        try:
            create_recurring_transaction(account=self.test_account_1,
                                         name=self.test_transaction_1,
                                         start_date=start_date,
                                         timespan=self.biweekly,
                                         amount=self.test_amount_1)
            create_recurring_transaction(account=self.test_account_1,
                                         name=self.test_transaction_1,
                                         start_date=start_date,
                                         timespan=self.monthly,
                                         amount=self.test_amount_2)
            create_recurring_transaction(account=self.test_account_1,
                                         name=self.test_transaction_1,
                                         start_date=start_date,
                                         timespan=self.yearly,
                                         amount=self.test_amount_3)
            create_single_transaction(account=self.test_account_1,
                                      name=self.test_transaction_1,
                                      date=self.test_date_1,
                                      amount=self.test_amount_single)
            create_single_transaction(account=self.test_account_1,
                                      name=self.test_transaction_1,
                                      date=self.test_date_2,
                                      amount=self.test_amount_single)
            create_single_transaction(account=self.test_account_1,
                                      name=self.test_transaction_1,
                                      date=self.test_date_3,
                                      amount=self.test_amount_single)
            self.session.commit()
            date_1 = start_date + relativedelta(weeks=+2)
            date_2 = start_date + relativedelta(months=+1)
            date_3 = start_date + relativedelta(years=+1)
            sum_1 = get_account_balance_on_date(self.test_account_1, date_1)
            sum_2 = get_account_balance_on_date(self.test_account_1, date_2)
            sum_3 = get_account_balance_on_date(self.test_account_1, date_3)
        except:
            raises = True

        balance = float(self.test_account_1.balance)
        # recurring transactions charge on the date of creation
        # base_sum is the sum of all transactions for their initial state
        base_sum = balance + self.test_amount_1 + self.test_amount_2 + self.test_amount_3
        # in 3 weeks we expect the biweekly payout to be 1x
        expected_sum_1 = base_sum + self.test_amount_1 + self.test_amount_single
        # in 1 month we expect
        #   monthly payout to be 1x
        #   biweekly payout to be 2x
        expected_sum_2 = base_sum + \
            (self.test_amount_1 * 2) + \
            self.test_amount_2 + (self.test_amount_single * 2)
        # in 1 year we expect
        #   monthly payout to be 12x
        #   biweekly payout to be 26
        #   yearly payout to be 1
        expected_sum_3 = base_sum + (self.test_amount_1 * 26) + \
            (self.test_amount_2 * 12) + \
            self.test_amount_3 + (self.test_amount_single * 3)
        expected_sum_1 = round(expected_sum_1, 2)
        expected_sum_2 = round(expected_sum_2, 2)
        expected_sum_3 = round(expected_sum_3, 2)
        self.assertFalse(raises)
        self.assertEqual(self.test_account_1.recurring_transactions.count(), 3)
        self.assertEqual(self.test_account_1.single_transactions.count(), 3)
        self.assertEqual(expected_sum_1, sum_1)
        self.assertEqual(expected_sum_2, sum_2)
        self.assertEqual(expected_sum_3, sum_3)
コード例 #10
0
class TestORMUser(unittest.TestCase):
  session = None
  test_name = 'test_user'
  test_email = '*****@*****.**'
  test_is_admin = True
  test_password = '******'

  def delete_all_rows(self):
    self.session.query(User).delete()
    self.session.commit()


  def setUp(self):
    self.session = SessionLocal()
    self.delete_all_rows()
  
  def tearDown(self):
    self.delete_all_rows()
    self.session.close()

  def test_create_user(self):
    raises = False
    try:
      prof = create_user(
        self.session,
        self.test_email,
        self.test_password,
        self.test_is_admin
      )
      self.session.commit()
    except:
      self.session.rollback()
      raises = True

    self.assertFalse(raises)
    self.assertEqual(len(self.session.query(User).all()), 1)
    self.assertEqual(prof.email, self.test_email)
    self.assertEqual(prof.is_admin, self.test_is_admin)
    self.assertTrue(check_hashed_string(self.test_password, prof.password))


  def test_get_users(self):
    new_user_2 = '*****@*****.**'
    raises = False
    try:
      prof1 = create_user(
        self.session,
        self.test_email,
        self.test_password,
        self.test_is_admin
      )
      prof2 = create_user(
        self.session,
        new_user_2,
        self.test_password,
        self.test_is_admin
      )
      self.session.commit()
      profs = get_users(self.session)
    except:
      raises = True

    self.assertFalse(raises)
    self.assertEqual(len(profs), 2)
    self.assertTrue(prof1 in profs)
    self.assertTrue(prof2 in profs)


  def test_get_user_by_email(self):
    raises = False
    try:
      created_prof = create_user(
        self.session,
        self.test_email,
        self.test_password,
        self.test_is_admin
      )
      self.session.commit()
      queried_prof = get_user_by_email(self.session, self.test_email)
    except:
      raises = True

    self.assertFalse(raises)
    self.assertEqual(created_prof, queried_prof)


  def test_get_user_by_id(self):
    raises = False
    try:
      created_prof = create_user(
        self.session,
        self.test_email,
        self.test_password,
        self.test_is_admin
      )
      self.session.commit()
      by_email = get_user_by_email(self.session, self.test_email)
      queried_prof = get_user_by_id(self.session, by_email.user_id)
    except:
      raises = True

    self.assertFalse(raises)
    self.assertEqual(created_prof, queried_prof)
コード例 #11
0
 def setUp(self):
   self.session = SessionLocal()
   self.delete_all_rows()
コード例 #12
0
class TestORMSingleTransactions(unittest.TestCase):
    session = None
    #User info
    test_email = '*****@*****.**'
    test_email_2 = '*****@*****.**'
    test_password = '******'
    test_user = None
    test_user_2 = None

    #Account Info
    test_account_name = 'test_account'
    test_balance_1 = 100.50
    test_balance_2 = 205.85
    test_account_1 = None
    test_account_2 = None

    #Transaction Info
    test_transaction_1 = 'account_1_trans'
    test_transaction_2 = 'account_2_trans'
    test_date_1 = date.today() + timedelta(days=1)
    test_date_2 = date.today() + timedelta(days=2)
    test_amount_1 = 500
    test_amount_2 = -200

    def delete_all_rows(self):
        self.session.query(SingleTransaction).delete()
        self.session.query(Account).delete()
        self.session.query(User).delete()
        self.session.commit()

    def setUp(self):
        self.session = SessionLocal()
        self.delete_all_rows()
        self.test_user = create_user(self.session, self.test_email,
                                     self.test_password)
        self.test_user_2 = create_user(self.session, self.test_email_2,
                                       self.test_password)
        self.test_account_1 = create_account(self.test_account_name,
                                             self.test_user,
                                             self.test_balance_1)
        self.test_account_2 = create_account(self.test_account_name,
                                             self.test_user_2,
                                             self.test_balance_2)
        self.session.commit()

    def tearDown(self):
        self.delete_all_rows()
        self.session.close()

    def test_create_single_transaction(self):
        raises = False
        try:
            trans = create_single_transaction(account=self.test_account_1,
                                              name=self.test_transaction_1,
                                              date=self.test_date_1,
                                              amount=self.test_amount_1)
            self.session.commit()
        except:
            raises = True

        self.assertFalse(raises)
        self.assertEqual(len(self.session.query(SingleTransaction).all()), 1)
        self.assertEqual(self.test_account_1.single_transactions.count(), 1)
        self.assertEqual(self.test_account_2.single_transactions.count(), 0)
        self.assertEqual(trans.account, self.test_account_1)
        self.assertEqual(trans.name, self.test_transaction_1)
        self.assertEqual(trans.date.date(), self.test_date_1)
        self.assertEqual(trans.amount, self.test_amount_1)
        self.assertEqual(self.test_account_1.single_transactions[0], trans)

    def test_get_all_single_transactions_on_date(self):
        raises = False
        try:
            create_single_transaction(account=self.test_account_1,
                                      name=self.test_transaction_1,
                                      date=self.test_date_1,
                                      amount=self.test_amount_1)
            create_single_transaction(account=self.test_account_1,
                                      name=self.test_transaction_1,
                                      date=self.test_date_2,
                                      amount=self.test_amount_2)
            self.session.commit()
            transactions_1 = get_all_single_transactions_on_date(
                self.test_account_1, self.test_date_1)
            transactions_2 = get_all_single_transactions_on_date(
                self.test_account_1, self.test_date_2)
            transactions_3 = get_all_single_transactions_on_date(
                self.test_account_2, self.test_date_2)
        except:
            raises = True

        self.assertFalse(raises)
        self.assertEqual(self.test_account_1.single_transactions.count(), 2)
        self.assertEqual(len(transactions_1), 1)
        self.assertEqual(len(transactions_2), 2)
        self.assertEqual(len(transactions_3), 0)

    def test_get_single_transaction_sum_on_date(self):
        raises = False
        try:
            create_single_transaction(account=self.test_account_1,
                                      name=self.test_transaction_1,
                                      date=self.test_date_1,
                                      amount=self.test_amount_1)
            create_single_transaction(account=self.test_account_1,
                                      name=self.test_transaction_1,
                                      date=self.test_date_2,
                                      amount=self.test_amount_2)
            self.session.commit()
            sum_1 = get_single_transaction_sum_on_date(self.test_account_1,
                                                       self.test_date_1)
            sum_2 = get_single_transaction_sum_on_date(self.test_account_1,
                                                       self.test_date_2)
            sum_3 = get_single_transaction_sum_on_date(self.test_account_2,
                                                       self.test_date_1)
            expected_sum_1 = self.test_amount_1
            expected_sum_2 = self.test_amount_1 + self.test_amount_2
            expected_sum_3 = 0
        except:
            raises = True

        self.assertFalse(raises)
        self.assertEqual(self.test_account_1.single_transactions.count(), 2)
        self.assertEqual(sum_1, expected_sum_1)
        self.assertEqual(sum_2, expected_sum_2)
        self.assertEqual(sum_3, expected_sum_3)
コード例 #13
0
class TestRouteTransaction(unittest.TestCase):
    test_email = '*****@*****.**'
    test_pass = '******'
    test_account_name = 'test_account'
    test_account_id = None
    test_balance = 150.50
    test_user = None
    session = None

    test_trans_name = 'transaction_name'
    test_trans_amount = 50.75
    test_trans_date = date.today()
    test_trans_timespan = Timespan(0, 2, 0, 0)

    def create_account(self):
        self.test_account = create_account(
            name=self.test_account_name, user=self.test_user, balance=self.test_balance)
        self.session.commit()
        self.test_account_id = self.test_account.account_id

    def create_test_user(self):
        self.test_user = create_user(
            session=self.session, email=self.test_email, password=self.test_pass)
        self.session.commit()

    def post_login(self, email: str, password: str):
        test_login = {
            'email': email,
            'password': password
        }
        return self.client.post(
            '/login',
            data=json.dumps(test_login),
            content_type='application/json')

    def get_logout(self):
        return self.client.get('/logout')

    def post_create_single_transaction(self, name: str, amount: float, date: date, account_id: str):
        data = {
            'name': name,
            'amount': amount,
            'date': date.strftime('%m-%d-%Y'),
            'account_id': account_id
        }
        return self.client.post(
            '/single-transactions',
            data=json.dumps(data),
            mimetype='application/json')

    def get_single_transaction(self, account_id: str):
        return self.client.get('/single-transactions/%s' % account_id)

    def post_create_recurring_transaction(
            self, name: str, amount: float, start_date: date, account_id: str, timespan: Timespan):
        data = {
            'name': name,
            'amount': amount,
            'start_date': start_date.strftime('%m-%d-%Y'),
            'account_id': account_id,
            'timespan': timespan.to_timespan_str()
        }
        return self.client.post(
            '/recurring-transactions',
            data=json.dumps(data),
            mimetype='application/json')

    def get_recurring_transaction(self, account_id: str):
        return self.client.get('/recurring-transactions/%s' % account_id)

    def delete_all_rows(self):
        self.session.query(RecurringTransaction).delete()
        self.session.query(SingleTransaction).delete()
        self.session.query(Account).delete()
        self.session.query(User).delete()
        self.session.commit()

    def setUp(self):
        self.session = SessionLocal()
        self.delete_all_rows()
        self.create_test_user()
        self.create_account()
        self.client = app.test_client()
        self.post_login(self.test_email, self.test_pass)

    def tearDown(self):
        self.delete_all_rows()
        self.session.close()

    def test_create_single_transaction(self):
        response = self.post_create_single_transaction(
            self.test_trans_name, self.test_trans_amount, self.test_trans_date, self.test_account_id)

        trans = json.loads(response.data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(trans['name'], self.test_trans_name)
        self.assertEqual(trans['amount'], self.test_trans_amount)
        self.assertEqual(parser.parse(
            trans['date']).date(), self.test_trans_date)

    def test_create_single_transaction_404_on_no_account(self):
        response = self.post_create_single_transaction(
            self.test_trans_name, self.test_trans_amount, self.test_trans_date, 'doesnotexist')

        self.assertEqual(response.status_code, 404)

    def test_create_single_transaction_401_on_not_logged_in(self):
        self.get_logout()
        response = self.post_create_single_transaction(
            self.test_trans_name, self.test_trans_amount, self.test_trans_date, self.test_account_id)

        self.assertEqual(response.status_code, 401)

    def test_get_single_transactions(self):
        create_single_transaction(
            account=self.test_account,
            name=self.test_trans_name,
            date=self.test_trans_date,
            amount=self.test_trans_amount
        )
        create_single_transaction(
            account=self.test_account,
            name=self.test_trans_name,
            date=self.test_trans_date,
            amount=self.test_trans_amount
        )
        self.session.commit()
        response = self.get_single_transaction(self.test_account_id)

        self.assertEqual(response.status_code, 200)

        transactions = json.loads(response.data)
        self.assertEqual(len(transactions), 2)

    def test_get_single_transactions_404_on_no_account(self):
        self.session.commit()
        response = self.get_single_transaction('does_not_exist')

        self.assertEqual(response.status_code, 404)

    def test_get_recurring_transactions_401_on_user_not_logged_in(self):
        self.session.commit()
        self.get_logout()
        response = self.get_single_transaction(self.test_account_id)

        self.assertEqual(response.status_code, 401)

    def test_create_recurring_transaction(self):
        response = self.post_create_recurring_transaction(
            self.test_trans_name, self.test_trans_amount, self.test_trans_date, 
            self.test_account_id, self.test_trans_timespan)

        trans = json.loads(response.data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(trans['name'], self.test_trans_name)
        self.assertEqual(trans['amount'], self.test_trans_amount)
        self.assertEqual(parser.parse(
            trans['start_date']).date(), self.test_trans_date)
        self.assertEqual(trans['timespan'], self.test_trans_timespan.to_timespan_str())

    def test_create_recurring_transaction_404_on_no_account(self):
        response = self.post_create_recurring_transaction(
            self.test_trans_name, self.test_trans_amount, self.test_trans_date, 
            'doesnotexist', self.test_trans_timespan)

        self.assertEqual(response.status_code, 404)

    def test_create_recurring_transaction_401_on_not_logged_in(self):
        self.get_logout()
        response = self.post_create_recurring_transaction(
            self.test_trans_name, self.test_trans_amount, self.test_trans_date, 
            self.test_account_id, self.test_trans_timespan)

        self.assertEqual(response.status_code, 401)

    def test_create_recurring_transaction_400_on_invalid_timespan(self):
        data = {
            'name': self.test_trans_name,
            'amount': self.test_trans_amount,
            'start_date': self.test_trans_date.strftime('%m-%d-%Y'),
            'account_id': self.test_account_id,
            'timespan': 'invalid'
        }
        response = self.client.post(
            '/recurring-transactions',
            data=json.dumps(data),
            mimetype='application/json')

        self.assertEqual(response.status_code, 400)

    def test_get_recurring_transactions(self):
        create_recurring_transaction(
            account=self.test_account,
            name=self.test_trans_name, 
            amount=self.test_trans_amount, 
            start_date=self.test_trans_date, 
            timespan=self.test_trans_timespan)
        create_recurring_transaction(
            account=self.test_account,
            name=self.test_trans_name, 
            amount=self.test_trans_amount, 
            start_date=self.test_trans_date, 
            timespan=self.test_trans_timespan)
        self.session.commit()
        
        response = self.get_recurring_transaction(self.test_account_id)

        self.assertEqual(response.status_code, 200)

        transactions = json.loads(response.data)
        self.assertEqual(len(transactions), 2)

    def test_get_recurring_transactions_404_on_no_account(self):
        self.session.commit()
        response = self.get_recurring_transaction('does_not_exist')

        self.assertEqual(response.status_code, 404)

    def test_get_recurring_transactions_401_on_user_not_logged_in(self):
        self.session.commit()
        self.get_logout()
        response = self.get_recurring_transaction(self.test_account_id)

        self.assertEqual(response.status_code, 401)