def test_set_limit(client): with set_current_user(): post_limit = lambda data : client.post('/budgeto/save-category-options', data=data) # Test add limit data = {'cat': '36', 'setLimit':'on', 'limit': '12334'} response = post_limit(data) assert response.status_code == 200 limit = Limit.get(user=g.user, category_id=data['cat']) assert limit.value == float(data['limit']) # Test wrong data response = post_limit({'cat': '36', 'limit': '-145'}) assert response.status_code == 200 response = post_limit({'cat': '36', 'setLimit':'on'}) assert response.status_code == 200 response = post_limit({'cat': '36', 'setLimit':'on', 'limit': '-145'}) assert response.status_code == 200 response = post_limit({'cat': '36', 'setLimit':'on', 'limit': ''}) assert response.status_code == 200 assert Limit.get(user=g.user, category_id=data['cat']) is None # Test removing limit response = post_limit({'cat': '36', 'setLimit':'off', 'limit': ''}) assert response.status_code == 200 assert Limit.get(user=g.user, category_id=data['cat']) is None
def test_validate_token(client): with set_current_user(): response = client.get('/budgeto/api/validate-token') assert response.status_code == 200 user = json.loads(response.data.decode('utf-8')) assert user['user_id'] == g.user.user_id assert user['is_admin'] == g.user.is_admin
def test_get_routes(client): with set_current_user(): for route in app.url_map.iter_rules(): if 'GET' in route.methods and route.endpoint != 'static': req = client.get(route.rule) print("Route to {0} validated with status '{1}'".format( route.rule, req.status)) assert req.status_code < 400
def fetch_transaction(client): # TODO: Make it work. with set_current_user(): data = {} with open("tests/resources/test_transactions.txt", 'r') as file: data['transactions'] = file.read() response = client.post('/budgeto/fetch-transactions', data=data) assert response.status_code == 200
def test_get_all_transactions_hierarchically(client): def assertion(transactions): assert len(transactions) == 3 assert len(transactions[1]['children']) > 0 assert transactions[2]['count'] > 0 assert type(transactions[3]['transactions']) is list with set_current_user(): t = TimeCalculator('transactions_hierarchically') assertion(Transaction.get_all_hierarchically(User.get(user_id=1))) t.get_running_time()
def test_add_transaction(client): with set_current_user(): data = {'desc': 'Test de transaction', 'amount': '10.55', 'date': date.today().strftime("%d-%m-%Y"), 'bank': '2', 'cat': '2'} response = client.post('/budgeto/add-transaction', data=data) assert response.status_code == 201 transaction = Transaction.get_latest(Transaction.upload_time) assert transaction.user == g.user assert transaction.bank_id == int(data['bank']) assert transaction.description == data['desc'] assert transaction.category_id == int(data['cat']) assert transaction.amount == float(data['amount']) assert transaction.date.strftime("%d-%m-%Y") == data['date']
def test_put_transaction(client, statements): with set_current_user(): for statement in statements: with codecs.open(statement['json'], 'r', encoding='utf8') as f: json_data = json.loads(f.read()) added_transactions = put_transactions(json_data, g.user) assert added_transactions == len(json_data['transactions']) transactions = Transaction.get_latest(Transaction.transaction_id, added_transactions) for i in range(0, added_transactions): inverse = added_transactions - i - 1 assert transactions[i].description == json_data['transactions'][inverse]['description'] assert transactions[i].amount == json_data['transactions'][inverse]['amount'] assert str(transactions[i].date) == json_data['transactions'][inverse]['date']
def test_transaction_fetcher(client): with set_current_user(): post = lambda json_data : client.post('/budgeto/transactions-fetcher', data={'transactions': json.dumps(json_data)}) truncate = lambda data, max: (data[:max - 3] + '...') if len(data) > max - 3 else data def validate_transaction(resp_t, json_t): print(json_t) assert resp_t.description == truncate(json_t['desc'], 256) assert resp_t.amount == float(json_t['amount'].replace(',','.') or 0) assert resp_t.date.strftime("%d-%m-%Y") == json_t['date'] or datetime.today() assert resp_t.bank.name == json_t['bank'] with open("tests/resources/test_transactions_limit_cases.txt", 'r') as file: json_data = json.loads(file.read()) response = post(json_data) assert response.status_code == 200 fetched_transactions = json.loads(response.data)['count'] transactions = Transaction.get_latest(Transaction.transaction_id, fetched_transactions) assert fetched_transactions == 12 # @see test_transactions_limit_cases.txt response_dict = {"":0, "bank":0, "date":0, "amount":0, "desc":0} for i in range(fetched_transactions): t = json_data[i] # Normal transaction validation validate_transaction(transactions[fetched_transactions-i-1], t) # Counting the number of success fetch response_dict[t['test']] += 1 # Assertion of working transactions assert response_dict[""] == 2 assert response_dict["desc"] == 1 assert response_dict["bank"] == 4 assert response_dict["date"] == 2 assert response_dict["amount"] == 3 # Testing duplicate values with one new value new_data = [json_data[0]] working_transaction = {"desc":"Working transaction","date":"23-01-2017","amount":"100.0","bank":"Tangerine"} new_data.append(working_transaction) response = post(new_data) assert response.status_code == 200 fetched_transactions = json.loads(response.data)['count'] assert fetched_transactions == 1 # @see test_transactions_limit_cases.txt transaction = Transaction.get_latest(Transaction.transaction_id) validate_transaction(transaction, working_transaction)
def test_get_transactions(client): def json_verifier(response, number_of_transactions): data = json.loads(response.data.decode('utf-8')) assert len(data.get('transactions', [])) == number_of_transactions with set_current_user(): response = client.get('/budgeto/api/transactions') assert response.status_code == 200 json_verifier(response, 0) add_to_db(Transaction(), description='Test de transaction', amount=10.55, date=date.today(), bank_id=2, user_id=g.user.user_id) response = client.get('/budgeto/api/transactions') assert response.status_code == 200 json_verifier(response, 1)