<s> from __future__ import unicode_literals <EOL> """<STR_LIT>""" <EOL> import unittest <EOL> import bson <EOL> from bson . binary import JAVA_LEGACY , OLD_UUID_SUBTYPE <EOL> from bson . objectid import ObjectId <EOL> from pymongo import ReadPreference <EOL> from pymongo . errors import DuplicateKeyError , InvalidOperation <EOL> from tornado import gen <EOL> from tornado . concurrent import Future <EOL> from tornado . testing import gen_test <EOL> import motor <EOL> import motor . motor_tornado <EOL> import test <EOL> from test import SkipTest <EOL> from test . tornado_tests import at_least , MotorTest , skip_if_mongos <EOL> from test . utils import delay <EOL> class MotorCollectionTest ( MotorTest ) : <EOL> @ gen_test <EOL> def test_collection ( self ) : <EOL> collection = motor . MotorCollection ( self . db , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , collection . name ) <EOL> yield collection . insert ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> doc = yield collection . find_one ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> self . assertEqual ( <NUM_LIT:1> , doc [ '<STR_LIT>' ] ) <EOL> self . assertRaises ( <EOL> TypeError , <EOL> motor . MotorCollection , <EOL> self . db , <EOL> '<STR_LIT>' , <EOL> capped = True ) <EOL> @ gen_test <EOL> def test_dotted_collection_name ( self ) : <EOL> for coll in ( <EOL> self . db . foo . bar , <EOL> self . db . foo . bar . baz ) : <EOL> yield coll . remove ( ) <EOL> self . assertEqual ( '<STR_LIT>' , ( yield coll . insert ( { '<STR_LIT>' : '<STR_LIT>' } ) ) ) <EOL> result = yield coll . find_one ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> yield coll . remove ( ) <EOL> self . assertEqual ( None , ( yield coll . find_one ( { '<STR_LIT>' : '<STR_LIT>' } ) ) ) <EOL> def test_call ( self ) : <EOL> try : <EOL> self . db . foo ( ) <EOL> except TypeError as e : <EOL> self . assertTrue ( '<STR_LIT>' in str ( e ) ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> @ gen_test <EOL> def test_find_is_async ( self ) : <EOL> coll = self . collection <EOL> yield coll . insert ( [ { '<STR_LIT>' : <NUM_LIT:1> } , { '<STR_LIT>' : <NUM_LIT:2> } ] ) <EOL> results = [ ] <EOL> futures = [ Future ( ) , Future ( ) ] <EOL> def callback ( result , error ) : <EOL> if result : <EOL> results . append ( result ) <EOL> futures . pop ( ) . set_result ( None ) <EOL> coll . find ( { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : delay ( <NUM_LIT:0.5> ) } ) . limit ( <NUM_LIT:1> ) . each ( callback ) <EOL> coll . find ( { '<STR_LIT>' : <NUM_LIT:2> } ) . limit ( <NUM_LIT:1> ) . each ( callback ) <EOL> yield futures <EOL> self . assertEqual ( [ { '<STR_LIT>' : <NUM_LIT:2> } , { '<STR_LIT>' : <NUM_LIT:1> } ] , results ) <EOL> @ gen_test <EOL> def test_update ( self ) : <EOL> yield self . collection . insert ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> result = yield self . collection . update ( <EOL> { '<STR_LIT>' : <NUM_LIT:1> } , { '<STR_LIT>' : { '<STR_LIT:foo>' : '<STR_LIT:bar>' } } ) <EOL> self . assertEqual ( <NUM_LIT:1> , result [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( True , result [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , result [ '<STR_LIT:n>' ] ) <EOL> self . assertEqual ( None , result . get ( '<STR_LIT>' ) ) <EOL> @ gen_test <EOL> def test_update_bad ( self ) : <EOL> coll = self . db . unique_collection <EOL> yield coll . ensure_index ( '<STR_LIT:s>' , unique = True ) <EOL> try : <EOL> yield coll . insert ( [ { '<STR_LIT:s>' : <NUM_LIT:1> } , { '<STR_LIT:s>' : <NUM_LIT:2> } ] ) <EOL> with self . assertRaises ( DuplicateKeyError ) : <EOL> yield coll . update ( { '<STR_LIT:s>' : <NUM_LIT:2> } , { '<STR_LIT>' : { '<STR_LIT:s>' : <NUM_LIT:1> } } ) <EOL> finally : <EOL> yield coll . drop ( ) <EOL> @ gen_test <EOL> def test_update_callback ( self ) : <EOL> yield self . check_optional_callback ( <EOL> self . collection . update , { } , { } ) <EOL> @ gen_test <EOL> def test_insert ( self ) : <EOL> collection = self . collection <EOL> self . assertEqual ( <NUM_LIT> , ( yield collection . insert ( { '<STR_LIT>' : <NUM_LIT> } ) ) ) <EOL> @ gen_test <EOL> def test_insert_many_one_bad ( self ) : <EOL> collection = self . collection <EOL> yield collection . insert ( { '<STR_LIT>' : <NUM_LIT:2> } ) <EOL> with self . assertRaises ( DuplicateKeyError ) : <EOL> yield collection . insert ( [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT>' : <NUM_LIT:2> } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> } ] ) <EOL> self . assertEqual ( <EOL> set ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) , <EOL> set ( ( yield collection . distinct ( '<STR_LIT>' ) ) ) ) <EOL> @ gen_test <EOL> def test_save_callback ( self ) : <EOL> yield self . check_optional_callback ( <EOL> self . collection . save , { } ) <EOL> @ gen_test <EOL> def test_save_with_id ( self ) : <EOL> self . assertEqual ( <EOL> <NUM_LIT:5> , <EOL> ( yield self . collection . save ( { '<STR_LIT>' : <NUM_LIT:5> } ) ) ) <EOL> @ gen_test <EOL> def test_save_without_id ( self ) : <EOL> collection = self . collection <EOL> result = yield collection . save ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( isinstance ( result , ObjectId ) ) <EOL> @ gen_test <EOL> def test_save_bad ( self ) : <EOL> coll = self . db . unique_collection <EOL> yield coll . ensure_index ( '<STR_LIT:s>' , unique = True ) <EOL> yield coll . save ( { '<STR_LIT:s>' : <NUM_LIT:1> } ) <EOL> try : <EOL> with self . assertRaises ( DuplicateKeyError ) : <EOL> yield coll . save ( { '<STR_LIT:s>' : <NUM_LIT:1> } ) <EOL> finally : <EOL> yield coll . drop ( ) <EOL> @ gen_test <EOL> def test_remove ( self ) : <EOL> yield self . collection . insert ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> result = yield self . collection . remove ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> self . assertEqual ( <NUM_LIT:1> , result [ '<STR_LIT:n>' ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , result [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( None , result . get ( '<STR_LIT>' ) ) <EOL> result = yield self . collection . remove ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> self . assertEqual ( <NUM_LIT:0> , result [ '<STR_LIT:n>' ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , result [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( None , result . get ( '<STR_LIT>' ) ) <EOL> @ gen_test <EOL> def test_remove_callback ( self ) : <EOL> yield self . check_optional_callback ( self . collection . remove ) <EOL> @ gen_test <EOL> def test_unacknowledged_remove ( self ) : <EOL> coll = self . collection <EOL> yield coll . remove ( ) <EOL> yield coll . insert ( [ { '<STR_LIT>' : i } for i in range ( <NUM_LIT:3> ) ] ) <EOL> coll . remove ( { '<STR_LIT>' : <NUM_LIT:0> } , w = <NUM_LIT:0> ) <EOL> coll . remove ( { '<STR_LIT>' : <NUM_LIT:1> } , w = <NUM_LIT:0> ) <EOL> coll . remove ( { '<STR_LIT>' : <NUM_LIT:2> } , w = <NUM_LIT:0> ) <EOL> while ( yield coll . count ( ) ) : <EOL> yield self . pause ( <NUM_LIT:0.1> ) <EOL> coll . database . connection . close ( ) <EOL> @ gen_test <EOL> def test_unacknowledged_insert ( self ) : <EOL> coll = self . db . test_unacknowledged_insert <EOL> coll . insert ( { '<STR_LIT>' : <NUM_LIT:1> } , w = <NUM_LIT:0> ) <EOL> while not ( yield coll . count ( ) ) : <EOL> yield self . pause ( <NUM_LIT:0.1> ) <EOL> future = coll . insert ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> yield coll . insert ( { '<STR_LIT>' : <NUM_LIT:1> } , w = <NUM_LIT:0> ) <EOL> with self . assertRaises ( DuplicateKeyError ) : <EOL> yield future <EOL> @ gen_test <EOL> def test_unacknowledged_save ( self ) : <EOL> collection_name = '<STR_LIT>' <EOL> coll = self . db [ collection_name ] <EOL> coll . save ( { '<STR_LIT>' : <NUM_LIT> } , w = <NUM_LIT:0> ) <EOL> while not ( yield coll . find_one ( { '<STR_LIT>' : <NUM_LIT> } ) ) : <EOL> yield self . pause ( <NUM_LIT:0.1> ) <EOL> coll . save ( { '<STR_LIT>' : <NUM_LIT> } ) <EOL> yield coll . save ( { '<STR_LIT>' : <NUM_LIT> } , w = <NUM_LIT:0> ) <EOL> coll . database . connection . close ( ) <EOL> @ gen_test <EOL> def test_unacknowledged_update ( self ) : <EOL> coll = self . collection <EOL> yield coll . insert ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> coll . update ( { '<STR_LIT>' : <NUM_LIT:1> } , { '<STR_LIT>' : { '<STR_LIT:a>' : <NUM_LIT:1> } } , w = <NUM_LIT:0> ) <EOL> while not ( yield coll . find_one ( { '<STR_LIT:a>' : <NUM_LIT:1> } ) ) : <EOL> yield self . pause ( <NUM_LIT:0.1> ) <EOL> coll . database . connection . close ( ) <EOL> @ gen_test <EOL> def test_nested_callbacks ( self ) : <EOL> results = [ <NUM_LIT:0> ] <EOL> future = Future ( ) <EOL> yield self . collection . remove ( ) <EOL> yield self . collection . insert ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> def callback ( result , error ) : <EOL> if error : <EOL> raise error <EOL> if not result : <EOL> return <EOL> results [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> if results [ <NUM_LIT:0> ] < <NUM_LIT:1000> : <EOL> self . collection . find ( { '<STR_LIT>' : <NUM_LIT:1> } ) . each ( callback ) <EOL> else : <EOL> future . set_result ( None ) <EOL> self . collection . find ( { '<STR_LIT>' : <NUM_LIT:1> } ) . each ( callback ) <EOL> yield future <EOL> self . assertEqual ( <NUM_LIT:1000> , results [ <NUM_LIT:0> ] ) <EOL> @ gen_test <EOL> def test_map_reduce ( self ) : <EOL> yield self . make_test_data ( ) <EOL> expected_result = [ { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:value>' : <NUM_LIT:100> } , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:value>' : <NUM_LIT:100> } ] <EOL> map_fn = bson . Code ( '<STR_LIT>' ) <EOL> reduce_fn = bson . Code ( '''<STR_LIT>''' ) <EOL> yield self . db . tmp_mr . drop ( ) <EOL> collection = self . collection <EOL> tmp_mr = yield collection . map_reduce ( map_fn , reduce_fn , '<STR_LIT>' ) <EOL> self . assertTrue ( <EOL> isinstance ( tmp_mr , motor . MotorCollection ) , <EOL> '<STR_LIT>' % tmp_mr ) <EOL> result = yield tmp_mr . find ( ) . sort ( [ ( '<STR_LIT>' , <NUM_LIT:1> ) ] ) . to_list ( length = <NUM_LIT:1000> ) <EOL> self . assertEqual ( expected_result , result ) <EOL> yield self . db . tmp_mr . drop ( ) <EOL> response = yield collection . map_reduce ( <EOL> map_fn , reduce_fn , '<STR_LIT>' , full_response = True ) <EOL> self . assertTrue ( <EOL> isinstance ( response , dict ) , <EOL> '<STR_LIT>' % response ) <EOL> self . assertEqual ( '<STR_LIT>' , response [ '<STR_LIT:result>' ] ) <EOL> result = yield tmp_mr . find ( ) . sort ( [ ( '<STR_LIT>' , <NUM_LIT:1> ) ] ) . to_list ( length = <NUM_LIT:1000> ) <EOL> self . assertEqual ( expected_result , result ) <EOL> yield self . db . tmp_mr . drop ( ) <EOL> result = yield collection . inline_map_reduce ( <EOL> map_fn , reduce_fn ) <EOL> result . sort ( key = lambda doc : doc [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( expected_result , result ) <EOL> @ gen_test <EOL> def test_indexes ( self ) : <EOL> test_collection = self . collection <EOL> idx_name = yield test_collection . create_index ( [ ( '<STR_LIT:foo>' , <NUM_LIT:1> ) ] ) <EOL> index_info = yield test_collection . index_information ( ) <EOL> self . assertEqual ( [ ( '<STR_LIT:foo>' , <NUM_LIT:1> ) ] , index_info [ idx_name ] [ '<STR_LIT:key>' ] ) <EOL> result = yield test_collection . ensure_index ( [ ( '<STR_LIT:foo>' , <NUM_LIT:1> ) ] ) <EOL> self . assertEqual ( None , result ) <EOL> result2 = yield test_collection . ensure_index ( [ ( '<STR_LIT:foo>' , <NUM_LIT:1> ) ] ) <EOL> self . assertEqual ( None , result2 ) <EOL> yield test_collection . ensure_index ( [ ( '<STR_LIT:bar>' , <NUM_LIT:1> ) ] ) <EOL> index_info = yield test_collection . index_information ( ) <EOL> self . assertTrue ( any ( [ <EOL> info [ '<STR_LIT:key>' ] == [ ( '<STR_LIT:bar>' , <NUM_LIT:1> ) ] for info in index_info . values ( ) ] ) ) <EOL> @ gen . coroutine <EOL> def _make_test_data ( self , n ) : <EOL> yield self . db . drop_collection ( "<STR_LIT:test>" ) <EOL> yield self . db . test . insert ( [ { '<STR_LIT>' : i } for i in range ( n ) ] ) <EOL> expected_sum = sum ( range ( n ) ) <EOL> raise gen . Return ( expected_sum ) <EOL> pipeline = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } <EOL> def assertAllDocs ( self , expected_sum , docs ) : <EOL> self . assertEqual ( expected_sum , sum ( doc [ '<STR_LIT>' ] for doc in docs ) ) <EOL> @ gen_test <EOL> def test_aggregate_callback ( self ) : <EOL> mongo_2_5_1 = yield at_least ( self . cx , ( <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:1> ) ) <EOL> future = Future ( ) <EOL> def cb ( result , error ) : <EOL> if error : <EOL> future . set_exception ( error ) <EOL> else : <EOL> future . set_result ( result ) <EOL> self . db . test . aggregate ( self . pipeline , cursor = False , callback = cb ) <EOL> yield future <EOL> if mongo_2_5_1 : <EOL> with self . assertRaises ( InvalidOperation ) : <EOL> self . db . test . aggregate ( self . pipeline , callback = cb ) <EOL> @ gen_test <EOL> def test_aggregation_cursor ( self ) : <EOL> mongo_2_5_1 = yield at_least ( self . cx , ( <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:1> ) ) <EOL> db = self . db <EOL> for collection_size in ( <NUM_LIT:10> , <NUM_LIT:1000> ) : <EOL> expected_sum = yield self . _make_test_data ( collection_size ) <EOL> reply = yield db . test . aggregate ( self . pipeline , cursor = False ) <EOL> self . assertAllDocs ( expected_sum , reply [ '<STR_LIT:result>' ] ) <EOL> if mongo_2_5_1 : <EOL> cursor = db . test . aggregate ( self . pipeline ) <EOL> docs = yield cursor . to_list ( collection_size ) <EOL> self . assertAllDocs ( expected_sum , docs ) <EOL> @ gen_test <EOL> def test_aggregation_cursor_to_list_callback ( self ) : <EOL> if not ( yield at_least ( self . cx , ( <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:1> ) ) ) : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> db = self . db <EOL> for collection_size in ( <NUM_LIT:10> , <NUM_LIT:1000> ) : <EOL> expected_sum = yield self . _make_test_data ( collection_size ) <EOL> cursor = db . test . aggregate ( self . pipeline ) <EOL> future = Future ( ) <EOL> def cb ( result , error ) : <EOL> if error : <EOL> future . set_exception ( error ) <EOL> else : <EOL> future . set_result ( result ) <EOL> cursor . to_list ( collection_size , callback = cb ) <EOL> docs = yield future <EOL> self . assertAllDocs ( expected_sum , docs ) <EOL> @ gen_test ( timeout = <NUM_LIT:30> ) <EOL> def test_parallel_scan ( self ) : <EOL> if not ( yield at_least ( self . cx , ( <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:5> ) ) ) : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> yield skip_if_mongos ( self . cx ) <EOL> collection = self . collection <EOL> yield collection . remove ( ) <EOL> yield collection . insert ( ( { '<STR_LIT>' : i } for i in range ( <NUM_LIT> ) ) , w = test . env . w ) <EOL> if test . env . is_replica_set : <EOL> client = self . motor_rsc ( ) <EOL> client . read_preference = ReadPreference . SECONDARY <EOL> collection = client . motor_test . test_collection <EOL> docs = [ ] <EOL> @ gen . coroutine <EOL> def f ( cursor ) : <EOL> self . assertTrue ( isinstance ( cursor , <EOL> motor . motor_tornado . MotorCommandCursor ) ) <EOL> while ( yield cursor . fetch_next ) : <EOL> docs . append ( cursor . next_object ( ) ) <EOL> cursors = yield collection . parallel_scan ( <NUM_LIT:3> ) <EOL> yield [ f ( cursor ) for cursor in cursors ] <EOL> self . assertEqual ( len ( docs ) , ( yield collection . count ( ) ) ) <EOL> def test_uuid_subtype ( self ) : <EOL> collection = self . db . test <EOL> self . assertEqual ( collection . uuid_subtype , OLD_UUID_SUBTYPE ) <EOL> collection . uuid_subtype = JAVA_LEGACY <EOL> self . assertEqual ( collection . uuid_subtype , JAVA_LEGACY ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
