<s> from datetime import datetime , timedelta <EOL> from django . conf import settings <EOL> from nose . tools import eq_ , raises <EOL> from kitsune . forums . models import Thread , Forum , ThreadLockedError <EOL> from kitsune . forums . tests import ForumTestCase , ThreadFactory , PostFactory <EOL> from kitsune . forums . views import sort_threads <EOL> from kitsune . sumo . tests import get <EOL> from kitsune . users . tests import UserFactory <EOL> class PostTestCase ( ForumTestCase ) : <EOL> def test_new_post_updates_thread ( self ) : <EOL> t = ThreadFactory ( ) <EOL> PostFactory ( thread = t ) <EOL> p = t . new_post ( author = t . creator , content = '<STR_LIT>' ) <EOL> p . save ( ) <EOL> t = Thread . objects . get ( id = t . id ) <EOL> eq_ ( p . id , t . last_post_id ) <EOL> def test_new_post_updates_forum ( self ) : <EOL> t = ThreadFactory ( ) <EOL> PostFactory ( thread = t ) <EOL> p = t . new_post ( author = t . creator , content = '<STR_LIT>' ) <EOL> p . save ( ) <EOL> f = Forum . objects . get ( id = t . forum_id ) <EOL> eq_ ( p . id , f . last_post_id ) <EOL> def test_update_post_does_not_update_thread ( self ) : <EOL> t = ThreadFactory ( ) <EOL> old = PostFactory ( thread = t ) <EOL> last = PostFactory ( thread = t ) <EOL> old . content = '<STR_LIT>' <EOL> old . save ( ) <EOL> eq_ ( last . id , old . thread . last_post_id ) <EOL> def test_update_forum_does_not_update_thread ( self ) : <EOL> t = ThreadFactory ( ) <EOL> old = PostFactory ( thread = t ) <EOL> last = PostFactory ( thread = t ) <EOL> old . content = '<STR_LIT>' <EOL> old . save ( ) <EOL> eq_ ( last . id , t . forum . last_post_id ) <EOL> def test_replies_count ( self ) : <EOL> t = ThreadFactory ( posts = [ { } , { } , { } ] ) <EOL> old = t . replies <EOL> eq_ ( <NUM_LIT:2> , old ) <EOL> t . new_post ( author = t . creator , content = '<STR_LIT:test>' ) . save ( ) <EOL> eq_ ( old + <NUM_LIT:1> , t . replies ) <EOL> def test_sticky_threads_first ( self ) : <EOL> t = ThreadFactory ( ) <EOL> yesterday = datetime . now ( ) - timedelta ( days = <NUM_LIT:1> ) <EOL> sticky = ThreadFactory ( forum = t . forum , is_sticky = True , posts = [ { '<STR_LIT>' : yesterday } ] ) <EOL> eq_ ( sticky . id , Thread . objects . all ( ) [ <NUM_LIT:0> ] . id ) <EOL> def test_thread_sorting ( self ) : <EOL> PostFactory ( created = datetime . now ( ) - timedelta ( days = <NUM_LIT:1> ) ) <EOL> PostFactory ( ) <EOL> Thread ( is_sticky = True ) <EOL> threads = Thread . objects . filter ( is_sticky = False ) <EOL> self . assert_ ( threads [ <NUM_LIT:0> ] . last_post . created > <EOL> threads [ <NUM_LIT:1> ] . last_post . created ) <EOL> def test_post_sorting ( self ) : <EOL> """<STR_LIT>""" <EOL> now = datetime . now ( ) <EOL> t = ThreadFactory ( posts = [ { '<STR_LIT>' : now - timedelta ( days = n ) } for n in [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:7> , <NUM_LIT:11> ] ] ) <EOL> posts = t . post_set . all ( ) <EOL> for i in range ( len ( posts ) - <NUM_LIT:1> ) : <EOL> self . assert_ ( posts [ i ] . created <= posts [ i + <NUM_LIT:1> ] . created ) <EOL> def test_sorting_creator ( self ) : <EOL> """<STR_LIT>""" <EOL> ThreadFactory ( creator__username = '<STR_LIT>' ) <EOL> ThreadFactory ( creator__username = '<STR_LIT>' ) <EOL> threads = sort_threads ( Thread . objects , <NUM_LIT:3> , <NUM_LIT:1> ) <EOL> self . assert_ ( threads [ <NUM_LIT:0> ] . creator . username >= threads [ <NUM_LIT:1> ] . creator . username ) <EOL> def test_sorting_replies ( self ) : <EOL> """<STR_LIT>""" <EOL> ThreadFactory ( posts = [ { } , { } , { } ] ) <EOL> ThreadFactory ( ) <EOL> threads = sort_threads ( Thread . objects , <NUM_LIT:4> ) <EOL> self . assert_ ( threads [ <NUM_LIT:0> ] . replies <= threads [ <NUM_LIT:1> ] . replies ) <EOL> def test_sorting_last_post_desc ( self ) : <EOL> """<STR_LIT>""" <EOL> ThreadFactory ( posts = [ { } , { } , { } ] ) <EOL> PostFactory ( created = datetime . now ( ) - timedelta ( days = <NUM_LIT:1> ) ) <EOL> threads = sort_threads ( Thread . objects , <NUM_LIT:5> , <NUM_LIT:1> ) <EOL> self . assert_ ( threads [ <NUM_LIT:0> ] . last_post . created >= threads [ <NUM_LIT:1> ] . last_post . created ) <EOL> def test_thread_last_page ( self ) : <EOL> """<STR_LIT>""" <EOL> t = ThreadFactory ( ) <EOL> test_data = ( ( t . replies , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:50> , <NUM_LIT:3> ) , <EOL> ( <NUM_LIT> , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:20> , <NUM_LIT:2> ) ) <EOL> for replies , pages in test_data : <EOL> t . replies = replies <EOL> eq_ ( t . last_page , pages ) <EOL> @ raises ( ThreadLockedError ) <EOL> def test_locked_thread ( self ) : <EOL> """<STR_LIT>""" <EOL> locked = ThreadFactory ( is_locked = True ) <EOL> user = UserFactory ( ) <EOL> locked . new_post ( author = user , content = '<STR_LIT>' ) <EOL> def test_unlocked_thread ( self ) : <EOL> unlocked = ThreadFactory ( ) <EOL> user = UserFactory ( ) <EOL> unlocked . new_post ( author = user , content = '<STR_LIT>' ) <EOL> def test_post_no_session ( self ) : <EOL> r = get ( self . client , '<STR_LIT>' , kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert ( settings . LOGIN_URL in r . redirect_chain [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> eq_ ( <NUM_LIT> , r . redirect_chain [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> class ThreadTestCase ( ForumTestCase ) : <EOL> def test_delete_no_session ( self ) : <EOL> """<STR_LIT>""" <EOL> r = get ( self . client , '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> assert ( settings . LOGIN_URL in r . redirect_chain [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> eq_ ( <NUM_LIT> , r . redirect_chain [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) </s>
