<s> import kombu <EOL> import mock <EOL> import mongoengine as me <EOL> from st2common . models import db <EOL> from st2common . models . db import stormbase <EOL> from st2common . persistence import base as persistence <EOL> from st2common . transport import publishers <EOL> from st2common . transport import utils as transport_utils <EOL> from st2tests import DbTestCase <EOL> FAKE_STATE_MGMT_XCHG = kombu . Exchange ( '<STR_LIT>' , type = '<STR_LIT>' ) <EOL> class FakeModelPublisher ( publishers . StatePublisherMixin ) : <EOL> def __init__ ( self , url ) : <EOL> super ( FakeModelPublisher , self ) . __init__ ( url , FAKE_STATE_MGMT_XCHG ) <EOL> class FakeModelDB ( stormbase . StormBaseDB ) : <EOL> state = me . StringField ( required = True ) <EOL> class FakeModel ( persistence . Access ) : <EOL> impl = db . MongoDBAccess ( FakeModelDB ) <EOL> publisher = None <EOL> @ classmethod <EOL> def _get_impl ( cls ) : <EOL> return cls . impl <EOL> @ classmethod <EOL> def _get_publisher ( cls ) : <EOL> if not cls . publisher : <EOL> cls . publisher = FakeModelPublisher ( transport_utils . get_messaging_urls ( ) ) <EOL> return cls . publisher <EOL> @ classmethod <EOL> def publish_state ( cls , model_object ) : <EOL> publisher = cls . _get_publisher ( ) <EOL> if publisher : <EOL> publisher . publish_state ( model_object , getattr ( model_object , '<STR_LIT:state>' , None ) ) <EOL> class StatePublisherTest ( DbTestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> super ( StatePublisherTest , cls ) . setUpClass ( ) <EOL> cls . access = FakeModel ( ) <EOL> def tearDown ( self ) : <EOL> FakeModelDB . drop_collection ( ) <EOL> super ( StatePublisherTest , self ) . tearDown ( ) <EOL> @ mock . patch . object ( publishers . PoolPublisher , '<STR_LIT>' , mock . MagicMock ( ) ) <EOL> def test_publish ( self ) : <EOL> instance = FakeModelDB ( state = '<STR_LIT>' ) <EOL> self . access . publish_state ( instance ) <EOL> publishers . PoolPublisher . publish . assert_called_with ( instance , <EOL> FAKE_STATE_MGMT_XCHG , <EOL> instance . state ) <EOL> def test_publish_unset ( self ) : <EOL> instance = FakeModelDB ( ) <EOL> self . assertRaises ( Exception , self . access . publish_state , instance ) <EOL> def test_publish_none ( self ) : <EOL> instance = FakeModelDB ( state = None ) <EOL> self . assertRaises ( Exception , self . access . publish_state , instance ) <EOL> def test_publish_empty_str ( self ) : <EOL> instance = FakeModelDB ( state = '<STR_LIT>' ) <EOL> self . assertRaises ( Exception , self . access . publish_state , instance ) </s>
