Skip to content

Commit 3e2ce14

Browse files
committed
Add unit tests for domain entity methods and validations
1 parent 132b0f9 commit 3e2ce14

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import datetime
2+
3+
import pytest
4+
5+
from conduit.core.exceptions import ArticlePermissionException
6+
from conduit.domain.entities.article import Article
7+
8+
9+
def test_article_assert_owner_raises() -> None:
10+
article = Article(
11+
id=1,
12+
author_id=1,
13+
slug="title-123",
14+
title="Title",
15+
description="desc",
16+
body="body",
17+
created_at=datetime.datetime.now(),
18+
)
19+
with pytest.raises(ArticlePermissionException):
20+
article.assert_owner(user_id=2)
21+
22+
23+
def test_article_update_content_updates_slug() -> None:
24+
article = Article(
25+
id=1,
26+
author_id=1,
27+
slug="title-123",
28+
title="Title",
29+
description="desc",
30+
body="body",
31+
created_at=datetime.datetime.now(),
32+
)
33+
article.update_content(title="New Title")
34+
assert article.title == "New Title"
35+
assert article.slug.startswith("new-title-")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import datetime
2+
3+
import pytest
4+
5+
from conduit.core.exceptions import CommentPermissionException
6+
from conduit.domain.entities.comment import Comment
7+
8+
9+
def test_comment_assert_owner_raises() -> None:
10+
comment = Comment(
11+
id=1,
12+
body="body",
13+
author_id=1,
14+
article_id=1,
15+
created_at=datetime.datetime.now(),
16+
)
17+
with pytest.raises(CommentPermissionException):
18+
comment.assert_owner(user_id=2)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import datetime
2+
3+
import pytest
4+
5+
from conduit.core.exceptions import (
6+
ArticleAlreadyFavoritedException,
7+
ArticleNotFavoritedException,
8+
)
9+
from conduit.domain.entities.favorite import Favorite
10+
11+
12+
def test_favorite_assert_not_favorited_raises() -> None:
13+
favorite = Favorite(
14+
user_id=1,
15+
article_id=1,
16+
created_at=datetime.datetime.now(),
17+
)
18+
with pytest.raises(ArticleAlreadyFavoritedException):
19+
favorite.assert_not_favorited(is_favorited=True)
20+
21+
22+
def test_favorite_assert_favorited_raises() -> None:
23+
favorite = Favorite(
24+
user_id=1,
25+
article_id=1,
26+
created_at=datetime.datetime.now(),
27+
)
28+
with pytest.raises(ArticleNotFavoritedException):
29+
favorite.assert_favorited(is_favorited=False)

tests/domain/test_follow_entity.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import datetime
2+
3+
import pytest
4+
5+
from conduit.core.exceptions import (
6+
OwnProfileFollowingException,
7+
ProfileAlreadyFollowedException,
8+
ProfileNotFollowedFollowedException,
9+
)
10+
from conduit.domain.entities.follow import Follow
11+
12+
13+
def test_follow_assert_not_self_raises() -> None:
14+
follow = Follow(
15+
follower_id=1,
16+
following_id=1,
17+
created_at=datetime.datetime.now(),
18+
)
19+
with pytest.raises(OwnProfileFollowingException):
20+
follow.assert_not_self()
21+
22+
23+
def test_follow_assert_not_already_followed_raises() -> None:
24+
follow = Follow(
25+
follower_id=1,
26+
following_id=2,
27+
created_at=datetime.datetime.now(),
28+
)
29+
with pytest.raises(ProfileAlreadyFollowedException):
30+
follow.assert_not_already_followed(is_followed=True)
31+
32+
33+
def test_follow_assert_followed_raises() -> None:
34+
follow = Follow(
35+
follower_id=1,
36+
following_id=2,
37+
created_at=datetime.datetime.now(),
38+
)
39+
with pytest.raises(ProfileNotFollowedFollowedException):
40+
follow.assert_followed(is_followed=False)

tests/domain/test_tag_entity.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import datetime
2+
3+
import pytest
4+
5+
from conduit.domain.entities.tag import Tag
6+
7+
8+
@pytest.mark.parametrize("invalid_tag", ["", " "])
9+
def test_tag_rename_validates(invalid_tag: str) -> None:
10+
tag = Tag(id=1, tag="tag", created_at=datetime.datetime.now())
11+
with pytest.raises(ValueError):
12+
tag.rename(invalid_tag)

tests/domain/test_user_entity.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import datetime
2+
3+
import pytest
4+
5+
from conduit.core.exceptions import (
6+
EmailAlreadyTakenException,
7+
UserNameAlreadyTakenException,
8+
)
9+
from conduit.domain.entities.user import User
10+
11+
12+
def test_user_ensure_unique_email_raises() -> None:
13+
user = User(
14+
id=1,
15+
username="user",
16+
email="user@example.com",
17+
password_hash="hash",
18+
bio="",
19+
image_url=None,
20+
created_at=datetime.datetime.now(),
21+
)
22+
with pytest.raises(EmailAlreadyTakenException):
23+
user.ensure_unique_email(exists=True)
24+
25+
26+
def test_user_ensure_unique_username_raises() -> None:
27+
user = User(
28+
id=1,
29+
username="user",
30+
email="user@example.com",
31+
password_hash="hash",
32+
bio="",
33+
image_url=None,
34+
created_at=datetime.datetime.now(),
35+
)
36+
with pytest.raises(UserNameAlreadyTakenException):
37+
user.ensure_unique_username(exists=True)
38+
39+
40+
@pytest.mark.parametrize("invalid_email", ["", "invalid"])
41+
def test_user_change_email_validates(invalid_email: str) -> None:
42+
user = User(
43+
id=1,
44+
username="user",
45+
email="user@example.com",
46+
password_hash="hash",
47+
bio="",
48+
image_url=None,
49+
created_at=datetime.datetime.now(),
50+
)
51+
with pytest.raises(ValueError):
52+
user.change_email(invalid_email)
53+
54+
55+
@pytest.mark.parametrize("invalid_username", ["", " ", "ab"])
56+
def test_user_change_username_validates(invalid_username: str) -> None:
57+
user = User(
58+
id=1,
59+
username="user",
60+
email="user@example.com",
61+
password_hash="hash",
62+
bio="",
63+
image_url=None,
64+
created_at=datetime.datetime.now(),
65+
)
66+
with pytest.raises(ValueError):
67+
user.change_username(invalid_username)

0 commit comments

Comments
 (0)