Ejemplo n.º 1
0
def test_save1(readings3):
    the_collection, objects = readings3

    # invalid document
    new_document = dict(foo=2, bar='hello')

    repo = MongoQueryMixin()
    with raises(ValueError):
        repo.save(the_collection, new_document)

    # good document
    RID = 1234567890L
    new_document2 = dict(reading_id=RID, raw='PM2.5 90 AQI',
                         source=dict(type=u'twitter',
                                     screen_name=u'Guangzhou_Air'))
    status0, objid0 = repo.save(the_collection, new_document2)
    assert status0 == 1
    assert objid0 is not None

    zero = 0

    # DESCENDING
    acursor = repo.find(the_collection, limit=4)
    o = [item for item in acursor]
    assert len(o) == 3
    assert o[zero]['reading_id'] == RID

    # ASCENDING
    from pymongo import ASCENDING
    acursor = repo.find(the_collection, limit=4, sort_order=ASCENDING)
    o = [item for item in acursor]
    assert len(o) == 3
    assert o[zero]['reading_id'] == 999L

    # unique keys test?
    status, objid = repo.save(the_collection, new_document2)
    assert status == 0
    assert objid == objid0