Пример #1
0
def test_force_bson_encodable_substitutes_illegal_value_with_strings():
    d = {
        'a_module': datetime,
        'some_legal_stuff': {'foo': 'bar', 'baz': [1, 23, 4]},
        'nested': {
            'dict': {
                'with': {
                    'illegal_module': mock
                }
            }
        },
        '$illegal': 'because it starts with a $',
        'il.legal': 'because it contains a .',
        12.7: 'illegal because it is not a string key'
    }
    expected = {
        'a_module': str(datetime),
        'some_legal_stuff': {'foo': 'bar', 'baz': [1, 23, 4]},
        'nested': {
            'dict': {
                'with': {
                    'illegal_module': str(mock)
                }
            }
        },
        '@illegal': 'because it starts with a $',
        'il,legal': 'because it contains a .',
        '12,7': 'illegal because it is not a string key'
    }
    assert force_bson_encodeable(d) == expected
Пример #2
0
def test_force_bson_encodable_doesnt_change_valid_document():
    d = {
        'int': 1,
        'string': 'foo',
        'float': 23.87,
        'list': ['a', 1, True],
        'bool': True,
        'cr4zy: _but_ [legal) Key!': '$illegal.key.as.value',
        'datetime': datetime.datetime.utcnow(),
        'tuple': (1, 2.0, 'three'),
        'none': None
    }
    assert force_bson_encodeable(d) == d
Пример #3
0
def test_force_bson_encodable_doesnt_change_valid_document():
    d = {
        "int": 1,
        "string": "foo",
        "float": 23.87,
        "list": ["a", 1, True],
        "bool": True,
        "cr4zy: _but_ [legal) Key!": "$illegal.key.as.value",
        "datetime": datetime.datetime.utcnow(),
        "tuple": (1, 2.0, "three"),
        "none": None,
    }
    assert force_bson_encodeable(d) == d
Пример #4
0
def test_force_bson_encodable_doesnt_change_valid_document():
    d = {
        "int": 1,
        "string": "foo",
        "float": 23.87,
        "list": ["a", 1, True],
        "bool": True,
        "cr4zy: _but_ [legal) Key!": "$illegal.key.as.value",
        "datetime": datetime.datetime.now(),
        "tuple": (1, 2.0, "three"),
        "none": None,
    }
    assert force_bson_encodeable(d) == d
Пример #5
0
def test_force_bson_encodable_substitutes_illegal_value_with_strings():
    d = {'a_module': datetime,
         'some_legal_stuff': {'foo': 'bar', 'baz': [1, 23, 4]},
         'nested': {'dict': {'with': {'illegal_module': mock}}},
         '$illegal': 'because it starts with a $',
         'il.legal': 'because it contains a .',
         12.7: 'illegal because it is not a string key'}
    expected = {'a_module': str(datetime),
                'some_legal_stuff': {'foo': 'bar', 'baz': [1, 23, 4]},
                'nested': {'dict': {'with': {'illegal_module': str(mock)}}},
                '@illegal': 'because it starts with a $',
                'il,legal': 'because it contains a .',
                '12,7': 'illegal because it is not a string key'}
    assert force_bson_encodeable(d) == expected
Пример #6
0
def test_pickle_numpy_arrays_son_manipulator():
    np = pytest.importorskip("numpy")
    sonm = PickleNumpyArrays()
    document = {
        'foo': 'bar',
        'some_array': np.eye(3),
        'nested': {
            'ones': np.ones(7)
        }
    }

    mod_doc = sonm.transform_incoming(deepcopy(document), 'fake_collection')
    mod_doc = force_bson_encodeable(mod_doc)
    redoc = sonm.transform_outgoing(mod_doc, 'fake_collection')
    assert redoc['foo'] == document['foo']
    assert np.all(redoc['some_array'] == document['some_array'])
    assert np.all(redoc['nested']['ones'] == document['nested']['ones'])
Пример #7
0
def test_pickle_numpy_arrays_son_manipulator():
    np = pytest.importorskip("numpy")
    sonm = PickleNumpyArrays()
    document = {
        'foo': 'bar',
        'some_array': np.eye(3),
        'nested': {
            'ones': np.ones(7)
        }
    }

    mod_doc = sonm.transform_incoming(deepcopy(document), 'fake_collection')
    mod_doc = force_bson_encodeable(mod_doc)
    redoc = sonm.transform_outgoing(mod_doc, 'fake_collection')
    assert redoc['foo'] == document['foo']
    assert np.all(redoc['some_array'] == document['some_array'])
    assert np.all(redoc['nested']['ones'] == document['nested']['ones'])
Пример #8
0
def test_force_bson_encodable_substitutes_illegal_value_with_strings():
    d = {
        "a_module": datetime,
        "some_legal_stuff": {"foo": "bar", "baz": [1, 23, 4]},
        "nested": {"dict": {"with": {"illegal_module": mock}}},
        "$illegal": "because it starts with a $",
        "il.legal": "because it contains a .",
        12.7: "illegal because it is not a string key",
    }
    expected = {
        "a_module": str(datetime),
        "some_legal_stuff": {"foo": "bar", "baz": [1, 23, 4]},
        "nested": {"dict": {"with": {"illegal_module": str(mock)}}},
        "@illegal": "because it starts with a $",
        "il,legal": "because it contains a .",
        "12,7": "illegal because it is not a string key",
    }
    assert force_bson_encodeable(d) == expected
Пример #9
0
def test_force_bson_encodable_substitutes_illegal_value_with_strings():
    d = {
        "a_module": datetime,
        "some_legal_stuff": {"foo": "bar", "baz": [1, 23, 4]},
        "nested": {"dict": {"with": {"illegal_module": mock}}},
        "$illegal": "because it starts with a $",
        "il.legal": "because it contains a .",
        12.7: "illegal because it is not a string key",
    }
    expected = {
        "a_module": str(datetime),
        "some_legal_stuff": {"foo": "bar", "baz": [1, 23, 4]},
        "nested": {"dict": {"with": {"illegal_module": str(mock)}}},
        "@illegal": "because it starts with a $",
        "il,legal": "because it contains a .",
        "12,7": "illegal because it is not a string key",
    }
    assert force_bson_encodeable(d) == expected
Пример #10
0
def test_force_bson_encodable_doesnt_change_valid_document():
    d = {'int': 1, 'string': 'foo', 'float': 23.87, 'list': ['a', 1, True],
         'bool': True, 'cr4zy: _but_ [legal) Key!': '$illegal.key.as.value',
         'datetime': datetime.datetime.now(), 'tuple': (1, 2.0, 'three'),
         'none': None}
    assert force_bson_encodeable(d) == d