Beispiel #1
0
    def test_as_data_minimal(self):
        msg = Response(9, command='spam', seq=10)
        data = msg.as_data()

        self.assertEqual(
            data, {
                'type': 'response',
                'seq': 10,
                'request_seq': 9,
                'command': 'spam',
                'success': True,
            })
Beispiel #2
0
    def test_as_data_error_minimal(self):
        msg = Response(9,
                       command='spam',
                       success=False,
                       message='oops!',
                       seq=10)
        data = msg.as_data()

        self.assertEqual(
            data, {
                'type': 'response',
                'seq': 10,
                'request_seq': 9,
                'command': 'spam',
                'success': False,
                'message': 'oops!',
            })
Beispiel #3
0
    def test_repr_full(self):
        msg = Response(9, command='spam', seq=10)
        result = repr(msg)

        self.assertEqual(
            result,
            "Response(command='spam', request_seq=9, success=True, seq=10)"
        )  # noqa
Beispiel #4
0
    def test_repr_error_full(self):
        msg = Response(9,
                       command='spam',
                       success=False,
                       message='oops!',
                       seq=10)
        result = repr(msg)

        self.assertEqual(
            result,
            "Response(command='spam', request_seq=9, success=False, message='oops!', seq=10)"
        )  # noqa
Beispiel #5
0
    def test_from_data_without_body(self):
        data = {
            'type': 'response',
            'seq': 10,
            'command': 'spam',
            'request_seq': 9,
            'success': True,
        }
        msg = Response.from_data(**data)

        self.assertEqual(msg.type, 'response')
        self.assertEqual(msg.seq, 10)
        self.assertEqual(msg.command, 'spam')
        self.assertEqual(msg.request_seq, 9)
        self.assertTrue(msg.success)
        self.assertIsNone(msg.body)
        self.assertIsNone(msg.message)
Beispiel #6
0
    def test_validation(self):
        # request_seq

        with self.assertRaises(TypeError):
            # missing
            Response(None, command='spam')
        with self.assertRaises(TypeError):
            # missing
            Response('', command='spam')
        with self.assertRaises(TypeError):
            # couldn't convert to int
            Response(object(), command='spam')
        with self.assertRaises(ValueError):
            # not non-negative
            Response(-1, command='spam')

        # command

        with self.assertRaises(TypeError):
            # missing
            Response(9, command=None)
        with self.assertRaises(TypeError):
            # missing
            Response(9, command='')

        class Other1(Response):
            COMMAND = 'eggs'

        with self.assertRaises(ValueError):
            # does not match
            Other1(9, command='spam')

        # body

        class Other2(Response):
            class BODY(FieldsNamespace):
                FIELDS = [
                    Field('a'),
                ]

            ERROR_BODY = BODY

        with self.assertRaises(ValueError):
            # unexpected
            Response(9, command='spam', body=11)
        with self.assertRaises(TypeError):
            # missing (implicitly required)
            Other2(9, command='spam')
        with self.assertRaises(TypeError):
            # missing (explicitly required)
            Other2.BODY_REQUIRED = True
            Other2(9, command='spam')
        with self.assertRaises(ValueError):
            # unexpected (error)
            Response(9, command='spam', body=11, success=False, message=':(')
        with self.assertRaises(TypeError):
            # missing (error) (implicitly required)
            Other2(9, command='spam', success=False, message=':(')
        with self.assertRaises(TypeError):
            # missing (error) (explicitly required)
            Other2.ERROR_BODY_REQUIRED = True
            Other2(9, command='spam', success=False, message=':(')

        # message

        with self.assertRaises(TypeError):
            # missing
            Response(9, command='spam', success=False)
Beispiel #7
0
    def test_coercion_success(self):
        msg1 = Response(9, success=1, command='spam')
        msg2 = Response(9, success=None, command='spam', message='oops!')

        self.assertIs(msg1.success, True)
        self.assertIs(msg2.success, False)
Beispiel #8
0
    def test_coercion_request_seq(self):
        msg = Response('9', command='spam')

        self.assertEqual(msg.request_seq, 9)
Beispiel #9
0
 def test_no_args(self):
     with self.assertRaises(TypeError):
         Response()