def test_the_number_of_posts_to_display_when_a_user_visits(self): ''' superuserのリクエストはすべての投稿を表示 それ以外のユーザーは公開済みの投稿を表示 ''' self.assertEqual(Post.objects.all().count(), 0) non_published_count = 2 published_count = 3 PostFactory.create_batch(size=non_published_count, author=self.superuser, published_at=None) PostFactory.create_batch(size=published_count, author=self.superuser) # superuserのリクエスト client = Client() client.force_login(self.superuser) response = client.get('/') self.assertTrue(response.wsgi_request.user.is_superuser) self.assertEqual(len(response.context['posts']), non_published_count + published_count) self.assertQuerysetEqual(response.context['posts'], Post.objects.all(), transform=lambda x: x) # 一般ユーザーのリクエスト response = self.client.get('/') self.assertIn('posts', response.context) self.assertTrue(Post.objects.all().count() == non_published_count + published_count) self.assertTrue(len(response.context['posts']) == published_count) self.assertQuerysetEqual(response.context['posts'], Post.objects.published(), transform=lambda x: x)
def populate(): ProfileFactory.create_batch(size=10) PostFactory.create_batch(size=4) PostImageFactory.create_batch(size=16) PaperFactory.create_batch(size=10) for p in Post.objects.all(): print(p) print("...population finished")
def test_pagination(self): ''' ページネーションの動作確認 ''' # 1ページ当たりの表示するPost数 views.IndexView.paginate_by = 10 # 15件のPostを作成 PostFactory.create_batch(size=15, author=self.superuser) # 1ページ目 response = self.client.get('/') self.assertIn('paginator', response.context) self.assertEquals(10, response.context['posts'].count()) self.assertEquals(15, Post.objects.all().count()) # 2ページ目 response = self.client.get('/?page=2') self.assertEquals(5, response.context['posts'].count()) # 3ページ目 response = self.client.get('/?page=3') self.assertEquals(404, response.status_code)
def setUp(self): PostFactory.create_batch(3)
def handle(self, *args, **options): PostCategoryFactory.create_batch(10) PostFactory.create_batch(20)
def test_post_image(self): PostFactory.create_batch(size=2) test_image = PostImageFactory(caption='Test') self.assertTrue(isinstance(test_image, PostImage)) self.assertEqual('Test', test_image.__str__())