Commits

Benjamin Smith  committed 547845a

Tests to increase coverage on frame.

  • Participants
  • Parent commits 1f1e74c

Comments (0)

Files changed (2)

File stompy/frame.py

 import socket
 import random
+from pprint import pformat
 from errno import EAGAIN
 from Queue import Queue
 from Queue import Empty as QueueEmpty
 
-
 class UnknownBrokerResponseError(Exception):
     """An unexpected response was received from the broker."""
 

File tests/test_frame.py

 #!/usr/bin/env python
 from dingus import Dingus, DingusTestCase, DontCare
-from unittest import TestCase
+#from unittest import TestCase
+import nose.tools as nose_tools
 import sys
 import socket
 from stompy import frame
-from stompy.frame import Frame
+from stompy.frame import Frame, IntermediateMessageQueue, UnknownBrokerResponseError
+from Queue import Empty as QueueEmpty
 
 
 class WhenSettingUp(DingusTestCase(Frame)):
         assert 'content-length:%i' % len(body) in this_frame.as_string()
 
 
-class WhenParsingFrames(DingusTestCase(Frame)):
+class WhenUsingIntermediateMQueue(DingusTestCase(IntermediateMessageQueue, 'Queue', 'QueueEmpty')):
+    
+    def setup(self):
+        super(WhenUsingIntermediateMQueue, self).setup()
+        self.queue = IntermediateMessageQueue()
+        self.frame = Frame()
+        self.frame.sock = Dingus()
+ 
+    def should_put_into_queue_and_get(self):
+        headers = {'destination': '/queue/nose_test',
+                   'persistent': 'true',
+                   'bytes_message': 'true'}
+        body = 'Testing'
+
+        this_frame = self.frame.build_frame({'command': 'SEND',
+                                             'headers': headers,
+                                             'body': body})
+
+        self.queue.put(this_frame)
+        extracted_frame = self.queue.get(this_frame)
+
+        assert 'destination' in extracted_frame.headers
+
+    def should_not_put_into_queue(self):
+        headers = {'persistent': 'true',
+                   'bytes_message': 'true'}
+        body = 'Testing'
+        this_frame = self.frame.build_frame({'command': 'SEND',
+                                             'headers': headers,
+                                             'body': body})
+
+        this_frame = Dingus()
+        self.queue.put(this_frame)
+        ret_frame = self.queue.get(this_frame)
+        assert this_frame.calls('parse_frame', nb=False)
+
+    def should_not_get_from_queue(self):
+        headers = {'destination': '/queue/nose_test',
+                   'persistent': 'true',
+                   'bytes_message': 'true'}
+        body = 'Testing'
+ 
+        this_frame = self.frame.build_frame({'command': 'SEND',
+                                             'headers': headers,
+                                             'body': body})
+
+        this_frame = Dingus()
+        extracted_frame = self.queue.get(this_frame)
+        print this_frame.calls
+        assert this_frame.calls('parse_frame', nb=False)
+
+    def should_not_get_message(self):
+        my_frame = Frame()
+        my_frame._getline = Dingus()
+        my_frame._getline.return_value = None
+        my_frame.iqueue.get = Dingus()
+        my_frame.iqueue.get.return_value = None
+        ret_frame = my_frame.get_message(nb=True)
+        assert ret_frame is None
+
+    def should_get_message_and_return_frame(self):
+        my_frame = Frame()
+        my_frame._getline = Dingus()
+        command = 'MESSAGE'
+        body = 'Test 1'
+        headers = {'session':'ID:nose-session123',
+                   'content-length':'%d' %len(body)}
+        my_frame.parse_frame = Dingus()
+        this_frame = my_frame.build_frame({'command': command,
+                                           'headers': headers,
+                                           'body': body})
+        my_frame.parse_frame.return_value = this_frame
+        ret_frame = my_frame.get_message(nb=True)
+        assert isinstance(ret_frame, Frame)
+
+    def should_get_reply(self):
+        my_frame = Frame()
+        command = 'SEND'
+        body = 'Test 1'
+        headers = {'destination': '/queue/nose_test',
+                   'persistent': 'true',
+                   'bytes_message': 'true'}
+        my_frame.parse_frame = Dingus()
+        this_frame = my_frame.build_frame({'command': command,
+                                           'headers': headers,
+                                           'body': body})
+        my_frame.parse_frame.return_value = this_frame
+        my_frame.rqueue.put_nowait(my_frame)
+        ret_frame = my_frame.get_reply(nb=True)
+        assert isinstance(ret_frame, Frame) 
+
+    def should_not_get_reply(self):
+        my_frame = Frame()
+        command = 'SEND'
+        body = 'Test 1'
+        headers = {'destination': '/queue/nose_test',
+                   'persistent': 'true',
+                   'bytes_message': 'true'}
+        my_frame.parse_frame = Dingus()
+        this_frame = my_frame.build_frame({'command': command,
+                                           'headers': headers,
+                                           'body': body})
+        my_frame.parse_frame.return_value = None
+        ret_frame = my_frame.get_reply(nb=True)
+        assert ret_frame is None
+
+
+
+class WhenParsingFrames(DingusTestCase(Frame,"UnknownBrokerResponseError")):
 
     def setup(self):
         super(WhenParsingFrames, self).setup()
         self.frame._getline()
 
         assert self.frame.sock.calls('recv', 1)
+
+    def should_not_get_line(self):
+        my_frame = Frame()
+        my_frame._getline = Dingus()
+        my_frame._getline.return_value = None
+        ret_value = my_frame.parse_frame()
+        assert ret_value is None
+
+    def should_fail_to_get_headers(self):
+        my_frame = Frame()
+        my_frame._getline = Dingus()
+        my_frame._getline.return_value = \
+                'RECEIPTreceipt-id:ID:nose-receipt123'
+
+        nose_tools.assert_raises(UnknownBrokerResponseError, my_frame.parse_frame) 
+
+    def should_return_frame_repr(self):
+        my_frame = Frame()
+        assert isinstance(repr(my_frame), type(''))