feat: add ClassifiedAdId value object and use it in ClassifiedAd
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
from .classified_ad import ClassifiedAd as ClassifiedAd
|
from .classified_ad import ClassifiedAd as ClassifiedAd
|
||||||
|
from .classified_ad_id import ClassifiedAdId as ClassifiedAdId
|
||||||
from .user_id import UserId as UserId
|
from .user_id import UserId as UserId
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from uuid import UUID
|
|
||||||
|
|
||||||
|
from domain.marketplace.classified_ad_id import ClassifiedAdId
|
||||||
from domain.marketplace.user_id import UserId
|
from domain.marketplace.user_id import UserId
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class ClassifiedAd:
|
class ClassifiedAd:
|
||||||
id: UUID
|
id: ClassifiedAdId
|
||||||
_ownerId: UserId
|
_ownerId: UserId
|
||||||
title: str
|
title: str
|
||||||
text: str
|
text: str
|
||||||
|
|||||||
7
domain/marketplace/classified_ad_id.py
Normal file
7
domain/marketplace/classified_ad_id.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class ClassifiedAdId:
|
||||||
|
value: UUID
|
||||||
@@ -4,11 +4,12 @@ from uuid import uuid4
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from domain.marketplace.classified_ad import ClassifiedAd
|
from domain.marketplace.classified_ad import ClassifiedAd
|
||||||
|
from domain.marketplace.classified_ad_id import ClassifiedAdId
|
||||||
from domain.marketplace.user_id import UserId
|
from domain.marketplace.user_id import UserId
|
||||||
|
|
||||||
|
|
||||||
def test_classified_ad_creation():
|
def test_classified_ad_creation():
|
||||||
ad_id = uuid4()
|
ad_id = ClassifiedAdId(value=uuid4())
|
||||||
owner_id = UserId(value=uuid4())
|
owner_id = UserId(value=uuid4())
|
||||||
|
|
||||||
ad = ClassifiedAd(
|
ad = ClassifiedAd(
|
||||||
@@ -28,24 +29,26 @@ def test_classified_ad_creation():
|
|||||||
|
|
||||||
def test_classified_ad_can_not_be_created_without_id():
|
def test_classified_ad_can_not_be_created_without_id():
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
ad = ClassifiedAd(
|
ClassifiedAd(
|
||||||
_ownerId=UserId(value=uuid4()),
|
_ownerId=UserId(value=uuid4()),
|
||||||
title="Title",
|
title="Title",
|
||||||
text="Text",
|
text="Text",
|
||||||
_price=Decimal("10.00"),
|
_price=Decimal("10.00"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_classified_ad_can_not_be_created_without_owner_id():
|
def test_classified_ad_can_not_be_created_without_owner_id():
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
ad = ClassifiedAd(
|
ClassifiedAd(
|
||||||
id=UserId(value=uuid4()),
|
id=ClassifiedAdId(value=uuid4()),
|
||||||
title="Title",
|
title="Title",
|
||||||
text="Text",
|
text="Text",
|
||||||
_price=Decimal("10.00"),
|
_price=Decimal("10.00"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_classified_ad_can_be_created_without_price():
|
def test_classified_ad_can_be_created_without_price():
|
||||||
ad_id = uuid4()
|
ad_id = ClassifiedAdId(value=uuid4())
|
||||||
owner_id = UserId(value=uuid4())
|
owner_id = UserId(value=uuid4())
|
||||||
|
|
||||||
ad = ClassifiedAd(
|
ad = ClassifiedAd(
|
||||||
@@ -61,9 +64,10 @@ def test_classified_ad_can_be_created_without_price():
|
|||||||
assert ad.text == "Test text content"
|
assert ad.text == "Test text content"
|
||||||
assert ad._price == Decimal("0.00")
|
assert ad._price == Decimal("0.00")
|
||||||
|
|
||||||
|
|
||||||
def test_classified_ad_is_immutable():
|
def test_classified_ad_is_immutable():
|
||||||
ad = ClassifiedAd(
|
ad = ClassifiedAd(
|
||||||
id=uuid4(),
|
id=ClassifiedAdId(value=uuid4()),
|
||||||
_ownerId=UserId(value=uuid4()),
|
_ownerId=UserId(value=uuid4()),
|
||||||
title="Title",
|
title="Title",
|
||||||
text="Text",
|
text="Text",
|
||||||
@@ -78,7 +82,7 @@ def test_classified_ad_is_immutable():
|
|||||||
|
|
||||||
|
|
||||||
def test_classified_ad_equality():
|
def test_classified_ad_equality():
|
||||||
ad_id = uuid4()
|
ad_id = ClassifiedAdId(value=uuid4())
|
||||||
owner_id = UserId(value=uuid4())
|
owner_id = UserId(value=uuid4())
|
||||||
|
|
||||||
ad1 = ClassifiedAd(
|
ad1 = ClassifiedAd(
|
||||||
|
|||||||
25
tests/test_classified_ad_id.py
Normal file
25
tests/test_classified_ad_id.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
from domain.marketplace.classified_ad_id import ClassifiedAdId
|
||||||
|
|
||||||
|
|
||||||
|
def test_classified_ad_id_creation():
|
||||||
|
ad_id = ClassifiedAdId(value=uuid4())
|
||||||
|
assert isinstance(ad_id.value, UUID)
|
||||||
|
|
||||||
|
|
||||||
|
def test_classified_ad_id_is_immutable():
|
||||||
|
ad_id = ClassifiedAdId(value=uuid4())
|
||||||
|
|
||||||
|
try:
|
||||||
|
ad_id.value = uuid4() # type: ignore
|
||||||
|
assert False, "Should have raised FrozenInstanceError"
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def test_classified_ad_id_equality():
|
||||||
|
uid = uuid4()
|
||||||
|
ad_id1 = ClassifiedAdId(value=uid)
|
||||||
|
ad_id2 = ClassifiedAdId(value=uid)
|
||||||
|
assert ad_id1 == ad_id2
|
||||||
Reference in New Issue
Block a user