feat: add UserId value object and update ClassifiedAd to use it
This commit is contained in:
@@ -1,35 +1,27 @@
|
||||
# Plan: ClassifiedAd Domain Entity
|
||||
# Plan: UserId Value Object
|
||||
|
||||
## Status: COMPLETED
|
||||
|
||||
## Objective
|
||||
Create the first Domain Entity representing a ClassifiedAd with the specified attributes.
|
||||
Create a UserId value object in the marketplace domain with an immutable UUID value.
|
||||
|
||||
## Decisions Made
|
||||
- **Location**: `domain/marketplace/classified_ad.py`
|
||||
- **Location**: `domain/marketplace/user_id.py`
|
||||
- **Class style**: `@dataclass(frozen=True)` for immutability
|
||||
- **Validation**: No validation, just hold data
|
||||
- **Tests**: Yes, create unit tests
|
||||
- **Tests**: Yes
|
||||
|
||||
## Entity Structure
|
||||
## Value Object Structure
|
||||
```python
|
||||
@dataclass(frozen=True)
|
||||
class ClassifiedAd:
|
||||
id: UUID
|
||||
_ownerId: UUID
|
||||
title: str
|
||||
text: str
|
||||
_price: Decimal
|
||||
class UserId:
|
||||
value: UUID
|
||||
```
|
||||
|
||||
## Files Created
|
||||
- `domain/__init__.py`
|
||||
- `domain/marketplace/__init__.py`
|
||||
- `domain/marketplace/classified_ad.py`
|
||||
- `tests/test_classified_ad.py`
|
||||
- `pyproject.toml`
|
||||
- `domain/marketplace/user_id.py`
|
||||
- `tests/test_user_id.py`
|
||||
|
||||
## Test Results
|
||||
- `test_classified_ad_creation` - PASSED
|
||||
- `test_classified_ad_is_immutable` - PASSED
|
||||
- `test_classified_ad_equality` - PASSED
|
||||
- `test_user_id_creation` - PASSED
|
||||
- `test_user_id_is_immutable` - PASSED
|
||||
- `test_user_id_equality` - PASSED
|
||||
@@ -1 +1 @@
|
||||
from .marketplace import classified_ad
|
||||
from .marketplace import classified_ad as classified_ad
|
||||
@@ -1 +1,2 @@
|
||||
from .classified_ad import ClassifiedAd
|
||||
from .classified_ad import ClassifiedAd as ClassifiedAd
|
||||
from .user_id import UserId as UserId
|
||||
@@ -2,11 +2,13 @@ from dataclasses import dataclass
|
||||
from decimal import Decimal
|
||||
from uuid import UUID
|
||||
|
||||
from domain.marketplace.user_id import UserId
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ClassifiedAd:
|
||||
id: UUID
|
||||
_ownerId: UUID
|
||||
_ownerId: UserId
|
||||
title: str
|
||||
text: str
|
||||
_price: Decimal
|
||||
7
domain/marketplace/user_id.py
Normal file
7
domain/marketplace/user_id.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from dataclasses import dataclass
|
||||
from uuid import UUID
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class UserId:
|
||||
value: UUID
|
||||
@@ -1,12 +1,13 @@
|
||||
from decimal import Decimal
|
||||
from uuid import UUID, uuid4
|
||||
from uuid import uuid4
|
||||
|
||||
from domain.marketplace.classified_ad import ClassifiedAd
|
||||
from domain.marketplace.user_id import UserId
|
||||
|
||||
|
||||
def test_classified_ad_creation():
|
||||
ad_id = uuid4()
|
||||
owner_id = uuid4()
|
||||
owner_id = UserId(value=uuid4())
|
||||
|
||||
ad = ClassifiedAd(
|
||||
id=ad_id,
|
||||
@@ -26,7 +27,7 @@ def test_classified_ad_creation():
|
||||
def test_classified_ad_is_immutable():
|
||||
ad = ClassifiedAd(
|
||||
id=uuid4(),
|
||||
_ownerId=uuid4(),
|
||||
_ownerId=UserId(value=uuid4()),
|
||||
title="Title",
|
||||
text="Text",
|
||||
_price=Decimal("10.00"),
|
||||
@@ -41,7 +42,7 @@ def test_classified_ad_is_immutable():
|
||||
|
||||
def test_classified_ad_equality():
|
||||
ad_id = uuid4()
|
||||
owner_id = uuid4()
|
||||
owner_id = UserId(value=uuid4())
|
||||
|
||||
ad1 = ClassifiedAd(
|
||||
id=ad_id,
|
||||
|
||||
25
tests/test_user_id.py
Normal file
25
tests/test_user_id.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from domain.marketplace.user_id import UserId
|
||||
|
||||
|
||||
def test_user_id_creation():
|
||||
user_id = UserId(value=uuid4())
|
||||
assert isinstance(user_id.value, UUID)
|
||||
|
||||
|
||||
def test_user_id_is_immutable():
|
||||
user_id = UserId(value=uuid4())
|
||||
|
||||
try:
|
||||
user_id.value = uuid4() # type: ignore
|
||||
assert False, "Should have raised FrozenInstanceError"
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
||||
def test_user_id_equality():
|
||||
uid = uuid4()
|
||||
user_id1 = UserId(value=uid)
|
||||
user_id2 = UserId(value=uid)
|
||||
assert user_id1 == user_id2
|
||||
Reference in New Issue
Block a user