コード例 #1
0
ファイル: test_mongodb.py プロジェクト: tesserae/tesserae-v5
def test_update(request, populate):
    conf = request.config
    conn = TessMongoConnection(conf.getoption('db_host'),
                               conf.getoption('db_port'),
                               conf.getoption('db_user'),
                               password=conf.getoption('db_passwd',
                                                       default=None),
                               db=conf.getoption('db_name', default=None))

    for key, val in populate.items():
        entity = entity_map[key]

        # test updating one entity at a time
        guinea_pig = entity(**val[0])
        guinea_pig.eats = 'pellet'
        result = conn.update(guinea_pig)
        assert result.matched_count == 1
        assert result.modified_count == 1
        assert conn.connection[key].count_documents({'_id':
                                                     guinea_pig.id}) == 1
        found = conn.connection[key].find({'_id': guinea_pig.id})
        assert found[0]['eats'] == 'pellet'

        # test updating multiple entities at a time
        ents = [entity(**entry) for entry in val]
        changes = {e.id: (gen_chars(), gen_chars()) for e in ents}
        for e in ents:
            k, v = changes[e.id]
            setattr(e, k, v)
        result = conn.update(ents)
        assert result.matched_count == len(val)
        assert result.modified_count == len(val)
        found = conn.connection[key].find()
        assert all([
            changes[doc['_id']][0] in doc
            and doc[changes[doc['_id']][0]] == changes[doc['_id']][1]
            for doc in found
        ])
コード例 #2
0
ファイル: test_mongodb.py プロジェクト: jdbelm/tesserae-v5
 def test_update(self, request, populate, entity_map):
     conf = request.config
     conn = TessMongoConnection(conf.getoption('db_host'),
                                conf.getoption('db_port'),
                                conf.getoption('db_user'),
                                password=conf.getoption('db_passwd',
                                                        default=None),
                                db=conf.getoption('db_name', default=None))
     for entity_type in populate:
         if len(populate[entity_type]) == 0:
             continue
         end = \
             len(populate[entity_type]) if len(populate[entity_type]) < 100 \
             else 100
         for query_ent in populate[entity_type][:end]:
             entity = entity_map[entity_type].json_decode(query_ent)
             entity.foo = 'bar'
             res = conn.update(entity)
             assert res.matched_count == 1
             assert res.modified_count == 1
             assert res.raw_result['foo'] == entity.foo