class TestPublisher(unittest.TestCase):
    """
    Units tests for the call center vector publisher class.
    """
    
    def setUp(self):
        self.connection_args = {'host':'localhost' ,
                                'port': '5672',
                                'userid':'guest',
                                'password':'******',
                                'virtual_host':'/',
                                'insist': False,
                                'ssl':False}
        
        self.conn = CallCenterVectorConnection(**self.connection_args)
        self.publish_args = {'connection': self.conn,
                             'routing_key': 'test_key',
                             'delivery_mode': 2,
                             'exchange': 'test_exchange'
                             }
        self.consumer_args = {'queue':'test_queue',
                              'exchange' : 'test_exchange',
                              'exchange_type': 'direct',
                              'routing_key': 'test_key'     }
        self.publisher= None
        self.consumer = None
        
        
    def tearDown(self):
        self.conn.close()
        self.publisher.close()
        if self.consumer:
            self.consumer.close()
        
        
    def test_publish_constructor_with_defined_connection(self):
        #test with connection definied
        self.publisher = CallCenterVectorPublisher(**self.publish_args)
        assert(self.publisher.channel())
        assert(self.publisher.delivery_mode())
        assert(self.publisher.connection())
        assert(self.publisher.routing_key())
        
        
    def test_publisher_constructor_without_connection(self):
        self.publisher = CallCenterVectorPublisher(delivery_mode=self.publish_args['delivery_mode'], exchange=self.publish_args['exchange'], 
                                                   routing_key=self.publish_args['routing_key'])
        assert(self.publisher.channel())
        assert(self.publisher.delivery_mode())
        assert(self.publisher.connection())
        assert(self.publisher.routing_key())
        
    
    def test_publish(self):
        self.publisher = CallCenterVectorPublisher(**self.publish_args)
        self.consumer = CallCenterVectorConsumer(**self.consumer_args)
        
        #create random message
        created_message = str(random.randrange(20))
        sent_message = self.publisher.publish_to_queue(created_message)
        self.assertEqual(created_message,sent_message,"The created message and returned message do not match: %s %s" % (created_message, sent_message))
        
        #delay for good faith
        time.sleep(2.5)
        #get message and test for integrity 
        recieved_message = self.consumer.basic_get()
        self.assertEqual(created_message,recieved_message,"MESSAGES DO NOT MATCH: %s %s" % (created_message,recieved_message))