Skip to content
This repository was archived by the owner on Dec 19, 2021. It is now read-only.

Commit cf48e21

Browse files
authored
Supercharge tests (#416)
This change makes the tests about 50% faster. Automatically imports the app context into every test. No longer need to decorate tests with `empty_test_db`.
1 parent 0361c01 commit cf48e21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+99
-296
lines changed

.github/workflows/pytest.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ jobs:
4545
run: pipenv run dev-install
4646

4747
- name: Create db
48-
run: createdb dwellingly_test
48+
run: |
49+
createdb dwellingly_test
50+
pipenv run flask db create
4951
5052
- name: Test
5153
run: |

conftest.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import jwt
33
from flask import current_app
44
from app import create_app
5-
from db import db
65

6+
from data.seed import Seed
77
from tests.factory_fixtures import * # noqa: F401, F403
88

99

@@ -19,6 +19,11 @@ def app(monkeypatch):
1919
return app
2020

2121

22+
@pytest.fixture(autouse=True)
23+
def app_context(app):
24+
Seed().destroy_all()
25+
26+
2227
@pytest.fixture
2328
def valid_header(admin_header):
2429
return admin_header
@@ -69,12 +74,6 @@ def _pm_header(pm=None):
6974
yield _pm_header
7075

7176

72-
@pytest.fixture
73-
def empty_test_db(app):
74-
db.drop_all()
75-
db.create_all()
76-
77-
7877
# ------------- NON-FIXTURE FUNCTIONS --------------------
7978
def has_valid_headers(response):
8079
if response.content_type != "application/json":

data/seed.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import random
1010
from faker import Faker
1111

12+
from db import db
1213
from models.user import UserModel, RoleEnum
1314
from models.users.admin import Admin
1415
from models.users.staff import Staff
@@ -287,3 +288,22 @@ def create_property_manager(self, payload):
287288
schema=UserSchema,
288289
payload=payload,
289290
)
291+
292+
def destroy_all(self):
293+
db.session.execute(
294+
"""
295+
DO
296+
$$
297+
DECLARE
298+
stmt text;
299+
BEGIN
300+
SELECT 'TRUNCATE ' || string_agg(format('%I.%I', schemaname, tablename), ',')
301+
INTO stmt
302+
FROM pg_tables
303+
WHERE schemaname in ('public');
304+
305+
EXECUTE stmt;
306+
END;
307+
$$
308+
"""
309+
)

tests/authorizations/test_emergency_contact_authorization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44

5-
@pytest.mark.usefixtures("client_class", "empty_test_db")
5+
@pytest.mark.usefixtures("client_class")
66
class TestEmergenyContactsAuthorizations:
77
def setup(self):
88
self.endpoint = "/api/emergencycontacts"

tests/authorizations/test_lease_authorization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44

5-
@pytest.mark.usefixtures("client_class", "empty_test_db")
5+
@pytest.mark.usefixtures("client_class")
66
class TestLeaseAuthorizations:
77
def valid_payload(self, tenant, lease):
88
return {"tenantID": tenant.id, **lease}

tests/authorizations/test_property_authorization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33

4-
@pytest.mark.usefixtures("client_class", "empty_test_db")
4+
@pytest.mark.usefixtures("client_class")
55
class TestPropertyAuthorizations:
66
def test_post_property(self, property_attributes):
77
property_attrs = property_attributes()

tests/authorizations/test_staff_tenant_authorization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33

4-
@pytest.mark.usefixtures("client_class", "empty_test_db")
4+
@pytest.mark.usefixtures("client_class")
55
class TestStaffTenantAuthorizations:
66
def setup(self):
77
self.endpoint = "/api/staff-tenants"

tests/authorizations/test_tenant_authorization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from conftest import is_valid
33

44

5-
@pytest.mark.usefixtures("client_class", "empty_test_db")
5+
@pytest.mark.usefixtures("client_class")
66
class TestTenantAuthorization:
77
def setup(self):
88
self.endpoint = "/api/tenants"

tests/authorizations/test_ticket_authorization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from conftest import is_valid
33

44

5-
@pytest.mark.usefixtures("empty_test_db", "client_class")
5+
@pytest.mark.usefixtures("client_class")
66
class TestTicketAuthorization:
77
def setup(self):
88
self.endpoint = "/api/tickets"

tests/authorizations/test_user_authentication.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from flask_jwt_extended import create_access_token, create_refresh_token
55

66

7-
@pytest.mark.usefixtures("client_class", "empty_test_db")
7+
@pytest.mark.usefixtures("client_class")
88
class TestUserAuthorization:
99
def test_user_auth(self, create_admin_user):
1010
password = "strongestpasswordever"
@@ -63,7 +63,7 @@ def test_get_user(self, pm_header):
6363
assert is_valid(unauthorized_user_response, 401)
6464

6565

66-
@pytest.mark.usefixtures("client_class", "empty_test_db")
66+
@pytest.mark.usefixtures("client_class")
6767
class TestUserDeleteAuthorization:
6868
def test_admin_is_authorized(self, admin_header, create_user):
6969
response = self.client.delete(
@@ -84,7 +84,7 @@ def test_pm_is_not_authorized(self, pm_header, create_user):
8484
assert response.status_code == 401
8585

8686

87-
@pytest.mark.usefixtures("client_class", "empty_test_db")
87+
@pytest.mark.usefixtures("client_class")
8888
class TestUserPatchAuthorization:
8989
def test_auth_token_is_required(self):
9090
response = self.client.patch("api/user/5", json={})
@@ -127,7 +127,7 @@ def test_pm_is_authorized_to_update_themselves(
127127
assert response == 200
128128

129129

130-
@pytest.mark.usefixtures("client_class", "empty_test_db")
130+
@pytest.mark.usefixtures("client_class")
131131
class TestUserLogic:
132132
# TODO: This doesn't belong in auth tests
133133
def test_password(self, header, create_user, faker):

0 commit comments

Comments
 (0)