def test_db_change(self) -> None: """Test that collection cache is busted when the database is changed.""" first_db = mongo.NoPiiMongoDatabase( mongomock.MongoClient('localhost').get_database('test')) first_db.basic.insert_one( { '_id': 'A124', 'multipleWords': 'A124', 'name': 'Original content, hopefully cached' }, ) self._collection.get_collection(first_db) second_db = mongo.NoPiiMongoDatabase( mongomock.MongoClient('localhost').get_database('test')) second_db.basic.insert_one( { '_id': 'A124', 'multipleWords': 'A124', 'name': 'New content in the DB' }, ) second_collection = self._collection.get_collection(second_db) cache_busted_a124 = second_collection.get('A124') assert cache_busted_a124 self.assertEqual('New content in the DB', cache_busted_a124.name)
def setUp(self) -> None: super().setUp() mongo.cache.clear() self.database = mongo.NoPiiMongoDatabase(mongomock.MongoClient().test) self.database.diagnostic_main_challenges.insert_many([ { 'categoryId': 'women', 'strategiesIntroduction': 'Voici vos strats', 'filters': ['for-women'], 'order': 1, }, { 'categoryId': 'bravo', 'strategiesIntroduction': 'Voici vos stratégies', 'order': 2, }, ]) self.database.action_templates.insert_one({ '_id': 'rec1CWahSiEtlwEHW', 'goal': 'Reorientation !', }) 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)) proto.CachedCollection.update_cache_version()
def setUp(self) -> None: """Set up mock environment.""" super().setUp() self._db = mongo.NoPiiMongoDatabase( mongomock.MongoClient().get_database('test')) self._collection: proto.MongoCachedCollection[test_pb2.Simple] = \ proto.MongoCachedCollection(test_pb2.Simple, 'basic')
def setUp(self) -> None: super().setUp() cache.clear() patcher = mongomock.patch(['mydata.com', 'myprivatedata.com']) patcher.start() self.addCleanup(patcher.stop) env_patcher = mock.patch.dict( os.environ, values={ 'MONGO_URL': 'mongodb://mydata.com/test', 'USERS_MONGO_URL': 'mongodb://myprivatedata.com/user_test', }) env_patcher.start() self.addCleanup(env_patcher.stop) self.addCleanup(mongo.cache.clear) self.database = mongo.NoPiiMongoDatabase( pymongo.MongoClient('mongodb://mydata.com/test').test) self._user_database = pymongo.MongoClient( 'mongodb://myprivatedata.com/user_test').user_test # TODO(cyrille): Use this to mock time whenever necessary. self.now: Optional[datetime.datetime] = None # Default values that shouldn't be expected, and should be overridden when necessary. user_name = 'Patrick' user_email = '*****@*****.**' user_user_id = f'{random.randrange(16**24):024x}' user_registration_date = datetime.datetime.now() - datetime.timedelta( days=90) # TODO(cyrille): Replace these values by personas. self.user = user_pb2.User(user_id=user_user_id) self.user.registered_at.FromDatetime(user_registration_date) self.user.profile.gender = user_profile_pb2.MASCULINE self.user.profile.name = user_name self.user.profile.email = user_email self.user.profile.year_of_birth = 1990 self.project = self.user.projects.add() self.project.project_id = '0' self.project.target_job.masculine_name = 'Coiffeur' self.project.target_job.feminine_name = 'Coiffeuse' self.project.target_job.name = 'Coiffeur / Coiffeuse' self.project.target_job.code_ogr = '123456' self.project.target_job.job_group.rome_id = 'B1234' self.project.target_job.job_group.name = 'Coiffure' self.project.network_estimate = 1 self.project.city.city_id = '69003' self.project.city.name = 'Lyon' self.project.city.departement_id = '69' self.project.city.departement_prefix = 'dans le ' self.project.city.departement_name = 'Rhône' self.project.city.region_id = '84' self.project.city.region_name = 'Auvergne-Rhône-Alpes' self._variables: dict[str, Any] = {} self._from: dict[str, Any] = {}
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 setUp(self) -> None: super().setUp() self.database = mongo.NoPiiMongoDatabase(mongomock.MongoClient().test) self.database.job_group_info.insert_one({ '_id': 'A1234', 'romeId': 'A1234', 'name': 'Main Job Group', 'jobs': [ { 'codeOgr': '56789', 'name': 'A job name', }, ], })
def test_param_overrides_project(self) -> None: """Explicit database is more relevant than scoring project's.""" project = diagnostic.scoring.ScoringProject(self.user.projects[0], self.user, self._db) self._db.diagnostic_main_challenges.insert_one({ 'categoryId': 'every-one', 'filters': ['constant(3)'], 'order': 1, }) _db = mongo.NoPiiMongoDatabase(mongomock.MongoClient().test) _db.diagnostic_main_challenges.insert_one({ 'categoryId': 'every-one-in-custom-db', 'filters': ['constant(2)'], 'order': 2, }) main_challenge = diagnostic.find_main_challenge(project, database=_db) assert main_challenge self.assertEqual('every-one-in-custom-db', main_challenge.category_id)
def _create_mock_scoring_project() -> scoring.ScoringProject: """Create a mock scoring_project.""" _db = mongo.NoPiiMongoDatabase(mongomock.MongoClient().test) _db.job_group_info.insert_one({ '_id': 'A1234', 'requirements': { 'diplomas': [{ 'name': 'Bac+2' }], }, }) user = user_pb2.User() project = project_pb2.Project() project.target_job.job_group.rome_id = 'A1234' project.created_at.FromDatetime(datetime.datetime.now()) project.job_search_started_at.FromDatetime(datetime.datetime.now() - datetime.timedelta(days=30)) return scoring.ScoringProject(project, user, database=_db)
def setUp(self) -> None: super().setUp() geo.i18n.cache.clear() self._db = mongo.NoPiiMongoDatabase(mongomock.MongoClient().test) self._db.departements.insert_many([ { '_id': '13', 'name': 'Bouches-du-Rhône', 'prefix': 'dans les ', }, { '_id': '19', 'name': 'Corrèze', 'prefix': 'en ', }, { '_id': '31', 'name': 'Haute-Garonne', 'prefix': 'en ', }, { '_id': '58', 'name': 'Nièvre', 'prefix': 'dans la ', }, { '_id': '75', 'name': 'Paris', 'prefix': 'à ', }, { '_id': '971', 'name': 'Guadeloupe', 'prefix': 'en ', }, { '_id': '974', 'name': 'La Réunion', 'prefix': 'à la ', }, ])
def setUp(self) -> None: super().setUp() i18n.cache.clear() # Pre-populate project's fields that are usualldy set. Individual tests # should not count on those values. self.project = project_pb2.Project() self.project.target_job.name = 'Boulanger / Boulangère' self.project.target_job.masculine_name = 'Boulanger' self.project.target_job.feminine_name = 'Boulangère' self.project.target_job.job_group.rome_id = 'Z9007' self.project.city.city_id = '69123' self.project.city.departement_id = '69' self.project.city.region_id = '84' self.project.city.postcodes = '69001-69002-69003-69004' self.project.city.name = 'Lyon' self.database = mongo.NoPiiMongoDatabase(mongomock.MongoClient().test) self.database.regions.insert_one({ '_id': '84', 'prefix': 'en ', 'name': 'Auvergne-Rhône-Alpes', }) self.scoring_project = scoring.ScoringProject(self.project, user_pb2.User(), self.database)
def setUp(self) -> None: super().setUp() self._db = mongo.NoPiiMongoDatabase(mongomock.MongoClient().test) proto.cache.clear()