本文整理汇总了Python中machina.test.factories.TopicPollFactory类的典型用法代码示例。如果您正苦于以下问题:Python TopicPollFactory类的具体用法?Python TopicPollFactory怎么用?Python TopicPollFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TopicPollFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_can_tell_if_a_user_can_vote_in_a_poll
def test_can_tell_if_a_user_can_vote_in_a_poll(self):
# Setup
def get_rendered(poll, user):
request = self.request_factory.get('/')
request.user = user
ForumPermissionHandlerMiddleware().process_request(request)
t = Template(
self.loadstatement + '{% get_permission \'can_vote_in_poll\' poll request.user as user_can_vote_in_poll %}'
'{% if user_can_vote_in_poll %}CAN_VOTE{% else %}CANNOT_VOTE{% endif %}')
c = Context({'poll': poll, 'request': request})
rendered = t.render(c)
return rendered
self.poll_1 = TopicPollFactory.create(topic=self.forum_1_topic)
self.poll_2 = TopicPollFactory.create(topic=self.forum_2_topic)
assign_perm('can_see_forum', self.g1, self.forum_1)
assign_perm('can_read_forum', self.g1, self.forum_1)
assign_perm('can_edit_own_posts', self.g1, self.forum_1)
assign_perm('can_delete_own_posts', self.g1, self.forum_1)
assign_perm('can_reply_to_topics', self.g1, self.forum_1)
assign_perm('can_see_forum', self.moderators, self.forum_1)
assign_perm('can_read_forum', self.moderators, self.forum_1)
assign_perm('can_edit_own_posts', self.moderators, self.forum_1)
assign_perm('can_delete_own_posts', self.moderators, self.forum_1)
assign_perm('can_edit_posts', self.moderators, self.forum_1)
assign_perm('can_delete_posts', self.moderators, self.forum_1)
assign_perm('can_vote_in_polls', self.g1, self.forum_1)
# Run & check
assert get_rendered(self.poll_1, self.u1) == 'CAN_VOTE'
assert get_rendered(self.poll_2, self.u1) == 'CANNOT_VOTE'
assert get_rendered(self.poll_1, self.superuser) == 'CAN_VOTE'
assert get_rendered(self.poll_2, self.superuser) == 'CAN_VOTE'
示例2: test_knows_if_a_user_can_vote_in_polls
def test_knows_if_a_user_can_vote_in_polls(self):
# Setup
poll_1 = TopicPollFactory.create(topic=self.forum_1_topic)
poll_2 = TopicPollFactory.create(topic=self.forum_3_topic)
poll_3 = TopicPollFactory.create(topic=self.forum_3_topic_2)
assign_perm('can_vote_in_polls', self.u1, self.forum_1)
# Run & check
assert self.perm_handler.can_vote_in_poll(poll_1, self.u1)
assert not self.perm_handler.can_vote_in_poll(poll_2, self.u1)
assert not self.perm_handler.can_vote_in_poll(poll_3, self.u1)
示例3: test_knows_that_an_anonymous_user_without_forum_key_cannot_vote_in_polls
def test_knows_that_an_anonymous_user_without_forum_key_cannot_vote_in_polls(self):
# Setup
u3 = AnonymousUser()
poll_1 = TopicPollFactory.create(topic=self.forum_1_topic)
poll_2 = TopicPollFactory.create(topic=self.forum_3_topic)
poll_3 = TopicPollFactory.create(topic=self.forum_3_topic_2)
assign_perm('can_vote_in_polls', u3, self.forum_1)
# Run & check
assert not self.perm_handler.can_vote_in_poll(poll_1, u3)
assert not self.perm_handler.can_vote_in_poll(poll_2, u3)
assert not self.perm_handler.can_vote_in_poll(poll_3, u3)
示例4: test_append_empty_forms_only_when_no_initial_data_is_provided
def test_append_empty_forms_only_when_no_initial_data_is_provided(self):
# Setup
poll = TopicPollFactory.create(topic=self.topic)
option_1 = TopicPollOptionFactory.create(poll=poll)
option_2 = TopicPollOptionFactory.create(poll=poll)
form_data_1 = {
'form-0-id': option_1.pk,
'form-0-text': faker.text(max_nb_chars=100),
'form-1-id': option_2.pk,
'form-1-text': option_2.text,
'form-INITIAL_FORMS': 2,
'form-TOTAL_FORMS': 2,
'form-MAX_NUM_FORMS': 1000,
}
self.topic.refresh_from_db()
# Run
form_1 = TopicPollOptionFormset(
data=form_data_1,
topic=self.topic)
form_2 = TopicPollOptionFormset(topic=self.topic) # poll already created
form_3 = TopicPollOptionFormset(topic=self.alt_topic) # no poll
# Check
assert form_1.total_form_count() == 2
assert form_2.total_form_count() == 2
assert form_3.total_form_count() == 2
示例5: test_can_update_options_associated_with_an_existing_poll
def test_can_update_options_associated_with_an_existing_poll(self):
# Setup
poll = TopicPollFactory.create(topic=self.topic)
option_1 = TopicPollOptionFactory.create(poll=poll)
option_2 = TopicPollOptionFactory.create(poll=poll)
form_data = {
'form-0-id': option_1.pk,
'form-0-text': faker.text(max_nb_chars=100),
'form-1-id': option_2.pk,
'form-1-text': option_2.text,
'form-INITIAL_FORMS': 2,
'form-TOTAL_FORMS': 2,
'form-MAX_NUM_FORMS': 1000,
}
self.topic.refresh_from_db()
# Run
form = TopicPollOptionFormset(
data=form_data,
topic=self.topic)
valid = form.is_valid()
# Check
assert valid
form.save()
option_1.refresh_from_db()
assert option_1.text == form_data['form-0-text']
示例6: test_can_initialize_poll_fields_from_topic_related_poll_object
def test_can_initialize_poll_fields_from_topic_related_poll_object(self):
# Setup
form_data = {
'subject': 'Re: {}'.format(faker.text(max_nb_chars=200)),
'content': '[b]{}[/b]'.format(faker.text()),
'topic_type': Topic.TYPE_CHOICES.topic_sticky,
}
assign_perm('can_create_polls', self.user, self.top_level_forum)
poll = TopicPollFactory.create(topic=self.post.topic)
# Run
form = TopicForm(
data=form_data,
user=self.user,
user_ip=faker.ipv4(),
forum=self.top_level_forum,
topic=self.topic,
instance=self.post)
# Check
assert 'poll_question' in form.fields
assert 'poll_max_options' in form.fields
assert 'poll_duration' in form.fields
assert 'poll_user_changes' in form.fields
assert form.fields['poll_question'].initial == poll.question
assert form.fields['poll_max_options'].initial == poll.max_options
assert form.fields['poll_duration'].initial == poll.duration
assert form.fields['poll_user_changes'].initial == poll.user_changes
示例7: setup
def setup(self):
self.loadstatement = '{% load forum_polls_tags %}'
self.request_factory = RequestFactory()
self.g1 = GroupFactory.create()
self.u1 = UserFactory.create()
self.u2 = UserFactory.create()
self.u1.groups.add(self.g1)
self.u2.groups.add(self.g1)
self.moderators = GroupFactory.create()
self.moderator = UserFactory.create()
self.moderator.groups.add(self.moderators)
self.superuser = UserFactory.create(is_superuser=True)
# Permission handler
self.perm_handler = PermissionHandler()
# Set up a top-level category
self.top_level_cat = create_category_forum()
# Set up some forums
self.forum_1 = create_forum(parent=self.top_level_cat)
self.forum_2 = create_forum(parent=self.top_level_cat)
# Set up some topics and posts
self.forum_1_topic = create_topic(forum=self.forum_1, poster=self.u1)
self.forum_2_topic = create_topic(forum=self.forum_2, poster=self.u2)
self.post_1 = PostFactory.create(topic=self.forum_1_topic, poster=self.u1)
self.post_2 = PostFactory.create(topic=self.forum_2_topic, poster=self.u2)
self.poll_1 = TopicPollFactory.create(topic=self.forum_1_topic)
self.poll_2 = TopicPollFactory.create(topic=self.forum_2_topic)
# Assign some permissions
assign_perm('can_see_forum', self.g1, self.forum_1)
assign_perm('can_read_forum', self.g1, self.forum_1)
assign_perm('can_edit_own_posts', self.g1, self.forum_1)
assign_perm('can_delete_own_posts', self.g1, self.forum_1)
assign_perm('can_reply_to_topics', self.g1, self.forum_1)
assign_perm('can_see_forum', self.moderators, self.forum_1)
assign_perm('can_read_forum', self.moderators, self.forum_1)
assign_perm('can_edit_own_posts', self.moderators, self.forum_1)
assign_perm('can_delete_own_posts', self.moderators, self.forum_1)
assign_perm('can_edit_posts', self.moderators, self.forum_1)
assign_perm('can_delete_posts', self.moderators, self.forum_1)
assign_perm('can_vote_in_polls', self.g1, self.forum_1)
示例8: test_knows_if_a_user_can_vote_again_in_a_poll
def test_knows_if_a_user_can_vote_again_in_a_poll(self):
# Setup
poll_1 = TopicPollFactory.create(topic=self.forum_1_topic, user_changes=True)
poll_option_1 = TopicPollOptionFactory.create(poll=poll_1)
TopicPollOptionFactory.create(poll=poll_1)
poll_2 = TopicPollFactory.create(topic=self.forum_3_topic)
poll_option_2 = TopicPollOptionFactory.create(poll=poll_2)
TopicPollOptionFactory.create(poll=poll_2)
TopicPollVoteFactory.create(poll_option=poll_option_1, voter=self.u1)
TopicPollVoteFactory.create(poll_option=poll_option_2, voter=self.u1)
assign_perm('can_vote_in_polls', self.u1, self.forum_1)
assign_perm('can_vote_in_polls', self.u1, self.forum_3)
# Run & check
assert self.perm_handler.can_vote_in_poll(poll_1, self.u1)
assert not self.perm_handler.can_vote_in_poll(poll_2, self.u1)
示例9: test_knows_that_a_user_should_no_vote_in_a_completed_poll
def test_knows_that_a_user_should_no_vote_in_a_completed_poll(self):
# Setup
poll = TopicPollFactory.create(topic=self.forum_1_topic, duration=2)
poll._meta.get_field('updated').auto_now = False
poll.created = dt.datetime(2000, 1, 12)
poll.save()
poll._meta.get_field('updated').auto_now = True
assign_perm('can_vote_in_polls', self.u1, self.forum_1)
# Run & check
assert not self.perm_handler.can_vote_in_poll(poll, self.u1)
示例10: test_cannot_be_associated_with_a_user_and_a_session_key_at_the_same_time_to_be_created
def test_cannot_be_associated_with_a_user_and_a_session_key_at_the_same_time_to_be_created(self): # noqa
# Setup
poll = TopicPollFactory.create(topic=self.topic, max_options=2)
option_1 = TopicPollOptionFactory.create(poll=poll)
TopicPollOptionFactory.create(poll=poll)
# Run & check
with pytest.raises(ValidationError):
vote = TopicPollVoteFactory.build(
poll_option=option_1, voter=self.u1, anonymous_key='ertyue')
vote.clean()
示例11: test_must_be_associated_with_a_user_or_a_session_key_to_be_created
def test_must_be_associated_with_a_user_or_a_session_key_to_be_created(self):
# Setup
poll = TopicPollFactory.create(topic=self.topic, max_options=2)
option_1 = TopicPollOptionFactory.create(poll=poll)
TopicPollOptionFactory.create(poll=poll)
# Run & check
with pytest.raises(ValidationError):
vote = TopicPollVoteFactory.build(
poll_option=option_1, voter=None, anonymous_key=None)
vote.clean()
示例12: test_knows_if_an_anonymous_user_can_vote_again_in_a_poll
def test_knows_if_an_anonymous_user_can_vote_again_in_a_poll(self):
# Setup
u3 = AnonymousUser()
u3.forum_key = 1
poll_1 = TopicPollFactory.create(topic=self.forum_1_topic, user_changes=True)
poll_option_1 = TopicPollOptionFactory.create(poll=poll_1)
TopicPollOptionFactory.create(poll=poll_1)
poll_2 = TopicPollFactory.create(topic=self.forum_3_topic)
poll_option_2 = TopicPollOptionFactory.create(poll=poll_2)
TopicPollOptionFactory.create(poll=poll_2)
TopicPollVoteFactory.create(poll_option=poll_option_1, anonymous_key=1)
TopicPollVoteFactory.create(poll_option=poll_option_2, anonymous_key=1)
assign_perm('can_vote_in_polls', u3, self.forum_1)
assign_perm('can_vote_in_polls', u3, self.forum_3)
# Run & check
assert self.perm_handler.can_vote_in_poll(poll_1, u3)
assert not self.perm_handler.can_vote_in_poll(poll_2, u3)
示例13: test_can_return_a_list_of_all_related_votes
def test_can_return_a_list_of_all_related_votes(self):
# Setup
u2 = UserFactory.create()
poll = TopicPollFactory.create(topic=self.topic, max_options=2)
option_1 = TopicPollOptionFactory.create(poll=poll)
option_2 = TopicPollOptionFactory.create(poll=poll)
# Run
vote_1 = TopicPollVoteFactory.create(poll_option=option_1, voter=self.u1)
vote_2 = TopicPollVoteFactory.create(poll_option=option_2, voter=self.u1)
vote_3 = TopicPollVoteFactory.create(poll_option=option_2, voter=u2)
# Check
assert set(poll.votes) == set([vote_1, vote_2, vote_3])
示例14: test_knows_its_percentage
def test_knows_its_percentage(self):
# Setup
u2 = UserFactory.create()
poll = TopicPollFactory.create(topic=self.topic, max_options=2)
option_1 = TopicPollOptionFactory.create(poll=poll)
option_2 = TopicPollOptionFactory.create(poll=poll)
TopicPollVoteFactory.create(poll_option=option_1, voter=self.u1)
TopicPollVoteFactory.create(poll_option=option_2, voter=self.u1)
TopicPollVoteFactory.create(poll_option=option_1, voter=u2)
TopicPollVoteFactory.create(poll_option=option_2, voter=u2)
# Run & check
assert option_1.percentage == 50
assert option_2.percentage == 50
示例15: test_inserts_the_topic_poll_in_the_context_if_applicable
def test_inserts_the_topic_poll_in_the_context_if_applicable(self):
# Setup
poll = TopicPollFactory.create(topic=self.topic)
option_1 = TopicPollOptionFactory.create(poll=poll)
option_2 = TopicPollOptionFactory.create(poll=poll)
correct_url = reverse(
'forum_moderation:queued_post', kwargs={'pk': self.post.pk})
# Run
response = self.client.get(correct_url, follow=True)
# Check
assert response.status_code == 200
assert response.context['poll'] == poll
assert set(response.context['poll_options']) == set({option_1, option_2, })