Exemplo n.º 1
0
    def test_parse_nested_dict_customer_no_defaults(self):

        locality = Dict('locality', 'type', 'name')
        address = Dict('address', locality, 'street')
        email = Dict('email', 'personal', 'business')
        customer = Dict('customer', 'name', email, address)

        class MyService(Service):
            class SimpleIO:
                input = customer

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        data = Bunch()
        data.customer = Bunch()
        data.customer.name = 'my-name'
        data.customer.email = Bunch()
        data.customer.email.personal = 'my-personal-email'
        data.customer.email.business = 'my-business-email'
        data.customer.address = Bunch()
        data.customer.address.street = 'my-street'
        data.customer.address.locality = Bunch()
        data.customer.address.locality.type = 'my-type'
        data.customer.address.locality.name = 'my-name'

        input = MyService._sio.parse_input(data, DATA_FORMAT.JSON)
        self.assertIsInstance(input, Bunch)

        self.assertEquals(input.customer.name, data.customer.name)
        self.assertEquals(input.customer.email.personal, data.customer.email.personal)
        self.assertEquals(input.customer.email.business, data.customer.email.business)
        self.assertEquals(input.customer.address.street, data.customer.address.street)
        self.assertEquals(input.customer.address.locality.type, data.customer.address.locality.type)
        self.assertEquals(input.customer.address.locality.name, data.customer.address.locality.name)
Exemplo n.º 2
0
    def test_parse_nested_dict_all_sio_elems(self):

        locality = Dict('locality', Int('type'), Text('name'), AsIs('coords'), Decimal('geo_skip'), Float('geo_diff'))
        address = Dict('address', locality, UUID('street_id'), CSV('prefs'), DateTime('since'), List('types'), Opaque('opaque1'))
        email = Dict('email', Text('value'), Bool('is_business'), Date('join_date'), DictList('preferred_order', 'name', 'pos'))
        customer = Dict('customer', 'name', email, address)

        class MyService(Service):
            class SimpleIO:
                input = customer

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        data = Bunch()
        data.customer = Bunch()
        data.customer.name = 'my-name'
        data.customer.email = Bunch()
        data.customer.email.value = 'my-email'
        data.customer.email.is_business = True
        data.customer.email.join_date = '1999-12-31'
        data.customer.email.preferred_order = [{'name':'address2', 'pos':'2'}, {'name':'address1', 'pos':'1'}]
        data.customer.address = Bunch()
        data.customer.address.locality = Bunch()
        data.customer.address.locality.type = '111'
        data.customer.address.locality.name = 'my-locality'
        data.customer.address.locality.coords = object()
        data.customer.address.locality.geo_skip = '123.456'
        data.customer.address.locality.geo_diff = '999.777'
        data.customer.address.street_id = uuid4().hex
        data.customer.address.prefs = '1,2,3,4'
        data.customer.address.since = '27-11-1988T11:22:33'
        data.customer.address.types = ['a', 'b', 'c', 'd']
        data.customer.address.opaque1 = object()

        input = MyService._sio.parse_input(data, DATA_FORMAT.JSON)
        self.assertIsInstance(input, Bunch)

        self.assertEquals(input.customer.name, data.customer.name)
        self.assertEquals(input.customer.email.value, data.customer.email.value)
        self.assertEquals(input.customer.email.is_business, data.customer.email.is_business)
        self.assertEquals(input.customer.email.join_date, dt_parse(data.customer.email.join_date))
        self.assertListEqual(input.customer.email.preferred_order, data.customer.email.preferred_order)
        self.assertEquals(input.customer.address.locality.type, int(data.customer.address.locality.type))
        self.assertEquals(input.customer.address.locality.name, data.customer.address.locality.name)
        self.assertIs(input.customer.address.locality.coords, data.customer.address.locality.coords)
        self.assertEquals(input.customer.address.locality.geo_skip, decimal_Decimal(data.customer.address.locality.geo_skip))
        self.assertEquals(input.customer.address.locality.geo_diff, float(data.customer.address.locality.geo_diff))
        self.assertEquals(input.customer.address.street_id, uuid_UUID(data.customer.address.street_id))
        self.assertEquals(input.customer.address.prefs, data.customer.address.prefs.split(','))
        self.assertEquals(input.customer.address.since, dt_parse(data.customer.address.since))
        self.assertEquals(input.customer.address.types, data.customer.address.types)
        self.assertIs(input.customer.address.opaque1, data.customer.address.opaque1)
Exemplo n.º 3
0
    def test_parse_nested_dict_customer_deep_defaults_elem_level(self):

        locality_default = object()

        locality = Dict('locality', '-type', '-name', default=locality_default)
        address = Dict('address', locality, '-street')
        email = Dict('email', 'personal', 'business')
        customer = Dict('customer', 'name', email, address)

        _default_input_value = 'default-input-value'

        class MyService(Service):
            class SimpleIO:
                input = customer
                default_input_value = 'default-input-value'

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        # Note that this locality has no type nor name but we expect for that Dict's default value to be used,
        # also, address has no street but since this Dict has no default value, again, SimpleIO one will be used.
        data = Bunch()
        data.customer = Bunch()
        data.customer.name = 'my-name'
        data.customer.email = Bunch()
        data.customer.email.personal = 'my-personal-email'
        data.customer.email.business = 'my-business-email'
        data.customer.address = Bunch()
        data.customer.address.locality = Bunch()

        input = MyService._sio.parse_input(data, DATA_FORMAT.JSON)
        self.assertIsInstance(input, Bunch)

        self.assertEquals(input.customer.name, data.customer.name)
        self.assertEquals(input.customer.email.personal,
                          data.customer.email.personal)
        self.assertEquals(input.customer.email.business,
                          data.customer.email.business)
        self.assertEquals(input.customer.address.street, _default_input_value)
        self.assertEquals(input.customer.address.locality.type,
                          locality_default)
        self.assertEquals(input.customer.address.locality.name,
                          locality_default)
Exemplo n.º 4
0
    def test_parse_nested_dict_customer_deep_defaults_sio_level(self):

        locality = Dict('locality', '-type', '-name')
        address = Dict('address', locality, 'street')
        email = Dict('email', 'personal', 'business')
        customer = Dict('customer', 'name', email, address)

        _default_input_value = 'default-input-value'

        class MyService(Service):
            class SimpleIO:
                input = customer
                default_input_value = 'default-input-value'

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        # Note that locality has no type nor name and we expect for the SimpleIO-level default value to be used
        data = Bunch()
        data.customer = Bunch()
        data.customer.name = 'my-name'
        data.customer.email = Bunch()
        data.customer.email.personal = 'my-personal-email'
        data.customer.email.business = 'my-business-email'
        data.customer.address = Bunch()
        data.customer.address.street = 'my-street'
        data.customer.address.locality = Bunch()

        input = MyService._sio.parse_input(data, DATA_FORMAT.JSON)
        self.assertIsInstance(input, Bunch)

        self.assertEquals(input.customer.name, data.customer.name)
        self.assertEquals(input.customer.email.personal,
                          data.customer.email.personal)
        self.assertEquals(input.customer.email.business,
                          data.customer.email.business)
        self.assertEquals(input.customer.address.street,
                          data.customer.address.street)
        self.assertEquals(input.customer.address.locality.type,
                          _default_input_value)
        self.assertEquals(input.customer.address.locality.name,
                          _default_input_value)
Exemplo n.º 5
0
    def test_parse_nested_dict_all_sio_elems_some_missing(self):

        _default_input_value = 'default-input-value'
        default_locality = 'default-locality'
        default_address = 'default-address'

        locality = Dict('locality',
                        Int('type'),
                        Text('name'),
                        AsIs('-coords'),
                        Decimal('geo_skip'),
                        Float('geo_diff'),
                        default=default_locality)

        address = Dict('address',
                       locality,
                       UUID('-street_id'),
                       CSV('prefs'),
                       DateTime('since'),
                       List('types'),
                       Opaque('opaque1'),
                       default=default_address)

        email = Dict('email', Text('value'), Bool('is_business'),
                     Date('-join_date'),
                     DictList('preferred_order', 'name', '-pos'))
        customer = Dict('customer', 'name', email, address)

        class MyService(Service):
            class SimpleIO:
                input = customer
                default_input_value = 'default-input-value'

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        # Note that 'join_date', 'street_id', 'coords' and one of 'pos' keys are missing in input below,
        # the test ensures that default values are used in their place.

        data = Bunch()
        data.customer = Bunch()
        data.customer.name = 'my-name'
        data.customer.email = Bunch()
        data.customer.email.value = 'my-email'
        data.customer.email.is_business = True
        data.customer.email.preferred_order = [{
            'name': 'address2',
            'pos': '2'
        }, {
            'name': 'address1'
        }]
        data.customer.address = Bunch()
        data.customer.address.locality = Bunch()
        data.customer.address.locality.type = '111'
        data.customer.address.locality.name = 'my-locality'
        data.customer.address.locality.geo_skip = '123.456'
        data.customer.address.locality.geo_diff = '999.777'
        data.customer.address.prefs = '1,2,3,4'
        data.customer.address.since = '27-11-1988T11:22:33'
        data.customer.address.types = ['a', 'b', 'c', 'd']
        data.customer.address.opaque1 = object()

        input = MyService._sio.parse_input(data, DATA_FORMAT.JSON)
        self.assertIsInstance(input, Bunch)

        self.assertEquals(input.customer.name, data.customer.name)
        self.assertEquals(input.customer.email.value,
                          data.customer.email.value)
        self.assertEquals(input.customer.email.is_business,
                          data.customer.email.is_business)
        self.assertEquals(input.customer.email.join_date, _default_input_value)

        self.assertDictEqual(input.customer.email.preferred_order[0],
                             data.customer.email.preferred_order[0])
        self.assertEquals(input.customer.email.preferred_order[1].name,
                          data.customer.email.preferred_order[1]['name'])
        self.assertEquals(input.customer.email.preferred_order[1].pos,
                          _default_input_value)

        self.assertEquals(input.customer.address.locality.type,
                          int(data.customer.address.locality.type))
        self.assertEquals(input.customer.address.locality.name,
                          data.customer.address.locality.name)
        self.assertEquals(input.customer.address.locality.coords,
                          default_locality)
        self.assertEquals(
            input.customer.address.locality.geo_skip,
            decimal_Decimal(data.customer.address.locality.geo_skip))
        self.assertEquals(input.customer.address.locality.geo_diff,
                          float(data.customer.address.locality.geo_diff))
        self.assertEquals(input.customer.address.street_id, default_address)
        self.assertEquals(input.customer.address.prefs,
                          data.customer.address.prefs.split(','))
        self.assertEquals(input.customer.address.since,
                          dt_parse(data.customer.address.since))
        self.assertEquals(input.customer.address.types,
                          data.customer.address.types)
        self.assertIs(input.customer.address.opaque1,
                      data.customer.address.opaque1)