Skip to content

Commit b80cdeb

Browse files
no need for app context as already pushed with client in setup fixture
1 parent cb87636 commit b80cdeb

File tree

5 files changed

+55
-75
lines changed

5 files changed

+55
-75
lines changed

tests/test_auth.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,19 @@ class TestAuth:
1616
@pytest.fixture(autouse=True)
1717
def setup(self, client):
1818
self.client = client
19-
with client.application.app_context():
20-
assert User.query.count() == 0
19+
assert User.query.count() == 0
2120

2221
def _verify_user_in_db(self, email, should_exist=True):
23-
with self.client.application.app_context():
24-
user = User.get(email=email)
25-
if should_exist:
26-
assert user is not None
27-
assert user.email == email
28-
return user
29-
else:
30-
assert user is None
22+
user = User.get(email=email)
23+
if should_exist:
24+
assert user is not None
25+
assert user.email == email
26+
return user
27+
else:
28+
assert user is None
3129

3230
def _count_users(self):
33-
with self.client.application.app_context():
34-
return User.query.count()
31+
return User.query.count()
3532

3633
def _test_invalid_request_data(self, endpoint, expected_status=422):
3734
response = self.client.post(endpoint, json={})
@@ -47,9 +44,7 @@ def _test_invalid_request_data(self, endpoint, expected_status=422):
4744
assert response.status_code == expected_status
4845

4946
def _decode_token(self, token):
50-
# Needs Flask app context for secret/algorithms from current_app.config
51-
with self.client.application.app_context():
52-
return decode_token(token, allow_expired=False)
47+
return decode_token(token, allow_expired=False)
5348

5449
def _assert_jwt_structure(self, token, expected_sub, expected_type, fresh=False):
5550
assert token.count(".") == 2, f"Token does not have three segments: {token}"

tests/test_category.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,19 @@ class TestCategory:
1313
@pytest.fixture(autouse=True)
1414
def setup(self, client):
1515
self.client = client
16-
with client.application.app_context():
17-
assert Category.query.count() == 0
16+
assert Category.query.count() == 0
1817

1918
def _count_categories(self):
20-
with self.client.application.app_context():
21-
return Category.query.count()
19+
return Category.query.count()
2220

2321
def _verify_category_in_db(self, name, should_exist=True):
24-
with self.client.application.app_context():
25-
category = Category.query.filter_by(name=name).first()
26-
if should_exist:
27-
assert category is not None
28-
assert category.name == name
29-
return category
30-
else:
31-
assert category is None
22+
category = Category.query.filter_by(name=name).first()
23+
if should_exist:
24+
assert category is not None
25+
assert category.name == name
26+
return category
27+
else:
28+
assert category is None
3229

3330
def test_create_category(self, create_category):
3431
response = create_category(self.TEST_CATEGORY_NAME)

tests/test_product.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,19 @@ class TestProduct:
1414
@pytest.fixture(autouse=True)
1515
def setup(self, client):
1616
self.client = client
17-
with client.application.app_context():
18-
assert Product.query.count() == 0
17+
assert Product.query.count() == 0
1918

2019
def _count_products(self):
21-
with self.client.application.app_context():
22-
return Product.query.count()
20+
return Product.query.count()
2321

2422
def _verify_product_in_db(self, name, should_exist=True):
25-
with self.client.application.app_context():
26-
product = Product.query.filter_by(name=name).first()
27-
if should_exist:
28-
assert product is not None
29-
assert product.name == name
30-
return product
31-
else:
32-
assert product is None
23+
product = Product.query.filter_by(name=name).first()
24+
if should_exist:
25+
assert product is not None
26+
assert product.name == name
27+
return product
28+
else:
29+
assert product is None
3330

3431
def test_create_product(self, create_product):
3532
response = create_product(self.TEST_PRODUCT_NAME, self.TEST_PRODUCT_DESC)

tests/test_relationships.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,34 @@ class TestRelationships:
1010
@pytest.fixture(autouse=True)
1111
def setup(self, client):
1212
self.client = client
13-
with client.application.app_context():
14-
assert Category.query.count() == 0
15-
assert Subcategory.query.count() == 0
16-
assert Product.query.count() == 0
13+
assert Category.query.count() == 0
14+
assert Subcategory.query.count() == 0
15+
assert Product.query.count() == 0
1716

1817
def _category_subcategory_ids(self, category_id):
19-
with self.client.application.app_context():
20-
category = Category.query.get(category_id)
21-
assert category is not None
22-
return sorted([subcategory.id for subcategory in category.subcategories])
18+
category = Category.query.get(category_id)
19+
assert category is not None
20+
return sorted([subcategory.id for subcategory in category.subcategories])
2321

2422
def _subcategory_category_ids(self, subcategory_id):
25-
with self.client.application.app_context():
26-
subcategory = Subcategory.query.get(subcategory_id)
27-
assert subcategory is not None
28-
return sorted([category.id for category in subcategory.categories])
23+
subcategory = Subcategory.query.get(subcategory_id)
24+
assert subcategory is not None
25+
return sorted([category.id for category in subcategory.categories])
2926

3027
def _subcategory_product_ids(self, subcategory_id):
31-
with self.client.application.app_context():
32-
subcategory = Subcategory.query.get(subcategory_id)
33-
assert subcategory is not None
34-
return sorted([product.id for product in subcategory.products])
28+
subcategory = Subcategory.query.get(subcategory_id)
29+
assert subcategory is not None
30+
return sorted([product.id for product in subcategory.products])
3531

3632
def _product_subcategory_ids(self, product_id):
37-
with self.client.application.app_context():
38-
product = Product.query.get(product_id)
39-
assert product is not None
40-
return sorted([subcategory.id for subcategory in product.subcategories])
33+
product = Product.query.get(product_id)
34+
assert product is not None
35+
return sorted([subcategory.id for subcategory in product.subcategories])
4136

4237
def _category_product_ids_via_subcategories(self, category_id):
43-
with self.client.application.app_context():
44-
category = Category.query.get(category_id)
45-
assert category is not None
46-
return sorted({product.id for subcategory in category.subcategories for product in subcategory.products.all()})
38+
category = Category.query.get(category_id)
39+
assert category is not None
40+
return sorted({product.id for subcategory in category.subcategories for product in subcategory.products.all()})
4741

4842
def _assert_related_collection(self, resp, key, expected_ids=None, status_code=200):
4943
assert resp.status_code == status_code

tests/test_subcategory.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,19 @@ class TestSubcategory:
1313
@pytest.fixture(autouse=True)
1414
def setup(self, client):
1515
self.client = client
16-
with client.application.app_context():
17-
assert Subcategory.query.count() == 0
16+
assert Subcategory.query.count() == 0
1817

1918
def _count_subcategories(self):
20-
with self.client.application.app_context():
21-
return Subcategory.query.count()
19+
return Subcategory.query.count()
2220

2321
def _verify_subcategory_in_db(self, name, should_exist=True):
24-
with self.client.application.app_context():
25-
subcategory = Subcategory.query.filter_by(name=name).first()
26-
if should_exist:
27-
assert subcategory is not None
28-
assert subcategory.name == name
29-
return subcategory
30-
else:
31-
assert subcategory is None
22+
subcategory = Subcategory.query.filter_by(name=name).first()
23+
if should_exist:
24+
assert subcategory is not None
25+
assert subcategory.name == name
26+
return subcategory
27+
else:
28+
assert subcategory is None
3229

3330
def test_create_subcategory(self, create_subcategory):
3431
response = create_subcategory(self.TEST_SUBCATEGORY_NAME)

0 commit comments

Comments
 (0)