def test_process_message_content_callback(self):
     """Test request processMessage for content w/callback."""
     response = []
     self.request = GetDelta(self.protocol, SHARE, 0,
                             callback=response.append)
     message = test_delta_info.get_message()
     self.request.processMessage(message)
     self.assertTrue(delta.from_message(message) in response)
class GetDeltaTestCase(RequestTestCase):
    """Test cases for GetDelta op."""

    request_class = GetDelta

    @defer.inlineCallbacks
    def setUp(self):
        """Initialize testing protocol."""
        yield super(GetDeltaTestCase, self).setUp()
        self.protocol = FakedProtocol()
        self.request = self.request_class(self.protocol, SHARE, 0)
        self.request.error = faked_error
        self.done_called = False
        self.request.done = was_called(self, 'done_called')

    def test_init(self):
        """Test request creation."""
        self.assertEqual(str(SHARE), self.request.share_id)

    def test_start(self):
        """Test request start."""
        self.request.start()

        self.assertEqual(1, len(self.request.protocol.messages))
        actual_msg, = self.request.protocol.messages
        self.assertEqual(protocol_pb2.Message.GET_DELTA, actual_msg.type)
        self.assertEqual(self.request.share_id,
                         actual_msg.get_delta.share)

    def test_process_message_error(self):
        """Test request processMessage on error."""
        message = protocol_pb2.Message()
        self.assertRaises(FakedError, self.request.processMessage, message)

    def test_process_message_ok(self):
        """Test request processMessage on sucess."""
        message = protocol_pb2.Message()
        message.type = protocol_pb2.Message.DELTA_END
        message.delta_end.generation = 100
        message.delta_end.full = True
        message.delta_end.free_bytes = 200
        self.request.processMessage(message)

        self.assertTrue(self.done_called, 'done() was called')
        self.assertEqual(self.request.end_generation,
                         message.delta_end.generation)
        self.assertEqual(self.request.full, message.delta_end.full)
        self.assertEqual(self.request.free_bytes,
                         message.delta_end.free_bytes)

    def test_process_message_content(self):
        """Test request processMessage for content."""
        message = test_delta_info.get_message()
        self.request.processMessage(message)
        self.assertTrue(delta.from_message(message) in self.request.response)

    def test_process_message_content_twice(self):
        """Test request processMessage for content."""
        message = test_delta_info.get_message()
        self.request.processMessage(message)
        message = test_delta_info.get_message()
        self.request.processMessage(message)
        self.assertEqual(len(self.request.response), 2)

    def test_process_message_content_callback(self):
        """Test request processMessage for content w/callback."""
        response = []
        self.request = GetDelta(self.protocol, SHARE, 0,
                                callback=response.append)
        message = test_delta_info.get_message()
        self.request.processMessage(message)
        self.assertTrue(delta.from_message(message) in response)

    def test_from_scratch_flag(self):
        """Test from scratch flag."""
        self.request = self.request_class(self.protocol, SHARE, 0,
                                          from_scratch=True)
        self.request.done = was_called(self, 'done_called')
        self.request.start()

        self.assertEqual(1, len(self.request.protocol.messages))
        actual_msg, = self.request.protocol.messages
        self.assertEqual(protocol_pb2.Message.GET_DELTA, actual_msg.type)
        self.assertEqual(self.request.share_id,
                         actual_msg.get_delta.share)