async def test_versioned_api(self): # Versioned API examples # Use connect=False to reduce overhead as client is not used to run # any operations. AsyncIOMotorClient = lambda _, server_api: self.asyncio_client( server_api=server_api, connect=False) uri = None # Start Versioned API Example 1 from pymongo.server_api import ServerApi client = AsyncIOMotorClient(uri, server_api=ServerApi("1")) # End Versioned API Example 1 # Start Versioned API Example 2 client = AsyncIOMotorClient(uri, server_api=ServerApi("1", strict=True)) # End Versioned API Example 2 # Start Versioned API Example 3 client = AsyncIOMotorClient(uri, server_api=ServerApi("1", strict=False)) # End Versioned API Example 3 # Start Versioned API Example 4 client = AsyncIOMotorClient(uri, server_api=ServerApi( "1", deprecation_errors=True))
def test_server_api_validation(self): with self.assertRaises(ValueError): ServerApi('2') with self.assertRaises(TypeError): ServerApi('1', strict='not-a-bool') with self.assertRaises(TypeError): ServerApi('1', deprecation_errors='not-a-bool') with self.assertRaises(TypeError): MongoClient(server_api='not-a-ServerApi')
def __init__(self): """Create a client and grab essential information from the server.""" self.connection_attempts = [] self.connected = False self.w = None self.nodes = set() self.replica_set_name = None self.cmd_line = None self.server_status = None self.version = Version(-1) # Needs to be comparable with Version self.auth_enabled = False self.test_commands_enabled = False self.server_parameters = None self.is_mongos = False self.mongoses = [] self.is_rs = False self.has_ipv6 = False self.tls = False self.ssl_certfile = False self.server_is_resolvable = is_server_resolvable() self.default_client_options = {} self.sessions_enabled = False self.client = None self.conn_lock = threading.Lock() self.is_data_lake = False if COMPRESSORS: self.default_client_options["compressors"] = COMPRESSORS if MONGODB_API_VERSION: server_api = ServerApi(MONGODB_API_VERSION) self.default_client_options["server_api"] = server_api
def get_mongoclient_by_envs() -> MongoClient: """ Use env MONGODB_DATABASE_URI to create and return a MongoClient instance """ database_uri = os.getenv("MONGODB_DATABASE_URI", "mongodb://localhost:27017/") logging.info(f"Connecting to MongoDB: {database_uri}") client: MongoClient = MongoClient(database_uri, tz_aware=True, server_api=ServerApi("1")) return client
def test_command_options(self): listener = OvertCommandListener() client = rs_or_single_client(server_api=ServerApi('1'), event_listeners=[listener]) self.addCleanup(client.close) coll = client.test.test coll.insert_many([{} for _ in range(100)]) self.addCleanup(coll.delete_many, {}) list(coll.find(batch_size=25)) client.admin.command('ping') self.assertServerApiInAllCommands(listener.results['started'])
def checkmongodbconnection(): try: c = MongoClient(MONGO_URI, server_api=ServerApi('1'), serverSelectionTimeoutMS=5000) c.admin.command('ismaster') time.sleep(2) c.close() return True except: print('\nCould not connect to MongoDB.\n\n') return False
def test_versioned_api(self): # Versioned API examples MongoClient = lambda _, server_api: rs_client(server_api=server_api, connect=False) uri = None # Start Versioned API Example 1 from pymongo.server_api import ServerApi client = MongoClient(uri, server_api=ServerApi("1")) # End Versioned API Example 1 # Start Versioned API Example 2 client = MongoClient(uri, server_api=ServerApi("1", strict=True)) # End Versioned API Example 2 # Start Versioned API Example 3 client = MongoClient(uri, server_api=ServerApi("1", strict=False)) # End Versioned API Example 3 # Start Versioned API Example 4 client = MongoClient(uri, server_api=ServerApi("1", deprecation_errors=True))
def test_command_options_txn(self): listener = OvertCommandListener() client = rs_or_single_client(server_api=ServerApi('1'), event_listeners=[listener]) self.addCleanup(client.close) coll = client.test.test coll.insert_many([{} for _ in range(100)]) self.addCleanup(coll.delete_many, {}) listener.reset() with client.start_session() as s, s.start_transaction(): coll.insert_many([{} for _ in range(100)], session=s) list(coll.find(batch_size=25, session=s)) client.test.command('find', 'test', session=s) self.assertServerApiInAllCommands(listener.results['started'])
def worker(workerthread, numofsymbols): try: #Create an initial value for each security last_value = [] for i in range(0, numofsymbols): last_value.append(round(random.uniform(1, 100), 2)) #Wait until MongoDB Server is online and ready for data while True: print('Checking MongoDB Connection') if checkmongodbconnection() == False: print('Problem connecting to MongoDB, sleeping 10 seconds') time.sleep(10) else: break print('Successfully connected to MongoDB') c = MongoClient(MONGO_URI, server_api=ServerApi("1", strict=False)) db = c.get_database(name=args.database) txtime = dt.now() txtime_end = txtime + timedelta(minutes=args.duration) if args.drop: print('\nDropping collection ' + args.collection + '\n') db.drop_collection(args.collection) if args.timeseries: collection = db.create_collection(name=args.collection, timeseries={ "timeField": "tx_time", "granularity": "seconds" }) print('Create collection result=' + collection.full_name) print( 'Data Generation Summary:\n{:<12} {:<12}\n{:<12} {:<12}\n{:<12} {:<12}' .format('# symbols', args.symbols, 'Database', args.database, 'Collection', args.collection)) print('\n{:<8} {:<50}'.format('Symbol', 'Company Name')) for x in range(len(company_name)): print('{:<8} {:<50}'.format(company_symbol[x], company_name[x])) print('\n{:<12} {:<12}'.format('Start time', txtime.strftime('%Y-%m-%d %H:%M:%S'))) if args.duration: print('{:<12} {:<12}\n'.format( 'End time', txtime_end.strftime('%Y-%m-%d %H:%M:%S'))) else: print('No end time - run until user stop (control-Z)\n\n') counter = 0 bContinue = True while bContinue: for i in range(0, numofsymbols): #Get the last value of this particular security x = getvalue(last_value[i]) last_value[i] = x try: if args.AsString: result = db[args.collection].insert_one({ 'company_symbol': company_symbol[i], 'company_name': company_name[i], 'price': x, 'tx_time': txtime.strftime('%Y-%m-%dT%H:%M:%SZ') }) else: result = db[args.collection].insert_one({ 'company_symbol': company_symbol[i], 'company_name': company_name[i], 'price': x, 'tx_time': txtime }) counter = counter + 1 if counter % 100 == 0: if args.duration > 0: print('Generated ' + str(counter) + ' samples ({0:.0%})'.format( counter / (numofsymbols * args.duration * 60))) else: print('Generated ' + str(counter)) if args.duration > 0: if txtime > txtime_end: bContinue = False continue except Exception as e: print("error: " + str(e)) txtime += timedelta(seconds=1) duration = txtime - dt.now() print('\nFinished - ' + str(duration).split('.')[0]) except: print('Unexpected error:', sys.exc_info()[0]) raise
from pymongo.mongo_client import MongoClient from pymongo.server_api import ServerApi # Replace <connection string> with your MongoDB deployment's connection string. conn_str = "<connection string>" # Set the version of the Versioned API on the client. client = pymongo.MongoClient(conn_str, server_api=ServerApi('1'), serverSelectionTimeoutMS=5000)
async def test_versioned_api_migration(self): client = self.asyncio_client(server_api=ServerApi("1", strict=True)) await client.db.sales.drop() # Start Versioned API Example 5 def strptime(s): return datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%SZ") await client.db.sales.insert_many([ { "_id": 1, "item": "abc", "price": 10, "quantity": 2, "date": strptime("2021-01-01T08:00:00Z"), }, { "_id": 2, "item": "jkl", "price": 20, "quantity": 1, "date": strptime("2021-02-03T09:00:00Z"), }, { "_id": 3, "item": "xyz", "price": 5, "quantity": 5, "date": strptime("2021-02-03T09:05:00Z"), }, { "_id": 4, "item": "abc", "price": 10, "quantity": 10, "date": strptime("2021-02-15T08:00:00Z"), }, { "_id": 5, "item": "xyz", "price": 5, "quantity": 10, "date": strptime("2021-02-15T09:05:00Z"), }, { "_id": 6, "item": "xyz", "price": 5, "quantity": 5, "date": strptime("2021-02-15T12:05:10Z"), }, { "_id": 7, "item": "xyz", "price": 5, "quantity": 10, "date": strptime("2021-02-15T14:12:12Z"), }, { "_id": 8, "item": "abc", "price": 10, "quantity": 5, "date": strptime("2021-03-16T20:20:13Z"), }, ]) # End Versioned API Example 5 with self.assertRaisesRegex( OperationFailure, "Provided apiStrict:true, but the command count is not in API Version 1", ): await client.db.command("count", "sales", query={}) # Start Versioned API Example 6 # pymongo.errors.OperationFailure: Provided apiStrict:true, but the command count is not in API Version 1, full error: {'ok': 0.0, 'errmsg': 'Provided apiStrict:true, but the command count is not in API Version 1', 'code': 323, 'codeName': 'APIStrictError'} # End Versioned API Example 6 # Start Versioned API Example 7 await client.db.sales.count_documents({})
def test_versioned_api_migration(self): # SERVER-58785 if (client_context.is_topology_type(["sharded"]) and not client_context.version.at_least(5, 0, 2)): self.skipTest("This test needs MongoDB 5.0.2 or newer") client = rs_client(server_api=ServerApi("1", strict=True)) client.db.sales.drop() # Start Versioned API Example 5 def strptime(s): return datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%SZ") client.db.sales.insert_many([{ "_id": 1, "item": "abc", "price": 10, "quantity": 2, "date": strptime("2021-01-01T08:00:00Z") }, { "_id": 2, "item": "jkl", "price": 20, "quantity": 1, "date": strptime("2021-02-03T09:00:00Z") }, { "_id": 3, "item": "xyz", "price": 5, "quantity": 5, "date": strptime("2021-02-03T09:05:00Z") }, { "_id": 4, "item": "abc", "price": 10, "quantity": 10, "date": strptime("2021-02-15T08:00:00Z") }, { "_id": 5, "item": "xyz", "price": 5, "quantity": 10, "date": strptime("2021-02-15T09:05:00Z") }, { "_id": 6, "item": "xyz", "price": 5, "quantity": 5, "date": strptime("2021-02-15T12:05:10Z") }, { "_id": 7, "item": "xyz", "price": 5, "quantity": 10, "date": strptime("2021-02-15T14:12:12Z") }, { "_id": 8, "item": "abc", "price": 10, "quantity": 5, "date": strptime("2021-03-16T20:20:13Z") }]) # End Versioned API Example 5 with self.assertRaisesRegex( OperationFailure, "Provided apiStrict:true, but the command " "count is not in API Version 1"): client.db.command('count', 'sales', query={}) # Start Versioned API Example 6 # pymongo.errors.OperationFailure: Provided apiStrict:true, but the command count is not in API Version 1, full error: {'ok': 0.0, 'errmsg': 'Provided apiStrict:true, but the command count is not in API Version 1', 'code': 323, 'codeName': 'APIStrictError'} # End Versioned API Example 6 # Start Versioned API Example 7 client.db.sales.count_documents({})
def _create_entity(self, entity_spec): if len(entity_spec) != 1: self._test_class.fail( "Entity spec %s did not contain exactly one top-level key" % (entity_spec, )) entity_type, spec = next(iteritems(entity_spec)) if entity_type == 'client': kwargs = {} observe_events = spec.get('observeEvents', []) ignore_commands = spec.get('ignoreCommandMonitoringEvents', []) if len(observe_events) or len(ignore_commands): ignore_commands = [cmd.lower() for cmd in ignore_commands] listener = EventListenerUtil(observe_events, ignore_commands) self._listeners[spec['id']] = listener kwargs['event_listeners'] = [listener] if client_context.is_mongos and spec.get('useMultipleMongoses'): kwargs['h'] = client_context.mongos_seeds() kwargs.update(spec.get('uriOptions', {})) server_api = spec.get('serverApi') if server_api: kwargs['server_api'] = ServerApi( server_api['version'], strict=server_api.get('strict'), deprecation_errors=server_api.get('deprecationErrors')) client = rs_or_single_client(**kwargs) self[spec['id']] = client self._test_class.addCleanup(client.close) return elif entity_type == 'database': client = self[spec['client']] if not isinstance(client, MongoClient): self._test_class.fail( 'Expected entity %s to be of type MongoClient, got %s' % (spec['client'], type(client))) options = parse_collection_or_database_options( spec.get('databaseOptions', {})) self[spec['id']] = client.get_database(spec['databaseName'], **options) return elif entity_type == 'collection': database = self[spec['database']] if not isinstance(database, Database): self._test_class.fail( 'Expected entity %s to be of type Database, got %s' % (spec['database'], type(database))) options = parse_collection_or_database_options( spec.get('collectionOptions', {})) self[spec['id']] = database.get_collection(spec['collectionName'], **options) return elif entity_type == 'session': client = self[spec['client']] if not isinstance(client, MongoClient): self._test_class.fail( 'Expected entity %s to be of type MongoClient, got %s' % (spec['client'], type(client))) opts = camel_to_snake_args(spec.get('sessionOptions', {})) if 'default_transaction_options' in opts: txn_opts = parse_spec_options( opts['default_transaction_options']) txn_opts = TransactionOptions(**txn_opts) opts = copy.deepcopy(opts) opts['default_transaction_options'] = txn_opts session = client.start_session(**dict(opts)) self[spec['id']] = session self._session_lsids[spec['id']] = copy.deepcopy(session.session_id) self._test_class.addCleanup(session.end_session) return elif entity_type == 'bucket': # TODO: implement the 'bucket' entity type self._test_class.skipTest( 'GridFS is not currently supported (PYTHON-2459)') self._test_class.fail('Unable to create entity of unknown type %s' % (entity_type, ))
def test_server_api_strict(self): api = ServerApi('1', strict=True, deprecation_errors=True) self.assertEqual(api.version, '1') self.assertTrue(api.strict) self.assertTrue(api.deprecation_errors)
def test_server_api_explicit_false(self): api = ServerApi('1', strict=False, deprecation_errors=False) self.assertEqual(api.version, '1') self.assertFalse(api.strict) self.assertFalse(api.deprecation_errors)
def test_server_api_defaults(self): api = ServerApi(ServerApiVersion.V1) self.assertEqual(api.version, '1') self.assertIsNone(api.strict) self.assertIsNone(api.deprecation_errors)