<s> from django . test import TestCase <EOL> from model_mommy import mommy <EOL> from offers . models import Offer , Provider <EOL> from offers . tasks import publish_offer , publish_latest_offer <EOL> from django . test . utils import override_settings <EOL> from django . contrib . auth . models import User <EOL> from django . core import mail <EOL> from datetime import timedelta <EOL> class OfferTaskTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . provider = mommy . make ( Provider ) <EOL> self . offer = mommy . make ( Offer , provider = self . provider , status = Offer . PUBLISHED ) <EOL> self . user = User . objects . create_user ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . user . user_profile . provider = self . provider <EOL> self . user . user_profile . save ( ) <EOL> def test_publish_offer_adds_provider_as_follower ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( self . offer . followers . count ( ) , <NUM_LIT:0> ) <EOL> result = publish_offer . delay ( self . offer . pk ) <EOL> self . assertTrue ( result . successful ( ) ) <EOL> self . assertIn ( self . user , self . offer . followers . all ( ) ) <EOL> self . assertEqual ( self . offer . followers . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( mail . outbox [ <NUM_LIT:0> ] . to ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:0> ] . to [ <NUM_LIT:0> ] , self . user . email ) <EOL> def test_publish_offer_does_nothing_with_invalid_offer ( self ) : <EOL> """<STR_LIT>""" <EOL> self . offer . status = Offer . REQUEST_COMPLETE <EOL> self . offer . save ( ) <EOL> self . assertEqual ( self . offer . followers . count ( ) , <NUM_LIT:0> ) <EOL> result = publish_offer . delay ( self . offer . pk ) <EOL> self . assertTrue ( result . successful ( ) ) <EOL> self . assertNotIn ( self . user , self . offer . followers . all ( ) ) <EOL> self . assertEqual ( self . offer . followers . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> def test_publish_latest_offer_publishes_oldest_offer ( self ) : <EOL> """<STR_LIT>""" <EOL> self . offer . delete ( ) <EOL> offers = mommy . make ( <EOL> Offer , <EOL> _quantity = <NUM_LIT:3> , <EOL> status = Offer . REQUEST_COMPLETE , <EOL> provider = self . provider , <EOL> creator = self . user <EOL> ) <EOL> mommy . make ( Offer , status = Offer . REQUEST_INCOMPLETE ) <EOL> mommy . make ( Offer , status = Offer . UNPUBLISHED ) <EOL> mommy . make ( Offer , status = Offer . PUBLISHED ) <EOL> offers [ <NUM_LIT:0> ] . readied_at -= timedelta ( days = <NUM_LIT:3> ) <EOL> offers [ <NUM_LIT:1> ] . readied_at -= timedelta ( days = <NUM_LIT:2> ) <EOL> offers [ <NUM_LIT:2> ] . readied_at -= timedelta ( days = <NUM_LIT:1> ) <EOL> self . assertTrue ( publish_latest_offer . delay ( ) . successful ( ) ) <EOL> new_offer = Offer . objects . get ( pk = offers [ <NUM_LIT:0> ] . pk ) <EOL> self . assertEqual ( new_offer . status , Offer . PUBLISHED ) <EOL> self . assertFalse ( new_offer . is_request ) <EOL> self . assertEqual ( Offer . objects . filter ( status = Offer . PUBLISHED , is_request = False ) . count ( ) , <NUM_LIT:2> ) <EOL> def test_publish_latest_offer_does_not_publish_empty_offers ( self ) : <EOL> """<STR_LIT>""" <EOL> self . offer . delete ( ) <EOL> mommy . make ( <EOL> Offer , <EOL> _quantity = <NUM_LIT:3> , <EOL> is_ready = False , <EOL> is_request = True , <EOL> status = Offer . UNPUBLISHED , <EOL> ) <EOL> mommy . make ( <EOL> Offer , <EOL> _quantity = <NUM_LIT:3> , <EOL> is_ready = True , <EOL> is_request = False , <EOL> status = Offer . UNPUBLISHED , <EOL> ) <EOL> self . assertTrue ( publish_latest_offer . delay ( ) . successful ( ) ) <EOL> self . assertEqual ( Offer . objects . filter ( status = Offer . PUBLISHED ) . count ( ) , <NUM_LIT:0> ) </s>
