Esempio n. 1
0
 def test_process_bools(self):
     test_dict = dictset(
         complete__bool='false',
         other_arg=5,
     )
     result_dict = docs.process_bools(test_dict)
     assert result_dict == dictset(complete=False, other_arg=5)
Esempio n. 2
0
 def test_process_bools(self):
     test_dict = dictset(
         complete__bool='false',
         other_arg=5,
     )
     result_dict = docs.process_bools(test_dict)
     assert result_dict == dictset(complete=False, other_arg=5)
Esempio n. 3
0
 def test_filter_fields(self, mock_fields):
     mock_fields.return_value = ('description', 'id', 'complete')
     params = docs.BaseMixin.filter_fields(dictset(
         description='nice',
         name='regular name',
         id__in__here=[1, 2, 3],
     ))
     assert params == dictset(
         description='nice',
         id__in__here=[1, 2, 3],
     )
Esempio n. 4
0
 def test_filter_fields(self):
     class MyModel(docs.BaseDocument):
         name = fields.StringField()
     params = MyModel.filter_fields(dictset(
         description='nice',
         name='regular name',
         id__in__here=[1, 2, 3],
     ))
     assert params == dictset(
         name='regular name',
         id__in__here=[1, 2, 3],
     )
Esempio n. 5
0
 def test_filter_fields(self):
     class MyModel(docs.BaseDocument):
         name = fields.StringField()
     params = MyModel.filter_fields(dictset(
         description='nice',
         name='regular name',
         id__in__here=[1, 2, 3],
     ))
     assert params == dictset(
         name='regular name',
         id__in__here=[1, 2, 3],
     )
Esempio n. 6
0
 def test_filter_fields(self, mock_fields):
     mock_fields.return_value = ('description', 'id', 'complete')
     params = docs.BaseMixin.filter_fields(
         dictset(
             description='nice',
             name='regular name',
             id__in__here=[1, 2, 3],
         ))
     assert params == dictset(
         description='nice',
         id__in__here=[1, 2, 3],
     )
Esempio n. 7
0
 def test_process_lists(self):
     test_dict = dictset(
         id__in='1,   2, 3',
         name__all='foo',
         other__arg='4',
         yet_other_arg=5,
     )
     result_dict = docs.process_lists(test_dict)
     expected = dictset(
         id__in=['1', '2', '3'],
         name__all=['foo'],
         other__arg='4',
         yet_other_arg=5,
     )
     assert result_dict == expected
Esempio n. 8
0
 def test_process_lists(self):
     test_dict = dictset(
         id__in='1,   2, 3',
         name__all='foo',
         other__arg='4',
         yet_other_arg=5,
     )
     result_dict = docs.process_lists(test_dict)
     expected = dictset(
         id__in=['1', '2', '3'],
         name__all=['foo'],
         other__arg='4',
         yet_other_arg=5,
     )
     assert result_dict == expected
Esempio n. 9
0
 def test_asdict(self):
     dset = dictset({'foo': "a:2,b:blabla,c:True,a:'d',a:1"})
     assert dset.asdict('foo') == {
         'a': ['2', "'d'", '1'],
         'b': 'blabla',
         'c': 'True'
     }
Esempio n. 10
0
 def test_process_bool_param(self):
     dset = dictset({'boo': 'true', 'foo': 'false'})
     assert dset.process_bool_param('boo')
     assert not dset.process_bool_param('foo')
     assert dset.boo
     assert not dset.foo
     assert isinstance(dset.boo, bool)
     assert isinstance(dset.foo, bool)
Esempio n. 11
0
 def test_process_bool_param(self):
     dset = dictset({'boo': 'true', 'foo': 'false'})
     assert dset.process_bool_param('boo')
     assert not dset.process_bool_param('foo')
     assert dset.boo
     assert not dset.foo
     assert isinstance(dset.boo, bool)
     assert isinstance(dset.foo, bool)
Esempio n. 12
0
 def test_pop_by_values(self):
     dset = dictset({'boo': '1', 'foo': '2'})
     dset.pop_by_values('3')
     assert dict(dset) == {'boo': '1', 'foo': '2'}
     dset.pop_by_values('1')
     assert dict(dset) == {'foo': '2'}
     dset.pop_by_values('2')
     assert dict(dset) == {}
Esempio n. 13
0
 def test_pop_by_values(self):
     dset = dictset({'boo': '1', 'foo': '2'})
     dset.pop_by_values('3')
     assert dict(dset) == {'boo': '1', 'foo': '2'}
     dset.pop_by_values('1')
     assert dict(dset) == {'foo': '2'}
     dset.pop_by_values('2')
     assert dict(dset) == {}
Esempio n. 14
0
 def test_asbool(self):
     dset1 = dictset({
         'foo': 'true', 'fruit': 'false',
         'baz': True, 'zoo': False})
     assert dset1.asbool('foo')
     assert dset1.asbool('baz')
     assert isinstance(dset1.asbool('foo'), bool)
     assert isinstance(dset1.asbool('baz'), bool)
     assert not dset1.asbool('fruit')
     assert not dset1.asbool('zoo')
Esempio n. 15
0
    def to_dict(self, **kwargs):
        _dict = dictset()
        _keys = kwargs.pop('_keys', [])
        _depth = kwargs.pop('_depth', 1)

        data = dictset(self._data).subset(_keys) if _keys else self._data

        for attr, val in data.items():
            _dict[attr] = val
            if _depth:
                kw = kwargs.copy()
                kw['_depth'] = _depth - 1

                if hasattr(val, 'to_dict'):
                    _dict[attr] = val.to_dict(**kw)
                elif isinstance(val, list):
                    _dict[attr] = to_dicts(val, **kw)

        _dict['_type'] = self.__class__.__name__
        return _dict
Esempio n. 16
0
 def test_reserved_params_dropped(self, mock_check, mock_drop, simple_model,
                                  memory_db):
     from nefertari.utils import dictset
     memory_db()
     mock_drop.side_effect = lambda x: dictset({'name': 'a'})
     try:
         simple_model.get_collection(_limit=1, _strict=True)
     except JHTTPBadRequest:
         raise Exception('Unexpected JHTTPBadRequest exception')
     mock_drop.assert_called_once_with({})
     mock_check.assert_called_once_with(['name'])
Esempio n. 17
0
 def test_reserved_params_dropped(
         self, mock_check, mock_drop, simple_model, memory_db):
     from nefertari.utils import dictset
     memory_db()
     mock_drop.side_effect = lambda x: dictset({'name': 'a'})
     try:
         simple_model.get_collection(_limit=1, __strict=True)
     except JHTTPBadRequest:
         raise Exception('Unexpected JHTTPBadRequest exception')
     mock_drop.assert_called_once_with({})
     mock_check.assert_called_once_with(['name'])
Esempio n. 18
0
 def test_process_datetime_param(self):
     dset = dictset({'boo': '2014-01-02T03:04:05Z'})
     dtime = dset.process_datetime_param('boo')
     assert dtime is dset.boo
     assert dtime == dset.boo
     assert isinstance(dtime, datetime)
     assert dtime.year == 2014
     assert dtime.month == 1
     assert dtime.day == 2
     assert dtime.hour == 3
     assert dtime.minute == 4
     assert dtime.second == 5
Esempio n. 19
0
 def test_process_datetime_param(self):
     dset = dictset({'boo': '2014-01-02T03:04:05Z'})
     dtime = dset.process_datetime_param('boo')
     assert dtime is dset.boo
     assert dtime == dset.boo
     assert isinstance(dtime, datetime)
     assert dtime.year == 2014
     assert dtime.month == 1
     assert dtime.day == 2
     assert dtime.hour == 3
     assert dtime.minute == 4
     assert dtime.second == 5
Esempio n. 20
0
 def test_asbool(self):
     dset1 = dictset({
         'foo': 'true',
         'fruit': 'false',
         'baz': True,
         'zoo': False
     })
     assert dset1.asbool('foo')
     assert dset1.asbool('baz')
     assert isinstance(dset1.asbool('foo'), bool)
     assert isinstance(dset1.asbool('baz'), bool)
     assert not dset1.asbool('fruit')
     assert not dset1.asbool('zoo')
Esempio n. 21
0
def obj2dict(obj, classkey=None):
    if isinstance(obj, dict):
        for k in obj.keys():
            obj[k] = obj2dict(obj[k], classkey)
        return obj
    elif hasattr(obj, "__iter__"):
        return [obj2dict(v, classkey) for v in obj]
    elif hasattr(obj, "__dict__"):
        data = dictset([(key, obj2dict(value, classkey))
                        for key, value in obj.__dict__.iteritems()
                        if not callable(value) and not key.startswith('_')])
        if classkey is not None and hasattr(obj, "__class__"):
            data[classkey] = obj.__class__.__name__
        return data
    else:
        return obj
Esempio n. 22
0
def obj2dict(obj, classkey=None):
    if isinstance(obj, dict):
        for k in obj.keys():
            obj[k] = obj2dict(obj[k], classkey)
        return obj
    elif issequence(obj):
        return [obj2dict(v, classkey) for v in obj]
    elif hasattr(obj, "__dict__"):
        data = dictset([
            (key, obj2dict(value, classkey))
            for key, value in obj.__dict__.items()
            if not six.callable(value) and not key.startswith('_')
        ])
        if classkey is not None and hasattr(obj, "__class__"):
            data[classkey] = obj.__class__.__name__
        return data
    else:
        return obj
Esempio n. 23
0
 def test_process_dict_param_pop(self):
     dset = dictset({'boo': 'a:1'})
     assert dset.process_dict_param('boo', pop=True) == {'a': '1'}
     assert 'boo' not in dset
Esempio n. 24
0
 def test_getattr(self):
     dset1 = dictset({'foo': 'bar', 'fruit': 'apple'})
     assert dset1.foo == 'bar'
     assert dset1.fruit == 'apple'
Esempio n. 25
0
 def test_asint_set(self):
     dset = dictset({'foo': '1'})
     assert dset.asint('foo', _set=True) == 1
     assert dset.foo == 1
Esempio n. 26
0
 def test_aslist_default(self):
     dset1 = dictset({'foo': '1,2,3'})
     assert dset1.aslist('foo1', default=['1']) == ['1']
     assert 'foo1' not in dset1
Esempio n. 27
0
 def test_aslist_set(self):
     dset1 = dictset({'foo': '1,2,3'})
     assert dset1.aslist('foo', _set=True) == ['1', '2', '3']
     assert dset1.foo == ['1', '2', '3']
Esempio n. 28
0
 def test_asbool_default(self):
     dset1 = dictset({'foo': 'true'})
     assert dset1.asbool('foo1', default=True)
     assert 'foo1' not in dset1
Esempio n. 29
0
 def __init__(self, data={}):
     self._data = dictset(data)
Esempio n. 30
0
 def test_asint_default(self):
     dset = dictset({'foo': '1'})
     assert dset.asint('foo1', default=2) == 2
     assert 'foo1' not in dset
Esempio n. 31
0
 def test_asbool_set(self):
     dset1 = dictset({'foo': 'true'})
     assert dset1.asbool('foo', _set=True)
     assert dset1.foo
     assert dset1.foo != 'true'
Esempio n. 32
0
 def test_asbool_pop(self):
     dset1 = dictset({'foo': 'true'})
     assert dset1.asbool('foo', pop=True)
     assert 'foo' not in dset1
Esempio n. 33
0
 def test_asbool_default(self):
     dset1 = dictset({'foo': 'true'})
     assert dset1.asbool('foo1', default=True)
     assert 'foo1' not in dset1
Esempio n. 34
0
 def test_aslist(self):
     dset1 = dictset({'foo': '1,2, 3'})
     assert dset1.aslist('foo') == ['1', '2', '3']
Esempio n. 35
0
 def test_aslist_set(self):
     dset1 = dictset({'foo': '1,2,3'})
     assert dset1.aslist('foo', _set=True) == ['1', '2', '3']
     assert dset1.foo == ['1', '2', '3']
Esempio n. 36
0
 def test_copy(self):
     dset1 = dictset({'foo': 'bar'})
     dset2 = dset1.copy()
     assert dset1 is not dset2
     assert dset1 == dset2
Esempio n. 37
0
 def test_process_dict_param(self):
     dset = dictset({'boo': 'a:1'})
     assert dset.process_dict_param('boo') == {'a': '1'}
     assert dset.boo == {'a': '1'}
Esempio n. 38
0
 def test_process_int_param_default(self):
     dset = dictset({'boo': '1'})
     assert dset.process_int_param('foo', default=2) == 2
     assert dset.foo == 2
Esempio n. 39
0
 def test_process_dict_param(self):
     dset = dictset({'boo': 'a:1'})
     assert dset.process_dict_param('boo') == {'a': '1'}
     assert dset.boo == {'a': '1'}
Esempio n. 40
0
 def test_aslist_default(self):
     dset1 = dictset({'foo': '1,2,3'})
     assert dset1.aslist('foo1', default=['1']) == ['1']
     assert 'foo1' not in dset1
Esempio n. 41
0
 def test_process_dict_param_type(self):
     dset = dictset({'boo': 'a:1'})
     assert dset.process_dict_param('boo', _type=lambda x: int(x)) == {
         'a': 1
     }
     assert dset.boo == {'a': 1}
Esempio n. 42
0
 def test_aslist(self):
     dset1 = dictset({'foo': '1,2, 3'})
     assert dset1.aslist('foo') == ['1', '2', '3']
Esempio n. 43
0
 def test_process_dict_param_pop(self):
     dset = dictset({'boo': 'a:1'})
     assert dset.process_dict_param('boo', pop=True) == {'a': '1'}
     assert 'boo' not in dset
Esempio n. 44
0
 def test_copy(self):
     dset1 = dictset({'foo': 'bar'})
     dset2 = dset1.copy()
     assert dset1 is not dset2
     assert dset1 == dset2
Esempio n. 45
0
 def test_setattr(self):
     dset1 = dictset()
     dset1.boo = 1
     assert dict(dset1) == {'boo': 1}
Esempio n. 46
0
 def test_asint(self):
     assert dictset({'foo': '1'}).asint('foo') == 1
Esempio n. 47
0
 def test_remove(self):
     dset1 = dictset({'foo': 'bar', 'fruit': 'apple'})
     dset2 = dset1.remove(['fruit'])
     assert dict(dset2) == {'foo': 'bar'}
Esempio n. 48
0
 def test_asint_default(self):
     dset = dictset({'foo': '1'})
     assert dset.asint('foo1', default=2) == 2
     assert 'foo1' not in dset
Esempio n. 49
0
 def test_getattr(self):
     dset1 = dictset({'foo': 'bar', 'fruit': 'apple'})
     assert dset1.foo == 'bar'
     assert dset1.fruit == 'apple'
Esempio n. 50
0
 def test_remove(self):
     dset1 = dictset({'foo': 'bar', 'fruit': 'apple'})
     dset2 = dset1.remove(['fruit'])
     assert dict(dset2) == {'foo': 'bar'}
Esempio n. 51
0
 def test_setattr(self):
     dset1 = dictset()
     dset1.boo = 1
     assert dict(dset1) == {'boo': 1}
Esempio n. 52
0
 def test_process_dict_param_type(self):
     dset = dictset({'boo': 'a:1'})
     assert dset.process_dict_param('boo', _type=lambda x: int(x)) == {
         'a': 1}
     assert dset.boo == {'a': 1}
Esempio n. 53
0
 def test_asint_set(self):
     dset = dictset({'foo': '1'})
     assert dset.asint('foo', _set=True) == 1
     assert dset.foo == 1
Esempio n. 54
0
 def __init__(self, data={}):
     self._data = dictset(data)
Esempio n. 55
0
 def test_asbool_set(self):
     dset1 = dictset({'foo': 'true'})
     assert dset1.asbool('foo', _set=True)
     assert dset1.foo
     assert dset1.foo != 'true'
Esempio n. 56
0
 def test_process_int_param_default(self):
     dset = dictset({'boo': '1'})
     assert dset.process_int_param('foo', default=2) == 2
     assert dset.foo == 2
Esempio n. 57
0
 def test_asbool_pop(self):
     dset1 = dictset({'foo': 'true'})
     assert dset1.asbool('foo', pop=True)
     assert 'foo' not in dset1
Esempio n. 58
0
 def test_asint(self):
     assert dictset({'foo': '1'}).asint('foo') == 1
Esempio n. 59
0
 def test_asfloat(self):
     assert dictset({'foo': '1.0'}).asfloat('foo') == 1.0