Skip to content

Commit

Permalink
Add new validators for domain services, refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madnoberson committed Mar 11, 2024
1 parent 2da0c22 commit 4293660
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 25 deletions.
9 changes: 9 additions & 0 deletions src/amdb/domain/constants/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@
"Rating value must be from 0 to 10 and be a multiple of 0.5"
)
INVALID_EMAIL = "Email is invalid"
INVALID_MOVIE_TITLE = (
"Number of characters in movie title must be more than 1 and less than 128"
)
INVALID_USER_NAME = (
"Number of characters in user name must be more than 1 and less than 128."
"User name must not contain spaces."
)
INVALID_REVIEW_TITLE = "Number of characters in review title must be more than 5 and less than 128"
INVALID_REVIEW_CONTENT = "Number of characters in review content must be more than 5 and less than 1024"
16 changes: 16 additions & 0 deletions src/amdb/domain/services/create_movie.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from datetime import date

from amdb.domain.entities.movie import MovieId, Movie
from amdb.domain.constants.exceptions import INVALID_MOVIE_TITLE
from amdb.domain.exception import DomainError


MOVIE_TITLE_MIN_LENGTH = 1
MOVIE_TITLE_MAX_LENGTH = 128


class CreateMovie:
Expand All @@ -11,10 +17,20 @@ def __call__(
title: str,
release_date: date,
) -> Movie:
self._validate_title(title)

return Movie(
id=id,
title=title,
release_date=release_date,
rating=0,
rating_count=0,
)

def _validate_title(self, title: str) -> None:
title_length = len(title)
if (
title_length < MOVIE_TITLE_MIN_LENGTH
or title_length > MOVIE_TITLE_MAX_LENGTH
):
raise DomainError(INVALID_MOVIE_TITLE)
18 changes: 18 additions & 0 deletions src/amdb/domain/services/create_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

from amdb.domain.entities.user import UserId, User
from amdb.domain.validators.email import ValidateEmail
from amdb.domain.constants.exceptions import INVALID_USER_NAME
from amdb.domain.exception import DomainError


USER_NAME_MIN_LENGTH = 1
USER_NAME_MAX_LENGTH = 128


class CreateUser:
Expand All @@ -23,8 +29,20 @@ def __call__(
else:
email = None

self._validate_name(name)

return User(
id=id,
name=name,
email=email,
)

def _validate_name(self, name: str) -> None:
name_length = len(name)
name_has_spaces = len(name.split()) != 1
if (
name_length < USER_NAME_MIN_LENGTH
or name_length > USER_NAME_MAX_LENGTH
or name_has_spaces
):
raise DomainError(INVALID_USER_NAME)
7 changes: 5 additions & 2 deletions src/amdb/domain/services/rate_movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def __call__(
rating: float,
current_timestamp: datetime,
) -> Rating:
if rating <= 0 or rating > 10 or rating % 0.5 != 0:
raise DomainError(INVALID_RATING_VALUE)
self._validate_rating(rating)

movie.rating = (movie.rating * movie.rating_count + rating) / (
movie.rating_count + 1
Expand All @@ -32,3 +31,7 @@ def __call__(
value=rating,
created_at=current_timestamp,
)

def _validate_rating(self, rating: float) -> None:
if rating <= 0 or rating > 10 or rating % 0.5 != 0:
raise DomainError(INVALID_RATING_VALUE)
30 changes: 30 additions & 0 deletions src/amdb/domain/services/review_movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
from amdb.domain.entities.user import User
from amdb.domain.entities.movie import Movie
from amdb.domain.entities.review import ReviewId, ReviewType, Review
from amdb.domain.constants.exceptions import (
INVALID_REVIEW_TITLE,
INVALID_REVIEW_CONTENT,
)
from amdb.domain.exception import DomainError


REVIEW_TITLE_MIN_LENGTH = 5
REVIEW_TITLE_MAX_LENGTH = 128
REVIEW_CONTENT_MIN_LENGTH = 5
REVIEW_CONTENT_MAX_LENGTH = 1024


class ReviewMovie:
Expand All @@ -17,6 +28,9 @@ def __call__(
type: ReviewType,
current_timestamp: datetime,
) -> Review:
self._validate_title(title)
self._validate_content(content)

return Review(
id=id,
user_id=user.id,
Expand All @@ -26,3 +40,19 @@ def __call__(
type=type,
created_at=current_timestamp,
)

def _validate_title(self, title: str) -> None:
title_length = len(title)
if (
title_length < REVIEW_TITLE_MIN_LENGTH
or title_length > REVIEW_CONTENT_MAX_LENGTH
):
raise DomainError(INVALID_REVIEW_TITLE)

def _validate_content(self, content: str) -> None:
content_length = len(content)
if (
content_length < REVIEW_CONTENT_MIN_LENGTH
or content_length > REVIEW_CONTENT_MAX_LENGTH
):
raise DomainError(INVALID_REVIEW_CONTENT)
6 changes: 3 additions & 3 deletions tests/unit/application/command_handlers/test_rate_movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_rate_movie(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_rate_movie_should_raise_error_when_movie_already_rated(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down Expand Up @@ -229,7 +229,7 @@ def test_rate_movie_should_raise_error_when_rating_is_invalid(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/application/command_handlers/test_register_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_register_user(
password_manager: PasswordManager,
):
command = RegisterUserCommand(
name="John Doe",
name="JohnDoe",
email="John@doe.com",
password="Secret",
)
Expand All @@ -45,7 +45,7 @@ def test_create_user_should_raise_error_when_user_name_already_exists(
unit_of_work: UnitOfWork,
password_manager: PasswordManager,
):
user_name = "John Doe"
user_name = "JohnDoe"

user = User(
id=UserId(uuid7()),
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_create_user_should_raise_error_when_user_email_already_exists(
unit_of_work.commit()

command = RegisterUserCommand(
name="Johny Doe",
name="JohnyDoe",
email=user_email,
password="Secret",
)
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/application/command_handlers/test_review_movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_review_movie(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_review_movie_should_raise_error_when_access_is_denied(
command = ReviewMovieCommand(
movie_id=MovieId(uuid7()),
title="Mid",
content="So-so",
content="So-so..",
type=ReviewType.NEUTRAL,
)
handler = ReviewMovieHandler(
Expand Down Expand Up @@ -159,7 +159,7 @@ def test_review_movie_should_raise_error_when_movie_already_reviewed(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/application/command_handlers/test_unrate_movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_unrate_movie(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down Expand Up @@ -154,7 +154,7 @@ def test_unrate_movie_should_raise_error_when_user_is_not_rating_owner(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_update_my_profile(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_get_detailed_movie(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_get_detailed_reviews(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_export_my_ratings_in_csv(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/application/query_handlers/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_login(

user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_login_should_raise_error_when_user_does_not_exist(
password_manager: PasswordManager,
):
login_query = LoginQuery(
name="John Doe",
name="JohnDoe",
password="secret",
)
login_handler = LoginHandler(
Expand All @@ -91,7 +91,7 @@ def test_login_should_raise_error_when_password_is_incorrect(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down Expand Up @@ -130,7 +130,7 @@ def test_login_should_raise_error_when_access_is_denied(

user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_get_my_detailed_ratings(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_get_non_detailed_movies(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_request_my_ratings_export(
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email="John@doe.com",
)
user_gateway.save(user)
Expand Down Expand Up @@ -81,7 +81,7 @@ def test_request_my_ratings_export_should_raise_error_when_user_cannot_use_sendi
):
user = User(
id=UserId(uuid7()),
name="John Doe",
name="JohnDoe",
email=email,
)
user_gateway.save(user)
Expand Down

0 comments on commit 4293660

Please sign in to comment.