def setUp(self): super(ScoringModelCheckTestCase, self).setUp() # Setup a DB with no problems. self._db = mongomock.MongoClient().test self._db.advice_modules.insert_one({'triggerScoringModel': 'advice-volunteer'}) self._db.application_tips.insert_one({'filters': ['for-women']}) self._db.associations.insert_one({'filters': ['for-long-search(7)']}) self._db.contact_lead.insert_one({'filters': ['for-single-parent']}) self._db.diagnostic_sentences.insert_one({'filters': ['for-women']}) self._db.events.insert_one({'filters': ['for-reorientation']}) self._db.jobboards.insert_one({'filters': ['for-young(25)']}) self._db.tip_templates.insert_one({'filters': ['for-complex-application']})
def test_find(): db = mongomock.MongoClient().image_database.images duplicate_finder.add(['tests/images/deeply/nested'], db) dups = duplicate_finder.find(db, match_time=False) assert len(dups) == 1 dup = dups[0] assert dup['total'] == 2 time_dups = duplicate_finder.find(db, match_time=True) assert dups == time_dups
def test_should_500_if_add_survey_raise_exception( mock_add: MagicMock, mock_get_collection: MagicMock, sut: AddSurveyController): mock_add.side_effect = Exception("Error on matrix") mock_get_collection.return_value = mongomock.MongoClient().db.collection http_request = HttpRequest(body=dict( question="any_question", answers=[dict(image="any_image", answer="any_answer")], )) response = sut.handle(http_request) assert response.status_code == 500 assert response.body["message"] == "Internal server error"
def test_creating_db_and_collections_insert_one(self, server_name, port, test_data): db_names = [f"test_db_{i}" for i in range(4)] collection_names = [f"collection_{i}" for i in range(3)] client = mongomock.MongoClient(server_name, port) for db in db_names: for coll in collection_names: insert_one(client, test_data, db_name=db, collection_name=coll) assert client.list_database_names() == db_names for db in db_names: assert getattr(client, db).list_collection_names() == collection_names
def test_score_policy_deny(client_fixture, snapshot): client_fixture["cpf"] = "12345678900" test_db = mongomock.MongoClient()["Test"] db = Database(test_db) db.start_process(client_fixture) score_policy_status, metadata = ScorePolicy(client_fixture["_id"], client_fixture["cpf"], testing=test_db).check() snapshot.assert_match(score_policy_status.status) snapshot.assert_match(score_policy_status.loan)
def test_find_fuzzy(): db = mongomock.MongoClient().image_database.images duplicate_finder.add(['tests/images/'], db) dups = duplicate_finder.find_threshold(db, 0) assert len(dups) == 2 assert dups[0]['total'] == 2 assert dups[1]['total'] == 5 dups = duplicate_finder.find_threshold(db, 10) assert len(dups) == 1 assert dups[0]['total'] == 8
def setUp(self): super(BulkOperationsTest, self).setUp() if self.test_with_pymongo: self.client = pymongo.MongoClient(host=os.environ.get('TEST_MONGO_HOST', 'localhost')) else: self.client = mongomock.MongoClient() self.db = self.client['somedb'] self.db.collection.drop() for _i in 'abx': self.db.collection.create_index( _i, unique=False, name='idx' + _i, sparse=True, background=True) self.bulk_op = self.db.collection.initialize_ordered_bulk_op()
def test_MongoDB_get_open_task_returns_none_if_no_task_open(self): # given mongo_database = MongoDb() mongo_database._database = mongomock.MongoClient()["newspaper"] mongo_database._database["tr"].insert_one( {"text": "This article is scraped"}) # when task = mongo_database.get_open_task("tr") # then assert task is None
def test_update_invalid(self): body_data = { 'name': 'b', 'duration': 4, 'uploaded_time': 'b', 'date': 'wef' } collection_name = 'collection_name' client = mongomock.MongoClient() persistence_gateway = PersistenceGateway(client) with self.assertRaises(ItemNotFound): persistence_gateway.update(collection_name, 3232, body_data)
def test_insert_many(self): collection = mongomock.MongoClient().db.collection objs = [ {'test_insert_many_1': 'test_value'}, {'test_insert_many_2': 'test_value'} ] self.hook.insert_many(collection, objs) result_objs = collection.find() result_objs = [result for result in result_objs] self.assertEqual(len(result_objs), 2)
def test_replace_many_with_upsert(self): collection = mongomock.MongoClient().db.collection obj1 = {'_id': '1', 'field': 'test_value_1'} obj2 = {'_id': '2', 'field': 'test_value_2'} self.hook.replace_many(collection, [obj1, obj2], upsert=True) result_obj = collection.find_one(filter='1') self.assertEqual('test_value_1', result_obj['field']) result_obj = collection.find_one(filter='2') self.assertEqual('test_value_2', result_obj['field'])
def setUp(self): super(_BaseTestCase, self).setUp() self.database = mongomock.MongoClient().test self.database.action_templates.insert_one({ '_id': 'rec1CWahSiEtlwEHW', 'goal': 'Reorientation !', }) self.user = user_pb2.User( features_enabled=user_pb2.Features(advisor=user_pb2.ACTIVE), profile=user_pb2.UserProfile(name='Margaux', gender=user_pb2.FEMININE)) advisor.clear_cache()
def test_update_one(self): collection = mongomock.MongoClient().db.collection obj = {'_id': '1', 'field': 0} collection.insert_one(obj) filter_doc = obj update_doc = {'$inc': {'field': 123}} self.hook.update_one(collection, filter_doc, update_doc) result_obj = collection.find_one(filter='1') self.assertEqual(123, result_obj['field'])
def test_find_many(self): collection = mongomock.MongoClient().db.collection objs = [{ 'test_find_many_1': 'test_value' }, { 'test_find_many_2': 'test_value' }] collection.insert(objs) result_objs = self.hook.find(collection, {}, find_one=False) self.assertGreater(len(list(result_objs)), 1)
def __create_new_connection(self, db_username: str, db_password: str, user_type: UserTypeEnum, db_hostname: str, db_port: int, db_name: str, is_tls: bool): uri = self.get_mongo_db_uri(db_username, db_password, user_type, db_hostname, db_port, db_name, is_tls) logger().debug(f'Connect to MongoDB using URI [{uri}]') if DbClient.is_mock_db_enabled(): self.mongo_db_client = mongomock.MongoClient(uri) else: self.mongo_db_client = MongoClient(uri) self.verify_connected_to_db() self.__db = self.mongo_db_client.get_database()
def setUp(self) -> None: super().setUp() self.database = mongo.NoPiiMongoDatabase(mongomock.MongoClient().test) i18n.cache.clear() self.user = user_pb2.User( features_enabled=features_pb2.Features( advisor=features_pb2.ACTIVE, workbench=features_pb2.ACTIVE), profile=user_profile_pb2.UserProfile( name='Margaux', gender=user_profile_pb2.FEMININE, email='*****@*****.**', locale='fr@tu')) proto.CachedCollection.update_cache_version()
def connect(self, force_reset: bool = False, ssh_tunnel: SSHTunnelForwarder = None ): # lgtm[py/conflicting-attributes] """ Connect to the source data """ if ssh_tunnel is not None: warnings.warn( f"SSH Tunnel not needed for {self.__class__.__name__}") if not self._collection or force_reset: self._collection = mongomock.MongoClient().db[self.name]
def test_existence(): # Mock collection and test data mock_collection = mongomock.MongoClient().db.collection name = "test" url = "url1" send_scraper_to_db(mock_collection, name, url, test=True) assert mock_collection.count_documents({}) == 1 docs = list_scrapers_from_db(mock_collection) assert docs[0]["name"] == "test" assert docs[0]["_id"] == "url1"
def test_should_raise_if_email_validator_raise_exception( mock_is_valid: MagicMock, mock_get_collection: MagicMock, sut: EmailValidation ): mock_is_valid.side_effect = Exception("Error on matrix") mock_get_collection.return_value = mongomock.MongoClient().db.collection input = { "email": "*****@*****.**", } with pytest.raises(Exception) as excinfo: assert sut.validate(input) assert type(excinfo.value) is Exception
def test_main_function(self, pymongo_mock, mock_log_info): """Basic usage.""" import_status.IMPORTERS = { 'missing-in-db-importer': import_status.Importer( name="missing in db", command='', is_imported=True, proto_type=None, key=None), 'in-both': import_status.Importer( name="in both", command='', is_imported=True, proto_type=None, key=None), 'in-both-with-meta': import_status.Importer( name="in both with meta", command='', is_imported=True, proto_type=job_pb2.JobGroup, key=None), 'in-both-not-needed': import_status.Importer( name="in both not needed", command='', is_imported=False, proto_type=None, key=None) } client = mongomock.MongoClient('mongodb://test_url/test_db') pymongo_mock.MongoClient.return_value = client mongo_db = client.get_database('test_db') mongo_db.create_collection('missing-in-importers') mongo_db.create_collection('in-both') mongo_db.create_collection('in-both-with-meta') two_days_ago = datetime.datetime.now() - datetime.timedelta(days=2) mongo_db.meta.insert_one({ '_id': 'in-both-with-meta', 'updated_at': two_days_ago, }) mongo_db.create_collection('in-both-not-needed') import_status.main('mongodb://test_url/test_db') mock_log_info.assert_any_call( '%s collection%s without importers:', _AnyColorText('1'), ' is') mock_log_info.assert_any_call( '%s collection%s not imported yet:', _AnyColorText('1'), ' is') mock_log_info.assert_any_call( 'Status report on imported collections (%d):', 3) mock_log_info.assert_any_call( '\t%s - %s - %s', _AnyColorText('in-both-not-needed'), _AnyColorText('in both not needed'), termcolor.colored('No import needed', 'green')) mock_log_info.assert_any_call( '\t%s - %s - %s', _AnyColorText('in-both'), _AnyColorText('in both'), termcolor.colored('Metainformation missing', 'red')) mock_log_info.assert_any_call( '\t%s - %s - %s', _AnyColorText('in-both-with-meta'), _AnyColorText('in both with meta (JobGroup)'), _AnyColorText('last import: %s' % two_days_ago))
def test_get_collection_returns_a_collection(self): mongots_client = mongots.MongoTSClient( mongo_client=mongomock.MongoClient()) mongots_database = mongots_client.get_database('TestDb') mongots_collection = mongots_database.get_collection('temperature') self.assertIsNotNone(mongots_collection) self.assertIsInstance( mongots_collection._collection, mongomock.Collection, ) self.assertEqual(mongots_collection._collection.name, 'temperature')
def setUp(self) -> None: """Set up mock environment.""" super().setUp() # Simulate a clean load of the modules. self.app = server.app.test_client() self.app_context = typing.cast( Callable[[], typing.ContextManager[None]], server.app.app_context) proto.clear_mongo_fetcher_cache() self._db = mongomock.MongoClient().get_database('test') server.app.config['DATABASE'] = self._db server._DB = self._db # pylint: disable=protected-access self._user_db = mongomock.MongoClient().get_database('user_test') server.app.config['USER_DATABASE'] = self._user_db server._USER_DB = self._user_db # pylint: disable=protected-access self._eval_db = mongomock.MongoClient().get_database('eval_test') server._EVAL_DB = self._eval_db # pylint: disable=protected-access server.app.config['EVAL_DATABASE'] = self._eval_db server.jobs._JOB_GROUPS_INFO.reset_cache() # pylint: disable=protected-access self._db.action_templates.insert_many([{ '_id': f'a{i:d}', 'actionTemplateId': f'a{i:d}', } for i in range(30)]) self._db.local_diagnosis.insert_one({ '_id': '38:M1403', 'bmo': { 'percentDifficult': 5, 'percentSeasonal': 10, }, 'salary': { 'shortText': '17 400 - 17 400', 'medianSalary': 17400.0, 'unit': 'ANNUAL_GROSS_SALARY', 'maxSalary': 17400.0, 'minSalary': 17400.0 } }) self._logging = mock.patch(server.__name__ + '.logging', spec=True) self._logging.start()
def test__equality(self): self.assertEqual(mongomock.MongoClient('mongodb://localhost:27017/'), mongomock.MongoClient('mongodb://localhost:27017/')) self.assertEqual(mongomock.MongoClient('mongodb://localhost:27017/'), mongomock.MongoClient('localhost')) self.assertNotEqual(mongomock.MongoClient('/var/socket/mongo.sock'), mongomock.MongoClient('localhost'))
def test_update_users_client_metrics(self, mock_requests): """Test update_users_client_metrics.""" mock_db = mongomock.MongoClient().test mock_db.user.insert_one({ '_id': mongomock.ObjectId('7ed900dbfbebdee97f9e2332'), 'registeredAt': '2017-11-17T10:57:12Z', }) patcher = mock.patch(sync_amplitude.__name__ + '._DB', new=mock_db) patcher.start() self.addCleanup(patcher.stop) mock_requests.get( 'https://amplitude.com/api/2/usersearch?user=7ed900dbfbebdee97f9e2332', json={'matches': [{ 'amplitude_id': 42 }]}) mock_requests.get( 'https://amplitude.com/api/2/useractivity?user=42', json={ 'events': [ { 'event_time': '2017-10-24 10:41:08.396000' }, # Event out of order, this one is actually the first of the session. { 'event_time': '2017-10-24 10:41:00.412000' }, # Last event of the session: 25 min, 5.1 sec later. { 'event_time': '2017-10-24 11:06:05.512000' }, # Event really later: next session. { 'event_time': '2017-10-24 13:06:05' }, ] }) sync_amplitude.main([ '--registered-from', '2017-11-14', '--registered-to', '2017-11-18', '--disable-sentry', '--no-dry-run' ]) user = user_pb2.User() proto.parse_from_mongo(mock_db.user.find_one({}), user) self.assertEqual('42', user.client_metrics.amplitude_id) self.assertEqual(25 * 60 + 5, user.client_metrics.first_session_duration_seconds)
def setUp(self): super(MailingTestCase, self).setUp() mail_advice.DRY_RUN = False self._db = mongomock.MongoClient().database self._db.advice_modules.insert_one({ 'adviceId': 'advice-to-send', 'emailFacts': ['Inbox 0 is awesome'], 'emailSubject': 'Advice Email Subject', 'emailSuggestionSentence': 'check your inbox', 'emailTitle': 'Advice Email Title', }) self._now = datetime.datetime(2016, 11, 24, 10, 0, 0) advisor.clear_cache()
def test_remove(): db = mongomock.MongoClient().image_database.images duplicate_finder.add(['tests'], db) assert db.count() > 0 duplicate_finder.remove(['test'], db) assert db.count() > 0 duplicate_finder.remove(['tests'], db) assert db.count() == 0 duplicate_finder.remove(['tests'], db) assert db.count() == 0
def test_display_command(self, pymongo_mock, mock_log_info): """Display the command to import a missing collection.""" client = mongomock.MongoClient('mongodb://test_url/test_db') pymongo_mock.MongoClient.return_value = client import_status.main('mongodb://test_url/test_db') mock_log_info.assert_any_call('%s collection%s not imported yet:', _AnyColorText('1'), ' is') mock_log_info.assert_any_call( 'To import "%s" in "%s", run:\n%s', 'Collection name', 'collection_id', 'my-long-command \\\n' ' --mongo_url "mongodb://test_url/test_db" ' '--mongo_collection "collection_id"\n')
def test_add(self): body_data = { 'name': 'b', 'duration': 4, 'uploaded_time': 'b', 'date': 'wef' } collection_name = 'collection_name' client = mongomock.MongoClient() persistence_gateway = PersistenceGateway(client) persistence_gateway.add(collection_name, 3232, body_data) assert client.mydatabase[collection_name].find_one({'_id': 3232}) == body_data
def test_run_importer(self, pymongo_mock, mock_subprocess_run): """Run the command to import a collection.""" client = mongomock.MongoClient('mongodb://test_url/test_db') pymongo_mock.MongoClient.return_value = client import_status.main(['mongodb://test_url/test_db', '--run', 'collection_id']) mock_subprocess_run.assert_called_once_with([ 'python', '/work/bob_emploi/data_analysis/importer/my-script-name.py', '--custom_importer_flag', 'value for custom flag', '--mongo_url', 'mongodb://test_url/test_db', '--mongo_collection', 'collection_id', ])
def test_add_duplicate(self): body_data = { 'name': 'b', 'duration': 4, 'uploaded_time': 'b', 'date': 'wef' } collection_name = 'collection_name' client = mongomock.MongoClient() persistence_gateway = PersistenceGateway(client) persistence_gateway.add(collection_name, 3232, body_data) with self.assertRaises(UnableToInsertDueToDuplicateKeyError): persistence_gateway.add(collection_name, 3232, body_data)