Exemplo n.º 1
0
    def test_duration_xml_duration(self):
        dur = datetime.timedelta(days=5 + 30 + 365, hours=1, minutes=1,
                                                   seconds=12, microseconds=8e5)

        str1 = 'P400DT3672.8S'
        str2 = 'P1Y1M5DT1H1M12.8S'

        self.assertEquals(dur, ProtocolBase().from_unicode(Duration, str1))
        self.assertEquals(dur, ProtocolBase().from_unicode(Duration, str2))

        self.assertEquals(dur, ProtocolBase().from_unicode(Duration,
                                      ProtocolBase().to_unicode(Duration, dur)))
Exemplo n.º 2
0
    def test_limits(self):
        try:
            ProtocolBase() \
                      .from_string(Integer, "1" * (Integer.__max_str_len__ + 1))
        except:
            pass
        else:
            raise Exception("must fail.")

        ProtocolBase().from_string(UnsignedInteger, "-1")
                                                 # This is not supposed to fail.

        assert not UnsignedInteger.validate_native(UnsignedInteger, -1)
Exemplo n.º 3
0
    def test_datetime_serialize_as(self):
        i = 1234567890123456
        v = datetime.datetime.fromtimestamp(i / 1e6)

        assert ProtocolBase().to_string(DateTime(serialize_as='sec'),
                                        v) == i // 1e6
        assert ProtocolBase().to_string(DateTime(serialize_as='sec_float'),
                                        v) == i / 1e6
        assert ProtocolBase().to_string(DateTime(serialize_as='msec'),
                                        v) == i // 1e3
        assert ProtocolBase().to_string(DateTime(serialize_as='msec_float'),
                                        v) == i / 1e3
        assert ProtocolBase().to_string(DateTime(serialize_as='usec'), v) == i
Exemplo n.º 4
0
    def __init__(self,
                 services,
                 tns,
                 name=None,
                 in_protocol=None,
                 out_protocol=None,
                 config=None,
                 classes=()):
        self.services = tuple(services)
        self.tns = tns
        self.name = name
        self.config = config
        self.classes = classes

        if self.name is None:
            self.name = self.__class__.__name__.split('.')[-1]

        logger.info("Initializing application {%s}%s...", self.tns, self.name)

        self.event_manager = EventManager(self)
        self.error_handler = None

        self.in_protocol = in_protocol
        self.out_protocol = out_protocol

        if self.in_protocol is None:
            from spyne.protocol import ProtocolBase
            self.in_protocol = ProtocolBase()

        if self.out_protocol is None:
            from spyne.protocol import ProtocolBase
            self.out_protocol = ProtocolBase()

        self.check_unique_method_keys()  # is this really necessary nowadays?

        # this needs to be after protocol assignments to give _static_when
        # functions as much info as possible about the application
        self.interface = Interface(self)

        # set_app needs to be after interface init because the protocols use it.
        self.in_protocol.set_app(self)
        # FIXME: this normally is another parameter to set_app but it's kept
        # separate for backwards compatibility reasons.
        self.in_protocol.message = self.in_protocol.REQUEST

        self.out_protocol.set_app(self)
        # FIXME: this normally is another parameter to set_app but it's kept
        # separate for backwards compatibility reasons.
        self.out_protocol.message = self.out_protocol.RESPONSE

        register_application(self)
Exemplo n.º 5
0
    def test_canonical_case(self):
        class TestSelfReference(ComplexModel):
            self_reference = SelfReference

        assert (TestSelfReference._type_info['self_reference'] is TestSelfReference)

        class SoapService(ServiceBase):
            @rpc(_returns=TestSelfReference)
            def view_categories(ctx):
                pass

        Application([SoapService], 'service.soap',
                            in_protocol=ProtocolBase(),
                            out_protocol=ProtocolBase())
Exemplo n.º 6
0
    def test_datetime_deserialize(self):
        i = 1234567890123456
        v = datetime.datetime.fromtimestamp(i / 1e6)

        assert ProtocolBase().from_unicode(
                    DateTime(serialize_as='sec'), i//1e6) == \
                                     datetime.datetime.fromtimestamp(i//1e6)
        assert ProtocolBase().from_unicode(
                    DateTime(serialize_as='sec_float'), i/1e6) == v

        assert ProtocolBase().from_unicode(
                    DateTime(serialize_as='msec'), i//1e3) == \
                                    datetime.datetime.fromtimestamp(i/1e3//1000)
        assert ProtocolBase().from_unicode(
                    DateTime(serialize_as='msec_float'), i/1e3) == v

        assert ProtocolBase().from_unicode(
                    DateTime(serialize_as='usec'), i) == v
Exemplo n.º 7
0
    def __init__(self,
                 services,
                 tns,
                 name=None,
                 in_protocol=None,
                 out_protocol=None,
                 config=None):
        self.services = tuple(services)
        self.tns = tns
        self.name = name
        self.config = config

        if self.name is None:
            self.name = self.__class__.__name__.split('.')[-1]

        logger.info("Initializing application {%s}%s...", self.tns, self.name)

        self.event_manager = EventManager(self)
        self.error_handler = None

        self.interface = Interface(self)
        self.in_protocol = in_protocol
        self.out_protocol = out_protocol

        if self.in_protocol is None:
            from spyne.protocol import ProtocolBase
            self.in_protocol = ProtocolBase()
        self.in_protocol.set_app(self)
        # FIXME: this normally is another parameter to set_app but it's kept
        # separate for backwards compatibility reasons.
        self.in_protocol.message = self.in_protocol.REQUEST

        if self.out_protocol is None:
            from spyne.protocol import ProtocolBase
            self.out_protocol = ProtocolBase()
        self.out_protocol.set_app(self)
        # FIXME: this normally is another parameter to set_app but it's kept
        # separate for backwards compatibility reasons.
        self.out_protocol.message = self.out_protocol.RESPONSE

        self.check_unique_method_keys()
        register_application(self)
Exemplo n.º 8
0
    def test_datetime_usec(self):
        # see the comments on time test for why the rounding here is weird

        # rounding 0.1 µsec down
        dt = ProtocolBase().from_unicode(DateTime, "2015-01-01 12:12:12.0000001")
        self.assertEquals(datetime.datetime(2015, 1, 1, 12, 12, 12), dt)

        # rounding 1.5 µsec up. 0.5 is rounded down by python 3 and up by
        # python 2 so we test with 1.5 µsec instead. frikkin' nonsense.
        dt = ProtocolBase().from_unicode(DateTime, "2015-01-01 12:12:12.0000015")
        self.assertEquals(datetime.datetime(2015, 1, 1, 12, 12, 12, 2), dt)

        # rounding 999998.8 µsec up
        dt = ProtocolBase().from_unicode(DateTime, "2015-01-01 12:12:12.9999988")
        self.assertEquals(datetime.datetime(2015, 1, 1, 12, 12, 12, 999999), dt)

        # rounding 999999.1 µsec down
        dt = ProtocolBase().from_unicode(DateTime, "2015-01-01 12:12:12.9999991")
        self.assertEquals(datetime.datetime(2015, 1, 1, 12, 12, 12, 999999), dt)

        # rounding 999999.8 µsec down, not up.
        dt = ProtocolBase().from_unicode(DateTime, "2015-01-01 12:12:12.9999998")
        self.assertEquals(datetime.datetime(2015, 1, 1, 12, 12, 12, 999999), dt)
Exemplo n.º 9
0
    def test_self_referential_array_workaround(self):
        from spyne.util.dictdoc import get_object_as_dict
        class Category(ComplexModel):
            id = Integer(min_occurs=1, max_occurs=1, nillable=False)

        Category._type_info['children'] = Array(Category)

        parent = Category()
        parent.children = [Category(id=0), Category(id=1)]

        d = get_object_as_dict(parent, Category)
        pprint(d)
        assert d['children'][0]['id'] == 0
        assert d['children'][1]['id'] == 1

        class SoapService(ServiceBase):
            @rpc(_returns=Category)
            def view_categories(ctx):
                pass

        Application([SoapService], 'service.soap',
                            in_protocol=ProtocolBase(),
                            out_protocol=ProtocolBase())
Exemplo n.º 10
0
        def test_duration(self):
            d = ProtocolBase().to_unicode(Duration, timedelta(0, 45))

            class SomeService(Service):
                @srpc(Duration, _returns=Duration)
                def some_call(p):
                    print(p)
                    print(type(p))
                    assert type(p) == timedelta
                    return p

            ctx = _dry_me([SomeService], {"some_call": [d]})

            s = self.loads(b''.join(ctx.out_string))
            d = {"some_callResponse": {"some_callResult": d}}
            print(s)
            print(d)
            assert s == d
Exemplo n.º 11
0
    def test_uuid_deserialize(self):
        value = uuid.UUID('12345678123456781234567812345678')

        assert ProtocolBase().from_unicode(Uuid,
                '12345678-1234-5678-1234-567812345678') == value
        assert ProtocolBase().from_unicode(Uuid(serialize_as='hex'),
                '12345678123456781234567812345678') == value
        assert ProtocolBase().from_unicode(Uuid(serialize_as='urn'),
                'urn:uuid:12345678-1234-5678-1234-567812345678') == value
        assert ProtocolBase().from_string(Uuid(serialize_as='bytes'),
                b'\x124Vx\x124Vx\x124Vx\x124Vx') == value
        assert ProtocolBase().from_string(Uuid(serialize_as='bytes_le'),
                b'xV4\x124\x12xV\x124Vx\x124Vx') == value
        assert ProtocolBase().from_unicode(Uuid(serialize_as='fields'),
                (305419896, 4660, 22136, 18, 52, 95073701484152)) == value
        assert ProtocolBase().from_unicode(Uuid(serialize_as='int'),
                24197857161011715162171839636988778104) == value
Exemplo n.º 12
0
    def test_uuid_serialize(self):
        value = uuid.UUID('12345678123456781234567812345678')

        assert ProtocolBase().to_bytes(Uuid, value) == \
                                '12345678-1234-5678-1234-567812345678'
        assert ProtocolBase().to_bytes(Uuid(serialize_as='hex'), value) == \
                                '12345678123456781234567812345678'
        assert ProtocolBase().to_bytes(Uuid(serialize_as='urn'), value) == \
                                'urn:uuid:12345678-1234-5678-1234-567812345678'
        assert ProtocolBase().to_bytes(Uuid(serialize_as='bytes'), value) == \
                                b'\x124Vx\x124Vx\x124Vx\x124Vx'
        assert ProtocolBase().to_bytes(Uuid(serialize_as='bytes_le'), value) == \
                                b'xV4\x124\x12xV\x124Vx\x124Vx'
        assert ProtocolBase().to_bytes(Uuid(serialize_as='fields'), value) == \
                                (305419896, 4660, 22136, 18, 52, 95073701484152)
        assert ProtocolBase().to_bytes(Uuid(serialize_as='int'), value) == \
                                24197857161011715162171839636988778104
Exemplo n.º 13
0
        def test_duration(self):
            d = ProtocolBase().to_string(Duration, timedelta(0, 45))

            class SomeService(ServiceBase):
                @srpc(Duration, _returns=Duration)
                def some_call(p):
                    print p
                    print type(p)
                    assert type(p) == timedelta
                    return p

            ctx = _dry_me([SomeService], {"some_call": [d]})

            s = ''.join(ctx.out_string)
            d = serializer.dumps({"some_callResponse": {
                "some_callResult": d
            }}, **dumps_kwargs)
            print s
            print d
            assert s == d
Exemplo n.º 14
0
BOOL_VALUES_BYTES_TRUE  = (b't', b'1', b'on', b'yes', b'true')
BOOL_VALUES_STR_TRUE    = (u't', u'1', u'on', u'yes', u'true')

BOOL_VALUES_BYTES_FALSE = (b'f', b'0', b'off', b'no', b'false')
BOOL_VALUES_STR_FALSE   = (u'f', u'0', u'off', u'no', u'false')

BOOL_VALUES_NONE = (None, '')


if six.PY2:
    bytes = str
else:
    unicode = str


_prot = ProtocolBase()

def _bool_from_int(i):
    if i in (0, 1):
        return i == 1
    raise ValueError(i)


def _bool_from_bytes(s):
    if s in BOOL_VALUES_NONE:
        return None

    s = s.strip()
    if s in BOOL_VALUES_NONE:
        return None
    s = s.lower()