Пример #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
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)
Пример #3
0
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)
Пример #4
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)
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)