def test_basic_queries_against_mongomock(self): disconnect_all() connect(host="mongomock://localhost:27017/mongoenginetest") class Person(Document): name = StringField() Person.drop_collection() assert Person.objects.count() == 0 bob = Person(name="Bob").save() john = Person(name="John").save() assert Person.objects.count() == 2 qs = Person.objects(name="Bob") assert qs.count() == 1 assert qs.first() == bob assert list(qs.as_pymongo()) == [{"_id": bob.id, "name": "Bob"}] pipeline = [{"$project": {"name": {"$toUpper": "$name"}}}] data = Person.objects.order_by("name").aggregate(pipeline) assert list(data) == [ {"_id": bob.id, "name": "BOB"}, {"_id": john.id, "name": "JOHN"}, ] Person.drop_collection() assert Person.objects.count() == 0
def mock_database(): disconnect_all() connect(host="mongomock://localhost", db="mongoenginetest", alias="student-db") connect(host="mongomock://localhost", db="mongoenginetest", alias="meeting-db") connect(host="mongomock://localhost", db="mongoenginetest", alias="admin-db")
def test_01(self): # assumes a localhost:27017 mongo instance mongo.disconnect_all() mongo.connect('edaac_test') # create project project = Project(name='test-project-flows') result = project.save() mongo.disconnect() self.assertIsNotNone(result)
def test_default_database_with_mocking(self): """Ensure that the default database is correctly set when using mongomock.""" disconnect_all() class SomeDocument(Document): pass conn = connect(host="mongomock://localhost:27017/mongoenginetest") some_document = SomeDocument() # database won't exist until we save a document some_document.save() assert conn.get_default_database().name == "mongoenginetest" assert conn.list_database_names()[0] == "mongoenginetest"
def test(self): # assumes a localhost:27017 mongo instance mongo.disconnect_all() mongo.connect('edaac_test') # create project project = Project(name='test-project', description='demonstrates the use of edaac models', technology=Technology(foundry='TestFoundry', process=45)) result = project.save() mongo.disconnect() self.assertIsNotNone(result)
def ensure_mongoclient_processlocal(): # this is to avoid mongoengine's MongoClient instances in subprocesses # resulting in "MongoClient opened before fork" warning # the source of the problem is that mongoengine stores MongoClients in # a module-global dictionary. here we simply clear that dictionary before # the connections are re-created in a forked process global mongo_pid if mongo_pid is None: # remember the main process that created the first MongoClient mongo_pid = os.getpid() elif mongo_pid != os.getpid(): # we're in a new process, disconnect # note this doesn't actually disconnect it just deletes the MongoClient from mongoengine import disconnect_all disconnect_all()
def test_default_database_with_mocking(self): """Ensure that the default database is correctly set when using mongomock. """ try: import mongomock except ImportError: raise SkipTest("you need mongomock installed to run this testcase") disconnect_all() class SomeDocument(Document): pass conn = connect(host="mongomock://localhost:27017/mongoenginetest") some_document = SomeDocument() # database won't exist until we save a document some_document.save() self.assertEqual(conn.get_default_database().name, "mongoenginetest") self.assertEqual(conn.list_database_names()[0], "mongoenginetest")
def test_disconnect_all(self): connections = mongoengine.connection._connections dbs = mongoengine.connection._dbs connection_settings = mongoengine.connection._connection_settings connect("mongoenginetest") connect("mongoenginetest2", alias="db1") class History(Document): pass class History1(Document): name = StringField() meta = {"db_alias": "db1"} History.drop_collection( ) # will trigger the caching of _collection attribute History.objects.first() History1.drop_collection() History1.objects.first() assert History._collection is not None assert History1._collection is not None assert len(connections) == 2 assert len(dbs) == 2 assert len(connection_settings) == 2 disconnect_all() assert History._collection is None assert History1._collection is None assert len(connections) == 0 assert len(dbs) == 0 assert len(connection_settings) == 0 with pytest.raises(ConnectionFailure): History.objects.first() with pytest.raises(ConnectionFailure): History1.objects.first()
def test_disconnect_all(self): connections = mongoengine.connection._connections dbs = mongoengine.connection._dbs connection_settings = mongoengine.connection._connection_settings connect('mongoenginetest') connect('mongoenginetest2', alias='db1') class History(Document): pass class History1(Document): name = StringField() meta = {'db_alias': 'db1'} History.drop_collection( ) # will trigger the caching of _collection attribute History.objects.first() History1.drop_collection() History1.objects.first() self.assertIsNotNone(History._collection) self.assertIsNotNone(History1._collection) self.assertEqual(len(connections), 2) self.assertEqual(len(dbs), 2) self.assertEqual(len(connection_settings), 2) disconnect_all() self.assertIsNone(History._collection) self.assertIsNone(History1._collection) self.assertEqual(len(connections), 0) self.assertEqual(len(dbs), 0) self.assertEqual(len(connection_settings), 0) with self.assertRaises(MongoEngineConnectionError): History.objects.first() with self.assertRaises(MongoEngineConnectionError): History1.objects.first()
def test_disconnect_all(self): connections = mongoengine.connection._connections dbs = mongoengine.connection._dbs connection_settings = mongoengine.connection._connection_settings connect("mongoenginetest") connect("mongoenginetest2", alias="db1") class History(Document): pass class History1(Document): name = StringField() meta = {"db_alias": "db1"} History.drop_collection( ) # will trigger the caching of _collection attribute History.objects.first() History1.drop_collection() History1.objects.first() self.assertIsNotNone(History._collection) self.assertIsNotNone(History1._collection) self.assertEqual(len(connections), 2) self.assertEqual(len(dbs), 2) self.assertEqual(len(connection_settings), 2) disconnect_all() self.assertIsNone(History._collection) self.assertIsNone(History1._collection) self.assertEqual(len(connections), 0) self.assertEqual(len(dbs), 0) self.assertEqual(len(connection_settings), 0) with self.assertRaises(ConnectionFailure): History.objects.first() with self.assertRaises(ConnectionFailure): History1.objects.first()
def test_disconnect_all(self): connections = mongoengine.connection._connections dbs = mongoengine.connection._dbs connection_settings = mongoengine.connection._connection_settings connect('mongoenginetest') connect('mongoenginetest2', alias='db1') class History(Document): pass class History1(Document): name = StringField() meta = {'db_alias': 'db1'} History.drop_collection() # will trigger the caching of _collection attribute History.objects.first() History1.drop_collection() History1.objects.first() self.assertIsNotNone(History._collection) self.assertIsNotNone(History1._collection) self.assertEqual(len(connections), 2) self.assertEqual(len(dbs), 2) self.assertEqual(len(connection_settings), 2) disconnect_all() self.assertIsNone(History._collection) self.assertIsNone(History1._collection) self.assertEqual(len(connections), 0) self.assertEqual(len(dbs), 0) self.assertEqual(len(connection_settings), 0) with self.assertRaises(MongoEngineConnectionError): History.objects.first() with self.assertRaises(MongoEngineConnectionError): History1.objects.first()
def app(): """ Create a new flask app instance for a testing environment with context https://flask.palletsprojects.com/en/1.1.x/appcontext/ """ app = create_app() mongoengine.disconnect_all() app.config["MONGODB_SETTINGS"]["db"] = "mymbafeeder_test" test_db = MongoEngine(app) app.config["TESTING"] = True # disable error catching app.config["WTF_CSRF_ENABLED"] = False app.config["DEBUG"] = False with app.app_context(): yield app mongoengine.connection.disconnect_all()
def test_02(self): # assumes a localhost:27017 mongo instance mongo.disconnect_all() mongo.connect('edaac_test') # retrieve project project = Project.objects(name='test-project-flows').first() self.assertIsNotNone(project) project.design = Design( name='test-design', rtl_files=['/path/to/rtl1.v', '/path/to/rtl2.v'], netlist_file='/path/to/netlist.v', sdc_file='/path/to/const.sdc') project.flows.append( Flow( flow_directory='/path/to/flow/directory', params={ 'param1': 'value1', 'param2': 'value2' }, stages=[ Stage( name='synth', tool=Tool(name='synth_tool', version='0.0.0'), machine='test-machine', collection_mode=DataCollectionMode.OFFLINE_FROM_LOGS. name, status=StageStatus.COMPLETED_SUCCESSFULLY.name, log_files=[ '/path/to/log1', '/path/to/drc', '/path/to/timing' ], metrics={} # should be extracted using edaac.parsers ), Stage( name='placement', tool=Tool(name='placement_tool', version='0.0.0'), machine='test-machine', collection_mode=DataCollectionMode.OFFLINE_FROM_LOGS. name, status=StageStatus.COMPLETED_SUCCESSFULLY.name, log_files=[ '/path/to/log1', '/path/to/drc', '/path/to/timing' ], metrics={} # should be extracted using edaac.parsers ), Stage( name='routing', tool=Tool(name='routing_tool', version='0.0.0'), machine='test-machine', collection_mode=DataCollectionMode.OFFLINE_FROM_LOGS. name, status=StageStatus.COMPLETED_SUCCESSFULLY.name, log_files=[ '/path/to/log1', '/path/to/drc', '/path/to/timing' ], metrics={} # should be extracted using edaac.parsers ) ], log_files=['/path/to/log1', '/path/to/log2'])) result = project.save() mongo.disconnect() self.assertIsNotNone(result)
def tearDownClass(cls): disconnect_all()
def disconnect_db_clients(): logging.info("[-] Disconnecting database clients") for client in db_clients: client.close() disconnect_all()
async def _on_shutdown(app: web.Application): mongoengine.disconnect_all() await ClSession.close()
def app(): app = create_app('testing') yield app disconnect_all()
def disconnect_db_clients(): logging.info("[-] Disconnecting default database client connection") db_default_client.close() disconnect_all()
def tearDownClass(cls): # Disconnect to make sure the app's database connection doesn't # clash with the test database connection disconnect_all()
ip_set2 = [ "27.34.0.0", "192.42.116.15", "192.82.198", "192.32.216", "14.224.0.0", "14.224.0.1", "2.32.0.0", "192.12.111" ] ip_set3 = [ "61.69.158.62", "3.128.0.0", "61.87.176.0", "220.240.228.236", "203.220.77.101", "24.232.0.0", "1.132.96.140", "1.132.96.120", "1.132.96.2" ] # initialising pymongo connection mongo_client = MongoClient(get_mongo_uri()) db = mongo_client['arc_dev'] # If we are in a production environment, we need these security values if env.get('FLASK_ENV', 'development') == 'production': disconnect_all() _db_name = str(env.get('MONGO_APP_DB')) _host = env.get('MONGO_HOST') _port = env.get('MONGO_PORT') _username = env.get('MONGO_APP_USER') _password = str(env.get('MONGO_APP_USER_PASSWORD')) mongo_uri = ( f'mongodb://{_username}:{_password}@{_host}:{_port}' f'/?authSource=admin' ) client = connect( db=_db_name, host=mongo_uri, alias='default', authentication_mechanism='SCRAM-SHA-1'
def setUpClass(cls): disconnect_all()
def global_disconnect(): disconnect_all()
def db_conn_kill(): mongoengine.disconnect_all()
def test_disconnect_all_silently_pass_if_no_connection_exist(self): disconnect_all()
def make_connection(index): mongoengine.disconnect_all() dbName = 'CRDT-DisCS_Node_Core_' + str(index) middlewareDBName = 'CRDT-DisCS_Node_Middleware_' + str(index) mongoengine.register_connection(alias='core', name=dbName) mongoengine.register_connection(alias='middle', name=middlewareDBName)