| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- from django.contrib.auth import get_user_model
- from news.forms import CommentForm
- from .test_constants import NEWS_COUNT_ON_HOME_PAGE
- User = get_user_model()
- def test_news_count_and_order(client, home_url, initial_news):
- response = client.get(home_url)
- object_list = response.context['object_list']
- news_count = len(object_list)
- assert news_count == NEWS_COUNT_ON_HOME_PAGE
- all_dates = [news.date for news in object_list]
- sorted_dates = sorted(all_dates, key=lambda x: x, reverse=True)
- assert all_dates == sorted_dates
- def test_comments_order(client, detail_url, initial_data):
- news_obj, _ = initial_data
- response = client.get(detail_url)
- assert 'news' in response.context
- news = response.context['news']
- all_comments = news.comment_set.all()
- all_dates = [comment.created for comment in all_comments]
- sorted_dates = sorted(all_dates, key=lambda x: x)
- assert all_dates == sorted_dates
- def test_anonymous_client_has_no_form(client, detail_url, initial_data):
- news_obj, _ = initial_data
- response = client.get(detail_url)
- assert 'form' not in response.context
- def test_authorized_client_has_form(client, detail_url, initial_data):
- news_obj, author = initial_data
- client.force_login(author)
- response = client.get(detail_url)
- assert 'form' in response.context
- assert isinstance(response.context['form'], CommentForm)
|