feat: use ClassifiedAdTitle value object for ClassifiedAd.title
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
from .classified_ad import ClassifiedAd as ClassifiedAd
|
from .classified_ad import ClassifiedAd as ClassifiedAd
|
||||||
from .classified_ad_id import ClassifiedAdId as ClassifiedAdId
|
from .classified_ad_id import ClassifiedAdId as ClassifiedAdId
|
||||||
|
from .classified_ad_title import ClassifiedAdTitle as ClassifiedAdTitle
|
||||||
from .money import Money as Money
|
from .money import Money as Money
|
||||||
from .price import Price as Price
|
from .price import Price as Price
|
||||||
from .user_id import UserId as UserId
|
from .user_id import UserId as UserId
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from dataclasses import dataclass, field
|
|||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from domain.marketplace.classified_ad_id import ClassifiedAdId
|
from domain.marketplace.classified_ad_id import ClassifiedAdId
|
||||||
|
from domain.marketplace.classified_ad_title import ClassifiedAdTitle
|
||||||
from domain.marketplace.price import Price
|
from domain.marketplace.price import Price
|
||||||
from domain.marketplace.user_id import UserId
|
from domain.marketplace.user_id import UserId
|
||||||
|
|
||||||
@@ -10,6 +11,6 @@ from domain.marketplace.user_id import UserId
|
|||||||
class ClassifiedAd:
|
class ClassifiedAd:
|
||||||
id: ClassifiedAdId
|
id: ClassifiedAdId
|
||||||
_ownerId: UserId
|
_ownerId: UserId
|
||||||
title: str
|
title: ClassifiedAdTitle
|
||||||
text: str
|
text: str
|
||||||
price: Price = field(default_factory=lambda: Price(amount=Decimal("0.00")))
|
price: Price = field(default_factory=lambda: Price(amount=Decimal("0.00")))
|
||||||
|
|||||||
10
domain/marketplace/classified_ad_title.py
Normal file
10
domain/marketplace/classified_ad_title.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class ClassifiedAdTitle:
|
||||||
|
title: str
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
if len(self.title) > 100:
|
||||||
|
raise ValueError(f"Title cannot be longer than 100 characters: {self.title}")
|
||||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
@@ -5,6 +5,7 @@ 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.classified_ad_id import ClassifiedAdId
|
||||||
|
from domain.marketplace.classified_ad_title import ClassifiedAdTitle
|
||||||
from domain.marketplace.price import Price
|
from domain.marketplace.price import Price
|
||||||
from domain.marketplace.user_id import UserId
|
from domain.marketplace.user_id import UserId
|
||||||
|
|
||||||
@@ -16,14 +17,14 @@ def test_classified_ad_creation():
|
|||||||
ad = ClassifiedAd(
|
ad = ClassifiedAd(
|
||||||
id=ad_id,
|
id=ad_id,
|
||||||
_ownerId=owner_id,
|
_ownerId=owner_id,
|
||||||
title="Test Title",
|
title=ClassifiedAdTitle(title="Test Title"),
|
||||||
text="Test text content",
|
text="Test text content",
|
||||||
price=Price(amount=Decimal("99.99")),
|
price=Price(amount=Decimal("99.99")),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert ad.id == ad_id
|
assert ad.id == ad_id
|
||||||
assert ad._ownerId == owner_id
|
assert ad._ownerId == owner_id
|
||||||
assert ad.title == "Test Title"
|
assert ad.title == ClassifiedAdTitle(title="Test Title")
|
||||||
assert ad.text == "Test text content"
|
assert ad.text == "Test text content"
|
||||||
assert ad.price == Price(amount=Decimal("99.99"))
|
assert ad.price == Price(amount=Decimal("99.99"))
|
||||||
|
|
||||||
@@ -32,7 +33,7 @@ def test_classified_ad_can_not_be_created_without_id():
|
|||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
ClassifiedAd(
|
ClassifiedAd(
|
||||||
_ownerId=UserId(value=uuid4()),
|
_ownerId=UserId(value=uuid4()),
|
||||||
title="Title",
|
title=ClassifiedAdTitle(title="Title"),
|
||||||
text="Text",
|
text="Text",
|
||||||
price=Price(amount=Decimal("10.00")),
|
price=Price(amount=Decimal("10.00")),
|
||||||
)
|
)
|
||||||
@@ -42,7 +43,7 @@ def test_classified_ad_can_not_be_created_without_owner_id():
|
|||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
ClassifiedAd(
|
ClassifiedAd(
|
||||||
id=ClassifiedAdId(value=uuid4()),
|
id=ClassifiedAdId(value=uuid4()),
|
||||||
title="Title",
|
title=ClassifiedAdTitle(title="Title"),
|
||||||
text="Text",
|
text="Text",
|
||||||
price=Price(amount=Decimal("10.00")),
|
price=Price(amount=Decimal("10.00")),
|
||||||
)
|
)
|
||||||
@@ -55,13 +56,13 @@ def test_classified_ad_can_be_created_without_price():
|
|||||||
ad = ClassifiedAd(
|
ad = ClassifiedAd(
|
||||||
id=ad_id,
|
id=ad_id,
|
||||||
_ownerId=owner_id,
|
_ownerId=owner_id,
|
||||||
title="Test Title",
|
title=ClassifiedAdTitle(title="Test Title"),
|
||||||
text="Test text content",
|
text="Test text content",
|
||||||
)
|
)
|
||||||
|
|
||||||
assert ad.id == ad_id
|
assert ad.id == ad_id
|
||||||
assert ad._ownerId == owner_id
|
assert ad._ownerId == owner_id
|
||||||
assert ad.title == "Test Title"
|
assert ad.title == ClassifiedAdTitle(title="Test Title")
|
||||||
assert ad.text == "Test text content"
|
assert ad.text == "Test text content"
|
||||||
assert ad.price == Price(amount=Decimal("0.00"))
|
assert ad.price == Price(amount=Decimal("0.00"))
|
||||||
|
|
||||||
@@ -71,7 +72,7 @@ def test_classified_ad_can_not_have_negative_price():
|
|||||||
ClassifiedAd(
|
ClassifiedAd(
|
||||||
id=ClassifiedAdId(value=uuid4()),
|
id=ClassifiedAdId(value=uuid4()),
|
||||||
_ownerId=UserId(value=uuid4()),
|
_ownerId=UserId(value=uuid4()),
|
||||||
title="Title",
|
title=ClassifiedAdTitle(title="Title"),
|
||||||
text="Text",
|
text="Text",
|
||||||
price=Price(amount=Decimal("-10.00")),
|
price=Price(amount=Decimal("-10.00")),
|
||||||
)
|
)
|
||||||
@@ -81,7 +82,7 @@ def test_classified_ad_is_immutable():
|
|||||||
ad = ClassifiedAd(
|
ad = ClassifiedAd(
|
||||||
id=ClassifiedAdId(value=uuid4()),
|
id=ClassifiedAdId(value=uuid4()),
|
||||||
_ownerId=UserId(value=uuid4()),
|
_ownerId=UserId(value=uuid4()),
|
||||||
title="Title",
|
title=ClassifiedAdTitle(title="Title"),
|
||||||
text="Text",
|
text="Text",
|
||||||
price=Price(amount=Decimal("10.00")),
|
price=Price(amount=Decimal("10.00")),
|
||||||
)
|
)
|
||||||
@@ -100,16 +101,28 @@ def test_classified_ad_equality():
|
|||||||
ad1 = ClassifiedAd(
|
ad1 = ClassifiedAd(
|
||||||
id=ad_id,
|
id=ad_id,
|
||||||
_ownerId=owner_id,
|
_ownerId=owner_id,
|
||||||
title="Title",
|
title=ClassifiedAdTitle(title="Title"),
|
||||||
text="Text",
|
text="Text",
|
||||||
price=Price(amount=Decimal("10.00")),
|
price=Price(amount=Decimal("10.00")),
|
||||||
)
|
)
|
||||||
ad2 = ClassifiedAd(
|
ad2 = ClassifiedAd(
|
||||||
id=ad_id,
|
id=ad_id,
|
||||||
_ownerId=owner_id,
|
_ownerId=owner_id,
|
||||||
title="Title",
|
title=ClassifiedAdTitle(title="Title"),
|
||||||
text="Text",
|
text="Text",
|
||||||
price=Price(amount=Decimal("10.00")),
|
price=Price(amount=Decimal("10.00")),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert ad1 == ad2
|
assert ad1 == ad2
|
||||||
|
|
||||||
|
|
||||||
|
def test_classified_ad_title_validation():
|
||||||
|
long_title = "a" * 101
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
ClassifiedAd(
|
||||||
|
id=ClassifiedAdId(value=uuid4()),
|
||||||
|
_ownerId=UserId(value=uuid4()),
|
||||||
|
title=ClassifiedAdTitle(title=long_title),
|
||||||
|
text="Text",
|
||||||
|
price=Price(amount=Decimal("10.00")),
|
||||||
|
)
|
||||||
|
|||||||
13
tests/test_classified_ad_title.py
Normal file
13
tests/test_classified_ad_title.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from domain.marketplace.classified_ad_title import ClassifiedAdTitle
|
||||||
|
|
||||||
|
|
||||||
|
def test_classified_ad_title_creation():
|
||||||
|
title = ClassifiedAdTitle(title="Test Ad")
|
||||||
|
|
||||||
|
assert title.title == "Test Ad"
|
||||||
|
|
||||||
|
def test_classified_ad_title_can_not_be_longer_then_100_chars():
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
ClassifiedAdTitle(title="efbqzzqmsjdlhkovmnmyykrjvlftrzzaefbqzzqmsjdlhkovmnmyykrjvlftrzzaefbqzzqmsjdlhkovmnmyykrjvlftrzzaefbqzzqmsjdlhkovmnmyykrjvlftrzzaefbqzzqmsjdlhkovmnmyykrjvlftrzza")
|
||||||
Reference in New Issue
Block a user