test_export.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:pymongo-schema 作者: pajachiet 项目源码 文件源码
def test04_write_json_with_mongo_objects():
    schema = {
        'db': {'coll': {'object': {'_id': {'type': 'oid', 'anonymized': ObjectId()},
                                   'date': {'type': 'date', 'anonymized': datetime(2015, 1, 1)}}}}
    }

    output = os.path.join(TEST_DIR, 'output_data_dict.json')
    output_maker = JsonOutput({})
    output_maker.data = schema
    with open(output, 'w') as out_fd:
        output_maker.write_data(out_fd)
    with open(output, 'r') as out_fd:
        # custom json_options - DEFAULT_JSON_OPTIONS sets tzinfo when loading datetime (!= schema)
        json_options = json_util.JSONOptions(tz_aware=False)
        # object_hook allows to interpret $oid, $date, etc from json and convert them to objects
        object_hook = partial(json_util.object_hook, json_options=json_options)
        assert json.load(out_fd, object_hook=object_hook) == schema
    os.remove(output)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号