Skip to content

Commit a61d573

Browse files
committed
refactoring test code for reusability
1 parent f5a3c68 commit a61d573

File tree

6 files changed

+65
-50
lines changed

6 files changed

+65
-50
lines changed

api/core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def run(self):
9797
app.logger.debug(threading.current_thread().name + ' new audit record')
9898
if is_prod:
9999
task.payload = encryptor.encrypt(task.payload)
100-
task.ip = struct.unpack("!I", socket.inet_aton(task.ip))[0]
100+
task.ip = struct.unpack('!I', socket.inet_aton(task.ip))[0]
101101
db.session.add(task)
102102
self.tasks.clear()
103103
db.session.commit()

api/dal/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class User(db.Model, ModelIter):
4646
audit = relationship('Audit')
4747

4848
def hash_password(self):
49-
self.password = generate_password_hash(str(self.password).encode("ascii"), method='sha256')
49+
self.password = generate_password_hash(str(self.password).encode('ascii'), method='sha256')
5050

5151
def password_correct(self, plain_password):
5252
return check_password_hash(self.password, plain_password)

api/tests/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from datetime import datetime, time
23

34
config = """
45
TESTING = True
@@ -31,3 +32,7 @@ def tear_files():
3132

3233
def endpoint(uri):
3334
return '/v1.0' + uri
35+
36+
37+
def front_end_date(date: datetime = datetime.utcnow(), _time: str = str(time.min)):
38+
return ' '.join([str(date.date()), _time])

api/tests/conftest.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from base64 import b64encode
12
import pytest
2-
from tests import tear_files, init
3+
from tests import tear_files, init, endpoint
4+
from tests.seeders import seed_admin
5+
36

47
@pytest.fixture(scope='module')
58
def client():
@@ -34,3 +37,20 @@ def no_db_client():
3437
with app.test_client() as client:
3538
yield client
3639
tear_files()
40+
41+
42+
@pytest.fixture(scope='module')
43+
def admin_login(client):
44+
45+
admin_resp = seed_admin(client)
46+
assert b'Redirecting...' in admin_resp.data, 'Must redirect upon valid admin creation'
47+
assert admin_resp.status_code == 302
48+
49+
auth = {
50+
'Authorization': 'Basic ' + b64encode(b'[email protected]:' + b'secured').decode()
51+
}
52+
# create a session
53+
login_resp = client.post(endpoint('/login'), headers=auth)
54+
assert 'token' in login_resp.json, 'token expected'
55+
assert login_resp.status_code == 200
56+
return login_resp.json

api/tests/seeders.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from flask.testing import FlaskClient
2-
3-
from tests import endpoint
2+
from tests import endpoint, front_end_date
43

54
admin_sample = {
65
'first_name': 'Test',
@@ -11,9 +10,9 @@
1110

1211
project_sample = {
1312
'active': True,
14-
'address': "1500 Sample St. Sunnyside CA 98996",
15-
'name': "Sample",
16-
'phone': "1234567890",
13+
'address': '1500 Sample St. Sunnyside CA 98996',
14+
'name': 'Sample',
15+
'phone': '1234567890',
1716
}
1817

1918
tenant_sample = {
@@ -25,20 +24,20 @@
2524
}
2625

2726
room_sample = {
28-
'description': "room number 1000",
29-
'name': "MA-1000",
30-
'picture': "",
27+
'description': 'room number 1000',
28+
'name': 'MA-1000',
29+
'picture': '',
3130
'project_id': 1,
3231
}
3332

3433
registration_sample = {
35-
'date': "2019-09-26 20:18:36",
36-
'deposit': "4400.00",
37-
'interval': "100",
38-
'rate': "1500.00",
39-
'reference1': "5555555555",
40-
'reference2': "",
41-
'reference3': "",
34+
'date': front_end_date(),
35+
'deposit': '4400.00',
36+
'interval': '100',
37+
'rate': '1500.00',
38+
'reference1': '5555555555',
39+
'reference2': '',
40+
'reference3': '',
4241
'room_id': 1,
4342
'tenant_id': 1
4443
}

api/tests/test_balance_payments.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,43 @@
1-
from base64 import b64encode
21
from flask.testing import FlaskClient
3-
from tests import endpoint
4-
from tests.seeders import seed_admin, seed_tenant, seed_new_agreement, seed_project, seed_room
2+
from tests.seeders import seed_tenant, seed_new_agreement, seed_project, seed_room
53

64

7-
def test_seeding_data(client: FlaskClient):
5+
def test_seeding_data(client: FlaskClient, admin_login: dict):
86

9-
admin_resp = seed_admin(client)
10-
assert b'Redirecting...' in admin_resp.data
11-
assert admin_resp.status_code == 302
12-
13-
# we need a cookie
14-
auth = {
15-
'Authorization': 'Basic ' + b64encode(b'[email protected]:' + b'secured').decode()
16-
}
17-
# create a session
18-
login_resp = client.post(endpoint('/login'), headers=auth)
19-
assert 'token' in login_resp.json
20-
assert login_resp.status_code == 200
21-
22-
token = login_resp.json['token']['value']
23-
24-
project_resp = seed_project(client, token)
7+
project_resp = seed_project(client, admin_login['token']['value'])
258
assert 'id' in project_resp.json
269
assert project_resp.status_code == 200
2710

28-
room_resp = seed_room(client, token, {'project_id': project_resp.json['id']})
11+
room_resp = seed_room(client, admin_login['token']['value'], {'project_id': project_resp.json['id']})
2912
assert 'id' in room_resp.json
3013
assert room_resp.status_code == 200
3114

32-
tenant_resp = seed_tenant(client, token)
15+
tenant_resp = seed_tenant(client, admin_login['token']['value'])
3316
assert 'id' in tenant_resp.json
3417
assert tenant_resp.status_code == 200
3518

36-
user_id = tenant_resp.json['id']
3719

38-
agreement_resp = seed_new_agreement(client, token, {'tenant_id': user_id})
39-
assert 'id' in agreement_resp.json
40-
assert agreement_resp.status_code == 200
20+
def test_agreement(client: FlaskClient, admin_login: dict):
21+
from dal.models import Balance, RentalAgreement, Tenant
4122

23+
tenants = Tenant.query.all()
24+
assert len(tenants) == 1
25+
tenant = tenants.pop()
26+
assert hasattr(tenant, 'id')
4227

43-
def test_init_balance_created(client: FlaskClient):
44-
from dal.models import Balance
45-
from dal.models import RentalAgreement
28+
tenant_id = tenant.id
29+
30+
agreement_resp = seed_new_agreement(client, admin_login['token']['value'], {'tenant_id': tenant_id})
31+
assert 'id' in agreement_resp.json
32+
assert agreement_resp.status_code == 200
4633

4734
balances = Balance.query.all()
48-
assert len(balances) == 1
49-
balance = Balance.query.first()
35+
assert len(balances) == 1, 'only one balance should exist'
36+
balance = balances.pop()
5037
assert isinstance(balance.agreement_id, int)
51-
assert balance.init_processed == False
38+
assert balance.init_processed == False, 'first balance must not be init processed'
5239
assert isinstance(balance.agreement, RentalAgreement)
40+
41+
42+
def test_new_balance(client, admin_login: dict):
43+
assert 'bro' in 'bron'

0 commit comments

Comments
 (0)