<s> from __future__ import absolute_import <EOL> from django . test import TestCase <EOL> from django . db import IntegrityError <EOL> from django . contrib . auth . models import User <EOL> from vote . models import Vote <EOL> from vote . compat import atomic <EOL> from tests . models import Comment , CustomVoteComment <EOL> class VoteTest ( TestCase ) : <EOL> through = Vote <EOL> model = Comment <EOL> field_name = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . user1 = User . objects . create_user ( "<STR_LIT>" , "<STR_LIT>" , '<STR_LIT>' ) <EOL> self . user2 = User . objects . create_user ( "<STR_LIT>" , "<STR_LIT>" , '<STR_LIT>' ) <EOL> self . user3 = User . objects . create_user ( "<STR_LIT>" , "<STR_LIT>" , '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> self . model . objects . all ( ) . delete ( ) <EOL> self . through . objects . all ( ) . delete ( ) <EOL> User . objects . all ( ) . delete ( ) <EOL> def call_api ( self , api_name , model = None , * args , ** kwargs ) : <EOL> model = model or self . model <EOL> votes = getattr ( model , self . field_name ) <EOL> api = getattr ( votes , api_name ) <EOL> return api ( * args , ** kwargs ) <EOL> def test_vote_up ( self ) : <EOL> comment = self . model . objects . create ( user = self . user1 , content = "<STR_LIT>" ) <EOL> self . call_api ( '<STR_LIT>' , comment , self . user2 ) <EOL> try : <EOL> with atomic ( ) : <EOL> self . call_api ( '<STR_LIT>' , comment , self . user2 ) <EOL> self . assertTrue ( <NUM_LIT:0> ) <EOL> except IntegrityError : <EOL> pass <EOL> self . assertEqual ( self . call_api ( '<STR_LIT:count>' , comment ) , <NUM_LIT:1> ) <EOL> def test_vote_down ( self ) : <EOL> comment = self . model . objects . create ( user = self . user1 , content = "<STR_LIT>" ) <EOL> self . assertEqual ( self . call_api ( '<STR_LIT:count>' ) , <NUM_LIT:0> ) <EOL> self . call_api ( '<STR_LIT>' , comment , self . user2 ) <EOL> self . call_api ( '<STR_LIT>' , comment , self . user2 ) <EOL> self . assertEqual ( self . call_api ( '<STR_LIT:count>' , comment ) , <NUM_LIT:1> ) <EOL> self . call_api ( '<STR_LIT>' , comment , self . user2 ) <EOL> self . assertEqual ( self . call_api ( '<STR_LIT:count>' , comment ) , <NUM_LIT:0> ) <EOL> def test_vote_exists ( self ) : <EOL> comment = self . model . objects . create ( user = self . user1 , content = "<STR_LIT>" ) <EOL> self . assertFalse ( self . call_api ( '<STR_LIT>' , comment , self . user2 ) ) <EOL> self . call_api ( '<STR_LIT>' , comment , self . user2 ) <EOL> self . assertTrue ( self . call_api ( '<STR_LIT>' , comment , self . user2 ) ) <EOL> def test_vote_all ( self ) : <EOL> comment = self . model . objects . create ( user = self . user1 , content = "<STR_LIT>" ) <EOL> self . assertFalse ( self . call_api ( '<STR_LIT>' , comment , self . user2 ) ) <EOL> self . call_api ( '<STR_LIT>' , comment , self . user2 ) <EOL> self . assertEqual ( len ( self . call_api ( '<STR_LIT:all>' , comment , self . user2 ) ) , <NUM_LIT:1> ) <EOL> def test_vote_count ( self ) : <EOL> comment1 = self . model . objects . create ( user = self . user1 , content = "<STR_LIT>" ) <EOL> comment2 = self . model . objects . create ( user = self . user1 , content = "<STR_LIT>" ) <EOL> self . call_api ( '<STR_LIT>' , comment1 , self . user2 ) <EOL> self . call_api ( '<STR_LIT>' , comment2 , self . user2 ) <EOL> self . assertEqual ( self . call_api ( '<STR_LIT:count>' , comment1 ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . call_api ( '<STR_LIT:count>' , comment2 ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . call_api ( '<STR_LIT:count>' ) , <NUM_LIT:2> ) <EOL> def test_vote_annotate ( self ) : <EOL> comments = [ <EOL> self . model ( user = self . user1 , content = "<STR_LIT>" % i ) for i in range ( <NUM_LIT:10> ) <EOL> ] <EOL> self . model . objects . bulk_create ( comments ) <EOL> comments = list ( self . model . objects . all ( ) ) <EOL> comment1 = comments [ <NUM_LIT:0> ] <EOL> self . call_api ( '<STR_LIT>' , comment1 , self . user2 ) <EOL> comment2 = comments [ <NUM_LIT:1> ] <EOL> self . call_api ( '<STR_LIT>' , comment2 , self . user2 ) <EOL> self . call_api ( '<STR_LIT>' , comment2 , self . user3 ) <EOL> comments = self . call_api ( '<STR_LIT>' ) <EOL> self . assertEqual ( comments [ <NUM_LIT:0> ] . pk , comment2 . pk ) <EOL> self . assertEqual ( comments [ <NUM_LIT:1> ] . pk , comment1 . pk ) <EOL> comments = list ( self . call_api ( '<STR_LIT>' , reverse = False ) ) <EOL> self . assertEqual ( comments [ - <NUM_LIT:1> ] . pk , comment2 . pk ) <EOL> self . assertEqual ( comments [ - <NUM_LIT:2> ] . pk , comment1 . pk ) <EOL> for comment in comments : <EOL> self . assertTrue ( hasattr ( comment , '<STR_LIT>' ) ) <EOL> comments = self . model . objects . filter ( user = self . user1 ) <EOL> comments = self . call_api ( '<STR_LIT>' , queryset = comments , user = self . user2 ) <EOL> self . assertEqual ( comments [ <NUM_LIT:0> ] . pk , comment2 . pk ) <EOL> self . assertEqual ( comments [ <NUM_LIT:1> ] . pk , comment1 . pk ) <EOL> for comment in comments : <EOL> self . assertTrue ( hasattr ( comment , '<STR_LIT>' ) ) <EOL> self . assertTrue ( hasattr ( comment , '<STR_LIT>' ) ) <EOL> if comment . pk in ( comment1 . pk , comment2 . pk ) : <EOL> self . assertTrue ( comment . is_voted ) <EOL> else : <EOL> self . assertFalse ( comment . is_voted ) <EOL> class CustomVoteTest ( VoteTest ) : <EOL> through = Vote <EOL> model = CustomVoteComment <EOL> field_name = '<STR_LIT>' </s>
