def test_get_index_entry_queryset(): now = timezone.now() pinned_post1 = PostFactory( status=Post.STATUS_CHOICES.published, pinned=True, show_on_index=True, pub_date=now, ) pinned_post2 = PostFactory( status=Post.STATUS_CHOICES.published, pinned=True, show_on_index=True, pub_date=now - timedelta(days=1), ) material = MaterialFactory(status=Material.STATUS.published, show_on_index=True, pub_date=now) post = PostFactory( status=Post.STATUS_CHOICES.published, pinned=False, show_on_index=True, pub_date=now - timedelta(days=1), ) expected = [pinned_post1.id, pinned_post2.id, material.id, post.id] assert [entry.id for entry in get_index_entry_queryset()] == expected
def setUp(self): self.client.login(username=self.users[0].username, password=self.password) self.private_user0 = PostFactory(user=self.users[0], title='private_user0', is_public=False) self.private_user5 = PostFactory(user=self.users[5], title='private_user5', is_public=False)
def test_post_vote_CRUD(self): post = PostFactory(author=self.user) # Create data = {'author': self.user.id, 'post': post.id, 'vote': 1} self.client.post(reverse('vote-list'), data=data) vote = Vote.objects.first() self.assertEqual(vote.vote, data['vote']) # Retrieve response = self.client.get( reverse('vote-detail', kwargs={'pk': vote.id})) self.assertEqual(response.data['id'], vote.id) self.assertEqual(response.data['post'], vote.post.id) # Update update_data = {'vote': -1} self.client.patch(reverse('vote-detail', kwargs={'pk': vote.id}), data=update_data) updated_vote = Vote.objects.first() self.assertEqual(updated_vote.vote, update_data['vote']) # Delete self.client.delete(reverse('vote-detail', kwargs={'pk': vote.id})) self.assertEqual(Vote.objects.count(), 0)
def handle(self, *args, **options): users = [] posts = [] votes = [] with open(settings.BASE_DIR + '/bot-config.json') as config: config = json.load(config) for x in range(config['number_of_users']): user = UserFactory(email=generate_random_email()) users.append(user) for user in users: random_posts = FuzzyInteger(0, config['max_posts_per_user']).fuzz() print('Random posts for User ({}) -> {}'.format( user.username, random_posts)) for _ in range(random_posts): posts.append(PostFactory(author=user)) for user in users: random_votes = FuzzyInteger(0, config['max_votes_per_user']).fuzz() print('Random votes for User ({}) -> {}'.format( user.username, random_votes)) for _ in range(random_votes): votes.append( VoteFactory(author=user, post=random.choice(posts)))
def setUp(self): self.client.login(username=self.users[0].username, password=self.password) _tags = tuple(tag.pk for tag in self.tags[0]) _relation_posts = tuple([self.posts[0]]) self.created_post = PostFactory.create(user=self.users[0], tags=_tags, relation_posts=_relation_posts)
def setUp(self): self.category1 = PostCategoryFactory(name="cate1") self.category2 = PostCategoryFactory(name="cate2") self.post = PostFactory( title="Post", show_on_index=True, status=Post.STATUS_CHOICES.published, category=self.category1, )
def setUp(self): self.view = views.UserPostListView self.user1 = UserFactory() self.user2 = UserFactory() self.url = reverse('blog:user-posts', kwargs={'username': self.user1.username}) self.posts_user1 = PostFactory.create_batch(2, author=self.user1) self.request = RequestFactory().get(self.url) self.response = self.view.as_view()(self.request, username=self.user1.username)
def setUp(self): self.category = PostCategoryFactory() self.category_post1 = PostFactory( title="Category Post1", show_on_index=True, status=Post.STATUS_CHOICES.published, category=self.category, ) self.category_post2 = PostFactory( title="Category Post2", show_on_index=True, status=Post.STATUS_CHOICES.published, category=self.category, ) self.no_category_post = PostFactory( title="No Category Post", show_on_index=False, status=Post.STATUS_CHOICES.published, category=None, )
def test_add_and_remove_posts_series(self, series): posts = [PostFactory() for _ in range(5)] for post in posts: series.posts.add(post) assert series.posts.count() == 5 for post in posts[:2]: series.posts.remove(post) assert series.posts.count() == 3
def setup_method(self): after_3_days = timezone.now() + timedelta(days=3) self.published_post = PostFactory( status=Post.STATUS_CHOICES.published, show_on_index=True, ) self.draft_post = PostFactory( status=Post.STATUS_CHOICES.draft, show_on_index=True, ) self.hidden_post = PostFactory( status=Post.STATUS_CHOICES.hidden, show_on_index=True, ) self.future_publishing_post = PostFactory( status=Post.STATUS_CHOICES.published, show_on_index=True, pub_date=after_3_days, ) self.future_draft_post = PostFactory( status=Post.STATUS_CHOICES.draft, show_on_index=True, pub_date=after_3_days, ) self.hide_on_index_published_post = PostFactory( status=Post.STATUS_CHOICES.published, show_on_index=False, )
def setup_method(self): site = Site.objects.get(name="example.com") post = PostFactory() self.comment = BlogCommentFactory(is_public=True, is_removed=False, site=site, content_object=post) form = BlogCommentForm(target_object=post, parent=self.comment) self.bound_form = BlogCommentForm( target_object=post, parent=self.comment, data=dict(**form.initial, comment="test comment"), )
def setup_method(self): site = Site.objects.get(name="example.com") post = PostFactory() self.visible_root_comment = BlogCommentFactory(is_public=True, is_removed=False, site=site, content_object=post) self.visible_child_comment = BlogCommentFactory( is_public=True, is_removed=False, parent=self.visible_root_comment, site=site, content_object=post, )
def setup_method(self): self.admin_user = User.objects.create_superuser( username="******", email="*****@*****.**", password="******") self.normal_user = User.objects.create_user( username="******", email="*****@*****.**", password="******") site = Site.objects.get(name="example.com") post = PostFactory(author=self.admin_user, body="正文") self.ct = str(post._meta) self.object_pk = post.pk self.client = APIClient() self.root_comment_a = BlogCommentFactory(is_public=True, is_removed=False, site=site, content_object=post) self.root_comment_b = BlogCommentFactory(is_public=True, is_removed=False, site=site, content_object=post) self.child_comment_b = BlogCommentFactory( is_public=True, is_removed=False, site=site, content_object=post, parent=self.root_comment_b, ) self.root_comment_c = BlogCommentFactory(is_public=True, is_removed=False, site=site, content_object=post) self.child_comment_c = BlogCommentFactory( is_public=True, is_removed=False, site=site, content_object=post, parent=self.root_comment_c, ) self.grandchild_comment_c = BlogCommentFactory( is_public=True, is_removed=False, site=site, content_object=post, parent=self.child_comment_c, )
def setup_method(self): self.moderator = User.objects.create_superuser( username="******", email="*****@*****.**", password="******", ) self.user = User.objects.create_user( username="******", email="*****@*****.**", password="******", **{"email_bound": True}, ) self.other_user = User.objects.create_user( username="******", email="*****@*****.**", password="******", **{"email_bound": True}, ) site = Site.objects.get(name="example.com") post = PostFactory(author=self.moderator, body="正文") self.ct = str(post._meta) self.object_pk = post.pk self.client = APIClient() self.url = reverse("comment-list") self.moderator_comment = BlogCommentFactory( is_public=True, is_removed=False, site=site, content_object=post, user=self.moderator, ) self.user_comment = BlogCommentFactory( is_public=True, is_removed=False, site=site, content_object=post, user=self.user, ) self.other_user_comment = BlogCommentFactory( is_public=True, is_removed=False, site=site, content_object=post, user=self.other_user, )
class PostDetailViewTestCase(TestCase): def setUp(self): category = PostCategoryFactory() self.index_post = PostFactory( show_on_index=True, status=Post.STATUS_CHOICES.published, category=category, ) self.not_index_post = PostFactory( show_on_index=False, status=Post.STATUS_CHOICES.published, category=None, ) self.draft_post = PostFactory( show_on_index=True, status=Post.STATUS_CHOICES.draft ) def test_good_view(self): url = self.index_post.get_absolute_url() response = self.client.get(url) assert response.status_code == 200 self.assertTemplateUsed(response, "blog/detail.html") self.assertIn("num_comments", response.context_data) self.assertIn("num_comment_participants", response.context_data) def test_only_public_posts_are_available(self): available_url1 = self.index_post.get_absolute_url() available_url2 = self.not_index_post.get_absolute_url() unavailable_url = self.draft_post.get_absolute_url() response = self.client.get(available_url1) self.assertEqual(response.status_code, 200) response = self.client.get(available_url2) self.assertEqual(response.status_code, 200) response = self.client.get(unavailable_url) self.assertEqual(response.status_code, 404) def test_headline(self): category_post_url = self.index_post.get_absolute_url() no_category_post_url = self.not_index_post.get_absolute_url() response = self.client.get(category_post_url) self.assertContains( response, "%s_%s" % (self.index_post.title, self.index_post.category) ) response = self.client.get(no_category_post_url) self.assertContains(response, "%s" % (self.not_index_post.title,))
def setUp(self): category = PostCategoryFactory() self.index_post = PostFactory( show_on_index=True, status=Post.STATUS_CHOICES.published, category=category, ) self.not_index_post = PostFactory( show_on_index=False, status=Post.STATUS_CHOICES.published, category=None, ) self.draft_post = PostFactory( show_on_index=True, status=Post.STATUS_CHOICES.draft )
def test_entries_display(self): course = CourseFactory() pinned_post = PostFactory( pinned=True, show_on_index=True, status=Post.STATUS_CHOICES.published, ) material = MaterialFactory( course=course, show_on_index=True, status=Material.STATUS.published, pub_date=timezone.now(), ) response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertContains(response, "[置顶] " + pinned_post.title) self.assertContains(response, pinned_post.brief) self.assertContains(response, material.title) self.assertContains(response, "系列教程")
def test_auto_populate_excerpt_from_body(self): post = PostFactory(body="正文" * 100) assert len(post.excerpt) == 150
def setUp(self): self.client.login(username=self.users[0].username, password=self.password) self.target_post = PostFactory(user=self.users[0])
def setUpTestData(cls): cls.password = '******' cls.users = UserFactory.create_batch(7) view_user = Permission.objects.get(codename='view_user') view_perm = Permission.objects.get(codename='view_permission') for user in cls.users: user.user_permissions.add(view_user, view_perm) cls.tags = [ ( TagFactory.create(user=cls.users[0]), TagFactory.create(user=cls.users[0], name='sample_a'), ), ( TagFactory.create(user=cls.users[1]), TagFactory.create(user=cls.users[1], name='sample_b'), ), ( TagFactory.create(user=cls.users[2]), TagFactory.create(user=cls.users[2], name='sample_c'), ), ( TagFactory.create(user=cls.users[3], name='info_d'), TagFactory.create(user=cls.users[3], name='sample_d'), TagFactory.create(user=cls.users[3], name='seed_d'), ), ( TagFactory.create(user=cls.users[4], name='data_e'), TagFactory.create(user=cls.users[4], name='sample_e'), TagFactory.create(user=cls.users[4], name='type_e'), ), ] posts = [ ( PostFactory(user=cls.users[0], title='post10'), PostFactory(user=cls.users[0], title='title20'), PostFactory(user=cls.users[0], title='case30'), ), ( PostFactory(user=cls.users[1], title='post11'), PostFactory(user=cls.users[1], title='title21'), PostFactory(user=cls.users[1], title='case31'), ), ( PostFactory(user=cls.users[2], title='post12'), PostFactory(user=cls.users[2], title='title22'), PostFactory(user=cls.users[2], title='case32'), ), (PostFactory(user=cls.users[3], title='bot13'), PostFactory(user=cls.users[3], title='bot23'), PostFactory(user=cls.users[3], title='private', is_public=False)), ( PostFactory(user=cls.users[5], title='shape15'), PostFactory(user=cls.users[5], title='shape25'), ), ] user_tag_post_combination = [ (cls.users[0], (cls.tags[0][0], ), (posts[0][0], posts[0][1])), (cls.users[0], cls.tags[0], posts[0]), (cls.users[1], (cls.tags[1][1], ), (posts[1][1], )), (cls.users[1], cls.tags[1], posts[1]), (cls.users[2], (cls.tags[2][0], ), (posts[2][0], posts[2][2])), (cls.users[2], cls.tags[2], posts[2]), (cls.users[3], cls.tags[3], posts[3]), (cls.users[4], cls.tags[4], tuple()), (cls.users[5], tuple(), posts[4]), ] cls.posts = [ PostFactory.create( user=_user, tags=(_tag.pk for _tag in _tags), relation_posts=(_post.pk for _post in _posts), ) for _user, _tags, _posts in user_tag_post_combination ]
def run(): admin_user = User.objects.get(username="******") for cate in Category.objects.all(): size = random.randint(10, 20) PostFactory.create_batch(size, author=admin_user, category=cate) print("Posts created.")
def test_auto_set_pub_date_for_published_post(self): post = PostFactory(status=Post.STATUS_CHOICES.published, ) assert post.pub_date == post.created
def post(user): return PostFactory(author=user, body="正文")