Skip to content

Commit

Permalink
mark tests as unit or integration
Browse files Browse the repository at this point in the history
  • Loading branch information
pgorecki committed Jan 5, 2023
1 parent eb8b7c7 commit 86bef33
Show file tree
Hide file tree
Showing 30 changed files with 188 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repos:
- --ignore-init-module-imports

- repo: https://github.com/pycqa/isort
rev: 5.10.1
rev: 5.11.2
hooks:
- id: isort
name: isort (python)
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ build-backend = "poetry.core.masonry.api"

[tool.poe.tasks]
test = { shell = "DATABASE_URL=postgresql://postgres:password@localhost:5433/postgres pytest src" }
test_domain = "pytest -k domain"
test_infrastructure = "pytest -k infrastructure"
test_application = "pytest -k application"
test_unit = "pytest -m unit"
test_integration = "pytest -m 'not unit'"
test_coverage = "pytest --cov=src --cov-report=html"
start = "uvicorn src.api.main:app --reload"
start_cli = { shell = "cd src && python -m cli" }
Expand Down
7 changes: 6 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
[pytest]
norecursedirs = tmp
norecursedirs = tmp

markers =
unit: marks test as unit test i.e. not using any external services (deselect with '-m "not unit"')
integration: marks tests as integration i.e. using a database (deselect with '-m "not integration"')
serial
6 changes: 3 additions & 3 deletions src/api/routers/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def get_all_listings(
query = GetAllListings()
with module.unit_of_work():
query_result = module.execute_query(query)
return dict(data=query_result.result)
return dict(data=query_result.payload)


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


@router.post(
Expand All @@ -66,7 +66,7 @@ async def create_listing(

query = GetListingDetails(listing_id=command_result.result)
query_result = module.execute_query(query)
return query_result.result
return query_result.payload


#
Expand Down
7 changes: 6 additions & 1 deletion src/api/tests/test_catalog.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import pytest

from modules.catalog.application.command import CreateListingDraftCommand
from seedwork.domain.value_objects import Money


@pytest.mark.integration
def test_empty_catalog_list(api_client):
response = api_client.get("/catalog")
assert response.status_code == 200
assert response.json() == {"data": []}


@pytest.mark.integration
def test_catalog_list_with_one_item(api, api_client):
# arrange
catalog_module = api.container.catalog_module()
Expand All @@ -28,7 +32,7 @@ def test_catalog_list_with_one_item(api, api_client):
assert response.json() == {
"data": [
{
"id": str(command_result.result),
"id": str(command_result.entity_id),
"title": "Foo",
"description": "Bar",
"ask_price_amount": 10.0,
Expand All @@ -38,6 +42,7 @@ def test_catalog_list_with_one_item(api, api_client):
}


@pytest.mark.integration
def test_catalog_list_with_two_items(api, api_client):
# arrange
catalog_module = api.container.catalog_module()
Expand Down
3 changes: 3 additions & 0 deletions src/api/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import pytest
from fastapi.testclient import TestClient

from api.main import app

client = TestClient(app)


@pytest.mark.integration
def test_homepage_returns_200():
response = client.get("/")
assert response.status_code == 200


@pytest.mark.integration
def test_docs_page_returns_200():
response = client.get("/docs")
assert response.status_code == 200
8 changes: 0 additions & 8 deletions src/modules/bidding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
)
from seedwork.application.modules import BusinessModule

#
# @dataclass
# class UnitOfWork:
# module: Any # FIXME: type
# db_session: Session
# correlation_id: uuid.UUID
# listing_repository: ListingRepository


class BiddingModule(BusinessModule):
supported_commands = (PlaceBidCommand, RetractBidCommand)
Expand Down
4 changes: 2 additions & 2 deletions src/modules/bidding/application/command/place_bid.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def place_bid(
bid = Bid(bidder=bidder, price=Money(command.amount))

listing: Listing = listing_repository.get_by_id(id=command.listing_id)
events = listing.place_bid(bid)
listing.place_bid(bid)

return CommandResult.ok(events=events)
return CommandResult.ok()
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from seedwork.domain.value_objects import UUID


@pytest.mark.unit
def test_listing_initial_price():
seller = Seller(id=UUID.v4())
listing = Listing(
Expand All @@ -19,6 +20,7 @@ def test_listing_initial_price():
assert listing.winning_bid is None


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


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


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


@pytest.mark.unit
def test_cannot_place_bid_if_listing_ended():
seller = Seller(id=UUID.v4())
bidder = Bidder(id=UUID.v4())
Expand All @@ -88,6 +93,7 @@ def test_cannot_place_bid_if_listing_ended():
listing.place_bid(bid)


@pytest.mark.unit
def test_retract_bid():
seller = Seller(id=UUID.v4())
bidder = Bidder(id=UUID.v4())
Expand All @@ -107,6 +113,7 @@ def test_retract_bid():
listing.retract_bid_of(bidder=bidder)


@pytest.mark.unit
def test_cancel_listing():
now = datetime.utcnow()
seller = Seller(id=UUID.v4())
Expand All @@ -122,6 +129,7 @@ def test_cancel_listing():
assert listing.time_left_in_listing == timedelta()


@pytest.mark.unit
def test_can_cancel_listing_with_bids():
now = datetime.utcnow()
seller = Seller(id=UUID.v4())
Expand All @@ -144,6 +152,7 @@ def test_can_cancel_listing_with_bids():
assert listing.time_left_in_listing == timedelta()


@pytest.mark.unit
def test_cannot_cancel_listing_with_bids():
now = datetime.utcnow()
seller = Seller(id=UUID.v4())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
import uuid

import pytest

from modules.bidding.domain.entities import Bid, Bidder, Listing, Money, Seller
from modules.bidding.infrastructure.listing_repository import (
ListingDataMapper,
Expand All @@ -10,11 +12,13 @@
from seedwork.domain.value_objects import UUID


@pytest.mark.integration
def test_listing_repo_is_empty(db_session):
repo = PostgresJsonListingRepository(db_session=db_session)
assert repo.count() == 0


@pytest.mark.unit
def test_listing_data_mapper_maps_entity_to_model():
listing = Listing(
id=UUID("00000000000000000000000000000001"),
Expand Down Expand Up @@ -58,6 +62,7 @@ def test_listing_data_mapper_maps_entity_to_model():
assert actual.data == expected.data


@pytest.mark.unit
def test_listing_data_mapper_maps_model_to_entity():
instance = ListingModel(
id=UUID("00000000000000000000000000000001"),
Expand All @@ -83,6 +88,7 @@ def test_listing_data_mapper_maps_model_to_entity():
assert actual == expected


@pytest.mark.integration
def test_listing_persistence(db_session):
original = Listing(
id=Listing.next_id(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ def create_listing_draft(
seller_id=command.seller_id,
)
repository.add(listing)
return CommandResult.ok(
result=listing.id, events=[ListingDraftCreatedEvent(listing_id=listing.id)]
return CommandResult.success(
entity_id=listing.id, events=[ListingDraftCreatedEvent(listing_id=listing.id)]
)
2 changes: 1 addition & 1 deletion src/modules/catalog/application/command/publish_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def publish_listing(

events = seller.publish_listing(listing)

return CommandResult.ok(events=events)
return CommandResult.success(entity_id=listing.id, events=events)
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ def update_listing_draft(
description=command.description,
ask_price=command.ask_price,
)
return CommandResult.ok(events=events)
return CommandResult.success(entity_id=listing.id, events=events)
4 changes: 2 additions & 2 deletions src/modules/catalog/application/query/get_all_listings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ def get_all_listings(
session: Session,
) -> QueryResult:
queryset = session.query(ListingModel)
result = [map_listing_model_to_dao(row) for row in queryset.all()]
payload = [map_listing_model_to_dao(row) for row in queryset.all()]
# TODO: add error handling
return QueryResult.ok(result)
return QueryResult.success(payload)
25 changes: 25 additions & 0 deletions src/modules/catalog/tests/application/test_create_listing_draft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest

from modules.catalog.application.command.create_listing_draft import (
CreateListingDraftCommand,
create_listing_draft,
)
from modules.catalog.domain.entities import Seller
from seedwork.domain.value_objects import Money
from seedwork.infrastructure.repository import InMemoryRepository


@pytest.mark.unit
def test_create_listing_draft():
# arrange
command = CreateListingDraftCommand(
title="foo", description="bar", ask_price=Money(1), seller_id=Seller.next_id()
)
repository = InMemoryRepository()

# act
result = create_listing_draft(command, repository)

# assert
assert repository.get_by_id(result.entity_id).title == "foo"
assert result.has_errors() is False
Original file line number Diff line number Diff line change
@@ -1,63 +1,16 @@
from modules.catalog.application.command.create_listing_draft import (
CreateListingDraftCommand,
create_listing_draft,
)
import pytest

from modules.catalog.application.command.publish_listing import (
PublishListingCommand,
publish_listing,
)
from modules.catalog.application.command.update_listing_draft import (
UpdateListingDraftCommand,
update_listing_draft,
)
from modules.catalog.domain.entities import Listing, Seller
from modules.catalog.domain.value_objects import ListingStatus
from seedwork.domain.value_objects import UUID, Money
from seedwork.domain.value_objects import Money
from seedwork.infrastructure.repository import InMemoryRepository


def test_create_listing_draft():
# arrange
command = CreateListingDraftCommand(
title="foo", description="bar", ask_price=Money(1), seller_id=Seller.next_id()
)
repository = InMemoryRepository()

# act
result = create_listing_draft(command, repository)

# assert
assert repository.get_by_id(result.result).title == "foo"
assert result.has_errors() is False


def test_update_listing_draft():
# arrange
repository = InMemoryRepository()
listing = Listing(
id=Listing.next_id(),
title="Tiny dragon",
description="Tiny dragon for sale",
ask_price=Money(1),
seller_id=UUID.v4(),
)
repository.add(listing)

command = UpdateListingDraftCommand(
listing_id=listing.id,
title="Tiny golden dragon",
description=listing.description,
ask_price=listing.ask_price,
modify_user_id=listing.seller_id,
)

# act
result = update_listing_draft(command, repository)

# assert
assert result.is_ok()


@pytest.mark.unit
def test_publish_listing():
# arrange
seller_repository = InMemoryRepository()
Expand Down Expand Up @@ -87,10 +40,11 @@ def test_publish_listing():
)

# assert
assert result.is_ok()
assert result.is_success()
assert listing.status == ListingStatus.PUBLISHED


@pytest.mark.unit
def test_publish_listing_and_break_business_rule():
# arrange
seller_repository = InMemoryRepository()
Expand Down
Loading

0 comments on commit 86bef33

Please sign in to comment.