Esempio n. 1
0
    def test_option(self):
        '''Test encoding and decoding of option values'''

        type_ = protocol.Option(protocol.UINT32)

        self._run_test(type_, None)
        self._run_test(type_, 1)
Esempio n. 2
0
    def test_option(self):
        '''Test option checks'''

        type_ = protocol.Option(protocol.STRING)

        self._run_test(type_, (None, True), ('abc', True), ('', True),
                       (1, False))
Esempio n. 3
0
class Assert(Step):
    '''"Assert" operation'''

    TAG = 8
    ARGS = ('key', protocol.STRING), \
        ('value', protocol.Option(protocol.STRING)),

    def __init__(self, key, value):
        super(Assert, self).__init__(key, value)

        self._key = key
        self._value = value

    key = property(operator.attrgetter('_key'),
                   doc=utils.format_doc('''
            Key for which to assert the given value

            :type: :class:`str`
        '''))
    value = property(operator.attrgetter('_value'),
                     doc=utils.format_doc('''
            Expected value

            :type: :class:`str` or :data:`None`
        '''))
Esempio n. 4
0
        def handle_test_and_set():
            '''Handle a "test_and_set" command'''

            key = recv(protocol.STRING)
            test_value = recv(protocol.Option(protocol.STRING))
            set_value = recv(protocol.Option(protocol.STRING))

            # Key doesn't exist and test_value is not None -> NotFound
            if key not in self._values and test_value is not None:
                for rbytes in protocol.UINT32.serialize(
                    errors.NotFound.CODE):
                    yield rbytes
                for rbytes in protocol.STRING.serialize(key):
                    yield rbytes

                return

            # Key doesn't exist and test_value is None -> create
            if key not in self._values and test_value is None:
                self._values[key] = set_value

                for rbytes in protocol.UINT32.serialize(
                    protocol.RESULT_SUCCESS):
                    yield rbytes
                for rbytes in protocol.Option(protocol.STRING).serialize(None):
                    yield rbytes

                return

            # Key exists
            orig_value = self._values[key]

            # Need to update?
            if test_value == orig_value:
                if set_value is not None:
                    self._values[key] = set_value
                else:
                    del self._values[key]

            # Return original value
            for rbytes in protocol.UINT32.serialize(
                protocol.RESULT_SUCCESS):
                yield rbytes

            for rbytes in protocol.Option(protocol.STRING).serialize(
                orig_value):
                yield rbytes
Esempio n. 5
0
        def handle_who_master():
            '''Handle a "who_master" command'''

            for rbytes in protocol.UINT32.serialize(
                protocol.RESULT_SUCCESS):
                yield rbytes
            for rbytes in protocol.Option(protocol.STRING).serialize(
                self.MASTER):
                yield rbytes
Esempio n. 6
0
    def test_multiple_correct_arguments(self):
        '''Test `validate_types` with multiple correct arguments'''

        client_utils.validate_types((
            ('name', protocol.STRING),
            ('age', protocol.Option(protocol.UINT32)),
        ), (
            'name',
            None,
        ))
Esempio n. 7
0
    def test_complex(self):
        '''Test encoding and decoding of a complex type value'''

        type_ = protocol.Product(
            protocol.UINT32, protocol.List(protocol.STRING),
            protocol.Option(protocol.Product(
                protocol.UINT32, protocol.STRING)))

        def handler(value):
            return (value[0], tuple(reversed(value[1])), value[2])

        self._run_test(type_, ((0, ('abc', 'def',), (1, 'abc'))), handler)
        self._run_test(type_, ((0, ('abc',), None)), handler)
Esempio n. 8
0
    def test_complex(self):
        '''Test checking of a complex, nested type'''

        type_ = protocol.Product(
            protocol.UINT32, protocol.List(protocol.STRING),
            protocol.Option(protocol.Product(protocol.UINT32,
                                             protocol.STRING)))

        self._run_test(type_, ((0, (
            'abc',
            'def',
        ), (1, 'abc')), True), ((0, ('abc', ), None), True),
                       ((-1, (), None), False))
Esempio n. 9
0
    def test_multiple_incorrect_arguments(self):
        '''Test `validate_types` with multiple incorrect arguments'''

        run_test = lambda value: client_utils.validate_types((
            ('name', protocol.STRING),
            ('age', protocol.Option(protocol.UINT32)),
        ), (
            'name',
            value,
        ))

        self.assertRaises(ValueError, run_test, -1)
        self.assertRaises(TypeError, run_test, '1')