Skip to content

Unittests v1 - #7

Open
amIworking wants to merge 15 commits into
mainfrom
unittests-v1
Open

Unittests v1#7
amIworking wants to merge 15 commits into
mainfrom
unittests-v1

Conversation

@amIworking

Copy link
Copy Markdown
Owner

Добавил юнит тесты для основных апи методов юзера и книги. Напиши, что нужно поправить/добавить, потом по тому же принципу добавлю для комментариев

@intsynko

Copy link
Copy Markdown
Collaborator

А где тут тесты то?)
Снимок экрана 2024-04-27 в 08 35 52

def test_book_list(self):
url = reverse('books-list')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут только успешные сценарии - можно проверить что если не залогинен получишь 401 или если книга не твоя при изменении получишь 400

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

так тут просто лист книг возвращается, ничего не запрещено

Comment thread backend/apps/books/tests/test_book.py Outdated
"genre": "novel",
"writing_date": "1984-01-01"}
self.book = Book.objects.create(author_id=self.author.id,
user_id=self.user.id, **book_data)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

модели в тестах удобно создавать через factory-boy погугли про такой пакет, так же с помощью него и дикты тоже можно генерировать

Comment thread backend/apps/books/tests/test_book.py Outdated
from apps.users.models import User
from apps.books.models import Author, BookReview, Book
from apps.users.views import UserView
class PositiveApiBookTestCase(APITestCase):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

обычно тесты не делят на позитивные и негативные - просто все подряд


from apps.users.models import User
from apps.users.views import UserView
class PositiveApiUserTestCase(APITestCase):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а этот тест?

def setUp(self):
user_data = {'email': 'test@mail.ru',
'password': '123456!'}
self.user = User.objects.create(**user_data)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут фабрику бы

self.other_user = UserFactory()
self.other_user_client = UserFactory.get_auth_client(user=self.other_user)

self.admin = User.objects.create_superuser(email='admin@mail.ru',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

здест тоже фабрику, если не получается - значит что то неправильно делаешь, должно работать так: UserFactory(is_superadmin=True)

"genre": "horror",
"writing_date": "2001-01-01",
"author": self.author.id,
"user": self.user.id}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

дикты тоже можно фабриками генерировать, во всех следующих тестах тоже

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants