Skip to content

Commit 86bef33

Browse files
committed
mark tests as unit or integration
1 parent eb8b7c7 commit 86bef33

30 files changed

+188
-160
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ repos:
1919
- --ignore-init-module-imports
2020

2121
- repo: https://github.com/pycqa/isort
22-
rev: 5.10.1
22+
rev: 5.11.2
2323
hooks:
2424
- id: isort
2525
name: isort (python)

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ build-backend = "poetry.core.masonry.api"
4040

4141
[tool.poe.tasks]
4242
test = { shell = "DATABASE_URL=postgresql://postgres:password@localhost:5433/postgres pytest src" }
43+
test_domain = "pytest -k domain"
44+
test_infrastructure = "pytest -k infrastructure"
45+
test_application = "pytest -k application"
46+
test_unit = "pytest -m unit"
47+
test_integration = "pytest -m 'not unit'"
4348
test_coverage = "pytest --cov=src --cov-report=html"
4449
start = "uvicorn src.api.main:app --reload"
4550
start_cli = { shell = "cd src && python -m cli" }

pytest.ini

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
[pytest]
2-
norecursedirs = tmp
2+
norecursedirs = tmp
3+
4+
markers =
5+
unit: marks test as unit test i.e. not using any external services (deselect with '-m "not unit"')
6+
integration: marks tests as integration i.e. using a database (deselect with '-m "not integration"')
7+
serial

src/api/routers/catalog.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async def get_all_listings(
2626
query = GetAllListings()
2727
with module.unit_of_work():
2828
query_result = module.execute_query(query)
29-
return dict(data=query_result.result)
29+
return dict(data=query_result.payload)
3030

3131

3232
@router.get("/catalog/{listing_id}", tags=["catalog"], response_model=ListingReadModel)
@@ -41,7 +41,7 @@ async def get_listing_details(
4141
query = GetListingDetails(listing_id=listing_id)
4242
with module.unit_of_work():
4343
query_result = module.execute_query(query)
44-
return query_result.result
44+
return query_result.payload
4545

4646

4747
@router.post(
@@ -66,7 +66,7 @@ async def create_listing(
6666

6767
query = GetListingDetails(listing_id=command_result.result)
6868
query_result = module.execute_query(query)
69-
return query_result.result
69+
return query_result.payload
7070

7171

7272
#

src/api/tests/test_catalog.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
import pytest
2+
13
from modules.catalog.application.command import CreateListingDraftCommand
24
from seedwork.domain.value_objects import Money
35

46

7+
@pytest.mark.integration
58
def test_empty_catalog_list(api_client):
69
response = api_client.get("/catalog")
710
assert response.status_code == 200
811
assert response.json() == {"data": []}
912

1013

14+
@pytest.mark.integration
1115
def test_catalog_list_with_one_item(api, api_client):
1216
# arrange
1317
catalog_module = api.container.catalog_module()
@@ -28,7 +32,7 @@ def test_catalog_list_with_one_item(api, api_client):
2832
assert response.json() == {
2933
"data": [
3034
{
31-
"id": str(command_result.result),
35+
"id": str(command_result.entity_id),
3236
"title": "Foo",
3337
"description": "Bar",
3438
"ask_price_amount": 10.0,
@@ -38,6 +42,7 @@ def test_catalog_list_with_one_item(api, api_client):
3842
}
3943

4044

45+
@pytest.mark.integration
4146
def test_catalog_list_with_two_items(api, api_client):
4247
# arrange
4348
catalog_module = api.container.catalog_module()

src/api/tests/test_common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
import pytest
12
from fastapi.testclient import TestClient
23

34
from api.main import app
45

56
client = TestClient(app)
67

78

9+
@pytest.mark.integration
810
def test_homepage_returns_200():
911
response = client.get("/")
1012
assert response.status_code == 200
1113

1214

15+
@pytest.mark.integration
1316
def test_docs_page_returns_200():
1417
response = client.get("/docs")
1518
assert response.status_code == 200

src/modules/bidding/__init__.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55
)
66
from seedwork.application.modules import BusinessModule
77

8-
#
9-
# @dataclass
10-
# class UnitOfWork:
11-
# module: Any # FIXME: type
12-
# db_session: Session
13-
# correlation_id: uuid.UUID
14-
# listing_repository: ListingRepository
15-
168

179
class BiddingModule(BusinessModule):
1810
supported_commands = (PlaceBidCommand, RetractBidCommand)

src/modules/bidding/application/command/place_bid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ def place_bid(
2323
bid = Bid(bidder=bidder, price=Money(command.amount))
2424

2525
listing: Listing = listing_repository.get_by_id(id=command.listing_id)
26-
events = listing.place_bid(bid)
26+
listing.place_bid(bid)
2727

28-
return CommandResult.ok(events=events)
28+
return CommandResult.ok()
File renamed without changes.

src/modules/bidding/domain/test_listing.py renamed to src/modules/bidding/tests/domain/test_listing.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from seedwork.domain.value_objects import UUID
99

1010

11+
@pytest.mark.unit
1112
def test_listing_initial_price():
1213
seller = Seller(id=UUID.v4())
1314
listing = Listing(
@@ -19,6 +20,7 @@ def test_listing_initial_price():
1920
assert listing.winning_bid is None
2021

2122

23+
@pytest.mark.unit
2224
def test_place_one_bid():
2325
now = datetime.utcnow()
2426
seller = Seller(id=UUID.v4())
@@ -34,6 +36,7 @@ def test_place_one_bid():
3436
assert listing.winning_bid == Bid(Money(20), bidder=bidder, placed_at=now)
3537

3638

39+
@pytest.mark.unit
3740
def test_place_two_bids():
3841
now = datetime.utcnow()
3942
seller = Seller(id=UUID.v4())
@@ -50,6 +53,7 @@ def test_place_two_bids():
5053
assert listing.winning_bid == Bid(Money(30), bidder=bidder2, placed_at=now)
5154

5255

56+
@pytest.mark.unit
5357
def test_place_two_bids_by_same_bidder():
5458
now = datetime.utcnow()
5559
seller = Seller(id=UUID.v4())
@@ -67,6 +71,7 @@ def test_place_two_bids_by_same_bidder():
6771
assert listing.winning_bid == Bid(price=Money(30), bidder=bidder, placed_at=now)
6872

6973

74+
@pytest.mark.unit
7075
def test_cannot_place_bid_if_listing_ended():
7176
seller = Seller(id=UUID.v4())
7277
bidder = Bidder(id=UUID.v4())
@@ -88,6 +93,7 @@ def test_cannot_place_bid_if_listing_ended():
8893
listing.place_bid(bid)
8994

9095

96+
@pytest.mark.unit
9197
def test_retract_bid():
9298
seller = Seller(id=UUID.v4())
9399
bidder = Bidder(id=UUID.v4())
@@ -107,6 +113,7 @@ def test_retract_bid():
107113
listing.retract_bid_of(bidder=bidder)
108114

109115

116+
@pytest.mark.unit
110117
def test_cancel_listing():
111118
now = datetime.utcnow()
112119
seller = Seller(id=UUID.v4())
@@ -122,6 +129,7 @@ def test_cancel_listing():
122129
assert listing.time_left_in_listing == timedelta()
123130

124131

132+
@pytest.mark.unit
125133
def test_can_cancel_listing_with_bids():
126134
now = datetime.utcnow()
127135
seller = Seller(id=UUID.v4())
@@ -144,6 +152,7 @@ def test_can_cancel_listing_with_bids():
144152
assert listing.time_left_in_listing == timedelta()
145153

146154

155+
@pytest.mark.unit
147156
def test_cannot_cancel_listing_with_bids():
148157
now = datetime.utcnow()
149158
seller = Seller(id=UUID.v4())

0 commit comments

Comments
 (0)