You've already forked akaunting-py
Basic API Call complete with exceptions/tests
This commit is contained in:
106
tests/api_test.py
Normal file
106
tests/api_test.py
Normal file
@@ -0,0 +1,106 @@
|
||||
import sys
|
||||
from this import d
|
||||
sys.path.insert(1, '.')
|
||||
import pytest
|
||||
import akauntingpy
|
||||
from akauntingpy.exceptions import *
|
||||
from helpers import RetrieveJSONFromFile
|
||||
from requests.auth import HTTPBasicAuth
|
||||
|
||||
class TestAPI:
|
||||
@pytest.fixture()
|
||||
def setUp(self):
|
||||
c = akauntingpy.Client("https://akaunting.guise.net.nz/api",
|
||||
"aaron@guise.net.nz",
|
||||
"L3Tm31N0w",
|
||||
1)
|
||||
yield c
|
||||
|
||||
@pytest.fixture()
|
||||
def setUpFailed(self):
|
||||
c = akauntingpy.Client("https://akaunting.guise.net.nz/api",
|
||||
"aaron@guise.net.nz",
|
||||
"L3Tm31N0w1",
|
||||
1)
|
||||
yield c
|
||||
|
||||
def test_init(self, setUp):
|
||||
c = setUp
|
||||
assert isinstance(c, akauntingpy.Client)
|
||||
assert c.url == "https://akaunting.guise.net.nz/api"
|
||||
assert isinstance(c.authentication, HTTPBasicAuth)
|
||||
|
||||
def test_ping_success(self, setUp, requests_mock):
|
||||
c = setUp
|
||||
requests_mock.get(c.url + '/ping',
|
||||
json=RetrieveJSONFromFile("data/pingSuccess.json"),
|
||||
status_code=200)
|
||||
data = c.ping()
|
||||
# The returned data should have status = ok
|
||||
assert data['status'] == 'ok'
|
||||
|
||||
def test_ping_failed(self, setUpFailed, requests_mock):
|
||||
c = setUpFailed
|
||||
requests_mock.get(c.url + '/ping',
|
||||
json=RetrieveJSONFromFile("data/pingFailure.json"),
|
||||
status_code=401)
|
||||
with pytest.raises(MissingPermission, match="Invalid credentials."):
|
||||
|
||||
data = c.ping()
|
||||
assert data['status_code'] == 401
|
||||
|
||||
def test_get_accounts(self, setUp, requests_mock):
|
||||
c = setUp
|
||||
requests_mock.get(c.url + "/accounts",
|
||||
json=RetrieveJSONFromFile("data/GetAccountsList.json"))
|
||||
data = c.get_accounts(params={'page': 1, 'limit': 200})
|
||||
|
||||
def test_create_transaction_success(self, setUp, requests_mock,
|
||||
type='income', # Payment method type
|
||||
account_id=2, # Account ID to assign
|
||||
category_id=3, # Category ID to assign
|
||||
contact_id=None, # Contact ID/Client to assign
|
||||
paid_at="2022-05-15", # Date of expense/transfer or income
|
||||
reference=None, # Reference for the payment
|
||||
payment_method="bank_transfer", # Payment method
|
||||
currency_code="NZD", # Currency code
|
||||
currency_rate=1, # Currency rate
|
||||
amount="25.00", # Amount received/paid
|
||||
):
|
||||
c = setUp
|
||||
requests_mock.post(c.url + "/transactions",
|
||||
json=RetrieveJSONFromFile("data/CreateTransactionIncomeSuccess.json"),
|
||||
status_code=201)
|
||||
data = c.create_transaction(type=type,
|
||||
account_id=account_id,
|
||||
category_id=category_id,
|
||||
paid_at=paid_at,
|
||||
contact_id=contact_id,
|
||||
payment_method=payment_method,
|
||||
reference=reference,
|
||||
currency_code=currency_code,
|
||||
currency_rate=currency_rate,
|
||||
amount=amount,
|
||||
description="Some description of the transaction"
|
||||
)
|
||||
|
||||
def test_create_transaction_failed(self, setUp, requests_mock,
|
||||
contact_id=None, # Contact ID/Client to assign
|
||||
reference=None, # Reference for the payment
|
||||
currency_code="NZD", # Currency code
|
||||
currency_rate=1, # Currency rate
|
||||
amount="25.00", # Amount received/paid
|
||||
):
|
||||
c = setUp
|
||||
requests_mock.post(c.url + "/transactions",
|
||||
json=RetrieveJSONFromFile("data/CreateTransactionIncomeFailed.json"),
|
||||
status_code=422)
|
||||
with pytest.raises(InvalidData, match="The given data was invalid.*"):
|
||||
data = c.create_transaction(contact_id=contact_id,
|
||||
reference=reference,
|
||||
currency_code=currency_code,
|
||||
currency_rate=currency_rate,
|
||||
amount=amount,
|
||||
description="Some description of the transaction"
|
||||
)
|
||||
|
||||
7
tests/helpers.py
Normal file
7
tests/helpers.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import json
|
||||
|
||||
def RetrieveJSONFromFile(filename):
|
||||
with open(filename) as f:
|
||||
data = json.load(f)
|
||||
|
||||
return data
|
||||
Reference in New Issue
Block a user