Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #5
0
    def test_parse_nested_dict_only_default_sio_level(self):

        _default_input_value = 'default-input-value'

        aaa = 'aaa'
        bbb = 'bbb'
        ccc = Dict('ccc', 'ddd', 'eee', '-fff')
        sss = Dict('sss', '-qqq')
        ggg = Dict('ggg', '-hhh', '-jjj', sss)

        class MyService(Service):
            class SimpleIO:
                input = Dict(aaa, bbb, ccc, ggg, '-ppp')
                default_input_value = _default_input_value

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

        data = {
            'aaa': {
                'bbb': 'bbb-111',
                'ccc': {
                    'ddd': 'ddd-111',
                    'eee': 'eee-111',
                },
                'ggg': {
                    'sss': {}
                }
            }
        }

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

        self.assertEquals(input.aaa.bbb, 'bbb-111')
        self.assertEquals(input.aaa.ccc.ddd, 'ddd-111')
        self.assertEquals(input.aaa.ccc.eee, 'eee-111')
        self.assertEquals(input.aaa.ccc.fff, _default_input_value)
        self.assertEquals(input.aaa.ccc.eee, 'eee-111')
        self.assertEquals(input.aaa.ggg.hhh, _default_input_value)
        self.assertEquals(input.aaa.ggg.sss.qqq, _default_input_value)
Пример #6
0
    def xtest_dict_without_key_names(self):

        sio = Dict('mykey')

        # Note that the dict will not expect any keys in particular on input because it has only a name and nothing else
        data = {
            'aaa': 'aaa-111',
            'bbb': 'bbb-111',
            'ccc': 'ccc-111',
            'ddd': 'ddd-111',
            'fff': 'fff-111'
        }
        parsed = self._parse(sio, data)

        self.assertDictEqual(parsed, data)
Пример #7
0
    def xtest_dict_with_key_names(self):

        sio = Dict('mykey', 'aaa', 'bbb', 'ccc', '-ddd', '-eee')

        # Note that 'eee' is optional hence it may be omitted and that 'fff' is not part of the dict's I/O definition
        data = {
            'aaa': 'aaa-111',
            'bbb': 'bbb-111',
            'ccc': 'ccc-111',
            'ddd': 'ddd-111',
            'fff': 'fff-111'
        }
        parsed = self._parse(sio, data)

        self.assertDictEqual(
            parsed, {
                'aaa': 'aaa-111',
                'bbb': 'bbb-111',
                'ccc': 'ccc-111',
                'ddd': 'ddd-111',
                'eee': NotGiven,
            })
Пример #8
0
 class SimpleIO:
     input = 'aaa', AsIs('bbb'), Bool('ccc'), CSV('ddd'), Date('eee'), DateTime('fff'), Decimal('ggg'), \
         Dict('hhh', 'a', 'b', 'c'), DictList('iii', 'd', 'e', 'f'), Float('jjj'), Int('mmm'), List('nnn'), \
         Opaque('ooo'), Text('ppp'), UUID('qqq')
Пример #9
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)
Пример #10
0
 class SimpleIO:
     input = Dict(aaa, bbb, ccc, ggg, '-ppp')
     default_input_value = _default_input_value
Пример #11
0
 class SimpleIO:
     input = '-aaa', AsIs('-bbb', default=bbb), Bool('-ccc', default=ccc), CSV('-ddd', default=ddd), \
         Date('-eee', default=eee), DateTime('-fff', default=fff), Decimal('-ggg', default=ggg), \
         Dict('-hhh', 'a', 'b', 'c', default=hhh), DictList('-iii', 'd', 'e', 'f', default=iii), \
         Float('-jjj', default=jjj), Int('-mmm', default=mmm), List('-nnn', default=nnn), \
         Opaque('-ooo', default=ooo), Text('-ppp', default=ppp), UUID('-qqq', default=qqq)