<s> from __future__ import absolute_import , division <EOL> from bson import BSON , ObjectId <EOL> from bson . son import SON <EOL> from pymongo . errors import OperationFailure , WriteError , BulkWriteError , DuplicateKeyError <EOL> from pymongo . results import InsertOneResult , InsertManyResult , UpdateResult , DeleteResult <EOL> from pymongo . collection import ReturnDocument <EOL> from pymongo . write_concern import WriteConcern <EOL> from twisted . internet import defer <EOL> from twisted . trial import unittest <EOL> import txmongo <EOL> from txmongo . protocol import MongoClientProtocol <EOL> import txmongo . filter as qf <EOL> mongo_host = "<STR_LIT:localhost>" <EOL> mongo_port = <NUM_LIT> <EOL> class _CallCounter ( object ) : <EOL> def __init__ ( self , original ) : <EOL> self . call_count = <NUM_LIT:0> <EOL> self . original = original <EOL> def __call__ ( self , this , * args , ** kwargs ) : <EOL> self . call_count += <NUM_LIT:1> <EOL> return self . original ( this , * args , ** kwargs ) <EOL> class _SingleCollectionTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . conn = txmongo . MongoConnection ( mongo_host , mongo_port ) <EOL> self . db = self . conn . mydb <EOL> self . coll = self . db . mycol <EOL> @ defer . inlineCallbacks <EOL> def tearDown ( self ) : <EOL> yield self . coll . drop ( ) <EOL> yield self . conn . disconnect ( ) <EOL> class TestMongoQueries ( _SingleCollectionTest ) : <EOL> timeout = <NUM_LIT:15> <EOL> @ defer . inlineCallbacks <EOL> def test_SingleCursorIteration ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:10> ) ] , safe = True ) <EOL> res = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:10> ) <EOL> @ defer . inlineCallbacks <EOL> def test_MultipleCursorIterations ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT> ) ] , safe = True ) <EOL> res = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT> ) <EOL> @ defer . inlineCallbacks <EOL> def test_FindWithCursor ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT> ) ] , safe = True ) <EOL> docs , d = yield self . coll . find_with_cursor ( ) <EOL> self . assertEqual ( len ( docs ) , <NUM_LIT> ) <EOL> total = <NUM_LIT:0> <EOL> while docs : <EOL> total += len ( docs ) <EOL> docs , d = yield d <EOL> self . assertEqual ( total , <NUM_LIT> ) <EOL> docs , d = yield self . coll . find ( cursor = True ) <EOL> self . assertEqual ( len ( docs ) , <NUM_LIT> ) <EOL> total = <NUM_LIT:0> <EOL> while docs : <EOL> total += len ( docs ) <EOL> docs , d = yield d <EOL> self . assertEqual ( total , <NUM_LIT> ) <EOL> @ defer . inlineCallbacks <EOL> def test_FindWithCursorLimit ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT> ) ] , safe = True ) <EOL> docs , d = yield self . coll . find_with_cursor ( limit = <NUM_LIT> ) <EOL> total = <NUM_LIT:0> <EOL> while docs : <EOL> total += len ( docs ) <EOL> docs , d = yield d <EOL> self . assertEqual ( total , <NUM_LIT> ) <EOL> docs , d = yield self . coll . find ( limit = <NUM_LIT> , cursor = True ) <EOL> total = <NUM_LIT:0> <EOL> while docs : <EOL> total += len ( docs ) <EOL> docs , d = yield d <EOL> self . assertEqual ( total , <NUM_LIT> ) <EOL> @ defer . inlineCallbacks <EOL> def test_LargeData ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : '<STR_LIT:U+0020>' * ( <NUM_LIT:2> ** <NUM_LIT> ) } for _ in range ( <NUM_LIT:4> ) ] , safe = True ) <EOL> res = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:4> ) <EOL> @ defer . inlineCallbacks <EOL> def test_SpecifiedFields ( self ) : <EOL> yield self . coll . insert ( [ dict ( ( k , v ) for k in "<STR_LIT>" ) for v in range ( <NUM_LIT:5> ) ] , safe = True ) <EOL> res = yield self . coll . find ( fields = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:c>' : <NUM_LIT:1> } ) <EOL> yield self . coll . count ( fields = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:c>' : <NUM_LIT:1> } ) <EOL> self . assertTrue ( all ( x in [ '<STR_LIT:a>' , '<STR_LIT:c>' , "<STR_LIT>" ] for x in res [ <NUM_LIT:0> ] . keys ( ) ) ) <EOL> res = yield self . coll . find ( fields = [ '<STR_LIT:a>' , '<STR_LIT:c>' ] ) <EOL> yield self . coll . count ( fields = [ '<STR_LIT:a>' , '<STR_LIT:c>' ] ) <EOL> self . assertTrue ( all ( x in [ '<STR_LIT:a>' , '<STR_LIT:c>' , "<STR_LIT>" ] for x in res [ <NUM_LIT:0> ] . keys ( ) ) ) <EOL> res = yield self . coll . find ( fields = [ ] ) <EOL> yield self . coll . count ( fields = [ ] ) <EOL> self . assertTrue ( all ( x in [ "<STR_LIT>" ] for x in res [ <NUM_LIT:0> ] . keys ( ) ) ) <EOL> yield self . assertFailure ( self . coll . find ( { } , fields = [ <NUM_LIT:1> ] ) , TypeError ) <EOL> @ defer . inlineCallbacks <EOL> def test_group ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i % <NUM_LIT:2> } for i in range ( <NUM_LIT:5> ) ] , safe = True ) <EOL> reduce_ = """<STR_LIT>""" <EOL> keys = { '<STR_LIT:v>' : <NUM_LIT:1> } <EOL> initial = { "<STR_LIT>" : <NUM_LIT:0> } <EOL> cond = { '<STR_LIT:v>' : { "<STR_LIT>" : [ <NUM_LIT:0> , <NUM_LIT:1> ] } } <EOL> final = """<STR_LIT>""" <EOL> res = yield self . coll . group ( keys , initial , reduce_ , cond , final ) <EOL> self . assertEqual ( len ( res [ "<STR_LIT>" ] ) , <NUM_LIT:2> ) <EOL> keys = """<STR_LIT>""" <EOL> res = yield self . coll . group ( keys , initial , reduce_ , cond , final ) <EOL> self . assertEqual ( len ( res [ "<STR_LIT>" ] ) , <NUM_LIT:1> ) <EOL> def __make_big_object ( self ) : <EOL> return { "<STR_LIT>" : ObjectId ( ) , '<STR_LIT:x>' : '<STR_LIT:a>' * <NUM_LIT:1000> } <EOL> @ defer . inlineCallbacks <EOL> def __check_no_open_cursors ( self ) : <EOL> status = yield self . db . command ( "<STR_LIT>" ) <EOL> if "<STR_LIT>" in status [ "<STR_LIT>" ] : <EOL> self . assertEqual ( status [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <NUM_LIT:0> ) <EOL> else : <EOL> self . assertEqual ( status [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <NUM_LIT:0> ) <EOL> @ defer . inlineCallbacks <EOL> def test_CursorClosing ( self ) : <EOL> obj_count_4mb = <NUM_LIT:4> * <NUM_LIT> ** <NUM_LIT:2> // len ( BSON . encode ( self . __make_big_object ( ) ) ) + <NUM_LIT:1> <EOL> first_batch = <NUM_LIT:5> <EOL> yield self . coll . insert ( <EOL> [ self . __make_big_object ( ) for _ in range ( first_batch + obj_count_4mb ) ] ) <EOL> result = yield self . coll . find ( limit = first_batch ) <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:5> ) <EOL> yield self . __check_no_open_cursors ( ) <EOL> @ defer . inlineCallbacks <EOL> def test_CursorClosingWithCursor ( self ) : <EOL> obj_count_4mb = <NUM_LIT:4> * <NUM_LIT> ** <NUM_LIT:2> // len ( BSON . encode ( self . __make_big_object ( ) ) ) + <NUM_LIT:1> <EOL> first_batch = <NUM_LIT:5> <EOL> yield self . coll . insert ( <EOL> [ self . __make_big_object ( ) for _ in range ( first_batch + obj_count_4mb ) ] ) <EOL> result = [ ] <EOL> docs , dfr = yield self . coll . find_with_cursor ( { } , limit = first_batch ) <EOL> while docs : <EOL> result . extend ( docs ) <EOL> docs , dfr = yield dfr <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:5> ) <EOL> yield self . __check_no_open_cursors ( ) <EOL> @ defer . inlineCallbacks <EOL> def test_GetMoreCount ( self ) : <EOL> counter = _CallCounter ( MongoClientProtocol . send_GETMORE ) <EOL> self . patch ( MongoClientProtocol , '<STR_LIT>' , counter ) <EOL> yield self . coll . insert ( [ { '<STR_LIT:x>' : <NUM_LIT> } for _ in range ( <NUM_LIT:20> ) ] ) <EOL> result = yield self . coll . find ( { } , limit = <NUM_LIT:10> ) <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:10> ) <EOL> self . assertEqual ( counter . call_count , <NUM_LIT:0> ) <EOL> @ defer . inlineCallbacks <EOL> def test_GetMoreCountWithCursor ( self ) : <EOL> counter = _CallCounter ( MongoClientProtocol . send_GETMORE ) <EOL> self . patch ( MongoClientProtocol , '<STR_LIT>' , counter ) <EOL> yield self . coll . insert ( [ { '<STR_LIT:x>' : <NUM_LIT> } for _ in range ( <NUM_LIT:20> ) ] ) <EOL> result = [ ] <EOL> docs , dfr = yield self . coll . find_with_cursor ( { } , limit = <NUM_LIT:5> ) <EOL> while docs : <EOL> result . extend ( docs ) <EOL> docs , dfr = yield dfr <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:5> ) <EOL> self . assertEqual ( counter . call_count , <NUM_LIT:0> ) <EOL> @ defer . inlineCallbacks <EOL> def test_AsClass ( self ) : <EOL> yield self . coll . insert ( { '<STR_LIT:x>' : <NUM_LIT> } ) <EOL> doc = yield self . coll . find_one ( { } ) <EOL> self . assertTrue ( type ( doc ) is dict ) <EOL> class CustomDict ( dict ) : <EOL> pass <EOL> doc = yield self . coll . find_one ( { } , as_class = CustomDict ) <EOL> self . assertTrue ( type ( doc ) is CustomDict ) <EOL> @ defer . inlineCallbacks <EOL> def test_FindOneNone ( self ) : <EOL> doc = yield self . coll . find_one ( ) <EOL> self . assertEqual ( doc , None ) <EOL> class TestMongoQueriesEdgeCases ( _SingleCollectionTest ) : <EOL> timeout = <NUM_LIT:15> <EOL> @ defer . inlineCallbacks <EOL> def test_BelowBatchThreshold ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:100> ) ] , safe = True ) <EOL> res = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:100> ) <EOL> @ defer . inlineCallbacks <EOL> def test_EqualToBatchThreshold ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT> ) ] , safe = True ) <EOL> res = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT> ) <EOL> @ defer . inlineCallbacks <EOL> def test_AboveBatchThreshold ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT> ) ] , safe = True ) <EOL> res = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT> ) <EOL> class TestLimit ( _SingleCollectionTest ) : <EOL> timeout = <NUM_LIT:15> <EOL> @ defer . inlineCallbacks <EOL> def test_LimitBelowBatchThreshold ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:50> ) ] , safe = True ) <EOL> res = yield self . coll . find ( limit = <NUM_LIT:20> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:20> ) <EOL> @ defer . inlineCallbacks <EOL> def test_LimitAboveBatchThreshold ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:200> ) ] , safe = True ) <EOL> res = yield self . coll . find ( limit = <NUM_LIT> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT> ) <EOL> @ defer . inlineCallbacks <EOL> def test_LimitAtBatchThresholdEdge ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:200> ) ] , safe = True ) <EOL> res = yield self . coll . find ( limit = <NUM_LIT:100> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:100> ) <EOL> yield self . coll . drop ( ) <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:200> ) ] , safe = True ) <EOL> res = yield self . coll . find ( limit = <NUM_LIT> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT> ) <EOL> yield self . coll . drop ( ) <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:200> ) ] , safe = True ) <EOL> res = yield self . coll . find ( limit = <NUM_LIT> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT> ) <EOL> @ defer . inlineCallbacks <EOL> def test_LimitAboveMessageSizeThreshold ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : '<STR_LIT:U+0020>' * ( <NUM_LIT:2> ** <NUM_LIT:20> ) } for _ in range ( <NUM_LIT:8> ) ] , safe = True ) <EOL> res = yield self . coll . find ( limit = <NUM_LIT:5> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:5> ) <EOL> @ defer . inlineCallbacks <EOL> def test_HardLimit ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:200> ) ] , safe = True ) <EOL> res = yield self . coll . find ( limit = - <NUM_LIT> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT> ) <EOL> @ defer . inlineCallbacks <EOL> def test_HardLimitAboveMessageSizeThreshold ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : '<STR_LIT:U+0020>' * ( <NUM_LIT:2> ** <NUM_LIT:20> ) } for _ in range ( <NUM_LIT:8> ) ] , safe = True ) <EOL> res = yield self . coll . find ( limit = - <NUM_LIT:6> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:4> ) <EOL> class TestSkip ( _SingleCollectionTest ) : <EOL> timeout = <NUM_LIT:15> <EOL> @ defer . inlineCallbacks <EOL> def test_Skip ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:5> ) ] , safe = True ) <EOL> res = yield self . coll . find ( skip = <NUM_LIT:3> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:2> ) <EOL> yield self . coll . drop ( ) <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:5> ) ] , safe = True ) <EOL> res = yield self . coll . find ( skip = <NUM_LIT:5> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:0> ) <EOL> yield self . coll . drop ( ) <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:5> ) ] , safe = True ) <EOL> res = yield self . coll . find ( skip = <NUM_LIT:6> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:0> ) <EOL> @ defer . inlineCallbacks <EOL> def test_SkipWithLimit ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:5> ) ] , safe = True ) <EOL> res = yield self . coll . find ( skip = <NUM_LIT:3> , limit = <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:1> ) <EOL> yield self . coll . drop ( ) <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:5> ) ] , safe = True ) <EOL> res = yield self . coll . find ( skip = <NUM_LIT:4> , limit = <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:1> ) <EOL> yield self . coll . drop ( ) <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:5> ) ] , safe = True ) <EOL> res = yield self . coll . find ( skip = <NUM_LIT:4> , limit = <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:1> ) <EOL> yield self . coll . drop ( ) <EOL> yield self . coll . insert ( [ { '<STR_LIT:v>' : i } for i in range ( <NUM_LIT:5> ) ] , safe = True ) <EOL> res = yield self . coll . find ( skip = <NUM_LIT:5> , limit = <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:0> ) <EOL> class TestCommand ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def test_SimpleCommand ( self ) : <EOL> pong = yield self . db . command ( "<STR_LIT>" ) <EOL> self . assertEqual ( pong [ "<STR_LIT>" ] , <NUM_LIT:1> ) <EOL> @ defer . inlineCallbacks <EOL> def test_ComplexCommand ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:y>' : <NUM_LIT> } ] , safe = True ) <EOL> result = yield self . db . command ( "<STR_LIT:count>" , "<STR_LIT>" , query = { '<STR_LIT:x>' : <NUM_LIT> } ) <EOL> self . assertEqual ( result [ '<STR_LIT:n>' ] , <NUM_LIT:1> ) <EOL> result = yield self . db . command ( SON ( [ ( "<STR_LIT:count>" , "<STR_LIT>" ) , ( "<STR_LIT>" , { '<STR_LIT:y>' : <NUM_LIT> } ) ] ) ) <EOL> self . assertEqual ( result [ '<STR_LIT:n>' ] , <NUM_LIT:1> ) <EOL> @ defer . inlineCallbacks <EOL> def test_CheckResult ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:y>' : <NUM_LIT> } ] , safe = True ) <EOL> self . assertFailure ( self . db . command ( "<STR_LIT>" , "<STR_LIT>" ) , OperationFailure ) <EOL> result = yield self . db . command ( "<STR_LIT>" , "<STR_LIT>" , check = False ) <EOL> self . assertFalse ( result [ "<STR_LIT>" ] ) <EOL> result = yield self . db . command ( "<STR_LIT>" , "<STR_LIT>" , check = True , <EOL> allowable_errors = [ "<STR_LIT>" ] ) <EOL> self . assertFalse ( result [ "<STR_LIT>" ] ) <EOL> class TestUpdate ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def test_SimpleUpdate ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:x>' : <NUM_LIT> } ] ) <EOL> yield self . coll . update ( { } , { "<STR_LIT>" : { '<STR_LIT:x>' : <NUM_LIT> } } ) <EOL> docs = yield self . coll . find ( fields = { "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertTrue ( { '<STR_LIT:x>' : <NUM_LIT> } in docs ) <EOL> self . assertTrue ( ( { '<STR_LIT:x>' : <NUM_LIT> } in docs ) or ( { '<STR_LIT:x>' : <NUM_LIT> } in docs ) ) <EOL> @ defer . inlineCallbacks <EOL> def test_MultiUpdate ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:x>' : <NUM_LIT> } ] ) <EOL> yield self . coll . update ( { } , { "<STR_LIT>" : { '<STR_LIT:x>' : <NUM_LIT> } } , multi = True ) <EOL> docs = yield self . coll . find ( fields = { "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertEqual ( len ( docs ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( all ( doc == { '<STR_LIT:x>' : <NUM_LIT> } for doc in docs ) ) <EOL> @ defer . inlineCallbacks <EOL> def test_Upsert ( self ) : <EOL> yield self . coll . update ( { } , { "<STR_LIT>" : { '<STR_LIT:x>' : <NUM_LIT> } } , upsert = True ) <EOL> yield self . coll . update ( { } , { "<STR_LIT>" : { '<STR_LIT:x>' : <NUM_LIT> } } , upsert = True ) <EOL> docs = yield self . coll . find ( fields = { "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertEqual ( len ( docs ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( docs [ <NUM_LIT:0> ] , { '<STR_LIT:x>' : <NUM_LIT> } ) <EOL> class TestSave ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def test_Save ( self ) : <EOL> yield self . assertFailure ( self . coll . save ( <NUM_LIT> ) , TypeError ) <EOL> yield self . coll . save ( { '<STR_LIT:x>' : <NUM_LIT:1> } ) <EOL> oid = ObjectId ( ) <EOL> yield self . coll . save ( { "<STR_LIT>" : oid , '<STR_LIT:x>' : <NUM_LIT:2> } ) <EOL> yield self . coll . save ( { "<STR_LIT>" : oid , '<STR_LIT:x>' : <NUM_LIT:3> } ) <EOL> docs = yield self . coll . find ( ) <EOL> self . assertTrue ( any ( doc [ '<STR_LIT:x>' ] == <NUM_LIT:1> for doc in docs ) ) <EOL> self . assertTrue ( { "<STR_LIT>" : oid , '<STR_LIT:x>' : <NUM_LIT:3> } in docs ) <EOL> class TestRemove ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def test_RemoveOne ( self ) : <EOL> docs = [ { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:x>' : <NUM_LIT> } ] <EOL> yield self . coll . insert ( docs ) <EOL> yield self . coll . remove ( { } , single = True ) <EOL> remaining = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( remaining ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( remaining [ <NUM_LIT:0> ] in docs ) <EOL> @ defer . inlineCallbacks <EOL> def test_RemoveMulti ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:y>' : <NUM_LIT> } ] ) <EOL> yield self . coll . remove ( { '<STR_LIT:x>' : { "<STR_LIT>" : True } } ) <EOL> remaining = yield self . coll . find ( fields = { "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertEqual ( remaining , [ { '<STR_LIT:y>' : <NUM_LIT> } ] ) <EOL> @ defer . inlineCallbacks <EOL> def test_RemoveById ( self ) : <EOL> oid = ObjectId ( ) <EOL> yield self . coll . insert ( [ { "<STR_LIT>" : oid , '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:y>' : <NUM_LIT> } ] ) <EOL> yield self . coll . remove ( oid ) <EOL> remaining = yield self . coll . find ( fields = { "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertEqual ( remaining , [ { '<STR_LIT:y>' : <NUM_LIT> } ] ) <EOL> def test_RemoveInvalid ( self ) : <EOL> self . assertFailure ( self . coll . remove ( <NUM_LIT> ) , TypeError ) <EOL> class TestDistinct ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . conn = txmongo . MongoConnection ( mongo_host , mongo_port ) <EOL> self . db = self . conn . mydb <EOL> self . coll = self . db . mycol <EOL> @ defer . inlineCallbacks <EOL> def tearDown ( self ) : <EOL> yield self . coll . drop ( ) <EOL> yield self . conn . disconnect ( ) <EOL> @ defer . inlineCallbacks <EOL> def test_Simple ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:x>' : <NUM_LIT> } ] ) <EOL> d = yield self . coll . distinct ( '<STR_LIT:x>' ) <EOL> self . assertEqual ( set ( d ) , { <NUM_LIT> , <NUM_LIT> } ) <EOL> @ defer . inlineCallbacks <EOL> def test_WithQuery ( self ) : <EOL> yield self . coll . insert ( [ { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:x>' : <NUM_LIT> } ] ) <EOL> d = yield self . coll . distinct ( '<STR_LIT:x>' , { '<STR_LIT:x>' : { "<STR_LIT>" : <NUM_LIT:20> } } ) <EOL> self . assertEqual ( set ( d ) , { <NUM_LIT> , <NUM_LIT> } ) <EOL> class TestMapReduce ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def test_MapReduce ( self ) : <EOL> yield self . coll . insert ( [ <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:5> } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:4> } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:4> } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:4> } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:4> } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:2> } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:5> } , <EOL> ] ) <EOL> t_map = """<STR_LIT>""" <EOL> t_reduce = """<STR_LIT>""" <EOL> result = yield self . coll . map_reduce ( t_map , t_reduce , out = { "<STR_LIT>" : <NUM_LIT:1> } ) <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:3> ) <EOL> self . assertTrue ( { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:value>" : <NUM_LIT> } in result ) <EOL> self . assertTrue ( { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:value>" : <NUM_LIT:10> } in result ) <EOL> self . assertTrue ( { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:value>" : <NUM_LIT:4> } in result ) <EOL> result = yield self . coll . map_reduce ( t_map , t_reduce , out = { "<STR_LIT>" : <NUM_LIT:1> } , full_response = True ) <EOL> self . assertTrue ( result [ "<STR_LIT>" ] , <NUM_LIT:1> ) <EOL> self . assertTrue ( "<STR_LIT>" in result ) <EOL> self . assertTrue ( "<STR_LIT>" in result ) <EOL> class TestInsertOne ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def test_Acknowledged ( self ) : <EOL> result = yield self . coll . insert_one ( { '<STR_LIT:x>' : <NUM_LIT> } ) <EOL> self . assertTrue ( isinstance ( result , InsertOneResult ) ) <EOL> self . assertEqual ( result . acknowledged , True ) <EOL> self . assertTrue ( isinstance ( result . inserted_id , ObjectId ) ) <EOL> count = yield self . coll . count ( ) <EOL> self . assertEqual ( count , <NUM_LIT:1> ) <EOL> @ defer . inlineCallbacks <EOL> def test_Unacknowledged ( self ) : <EOL> oid = ObjectId ( ) <EOL> doc = { '<STR_LIT:x>' : <NUM_LIT> , "<STR_LIT>" : oid } <EOL> result = yield self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) . insert_one ( doc ) <EOL> self . assertEqual ( result . acknowledged , False ) <EOL> self . assertEqual ( result . inserted_id , oid ) <EOL> count = yield self . coll . count ( ) <EOL> self . assertEqual ( count , <NUM_LIT:1> ) <EOL> @ defer . inlineCallbacks <EOL> def test_Failures ( self ) : <EOL> yield self . coll . insert_one ( { "<STR_LIT>" : <NUM_LIT:1> } ) <EOL> yield self . assertFailure ( self . coll . insert_one ( { "<STR_LIT>" : <NUM_LIT:1> } ) , DuplicateKeyError ) <EOL> yield self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) . insert_one ( { "<STR_LIT>" : <NUM_LIT:1> } ) <EOL> yield self . assertFailure ( self . coll . insert_one ( { '<STR_LIT:$>' : <NUM_LIT:1> } ) , WriteError ) <EOL> yield self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) . insert_one ( { '<STR_LIT:$>' : <NUM_LIT:1> } ) <EOL> class TestInsertMany ( _SingleCollectionTest ) : <EOL> def setUp ( self ) : <EOL> self . more_than_1k = [ { "<STR_LIT>" : i } for i in range ( <NUM_LIT> ) ] <EOL> return super ( TestInsertMany , self ) . setUp ( ) <EOL> @ defer . inlineCallbacks <EOL> def test_InvalidArg ( self ) : <EOL> yield self . assertFailure ( self . coll . insert_many ( { '<STR_LIT:x>' : <NUM_LIT> } ) , TypeError ) <EOL> @ defer . inlineCallbacks <EOL> def test_Acknowledged ( self ) : <EOL> result = yield self . coll . insert_many ( [ { '<STR_LIT:x>' : <NUM_LIT> } for _ in range ( <NUM_LIT:100> ) ] ) <EOL> self . assertTrue ( isinstance ( result , InsertManyResult ) ) <EOL> self . assertEqual ( result . acknowledged , True ) <EOL> docs = yield self . coll . find ( ) <EOL> ids = set ( doc [ "<STR_LIT>" ] for doc in docs ) <EOL> self . assertEqual ( ids , set ( result . inserted_ids ) ) <EOL> @ defer . inlineCallbacks <EOL> def test_Unacknowledged ( self ) : <EOL> result = yield self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) . insert_many ( [ { '<STR_LIT:x>' : <NUM_LIT> } for _ in range ( <NUM_LIT:100> ) ] ) <EOL> self . assertTrue ( isinstance ( result , InsertManyResult ) ) <EOL> self . assertEqual ( result . acknowledged , False ) <EOL> docs = yield self . coll . find ( ) <EOL> self . assertEqual ( { doc [ "<STR_LIT>" ] for doc in docs } , set ( result . inserted_ids ) ) <EOL> @ defer . inlineCallbacks <EOL> def test_OrderedAck_Ok ( self ) : <EOL> result = yield self . coll . insert_many ( self . more_than_1k ) <EOL> found = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( result . inserted_ids ) , len ( self . more_than_1k ) ) <EOL> self . assertEqual ( len ( found ) , len ( self . more_than_1k ) ) <EOL> self . assertEqual ( set ( result . inserted_ids ) , { doc [ "<STR_LIT>" ] for doc in found } ) <EOL> @ defer . inlineCallbacks <EOL> def test_OrderedUnack_Ok ( self ) : <EOL> w_0 = self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) <EOL> result = yield w_0 . insert_many ( self . more_than_1k ) <EOL> found = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( result . inserted_ids ) , len ( self . more_than_1k ) ) <EOL> self . assertEqual ( len ( found ) , len ( self . more_than_1k ) ) <EOL> self . assertEqual ( set ( result . inserted_ids ) , { doc [ "<STR_LIT>" ] for doc in found } ) <EOL> @ defer . inlineCallbacks <EOL> def testOrderedAck_Fail ( self ) : <EOL> self . more_than_1k [ <NUM_LIT> ] = self . more_than_1k [ <NUM_LIT> ] <EOL> error = yield self . assertFailure ( self . coll . insert_many ( self . more_than_1k ) , BulkWriteError ) <EOL> self . assertEqual ( error . details [ "<STR_LIT>" ] , <NUM_LIT> ) <EOL> self . assertEqual ( ( yield self . coll . count ( ) ) , <NUM_LIT> ) <EOL> self . assertEqual ( len ( error . details [ "<STR_LIT>" ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( error . details [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT:index>" ] , <NUM_LIT> ) <EOL> self . assertEqual ( error . details [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] , { "<STR_LIT>" : <NUM_LIT> } ) <EOL> @ defer . inlineCallbacks <EOL> def test_OrderedUnack_Fail ( self ) : <EOL> self . more_than_1k [ <NUM_LIT> ] = self . more_than_1k [ <NUM_LIT> ] <EOL> w_0 = self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) <EOL> result = yield w_0 . insert_many ( self . more_than_1k ) <EOL> self . assertEqual ( len ( result . inserted_ids ) , len ( self . more_than_1k ) ) <EOL> found = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( found ) , <NUM_LIT> ) <EOL> self . assertEqual ( { doc [ "<STR_LIT>" ] for doc in found [ : <NUM_LIT> ] } , set ( result . inserted_ids [ : <NUM_LIT> ] ) ) <EOL> @ defer . inlineCallbacks <EOL> def test_UnorderedAck_Fail ( self ) : <EOL> self . more_than_1k [ <NUM_LIT> ] = self . more_than_1k [ <NUM_LIT> ] <EOL> error = yield self . assertFailure ( self . coll . insert_many ( self . more_than_1k , ordered = False ) , <EOL> BulkWriteError ) <EOL> self . assertEqual ( error . details [ "<STR_LIT>" ] , len ( self . more_than_1k ) - <NUM_LIT:1> ) <EOL> self . assertEqual ( ( yield self . coll . count ( ) ) , len ( self . more_than_1k ) - <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( error . details [ "<STR_LIT>" ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( error . details [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT:index>" ] , <NUM_LIT> ) <EOL> self . assertEqual ( error . details [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] , { "<STR_LIT>" : <NUM_LIT> } ) <EOL> @ defer . inlineCallbacks <EOL> def test_UnorderedUnack_Fail ( self ) : <EOL> self . more_than_1k [ <NUM_LIT> ] = self . more_than_1k [ <NUM_LIT> ] <EOL> w_0 = self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) <EOL> result = yield w_0 . insert_many ( self . more_than_1k , ordered = False ) <EOL> self . assertEqual ( len ( result . inserted_ids ) , len ( self . more_than_1k ) ) <EOL> found = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( found ) , len ( self . more_than_1k ) - <NUM_LIT:1> ) <EOL> self . assertEqual ( { doc [ "<STR_LIT>" ] for doc in found } , set ( result . inserted_ids ) - { <NUM_LIT> } ) <EOL> @ defer . inlineCallbacks <EOL> def test_MoreThan16Mb ( self ) : <EOL> mb40 = [ { "<STR_LIT>" : i , '<STR_LIT:x>' : '<STR_LIT:y>' * ( <NUM_LIT:8> * <NUM_LIT> ** <NUM_LIT:2> ) } for i in range ( <NUM_LIT:5> ) ] <EOL> result = yield self . coll . insert_many ( mb40 ) <EOL> self . assertEqual ( result . inserted_ids , list ( range ( <NUM_LIT:5> ) ) ) <EOL> found = yield self . coll . find ( ) <EOL> self . assertEqual ( len ( found ) , <NUM_LIT:5> ) <EOL> total_size = sum ( len ( BSON . encode ( doc ) ) for doc in found ) <EOL> self . assertGreater ( total_size , <NUM_LIT> * <NUM_LIT> ** <NUM_LIT:2> ) <EOL> class TestUpdateOne ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def setUp ( self ) : <EOL> yield super ( TestUpdateOne , self ) . setUp ( ) <EOL> yield self . coll . insert_many ( [ { '<STR_LIT:x>' : <NUM_LIT:1> } , { '<STR_LIT:x>' : <NUM_LIT:2> } ] ) <EOL> @ defer . inlineCallbacks <EOL> def test_Acknowledged ( self ) : <EOL> result = yield self . coll . update_one ( { '<STR_LIT:x>' : { "<STR_LIT>" : True } } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT> } } ) <EOL> self . assertTrue ( isinstance ( result , UpdateResult ) ) <EOL> self . assertEqual ( result . acknowledged , True ) <EOL> self . assertEqual ( result . matched_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . modified_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . upserted_id , None ) <EOL> @ defer . inlineCallbacks <EOL> def test_Unacknowledged ( self ) : <EOL> coll = self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) <EOL> result = yield coll . update_one ( { '<STR_LIT:x>' : { "<STR_LIT>" : True } } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT> } } ) <EOL> self . assertTrue ( isinstance ( result , UpdateResult ) ) <EOL> self . assertEqual ( result . acknowledged , False ) <EOL> @ defer . inlineCallbacks <EOL> def test_UpsertAcknowledged ( self ) : <EOL> result = yield self . coll . update_one ( { '<STR_LIT:y>' : <NUM_LIT> } , { "<STR_LIT>" : { '<STR_LIT:z>' : <NUM_LIT> } } , upsert = True ) <EOL> self . assertTrue ( isinstance ( result . upserted_id , ObjectId ) ) <EOL> doc = yield self . coll . find_one ( { "<STR_LIT>" : result . upserted_id } , fields = { "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertEqual ( doc , { '<STR_LIT:y>' : <NUM_LIT> , '<STR_LIT:z>' : <NUM_LIT> } ) <EOL> @ defer . inlineCallbacks <EOL> def test_UpsertUnacknowledged ( self ) : <EOL> coll = self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) <EOL> result = yield coll . update_one ( { '<STR_LIT:y>' : <NUM_LIT> } , { "<STR_LIT>" : { '<STR_LIT:z>' : <NUM_LIT> } } , upsert = True ) <EOL> self . assertEqual ( result . acknowledged , False ) <EOL> cnt = yield self . coll . count ( ) <EOL> self . assertEqual ( cnt , <NUM_LIT:3> ) <EOL> @ defer . inlineCallbacks <EOL> def test_InvalidUpdate ( self ) : <EOL> yield self . assertFailure ( self . coll . update_one ( { '<STR_LIT:x>' : <NUM_LIT:1> } , { '<STR_LIT:y>' : <NUM_LIT> } ) , ValueError ) <EOL> @ defer . inlineCallbacks <EOL> def test_Failures ( self ) : <EOL> yield self . assertFailure ( self . coll . update_one ( { '<STR_LIT:x>' : <NUM_LIT:1> } , { "<STR_LIT>" : { "<STR_LIT>" : <NUM_LIT:1> } } ) , WriteError ) <EOL> yield self . assertFailure ( self . coll . update_one ( { '<STR_LIT:x>' : <NUM_LIT:1> } , { "<STR_LIT>" : { '<STR_LIT:$>' : <NUM_LIT:1> } } ) , WriteError ) <EOL> yield self . coll . create_index ( qf . sort ( qf . ASCENDING ( '<STR_LIT:x>' ) ) , unique = True ) <EOL> yield self . assertFailure ( self . coll . update_one ( { '<STR_LIT:x>' : <NUM_LIT:2> } , { "<STR_LIT>" : { '<STR_LIT:x>' : <NUM_LIT:1> } } ) , DuplicateKeyError ) <EOL> class TestReplaceOne ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def setUp ( self ) : <EOL> yield super ( TestReplaceOne , self ) . setUp ( ) <EOL> yield self . coll . insert_many ( [ { '<STR_LIT:x>' : <NUM_LIT:1> } , { '<STR_LIT:x>' : <NUM_LIT:2> } ] ) <EOL> @ defer . inlineCallbacks <EOL> def test_Acknowledged ( self ) : <EOL> result = yield self . coll . replace_one ( { '<STR_LIT:x>' : { "<STR_LIT>" : True } } , { '<STR_LIT:y>' : <NUM_LIT> } ) <EOL> self . assertTrue ( isinstance ( result , UpdateResult ) ) <EOL> self . assertEqual ( result . acknowledged , True ) <EOL> self . assertEqual ( result . matched_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . modified_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . upserted_id , None ) <EOL> @ defer . inlineCallbacks <EOL> def test_Unacknowledged ( self ) : <EOL> coll = self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) <EOL> result = yield coll . replace_one ( { '<STR_LIT:x>' : { "<STR_LIT>" : True } } , { '<STR_LIT:y>' : <NUM_LIT> } ) <EOL> self . assertTrue ( isinstance ( result , UpdateResult ) ) <EOL> self . assertEqual ( result . acknowledged , False ) <EOL> @ defer . inlineCallbacks <EOL> def test_UpsertAcknowledged ( self ) : <EOL> result = yield self . coll . replace_one ( { '<STR_LIT:x>' : <NUM_LIT:5> } , { '<STR_LIT:y>' : <NUM_LIT> } , upsert = True ) <EOL> self . assertTrue ( isinstance ( result . upserted_id , ObjectId ) ) <EOL> doc = yield self . coll . find_one ( { "<STR_LIT>" : result . upserted_id } , fields = { "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertEqual ( doc , { '<STR_LIT:y>' : <NUM_LIT> } ) <EOL> @ defer . inlineCallbacks <EOL> def test_UpsertUnacknowledged ( self ) : <EOL> coll = self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) <EOL> result = yield coll . replace_one ( { '<STR_LIT:x>' : <NUM_LIT:5> } , { '<STR_LIT:y>' : <NUM_LIT> } , upsert = True ) <EOL> self . assertEqual ( result . acknowledged , False ) <EOL> doc = yield self . coll . find_one ( { "<STR_LIT:y>" : { "<STR_LIT>" : True } } , fields = { "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertEqual ( doc , { '<STR_LIT:y>' : <NUM_LIT> } ) <EOL> @ defer . inlineCallbacks <EOL> def test_InvalidReplace ( self ) : <EOL> yield self . assertFailure ( self . coll . replace_one ( { '<STR_LIT:x>' : <NUM_LIT:1> } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT> } } ) , <EOL> ValueError ) <EOL> @ defer . inlineCallbacks <EOL> def test_Failures ( self ) : <EOL> yield self . assertFailure ( self . coll . replace_one ( { '<STR_LIT:x>' : <NUM_LIT:1> } , { '<STR_LIT:x>' : { '<STR_LIT:$>' : <NUM_LIT:5> } } ) , WriteError ) <EOL> yield self . coll . create_index ( qf . sort ( qf . ASCENDING ( '<STR_LIT:x>' ) ) , unique = True ) <EOL> yield self . assertFailure ( self . coll . replace_one ( { '<STR_LIT:x>' : <NUM_LIT:1> } , { '<STR_LIT:x>' : <NUM_LIT:2> } ) , DuplicateKeyError ) <EOL> class TestUpdateMany ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def setUp ( self ) : <EOL> yield super ( TestUpdateMany , self ) . setUp ( ) <EOL> yield self . coll . insert_many ( [ { '<STR_LIT:x>' : <NUM_LIT:1> } , { '<STR_LIT:x>' : <NUM_LIT:2> } ] ) <EOL> @ defer . inlineCallbacks <EOL> def test_Acknowledged ( self ) : <EOL> result = yield self . coll . update_many ( { '<STR_LIT:x>' : { "<STR_LIT>" : True } } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT:5> } } ) <EOL> self . assertTrue ( isinstance ( result , UpdateResult ) ) <EOL> self . assertEqual ( result . acknowledged , True ) <EOL> self . assertEqual ( result . matched_count , <NUM_LIT:2> ) <EOL> self . assertEqual ( result . modified_count , <NUM_LIT:2> ) <EOL> self . assertEqual ( result . upserted_id , None ) <EOL> cnt = yield self . coll . count ( { '<STR_LIT:y>' : <NUM_LIT:5> } ) <EOL> self . assertEqual ( cnt , <NUM_LIT:2> ) <EOL> @ defer . inlineCallbacks <EOL> def test_Unacknowledged ( self ) : <EOL> coll = self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) <EOL> result = yield coll . update_many ( { '<STR_LIT:x>' : { "<STR_LIT>" : True } } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT:5> } } ) <EOL> self . assertTrue ( isinstance ( result , UpdateResult ) ) <EOL> self . assertEqual ( result . acknowledged , False ) <EOL> cnt = yield self . coll . count ( { '<STR_LIT:y>' : <NUM_LIT:5> } ) <EOL> self . assertEqual ( cnt , <NUM_LIT:2> ) <EOL> @ defer . inlineCallbacks <EOL> def test_UpsertAcknowledged ( self ) : <EOL> result = yield self . coll . update_many ( { '<STR_LIT:x>' : <NUM_LIT:5> } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT:5> } } , upsert = True ) <EOL> self . assertEqual ( result . acknowledged , True ) <EOL> self . assertEqual ( result . matched_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( result . modified_count , <NUM_LIT:0> ) <EOL> self . assertTrue ( isinstance ( result . upserted_id , ObjectId ) ) <EOL> cnt = yield self . coll . count ( ) <EOL> self . assertEqual ( cnt , <NUM_LIT:3> ) <EOL> @ defer . inlineCallbacks <EOL> def test_UpsertUnacknowledged ( self ) : <EOL> coll = self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) <EOL> result = yield coll . update_many ( { '<STR_LIT:x>' : <NUM_LIT:5> } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT:5> } } , upsert = True ) <EOL> self . assertEqual ( result . acknowledged , False ) <EOL> cnt = yield self . coll . count ( ) <EOL> self . assertEqual ( cnt , <NUM_LIT:3> ) <EOL> @ defer . inlineCallbacks <EOL> def test_InvalidUpdate ( self ) : <EOL> yield self . assertFailure ( self . coll . update_many ( { '<STR_LIT:x>' : <NUM_LIT:1> } , { '<STR_LIT:y>' : <NUM_LIT> } ) , ValueError ) <EOL> @ defer . inlineCallbacks <EOL> def test_Failures ( self ) : <EOL> yield self . assertFailure ( self . coll . update_many ( { } , { "<STR_LIT>" : { "<STR_LIT>" : <NUM_LIT:1> } } ) , WriteError ) <EOL> yield self . assertFailure ( self . coll . update_many ( { } , { "<STR_LIT>" : { '<STR_LIT:$>' : <NUM_LIT:1> } } ) , WriteError ) <EOL> yield self . coll . create_index ( qf . sort ( qf . ASCENDING ( '<STR_LIT:x>' ) ) , unique = True ) <EOL> yield self . assertFailure ( self . coll . update_many ( { '<STR_LIT:x>' : <NUM_LIT:2> } , { "<STR_LIT>" : { '<STR_LIT:x>' : <NUM_LIT:1> } } ) , DuplicateKeyError ) <EOL> class TestDeleteOne ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def setUp ( self ) : <EOL> yield super ( TestDeleteOne , self ) . setUp ( ) <EOL> yield self . coll . insert_many ( [ { '<STR_LIT:x>' : <NUM_LIT:1> } , { '<STR_LIT:x>' : <NUM_LIT:1> } ] ) <EOL> @ defer . inlineCallbacks <EOL> def test_Acknowledged ( self ) : <EOL> result = yield self . coll . delete_one ( { '<STR_LIT:x>' : <NUM_LIT:1> } ) <EOL> self . assertTrue ( isinstance ( result , DeleteResult ) ) <EOL> self . assertEqual ( result . acknowledged , True ) <EOL> self . assertEqual ( result . deleted_count , <NUM_LIT:1> ) <EOL> cnt = yield self . coll . count ( ) <EOL> self . assertEqual ( cnt , <NUM_LIT:1> ) <EOL> @ defer . inlineCallbacks <EOL> def test_Unacknowledged ( self ) : <EOL> coll = self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) <EOL> result = yield coll . delete_one ( { '<STR_LIT:x>' : <NUM_LIT:1> } ) <EOL> self . assertTrue ( isinstance ( result , DeleteResult ) ) <EOL> self . assertEqual ( result . acknowledged , False ) <EOL> cnt = yield self . coll . count ( ) <EOL> self . assertEqual ( cnt , <NUM_LIT:1> ) <EOL> @ defer . inlineCallbacks <EOL> def test_Failures ( self ) : <EOL> yield self . assertFailure ( self . coll . delete_one ( { '<STR_LIT:x>' : { '<STR_LIT:$>' : <NUM_LIT:1> } } ) , WriteError ) <EOL> class TestDeleteMany ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def setUp ( self ) : <EOL> yield super ( TestDeleteMany , self ) . setUp ( ) <EOL> yield self . coll . insert_many ( [ { '<STR_LIT:x>' : <NUM_LIT:1> } , { '<STR_LIT:x>' : <NUM_LIT:1> } ] ) <EOL> @ defer . inlineCallbacks <EOL> def test_Acknowledged ( self ) : <EOL> result = yield self . coll . delete_many ( { '<STR_LIT:x>' : <NUM_LIT:1> } ) <EOL> self . assertTrue ( isinstance ( result , DeleteResult ) ) <EOL> self . assertEqual ( result . acknowledged , True ) <EOL> self . assertEqual ( result . deleted_count , <NUM_LIT:2> ) <EOL> cnt = yield self . coll . count ( ) <EOL> self . assertEqual ( cnt , <NUM_LIT:0> ) <EOL> @ defer . inlineCallbacks <EOL> def test_Unacknowledged ( self ) : <EOL> coll = self . coll . with_options ( write_concern = WriteConcern ( w = <NUM_LIT:0> ) ) <EOL> result = yield coll . delete_many ( { '<STR_LIT:x>' : <NUM_LIT:1> } ) <EOL> self . assertTrue ( isinstance ( result , DeleteResult ) ) <EOL> self . assertEqual ( result . acknowledged , False ) <EOL> cnt = yield self . coll . count ( ) <EOL> self . assertEqual ( cnt , <NUM_LIT:0> ) <EOL> @ defer . inlineCallbacks <EOL> def test_Failures ( self ) : <EOL> yield self . assertFailure ( self . coll . delete_many ( { '<STR_LIT:x>' : { '<STR_LIT:$>' : <NUM_LIT:1> } } ) , WriteError ) <EOL> class TestFindOneAndDelete ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def setUp ( self ) : <EOL> yield super ( TestFindOneAndDelete , self ) . setUp ( ) <EOL> yield self . coll . insert_many ( [ { '<STR_LIT:x>' : <NUM_LIT:1> , '<STR_LIT:y>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT:x>' : <NUM_LIT:2> , '<STR_LIT:y>' : <NUM_LIT:2> } , <EOL> { '<STR_LIT:x>' : <NUM_LIT:3> , '<STR_LIT:y>' : <NUM_LIT:3> } ] ) <EOL> @ defer . inlineCallbacks <EOL> def test_Sort ( self ) : <EOL> doc = yield self . coll . find_one_and_delete ( { '<STR_LIT:x>' : { "<STR_LIT>" : True } } , <EOL> sort = qf . sort ( qf . ASCENDING ( '<STR_LIT:y>' ) ) ) <EOL> self . assertEqual ( doc [ '<STR_LIT:x>' ] , <NUM_LIT:1> ) <EOL> doc = yield self . coll . find_one_and_delete ( { '<STR_LIT:x>' : { "<STR_LIT>" : True } } , <EOL> sort = qf . sort ( qf . DESCENDING ( '<STR_LIT:y>' ) ) ) <EOL> self . assertEqual ( doc [ '<STR_LIT:x>' ] , <NUM_LIT:3> ) <EOL> cnt = yield self . coll . count ( ) <EOL> self . assertEqual ( cnt , <NUM_LIT:1> ) <EOL> @ defer . inlineCallbacks <EOL> def test_Projection ( self ) : <EOL> doc = yield self . coll . find_one_and_delete ( { '<STR_LIT:x>' : <NUM_LIT:2> } , { '<STR_LIT:y>' : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertEqual ( doc , { '<STR_LIT:y>' : <NUM_LIT:2> } ) <EOL> class TestFindOneAndReplace ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def setUp ( self ) : <EOL> yield super ( TestFindOneAndReplace , self ) . setUp ( ) <EOL> yield self . coll . insert_many ( [ { '<STR_LIT:x>' : <NUM_LIT:10> , '<STR_LIT:y>' : <NUM_LIT:10> } , <EOL> { '<STR_LIT:x>' : <NUM_LIT:20> , '<STR_LIT:y>' : <NUM_LIT:20> } , <EOL> { '<STR_LIT:x>' : <NUM_LIT:30> , '<STR_LIT:y>' : <NUM_LIT:30> } ] ) <EOL> @ defer . inlineCallbacks <EOL> def test_Sort ( self ) : <EOL> doc = yield self . coll . find_one_and_replace ( { } , { '<STR_LIT:x>' : <NUM_LIT:5> , '<STR_LIT:y>' : <NUM_LIT:5> } , <EOL> projection = { "<STR_LIT>" : <NUM_LIT:0> } , <EOL> sort = qf . sort ( qf . ASCENDING ( '<STR_LIT:y>' ) ) ) <EOL> self . assertEqual ( doc , { '<STR_LIT:x>' : <NUM_LIT:10> , '<STR_LIT:y>' : <NUM_LIT:10> } ) <EOL> doc = yield self . coll . find_one_and_replace ( { } , { '<STR_LIT:x>' : <NUM_LIT> , '<STR_LIT:y>' : <NUM_LIT> } , <EOL> projection = { "<STR_LIT>" : <NUM_LIT:0> } , <EOL> sort = qf . sort ( qf . DESCENDING ( '<STR_LIT:y>' ) ) ) <EOL> self . assertEqual ( doc , { '<STR_LIT:x>' : <NUM_LIT:30> , '<STR_LIT:y>' : <NUM_LIT:30> } ) <EOL> ys = yield self . coll . distinct ( '<STR_LIT:y>' ) <EOL> self . assertEqual ( set ( ys ) , { <NUM_LIT:5> , <NUM_LIT:20> , <NUM_LIT> } ) <EOL> @ defer . inlineCallbacks <EOL> def test_InvalidReplace ( self ) : <EOL> yield self . assertFailure ( self . coll . find_one_and_replace ( { } , { "<STR_LIT>" : { '<STR_LIT:z>' : <NUM_LIT:1> } } ) , ValueError ) <EOL> @ defer . inlineCallbacks <EOL> def test_Upsert ( self ) : <EOL> doc = yield self . coll . find_one_and_replace ( { '<STR_LIT:x>' : <NUM_LIT> } , { '<STR_LIT:x>' : <NUM_LIT:50> } , upsert = True ) <EOL> self . assertEqual ( doc , None ) <EOL> xs = yield self . coll . distinct ( '<STR_LIT:x>' ) <EOL> self . assertEqual ( set ( xs ) , { <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT:50> } ) <EOL> @ defer . inlineCallbacks <EOL> def test_ReturnDocument ( self ) : <EOL> doc = yield self . coll . find_one_and_replace ( { '<STR_LIT:x>' : <NUM_LIT:10> } , { '<STR_LIT:x>' : <NUM_LIT:15> } , <EOL> return_document = ReturnDocument . BEFORE ) <EOL> self . assertEqual ( doc [ '<STR_LIT:x>' ] , <NUM_LIT:10> ) <EOL> doc = yield self . coll . find_one_and_replace ( { '<STR_LIT:x>' : <NUM_LIT:20> } , { '<STR_LIT:x>' : <NUM_LIT> } , <EOL> return_document = ReturnDocument . AFTER ) <EOL> self . assertEqual ( doc [ '<STR_LIT:x>' ] , <NUM_LIT> ) <EOL> @ defer . inlineCallbacks <EOL> def test_InvalidReturnDocument ( self ) : <EOL> yield self . assertFailure ( self . coll . find_one_and_replace ( { } , { } , return_document = <NUM_LIT:1> ) , <EOL> ValueError ) <EOL> class TestFindOneAndUpdate ( _SingleCollectionTest ) : <EOL> @ defer . inlineCallbacks <EOL> def setUp ( self ) : <EOL> yield super ( TestFindOneAndUpdate , self ) . setUp ( ) <EOL> yield self . coll . insert_many ( [ { '<STR_LIT:x>' : <NUM_LIT:10> , '<STR_LIT:y>' : <NUM_LIT:10> } , <EOL> { '<STR_LIT:x>' : <NUM_LIT:20> , '<STR_LIT:y>' : <NUM_LIT:20> } , <EOL> { '<STR_LIT:x>' : <NUM_LIT:30> , '<STR_LIT:y>' : <NUM_LIT:30> } ] ) <EOL> @ defer . inlineCallbacks <EOL> def test_Sort ( self ) : <EOL> doc = yield self . coll . find_one_and_update ( { } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT:5> } } , <EOL> projection = { "<STR_LIT>" : <NUM_LIT:0> } , <EOL> sort = qf . sort ( qf . ASCENDING ( '<STR_LIT:y>' ) ) ) <EOL> self . assertEqual ( doc , { '<STR_LIT:x>' : <NUM_LIT:10> , '<STR_LIT:y>' : <NUM_LIT:10> } ) <EOL> doc = yield self . coll . find_one_and_update ( { } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT> } } , <EOL> projection = { "<STR_LIT>" : <NUM_LIT:0> } , <EOL> sort = qf . sort ( qf . DESCENDING ( '<STR_LIT:y>' ) ) ) <EOL> self . assertEqual ( doc , { '<STR_LIT:x>' : <NUM_LIT:30> , '<STR_LIT:y>' : <NUM_LIT:30> } ) <EOL> ys = yield self . coll . distinct ( '<STR_LIT:y>' ) <EOL> self . assertEqual ( set ( ys ) , { <NUM_LIT:5> , <NUM_LIT:20> , <NUM_LIT> } ) <EOL> def test_InvalidUpdate ( self ) : <EOL> self . assertFailure ( self . coll . find_one_and_update ( { } , { '<STR_LIT:x>' : <NUM_LIT> } ) , ValueError ) <EOL> @ defer . inlineCallbacks <EOL> def test_Upsert ( self ) : <EOL> doc = yield self . coll . find_one_and_update ( { '<STR_LIT:x>' : <NUM_LIT> } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT> } } , <EOL> upsert = True ) <EOL> self . assertEqual ( doc , None ) <EOL> inserted = yield self . coll . find_one ( { '<STR_LIT:x>' : <NUM_LIT> } ) <EOL> self . assertEqual ( inserted [ '<STR_LIT:y>' ] , <NUM_LIT> ) <EOL> @ defer . inlineCallbacks <EOL> def test_ReturnDocument ( self ) : <EOL> doc = yield self . coll . find_one_and_update ( { '<STR_LIT:x>' : <NUM_LIT:10> } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT:5> } } , <EOL> return_document = ReturnDocument . BEFORE ) <EOL> self . assertEqual ( doc [ '<STR_LIT:y>' ] , <NUM_LIT:10> ) <EOL> doc = yield self . coll . find_one_and_update ( { '<STR_LIT:x>' : <NUM_LIT:10> } , { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT:15> } } , <EOL> return_document = ReturnDocument . AFTER ) <EOL> self . assertEqual ( doc [ '<STR_LIT:y>' ] , <NUM_LIT:15> ) </s>
