refactor a little

This commit is contained in:
2022-05-27 23:11:18 +12:00
parent 88f161082d
commit d57798a9c0
2 changed files with 61 additions and 64 deletions

View File

@@ -39,8 +39,7 @@ class Client(object):
self.currency_rate = currency_rate self.currency_rate = currency_rate
def call(self, method="get", endpoint=None, def call(self, method="get", endpoint=None,
**params) -> dict(): **params) -> dict:
response = None
response = requests.request(method=method, response = requests.request(method=method,
url=self.url + "/" + endpoint, url=self.url + "/" + endpoint,
@@ -84,7 +83,7 @@ class Client(object):
return data['data'] return data['data']
def create_transaction(self, def create_transaction(self,
type='income', # Payment method type transaction_type='income', # Payment method type
account_id=None, # Account ID to assign account_id=None, # Account ID to assign
category_id=None, # Category ID to assign category_id=None, # Category ID to assign
contact_id=None, # Contact ID/Client to assign contact_id=None, # Contact ID/Client to assign
@@ -107,8 +106,8 @@ class Client(object):
data = self.call(endpoint="transactions", data = self.call(endpoint="transactions",
method="POST", method="POST",
search="type:" + type, search="type:" + transaction_type,
type=type, type=transaction_type,
account_id=account_id, account_id=account_id,
category_id=category_id, category_id=category_id,
paid_at=paid_at, paid_at=paid_at,

View File

@@ -1,27 +1,27 @@
import sys import sys
from this import d
sys.path.insert(1, '.') sys.path.insert(1, '.')
import pytest from helpers import RetrieveJSONFromFile
import akauntingpy import akauntingpy
from akauntingpy.exceptions import * from akauntingpy.exceptions import *
from helpers import RetrieveJSONFromFile import pytest
from requests.auth import HTTPBasicAuth from requests.auth import HTTPBasicAuth
class TestAPI: class TestAPI:
@pytest.fixture() @pytest.fixture()
def setUp(self): def setUp(self):
c = akauntingpy.Client("https://akaunting.guise.net.nz/api", c = akauntingpy.Client("https://akaunting.guise.net.nz/api",
"aaron@guise.net.nz", "aaron@guise.net.nz",
"L3Tm31N0w", "L3Tm31N0w",
1) 1)
yield c yield c
@pytest.fixture() @pytest.fixture()
def setUpFailed(self): def setUpFailed(self):
c = akauntingpy.Client("https://akaunting.guise.net.nz/api", c = akauntingpy.Client("https://akaunting.guise.net.nz/api",
"aaron@guise.net.nz", "aaron@guise.net.nz",
"L3Tm31N0w1", "L3Tm31N0w1",
1) 1)
yield c yield c
def test_init(self, setUp): def test_init(self, setUp):
@@ -31,21 +31,20 @@ class TestAPI:
assert isinstance(c.authentication, HTTPBasicAuth) assert isinstance(c.authentication, HTTPBasicAuth)
def test_ping_success(self, setUp, requests_mock): def test_ping_success(self, setUp, requests_mock):
c = setUp c = setUp
requests_mock.get(c.url + '/ping', requests_mock.get(c.url + '/ping',
json=RetrieveJSONFromFile("data/pingSuccess.json"), json=RetrieveJSONFromFile("data/pingSuccess.json"),
status_code=200) status_code=200)
data = c.ping() data = c.ping()
# The returned data should have status = ok # The returned data should have status = ok
assert data['status'] == 'ok' assert data['status'] == 'ok'
def test_ping_failed(self, setUpFailed, requests_mock): def test_ping_failed(self, setUpFailed, requests_mock):
c = setUpFailed c = setUpFailed
requests_mock.get(c.url + '/ping', requests_mock.get(c.url + '/ping',
json=RetrieveJSONFromFile("data/pingFailure.json"), json=RetrieveJSONFromFile("data/pingFailure.json"),
status_code=401) status_code=401)
with pytest.raises(MissingPermission, match="Invalid credentials."): with pytest.raises(MissingPermission, match="Invalid credentials."):
data = c.ping() data = c.ping()
assert data['status_code'] == 401 assert data['status_code'] == 401
@@ -59,7 +58,7 @@ class TestAPI:
c = setUp c = setUp
requests_mock.get(c.url + "/accounts?search=number%3A38-9011-0510023-03&params=page&params=limit&company_id=1", requests_mock.get(c.url + "/accounts?search=number%3A38-9011-0510023-03&params=page&params=limit&company_id=1",
json=RetrieveJSONFromFile("data/GetAccountsSearch.json")) json=RetrieveJSONFromFile("data/GetAccountsSearch.json"))
data = c.get_accounts(search="number:38-9011-0510023-03",params={'page': 1, 'limit': 200}) data = c.get_accounts(search="number:38-9011-0510023-03", params={'page': 1, 'limit': 200})
def test_get_account_search_not_found(self, setUp, requests_mock): def test_get_account_search_not_found(self, setUp, requests_mock):
c = setUp c = setUp
@@ -69,22 +68,22 @@ class TestAPI:
data = c.get_accounts(search="number:arandomvalue") data = c.get_accounts(search="number:arandomvalue")
def test_create_transaction_income_success(self, setUp, requests_mock, def test_create_transaction_income_success(self, setUp, requests_mock,
type='income', # Payment method type transaction_type='income', # Payment method type
account_id=2, # Account ID to assign account_id=2, # Account ID to assign
category_id=3, # Category ID to assign category_id=3, # Category ID to assign
contact_id=None, # Contact ID/Client to assign contact_id=None, # Contact ID/Client to assign
paid_at="2022-05-15", # Date of expense/transfer or income paid_at="2022-05-15", # Date of expense/transfer or income
reference=None, # Reference for the payment reference=None, # Reference for the payment
payment_method="bank_transfer", # Payment method payment_method="bank_transfer", # Payment method
currency_code="NZD", # Currency code currency_code="NZD", # Currency code
currency_rate=1, # Currency rate currency_rate=1, # Currency rate
amount="25.00", # Amount received/paid amount="25.00", # Amount received/paid
): ):
c = setUp c = setUp
requests_mock.post(c.url + "/transactions", requests_mock.post(c.url + "/transactions",
json=RetrieveJSONFromFile("data/CreateTransactionIncomeSuccess.json"), json=RetrieveJSONFromFile("data/CreateTransactionIncomeSuccess.json"),
status_code=201) status_code=201)
data = c.create_transaction(type=type, data = c.create_transaction(transaction_type=transaction_type,
account_id=account_id, account_id=account_id,
category_id=category_id, category_id=category_id,
paid_at=paid_at, paid_at=paid_at,
@@ -98,16 +97,16 @@ class TestAPI:
) )
def test_create_transaction_income_failed(self, setUp, requests_mock, def test_create_transaction_income_failed(self, setUp, requests_mock,
contact_id=None, # Contact ID/Client to assign contact_id=None, # Contact ID/Client to assign
reference=None, # Reference for the payment reference=None, # Reference for the payment
currency_code="NZD", # Currency code currency_code="NZD", # Currency code
currency_rate=1, # Currency rate currency_rate=1, # Currency rate
amount="25.00", # Amount received/paid amount="25.00", # Amount received/paid
): ):
c = setUp c = setUp
requests_mock.post(c.url + "/transactions", requests_mock.post(c.url + "/transactions",
json=RetrieveJSONFromFile("data/CreateTransactionIncomeFailed.json"), json=RetrieveJSONFromFile("data/CreateTransactionIncomeFailed.json"),
status_code=422) status_code=422)
with pytest.raises(InvalidData, match="The given data was invalid.*"): with pytest.raises(InvalidData, match="The given data was invalid.*"):
data = c.create_transaction(contact_id=contact_id, data = c.create_transaction(contact_id=contact_id,
reference=reference, reference=reference,
@@ -120,9 +119,9 @@ class TestAPI:
def test_create_transaction_expense_success(self, setUp, requests_mock): def test_create_transaction_expense_success(self, setUp, requests_mock):
c = setUp c = setUp
requests_mock.post(c.url + "/transactions", requests_mock.post(c.url + "/transactions",
json=RetrieveJSONFromFile("data/CreateTransactionExpenseSuccess.json"), json=RetrieveJSONFromFile("data/CreateTransactionExpenseSuccess.json"),
status_code=201) status_code=201)
data = c.create_transaction(type="expense", data = c.create_transaction(transaction_type="expense",
amount=100.00, amount=100.00,
account_id=3, account_id=3,
paid_at="2022-05-16", paid_at="2022-05-16",
@@ -132,4 +131,3 @@ class TestAPI:
category_id="4", category_id="4",
description="Some expenditures" description="Some expenditures"
) )