<s> from __future__ import unicode_literals <EOL> from django . test import TestCase , RequestFactory <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . cache import cache <EOL> from django . contrib . auth import get_user_model <EOL> from django . utils import timezone <EOL> from django . template import Template , Context <EOL> from django . utils . html import strip_tags <EOL> from ... core . tests import utils <EOL> from . models import CommentPoll , CommentPollChoice , CommentPollVote , PollMode <EOL> from . forms import PollVoteManyForm <EOL> from . utils . render_static import post_render_static_polls <EOL> from . utils import render <EOL> User = get_user_model ( ) <EOL> class PollViewTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> cache . clear ( ) <EOL> self . user = utils . create_user ( ) <EOL> self . user2 = utils . create_user ( ) <EOL> self . category = utils . create_category ( ) <EOL> self . topic = utils . create_topic ( self . category , user = self . user ) <EOL> self . topic2 = utils . create_topic ( self . category , user = self . user2 ) <EOL> self . comment = utils . create_comment ( topic = self . topic ) <EOL> self . user_comment = utils . create_comment ( topic = self . topic , user = self . user ) <EOL> self . poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:foo>' ) <EOL> self . poll_multi = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:bar>' , choice_max = <NUM_LIT:2> ) <EOL> def test_poll_close_logged_in ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . poll . pk , } ) , <EOL> { } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_poll_close_wrong_user ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . poll . pk , } ) , <EOL> { } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_poll_close_get ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . poll . pk , } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_poll_close_post ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> poll = CommentPoll . objects . create ( comment = self . user_comment , name = '<STR_LIT:foo>' ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : poll . pk , } ) , <EOL> { } ) <EOL> expected_url = poll . get_absolute_url ( ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> , target_status_code = <NUM_LIT> ) <EOL> self . assertTrue ( CommentPoll . objects . get ( pk = poll . pk ) . is_closed ) <EOL> def test_poll_close_open_post ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> poll = CommentPoll . objects . create ( comment = self . user_comment , name = '<STR_LIT:foo>' , close_at = timezone . now ( ) ) <EOL> self . assertTrue ( poll . is_closed ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : poll . pk , } ) , <EOL> { } ) <EOL> expected_url = poll . get_absolute_url ( ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> , target_status_code = <NUM_LIT> ) <EOL> self . assertFalse ( CommentPoll . objects . get ( pk = poll . pk ) . is_closed ) <EOL> def test_poll_vote_logged_in ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . poll . pk , } ) , <EOL> { } ) <EOL> expected_url = reverse ( '<STR_LIT>' ) + "<STR_LIT>" + self . poll . get_absolute_url ( ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> ) <EOL> def test_poll_vote_get ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . poll . pk , } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_poll_vote_post ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> choice = CommentPollChoice . objects . create ( poll = self . poll , number = <NUM_LIT:1> , description = "<STR_LIT>" ) <EOL> form_data = { '<STR_LIT>' : choice . pk , } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . poll . pk , } ) , <EOL> form_data ) <EOL> expected_url = self . poll . get_absolute_url ( ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> , target_status_code = <NUM_LIT> ) <EOL> self . assertEqual ( len ( CommentPollVote . objects . filter ( choice = choice ) ) , <NUM_LIT:1> ) <EOL> def test_poll_vote_post_invalid ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . poll . pk , } ) , <EOL> { } , follow = True ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> def test_poll_vote_post_invalid_redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . poll . pk , } ) , <EOL> { } ) <EOL> expected_url = self . poll . get_absolute_url ( ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> , target_status_code = <NUM_LIT> ) <EOL> def test_poll_vote_post_multi ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> choice_a = CommentPollChoice . objects . create ( poll = self . poll_multi , number = <NUM_LIT:1> , description = "<STR_LIT>" ) <EOL> choice_b = CommentPollChoice . objects . create ( poll = self . poll_multi , number = <NUM_LIT:2> , description = "<STR_LIT>" ) <EOL> CommentPollChoice . objects . create ( poll = self . poll_multi , number = <NUM_LIT:3> , description = "<STR_LIT>" ) <EOL> form_data = { '<STR_LIT>' : [ choice_a . pk , choice_b . pk ] } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . poll_multi . pk , } ) , <EOL> form_data ) <EOL> expected_url = self . poll . get_absolute_url ( ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> , target_status_code = <NUM_LIT> ) <EOL> self . assertEqual ( len ( CommentPollVote . objects . all ( ) ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( CommentPollVote . objects . filter ( choice = choice_a . pk ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( CommentPollVote . objects . filter ( choice = choice_b . pk ) ) , <NUM_LIT:1> ) <EOL> def test_poll_vote_post_count ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> choice_a = CommentPollChoice . objects . create ( poll = self . poll_multi , number = <NUM_LIT:1> , description = "<STR_LIT>" ) <EOL> choice_b = CommentPollChoice . objects . create ( poll = self . poll_multi , number = <NUM_LIT:2> , description = "<STR_LIT>" ) <EOL> choice_c = CommentPollChoice . objects . create ( poll = self . poll_multi , number = <NUM_LIT:3> , description = "<STR_LIT>" ) <EOL> form_data = { '<STR_LIT>' : [ choice_a . pk , choice_b . pk ] } <EOL> response = self . client . post ( <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . poll_multi . pk , } ) , form_data <EOL> ) <EOL> expected_url = self . poll . get_absolute_url ( ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> , target_status_code = <NUM_LIT> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice_a . pk ) . vote_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice_b . pk ) . vote_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice_c . pk ) . vote_count , <NUM_LIT:0> ) <EOL> form_data = { '<STR_LIT>' : [ choice_a . pk ] } <EOL> response = self . client . post ( <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . poll_multi . pk , } ) , form_data <EOL> ) <EOL> expected_url = self . poll . get_absolute_url ( ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> , target_status_code = <NUM_LIT> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice_a . pk ) . vote_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice_b . pk ) . vote_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice_c . pk ) . vote_count , <NUM_LIT:0> ) <EOL> def test_poll_voters_logged_in ( self ) : <EOL> """<STR_LIT>""" <EOL> poll_choice = CommentPollChoice . objects . create ( poll = self . poll , number = <NUM_LIT:1> , description = "<STR_LIT>" ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : poll_choice . pk , } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_poll_voters ( self ) : <EOL> """<STR_LIT>""" <EOL> poll_choice = CommentPollChoice . objects . create ( poll = self . poll , number = <NUM_LIT:1> , description = "<STR_LIT>" ) <EOL> poll_choice2 = CommentPollChoice . objects . create ( poll = self . poll , number = <NUM_LIT:2> , description = "<STR_LIT>" ) <EOL> vote = CommentPollVote . objects . create ( voter = self . user , choice = poll_choice ) <EOL> CommentPollVote . objects . create ( voter = self . user2 , choice = poll_choice2 ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : poll_choice . pk , } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , poll_choice ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ vote ] ) <EOL> def test_poll_voters_secret ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT>' , mode = PollMode . SECRET ) <EOL> poll_choice = CommentPollChoice . objects . create ( poll = poll , number = <NUM_LIT:1> , description = "<STR_LIT>" ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : poll_choice . pk , } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_poll_voters_secret_closed ( self ) : <EOL> """<STR_LIT>""" <EOL> yesterday = timezone . now ( ) - timezone . timedelta ( days = <NUM_LIT:1> ) <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT>' , <EOL> mode = PollMode . SECRET , close_at = yesterday ) <EOL> poll_choice = CommentPollChoice . objects . create ( poll = poll , number = <NUM_LIT:1> , description = "<STR_LIT>" ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : poll_choice . pk , } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> class PollFormTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> cache . clear ( ) <EOL> self . user = utils . create_user ( ) <EOL> self . user2 = utils . create_user ( ) <EOL> self . category = utils . create_category ( ) <EOL> self . topic = utils . create_topic ( self . category , user = self . user ) <EOL> self . comment = utils . create_comment ( topic = self . topic ) <EOL> self . comment2 = utils . create_comment ( topic = self . topic ) <EOL> self . poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:foo>' ) <EOL> self . poll_choice = CommentPollChoice . objects . create ( poll = self . poll , number = <NUM_LIT:1> , description = "<STR_LIT>" ) <EOL> self . poll_choice2 = CommentPollChoice . objects . create ( poll = self . poll , number = <NUM_LIT:2> , description = "<STR_LIT>" ) <EOL> self . poll_vote = CommentPollVote . objects . create ( voter = self . user , choice = self . poll_choice ) <EOL> self . poll_vote2 = CommentPollVote . objects . create ( voter = self . user2 , choice = self . poll_choice ) <EOL> self . poll_choice . votes = [ self . poll_vote ] <EOL> self . poll . choices = [ self . poll_choice , self . poll_choice2 ] <EOL> self . poll_multi = CommentPoll . objects . create ( comment = self . comment2 , name = '<STR_LIT:bar>' , choice_max = <NUM_LIT:2> ) <EOL> self . poll_multi_choice = CommentPollChoice . objects . create ( poll = self . poll_multi , number = <NUM_LIT:1> , description = "<STR_LIT>" ) <EOL> self . poll_multi_choice2 = CommentPollChoice . objects . create ( poll = self . poll_multi , number = <NUM_LIT:2> , description = "<STR_LIT>" ) <EOL> self . poll_multi_choice3 = CommentPollChoice . objects . create ( poll = self . poll_multi , number = <NUM_LIT:3> , description = "<STR_LIT>" ) <EOL> self . poll_multi_vote = CommentPollVote . objects . create ( voter = self . user , choice = self . poll_multi_choice ) <EOL> self . poll_multi_vote2 = CommentPollVote . objects . create ( voter = self . user , choice = self . poll_multi_choice2 ) <EOL> self . poll_multi_vote3 = CommentPollVote . objects . create ( voter = self . user2 , choice = self . poll_multi_choice ) <EOL> self . poll_multi_choice . votes = [ self . poll_multi_vote ] <EOL> self . poll_multi_choice2 . votes = [ self . poll_multi_vote2 ] <EOL> self . poll_multi . choices = [ self . poll_multi_choice , self . poll_multi_choice2 ] <EOL> def test_vote_load_initial_single ( self ) : <EOL> """<STR_LIT>""" <EOL> form = PollVoteManyForm ( user = self . user , poll = self . poll ) <EOL> form . load_initial ( ) <EOL> self . assertEqual ( form . initial , { '<STR_LIT>' : self . poll_choice . pk , } ) <EOL> def test_vote_load_initial_multi ( self ) : <EOL> """<STR_LIT>""" <EOL> form = PollVoteManyForm ( user = self . user , poll = self . poll_multi ) <EOL> form . load_initial ( ) <EOL> self . assertDictEqual ( form . initial , { '<STR_LIT>' : [ self . poll_multi_choice . pk , self . poll_multi_choice2 . pk ] , } ) <EOL> def test_vote_load_initial_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> CommentPollVote . objects . all ( ) . delete ( ) <EOL> self . poll_choice . votes = [ ] <EOL> form = PollVoteManyForm ( user = self . user , poll = self . poll ) <EOL> form . load_initial ( ) <EOL> self . assertEqual ( form . initial , { } ) <EOL> def test_vote_load_initial_choice_limit ( self ) : <EOL> """<STR_LIT>""" <EOL> self . poll_multi . choice_max = <NUM_LIT:1> <EOL> form = PollVoteManyForm ( user = self . user , poll = self . poll_multi ) <EOL> form . load_initial ( ) <EOL> self . assertDictEqual ( form . initial , { '<STR_LIT>' : self . poll_multi_choice . pk , } ) <EOL> self . poll . choice_max = <NUM_LIT:2> <EOL> form = PollVoteManyForm ( user = self . user , poll = self . poll ) <EOL> form . load_initial ( ) <EOL> self . assertDictEqual ( form . initial , { '<STR_LIT>' : [ self . poll_choice . pk , ] , } ) <EOL> def test_vote_poll_closed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . poll . close_at = timezone . now ( ) <EOL> self . poll . save ( ) <EOL> form_data = { '<STR_LIT>' : self . poll_choice . pk , } <EOL> form = PollVoteManyForm ( user = self . user , poll = self . poll , data = form_data ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> def test_create_vote_single ( self ) : <EOL> """<STR_LIT>""" <EOL> CommentPollVote . objects . all ( ) . delete ( ) <EOL> form_data = { '<STR_LIT>' : self . poll_choice . pk , } <EOL> form = PollVoteManyForm ( user = self . user , poll = self . poll , data = form_data ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save_m2m ( ) <EOL> self . assertEqual ( len ( CommentPollVote . objects . all ( ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( CommentPollVote . objects . filter ( choice = self . poll_choice , is_removed = False ) ) , <NUM_LIT:1> ) <EOL> def test_create_vote_multi ( self ) : <EOL> """<STR_LIT>""" <EOL> CommentPollVote . objects . all ( ) . delete ( ) <EOL> self . poll_multi_choice . votes = [ ] <EOL> self . poll_multi_choice2 . votes = [ ] <EOL> form_data = { '<STR_LIT>' : [ self . poll_multi_choice . pk , self . poll_multi_choice2 . pk ] , } <EOL> form = PollVoteManyForm ( user = self . user , poll = self . poll_multi , data = form_data ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_create_vote_multi_invalid ( self ) : <EOL> """<STR_LIT>""" <EOL> CommentPollVote . objects . all ( ) . delete ( ) <EOL> self . poll_multi_choice . votes = [ ] <EOL> self . poll_multi_choice2 . votes = [ ] <EOL> form_data = { '<STR_LIT>' : [ self . poll_multi_choice . pk , <EOL> self . poll_multi_choice2 . pk , <EOL> self . poll_multi_choice3 . pk ] , } <EOL> form = PollVoteManyForm ( user = self . user , poll = self . poll_multi , data = form_data ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> def test_update_vote_single ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( len ( CommentPollVote . objects . filter ( choice = self . poll_choice2 , is_removed = False ) ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( CommentPollVote . objects . filter ( choice = self . poll_choice , is_removed = False ) ) , <NUM_LIT:2> ) <EOL> form_data = { '<STR_LIT>' : self . poll_choice2 . pk , } <EOL> form = PollVoteManyForm ( user = self . user , poll = self . poll , data = form_data ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save_m2m ( ) <EOL> self . assertEqual ( len ( CommentPollVote . objects . filter ( choice = self . poll_choice2 , is_removed = False ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( CommentPollVote . objects . filter ( choice = self . poll_choice , is_removed = False ) ) , <NUM_LIT:1> ) <EOL> class CommentPollTemplateTagsTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> cache . clear ( ) <EOL> self . user = utils . create_user ( ) <EOL> self . category = utils . create_category ( ) <EOL> self . topic = utils . create_topic ( category = self . category ) <EOL> self . user_comment = utils . create_comment ( topic = self . topic , user = self . user , comment_html = "<STR_LIT>" ) <EOL> self . user_poll = CommentPoll . objects . create ( comment = self . user_comment , name = '<STR_LIT:foo>' ) <EOL> self . user_comment_with_polls = self . user_comment . __class__ . objects . filter ( pk = self . user_comment . pk ) . with_polls ( self . user ) . first ( ) <EOL> self . request = RequestFactory ( ) . get ( '<STR_LIT:/>' ) <EOL> self . request . user = self . user <EOL> def test_render_polls_form ( self ) : <EOL> """<STR_LIT>""" <EOL> res = [ ] <EOL> def mock_render_to_string ( tlt , ctx ) : <EOL> res . append ( tlt ) <EOL> res . append ( ctx ) <EOL> org_render_to_string , render . render_to_string = render . render_to_string , mock_render_to_string <EOL> try : <EOL> render . render_polls ( self . user_comment_with_polls , self . request , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:2> ) <EOL> template , context = res [ <NUM_LIT:0> ] , res [ <NUM_LIT:1> ] <EOL> self . assertEqual ( template , '<STR_LIT>' ) <EOL> self . assertEqual ( context [ '<STR_LIT>' ] . poll , self . user_poll ) <EOL> self . assertIsInstance ( context [ '<STR_LIT>' ] , CommentPoll ) <EOL> self . assertEqual ( context [ '<STR_LIT:user>' ] , self . user ) <EOL> self . assertEqual ( context [ '<STR_LIT>' ] , self . user_comment_with_polls ) <EOL> self . assertEqual ( context [ '<STR_LIT>' ] , self . request ) <EOL> self . assertEqual ( context [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> finally : <EOL> render . render_to_string = org_render_to_string <EOL> def test_render_polls_template_form ( self ) : <EOL> """<STR_LIT>""" <EOL> out = Template ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) . render ( Context ( { '<STR_LIT>' : self . user_comment_with_polls , '<STR_LIT>' : self . request , '<STR_LIT>' : '<STR_LIT:foo>' } ) ) <EOL> self . assertNotEqual ( out . strip ( ) , "<STR_LIT>" ) <EOL> self . assertTrue ( "<STR_LIT>" not in out ) <EOL> form_id = '<STR_LIT>' % self . user_poll . pk <EOL> self . assertTrue ( form_id in out ) <EOL> show_link = '<STR_LIT>' % { '<STR_LIT>' : self . user_poll . pk } <EOL> self . assertTrue ( show_link in out ) <EOL> def test_render_polls_template_form_not_author ( self ) : <EOL> """<STR_LIT>""" <EOL> request = RequestFactory ( ) . get ( '<STR_LIT:/>' ) <EOL> request . user = utils . create_user ( ) <EOL> out = Template ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) . render ( Context ( { '<STR_LIT>' : self . user_comment_with_polls , '<STR_LIT>' : request , '<STR_LIT>' : '<STR_LIT:foo>' } ) ) <EOL> self . assertNotEqual ( out . strip ( ) , "<STR_LIT>" ) <EOL> form_id = '<STR_LIT>' % self . user_poll . pk <EOL> self . assertTrue ( form_id in out ) <EOL> def test_render_polls_template_form_close ( self ) : <EOL> """<STR_LIT>""" <EOL> out = Template ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) . render ( Context ( { '<STR_LIT>' : self . user_comment_with_polls , '<STR_LIT>' : self . request , '<STR_LIT>' : '<STR_LIT:foo>' } ) ) <EOL> self . assertNotEqual ( out . strip ( ) , "<STR_LIT>" ) <EOL> close_link = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . user_poll . pk } ) <EOL> self . assertTrue ( close_link in out ) <EOL> def test_render_polls_template_form_close_not_author ( self ) : <EOL> """<STR_LIT>""" <EOL> request = RequestFactory ( ) . get ( '<STR_LIT:/>' ) <EOL> request . user = utils . create_user ( ) <EOL> out = Template ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) . render ( Context ( { '<STR_LIT>' : self . user_comment_with_polls , '<STR_LIT>' : request , '<STR_LIT>' : '<STR_LIT:foo>' } ) ) <EOL> self . assertNotEqual ( out . strip ( ) , "<STR_LIT>" ) <EOL> close_link = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . user_poll . pk } ) <EOL> self . assertTrue ( close_link not in out ) <EOL> def test_render_polls_template_form_open ( self ) : <EOL> """<STR_LIT>""" <EOL> self . user_comment_with_polls . polls [ <NUM_LIT:0> ] . close_at = timezone . now ( ) <EOL> out = Template ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) . render ( Context ( { '<STR_LIT>' : self . user_comment_with_polls , '<STR_LIT>' : self . request , '<STR_LIT>' : '<STR_LIT:foo>' } ) ) <EOL> self . assertNotEqual ( out . strip ( ) , "<STR_LIT>" ) <EOL> open_link = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . user_poll . pk } ) <EOL> self . assertTrue ( open_link in out ) <EOL> def test_render_polls_secret ( self ) : <EOL> """<STR_LIT>""" <EOL> comment = utils . create_comment ( topic = self . topic , comment_html = "<STR_LIT>" ) <EOL> CommentPoll . objects . create ( comment = comment , name = '<STR_LIT:bar>' , mode = PollMode . SECRET ) <EOL> user_comment_with_polls = comment . __class__ . objects . filter ( pk = comment . pk ) . with_polls ( self . user ) . first ( ) <EOL> out = Template ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) . render ( Context ( { '<STR_LIT>' : user_comment_with_polls , '<STR_LIT>' : self . request , '<STR_LIT>' : '<STR_LIT:foo>' } ) ) <EOL> self . assertNotEqual ( out . strip ( ) , "<STR_LIT>" ) <EOL> self . assertFalse ( '<STR_LIT>' in out ) <EOL> self . assertTrue ( '<STR_LIT>' in out ) <EOL> def test_render_polls_secret_closed ( self ) : <EOL> """<STR_LIT>""" <EOL> comment = utils . create_comment ( topic = self . topic , comment_html = "<STR_LIT>" ) <EOL> yesterday = timezone . now ( ) - timezone . timedelta ( days = <NUM_LIT:1> ) <EOL> CommentPoll . objects . create ( comment = comment , name = '<STR_LIT:bar>' , mode = PollMode . SECRET , close_at = yesterday ) <EOL> user_comment_with_polls = comment . __class__ . objects . filter ( pk = comment . pk ) . with_polls ( self . user ) . first ( ) <EOL> out = Template ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) . render ( Context ( { '<STR_LIT>' : user_comment_with_polls , '<STR_LIT>' : self . request , '<STR_LIT>' : '<STR_LIT:foo>' } ) ) <EOL> self . assertNotEqual ( out . strip ( ) , "<STR_LIT>" ) <EOL> self . assertFalse ( '<STR_LIT>' in out ) <EOL> self . assertFalse ( '<STR_LIT>' in out ) <EOL> self . assertTrue ( '<STR_LIT>' in out ) <EOL> class PollModelsTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> cache . clear ( ) <EOL> self . user = utils . create_user ( ) <EOL> self . category = utils . create_category ( ) <EOL> self . topic = utils . create_topic ( category = self . category , user = self . user ) <EOL> self . comment = utils . create_comment ( topic = self . topic ) <EOL> self . poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:foo>' ) <EOL> self . choice = CommentPollChoice . objects . create ( poll = self . poll , number = <NUM_LIT:1> , description = <NUM_LIT:1> ) <EOL> self . vote = CommentPollVote . objects . create ( choice = self . choice , voter = self . user ) <EOL> self . poll . choices = list ( CommentPollChoice . objects . filter ( poll = self . poll ) ) <EOL> for c in self . poll . choices : <EOL> c . votes = list ( CommentPollVote . objects . filter ( choice = c , voter = self . user ) ) <EOL> def test_poll_is_multiple_choice ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:bar>' , choice_max = <NUM_LIT:2> ) <EOL> self . assertFalse ( self . poll . is_multiple_choice ) <EOL> self . assertTrue ( poll . is_multiple_choice ) <EOL> def test_poll_has_choice_min ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:bar>' , choice_min = <NUM_LIT:2> ) <EOL> self . assertFalse ( self . poll . has_choice_min ) <EOL> self . assertTrue ( poll . has_choice_min ) <EOL> def test_poll_is_closed ( self ) : <EOL> """<STR_LIT>""" <EOL> yesterday = timezone . now ( ) - timezone . timedelta ( days = <NUM_LIT:1> ) <EOL> tomorrow = timezone . now ( ) + timezone . timedelta ( days = <NUM_LIT:1> ) <EOL> poll_old = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:bar>' , close_at = yesterday ) <EOL> poll_new = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT>' , close_at = tomorrow ) <EOL> self . assertFalse ( self . poll . is_closed ) <EOL> self . assertTrue ( poll_old . is_closed ) <EOL> self . assertFalse ( poll_new . is_closed ) <EOL> def test_poll_has_user_voted ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:bar>' ) <EOL> CommentPollChoice . objects . create ( poll = poll , number = <NUM_LIT:1> , description = <NUM_LIT:1> ) <EOL> poll . choices = list ( CommentPollChoice . objects . filter ( poll = poll ) ) <EOL> for c in poll . choices : <EOL> c . votes = [ ] <EOL> self . assertTrue ( self . poll . has_user_voted ) <EOL> self . assertFalse ( poll . has_user_voted ) <EOL> def test_poll_mode_txt ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:bar>' ) <EOL> self . assertEqual ( poll . mode_txt , '<STR_LIT:default>' ) <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT>' , mode = PollMode . SECRET ) <EOL> self . assertEqual ( poll . mode_txt , '<STR_LIT>' ) <EOL> def test_poll_total_votes ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:bar>' ) <EOL> CommentPollChoice . objects . create ( poll = poll , number = <NUM_LIT:1> , description = '<STR_LIT:foo>' , vote_count = <NUM_LIT:5> ) <EOL> CommentPollChoice . objects . create ( poll = poll , number = <NUM_LIT:2> , description = '<STR_LIT:bar>' , vote_count = <NUM_LIT:5> ) <EOL> poll . choices = list ( CommentPollChoice . objects . filter ( poll = poll ) ) <EOL> self . assertEqual ( poll . total_votes , <NUM_LIT:10> ) <EOL> def test_poll_is_secret ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:bar>' ) <EOL> self . assertFalse ( poll . is_secret ) <EOL> poll . mode = PollMode . SECRET <EOL> self . assertTrue ( poll . is_secret ) <EOL> def test_poll_can_show_results ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:bar>' ) <EOL> self . assertTrue ( poll . can_show_results ) <EOL> poll . mode = PollMode . SECRET <EOL> self . assertFalse ( poll . can_show_results ) <EOL> yesterday = timezone . now ( ) - timezone . timedelta ( days = <NUM_LIT:1> ) <EOL> poll . close_at = yesterday <EOL> self . assertTrue ( poll . can_show_results ) <EOL> def test_poll_update_or_create_many ( self ) : <EOL> """<STR_LIT>""" <EOL> poll_raw = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:title>' : '<STR_LIT:foo>' , '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : timezone . now ( ) , '<STR_LIT>' : PollMode . SECRET } <EOL> CommentPoll . update_or_create_many ( comment = self . comment , polls_raw = [ poll_raw ] ) <EOL> poll = CommentPoll . objects . all ( ) . order_by ( '<STR_LIT>' ) . last ( ) <EOL> self . assertEqual ( poll . name , poll_raw [ '<STR_LIT:name>' ] ) <EOL> self . assertEqual ( poll . title , poll_raw [ '<STR_LIT:title>' ] ) <EOL> self . assertEqual ( poll . choice_min , poll_raw [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( poll . choice_max , poll_raw [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( poll . close_at , poll_raw [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( poll . mode , poll_raw [ '<STR_LIT>' ] ) <EOL> CommentPoll . update_or_create_many ( comment = self . comment , polls_raw = [ { '<STR_LIT:name>' : poll . name , '<STR_LIT:title>' : '<STR_LIT:bar>' } ] ) <EOL> poll_updated = CommentPoll . objects . all ( ) . order_by ( '<STR_LIT>' ) . last ( ) <EOL> self . assertEqual ( poll . pk , poll_updated . pk ) <EOL> self . assertEqual ( poll_updated . title , '<STR_LIT:bar>' ) <EOL> def test_poll_update_or_create_many_update_un_remove ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT>' , is_removed = True ) <EOL> CommentPoll . update_or_create_many ( comment = poll . comment , polls_raw = [ { '<STR_LIT:name>' : poll . name } ] ) <EOL> poll_updated = CommentPoll . objects . all ( ) . order_by ( '<STR_LIT>' ) . last ( ) <EOL> self . assertEqual ( poll . pk , poll_updated . pk ) <EOL> self . assertFalse ( poll_updated . is_removed ) <EOL> def test_poll_choice_vote ( self ) : <EOL> """<STR_LIT>""" <EOL> choice = CommentPollChoice . objects . create ( poll = self . poll , number = <NUM_LIT:5> , description = "<STR_LIT>" ) <EOL> vote = CommentPollVote . objects . create ( choice = choice , voter = self . user ) <EOL> choice . votes = list ( CommentPollVote . objects . filter ( choice = choice , voter = self . user ) ) <EOL> self . assertEqual ( choice . vote , vote ) <EOL> choice . votes = [ ] <EOL> self . assertIsNone ( choice . vote ) <EOL> del choice . votes <EOL> self . assertIsNone ( choice . vote ) <EOL> choice . votes = [ vote , vote ] <EOL> self . assertRaises ( AssertionError , lambda : choice . vote ) <EOL> def test_poll_choice_votes_percentage ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT>' ) <EOL> choice = CommentPollChoice . objects . create ( poll = poll , number = <NUM_LIT:1> , description = "<STR_LIT>" , vote_count = <NUM_LIT:1> ) <EOL> poll . total_votes = <NUM_LIT:2> <EOL> self . assertEqual ( choice . votes_percentage , <NUM_LIT:50> ) <EOL> poll . total_votes = <NUM_LIT:3> <EOL> self . assertEqual ( '<STR_LIT>' . format ( choice . votes_percentage ) , '<STR_LIT>' ) <EOL> poll . total_votes = <NUM_LIT:0> <EOL> self . assertEqual ( choice . votes_percentage , <NUM_LIT:0> ) <EOL> def test_poll_choice_increase_vote_count ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT>' ) <EOL> choice = CommentPollChoice . objects . create ( poll = poll , number = <NUM_LIT:1> , description = "<STR_LIT>" ) <EOL> choice2 = CommentPollChoice . objects . create ( poll = poll , number = <NUM_LIT:2> , description = "<STR_LIT>" ) <EOL> CommentPollVote . objects . create ( choice = choice , voter = self . user ) <EOL> CommentPollVote . objects . create ( choice = choice2 , voter = self . user ) <EOL> user2 = utils . create_user ( ) <EOL> CommentPollVote . objects . create ( choice = choice , voter = user2 ) <EOL> CommentPollChoice . increase_vote_count ( poll , self . user ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = self . choice . pk ) . vote_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice . pk ) . vote_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice2 . pk ) . vote_count , <NUM_LIT:1> ) <EOL> CommentPollChoice . objects . filter ( pk = choice . pk ) . update ( is_removed = True ) <EOL> CommentPollChoice . increase_vote_count ( poll , self . user ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice . pk ) . vote_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice2 . pk ) . vote_count , <NUM_LIT:2> ) <EOL> def test_poll_choice_decrease_vote_count ( self ) : <EOL> """<STR_LIT>""" <EOL> poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT>' ) <EOL> choice = CommentPollChoice . objects . create ( poll = poll , number = <NUM_LIT:1> , description = "<STR_LIT>" , vote_count = <NUM_LIT:2> ) <EOL> choice2 = CommentPollChoice . objects . create ( poll = poll , number = <NUM_LIT:2> , description = "<STR_LIT>" , vote_count = <NUM_LIT:2> ) <EOL> CommentPollVote . objects . create ( choice = choice , voter = self . user ) <EOL> CommentPollVote . objects . create ( choice = choice2 , voter = self . user ) <EOL> user2 = utils . create_user ( ) <EOL> CommentPollVote . objects . create ( choice = choice , voter = user2 ) <EOL> CommentPollChoice . decrease_vote_count ( poll , self . user ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = self . choice . pk ) . vote_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice . pk ) . vote_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice2 . pk ) . vote_count , <NUM_LIT:1> ) <EOL> CommentPollChoice . objects . filter ( pk = choice . pk ) . update ( is_removed = True ) <EOL> CommentPollChoice . decrease_vote_count ( poll , self . user ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice . pk ) . vote_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( CommentPollChoice . objects . get ( pk = choice2 . pk ) . vote_count , <NUM_LIT:0> ) <EOL> def test_poll_choice_update_or_create_many ( self ) : <EOL> """<STR_LIT>""" <EOL> choice_raw = { '<STR_LIT>' : '<STR_LIT:foo>' , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:description>' : '<STR_LIT>' } <EOL> CommentPollChoice . update_or_create_many ( comment = self . comment , choices_raw = [ choice_raw ] ) <EOL> choice = CommentPollChoice . objects . all ( ) . order_by ( '<STR_LIT>' ) . last ( ) <EOL> self . assertTrue ( CommentPollChoice . objects . get ( pk = self . choice . pk ) . is_removed ) <EOL> self . assertEqual ( choice . poll , self . poll ) <EOL> self . assertEqual ( choice . number , <NUM_LIT:2> ) <EOL> self . assertEqual ( choice . description , '<STR_LIT>' ) <EOL> self . assertFalse ( choice . is_removed ) <EOL> choice_raw2 = { '<STR_LIT>' : '<STR_LIT:foo>' , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:description>' : '<STR_LIT>' } <EOL> choice_raw [ '<STR_LIT:description>' ] = '<STR_LIT>' <EOL> CommentPollChoice . update_or_create_many ( comment = self . comment , choices_raw = [ choice_raw , choice_raw2 ] ) <EOL> choice_updated = CommentPollChoice . objects . all ( ) . order_by ( '<STR_LIT>' ) . last ( ) <EOL> self . assertFalse ( CommentPollChoice . objects . get ( pk = self . choice . pk ) . is_removed ) <EOL> self . assertEqual ( choice_updated . poll , self . poll ) <EOL> self . assertEqual ( choice_updated . number , <NUM_LIT:2> ) <EOL> self . assertEqual ( choice_updated . description , '<STR_LIT>' ) <EOL> self . assertFalse ( choice . is_removed ) <EOL> def test_poll_choice_update_or_create_many_removed_poll ( self ) : <EOL> """<STR_LIT>""" <EOL> CommentPoll . objects . filter ( pk = self . poll . pk ) . update ( is_removed = True ) <EOL> choice_raw = { '<STR_LIT>' : '<STR_LIT:foo>' , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:description>' : '<STR_LIT>' } <EOL> self . assertRaises ( KeyError , CommentPollChoice . update_or_create_many , <EOL> comment = self . comment , choices_raw = [ choice_raw ] ) <EOL> class PollUtilsTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> cache . clear ( ) <EOL> self . user = utils . create_user ( ) <EOL> self . category = utils . create_category ( ) <EOL> self . topic = utils . create_topic ( category = self . category , user = self . user ) <EOL> self . comment = utils . create_comment ( topic = self . topic , comment_html = "<STR_LIT>" ) <EOL> self . poll = CommentPoll . objects . create ( comment = self . comment , name = '<STR_LIT:foo>' , title = "<STR_LIT>" ) <EOL> self . choice = CommentPollChoice . objects . create ( poll = self . poll , number = <NUM_LIT:1> , description = "<STR_LIT>" ) <EOL> self . choice = CommentPollChoice . objects . create ( poll = self . poll , number = <NUM_LIT:2> , description = "<STR_LIT>" ) <EOL> def test_post_render_static_polls ( self ) : <EOL> """<STR_LIT>""" <EOL> comment_html = post_render_static_polls ( self . comment ) <EOL> self . assertTrue ( '<STR_LIT>' in comment_html ) <EOL> comment_parts = [ <EOL> l . strip ( ) <EOL> for l in strip_tags ( comment_html ) . splitlines ( ) <EOL> if l . strip ( ) <EOL> ] <EOL> self . assertEqual ( comment_parts , [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> def test_post_render_static_polls_many ( self ) : <EOL> """<STR_LIT>""" <EOL> comment = utils . create_comment ( topic = self . topic , comment_html = "<STR_LIT>" ) <EOL> CommentPoll . objects . create ( comment = comment , name = '<STR_LIT:foo>' , title = "<STR_LIT>" ) <EOL> CommentPoll . objects . create ( comment = comment , name = '<STR_LIT:bar>' , title = "<STR_LIT>" ) <EOL> comment_html = post_render_static_polls ( comment ) <EOL> self . assertTrue ( '<STR_LIT>' in comment_html ) <EOL> self . assertTrue ( '<STR_LIT>' in comment_html ) <EOL> def test_post_render_static_polls_close_at ( self ) : <EOL> """<STR_LIT>""" <EOL> now = timezone . now ( ) <EOL> comment = utils . create_comment ( topic = self . topic , comment_html = "<STR_LIT>" ) <EOL> CommentPoll . objects . create ( comment = comment , name = '<STR_LIT:foo>' , title = "<STR_LIT>" , close_at = now ) <EOL> comment_html = post_render_static_polls ( comment ) <EOL> self . assertTrue ( '<STR_LIT>' in comment_html ) <EOL> self . assertTrue ( '<STR_LIT>' in comment_html ) <EOL> self . assertTrue ( '<STR_LIT>' in comment_html ) <EOL> self . assertTrue ( '<STR_LIT>' in comment_html ) <EOL> def test_post_render_static_polls_no_poll ( self ) : <EOL> """<STR_LIT>""" <EOL> comment = utils . create_comment ( topic = self . topic , comment_html = "<STR_LIT:foo>" ) <EOL> comment_html = post_render_static_polls ( comment ) <EOL> self . assertEqual ( comment_html , '<STR_LIT:foo>' ) <EOL> def test_post_render_static_polls_removed_poll ( self ) : <EOL> """<STR_LIT>""" <EOL> self . poll . is_removed = True <EOL> self . poll . save ( ) <EOL> comment_html = post_render_static_polls ( self . comment ) <EOL> self . assertEqual ( comment_html , "<STR_LIT>" ) </s>
