Skip to content

Commit

Permalink
Added Base testing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpro2022 committed Dec 9, 2023
1 parent 7c52b6b commit 68da444
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __tablename__(cls):
id: Mapped[int] = mapped_column(primary_key=True)

def __repr__(self) -> str:
return (f'\nid: {self.id}\n')
return f'\nid: {self.id}\n'


engine = create_async_engine(settings.database_url)
Expand Down
27 changes: 19 additions & 8 deletions tests/unit_tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import pytest

from tests.conftest import Post
from tests.conftest import Base, Post

BASE_FIELDS = ('id',)
POST_MODEL_FIELDS = ('id', 'title', 'content', 'created', 'updated', 'likes', 'dislikes', 'author_id', 'author')
POST_SAVE_DATA = {'title': 'Another New post title.', 'content': 'POST New post content.', 'author_id': 1}

class BaseTest(Base):
pass

def test_model_fields() -> None:
for field in POST_MODEL_FIELDS:
assert hasattr(Post, field)

@pytest.mark.parametrize('class_, class_fields', (
(Base, BASE_FIELDS),
(Post, POST_MODEL_FIELDS),
))
def test_model_fields(class_, class_fields) -> None:
for field in class_fields:
assert hasattr(class_, field)

def test_model_repr() -> None:
representation = str(Post(**POST_SAVE_DATA))
for attr_name in POST_MODEL_FIELDS[1:-1]:
assert representation.find(attr_name) != -1

@pytest.mark.parametrize('instance, instance_fields', (
(BaseTest(), BASE_FIELDS),
(Post(**POST_SAVE_DATA), POST_MODEL_FIELDS[1:-1]),
))
def test_model_repr(instance, instance_fields) -> None:
for attr_name in instance_fields:
assert instance.__repr__().find(attr_name) != -1

0 comments on commit 68da444

Please sign in to comment.