def _user_and_its_nps() -> Tuple[user_pb2.User, user_pb2.NPSSurveyResponse]: user = user_pb2.User() project = user.projects.add() project.original_self_diagnostic.CopyFrom( diagnostic_pb2.SelfDiagnostic( status=diagnostic_pb2.KNOWN_SELF_DIAGNOSTIC, category_id='stuck-market')) project.diagnostic.category_id = 'stuck-market' response = user.net_promoter_score_survey_response response.responded_at.FromDatetime(datetime.datetime(2018, 11, 20, 18, 29)) response.has_actions_idea = boolean_pb2.TRUE response.nps_self_diagnostic.CopyFrom( diagnostic_pb2.SelfDiagnostic( status=diagnostic_pb2.KNOWN_SELF_DIAGNOSTIC, category_id='missing-diploma')) response.score = 0 return user, response
def convert_answer_to_user(answer: dict[str, str]) -> user_pb2.User: """Convert one answer to Bayes survey to a user proto.""" frustrations: set[user_profile_pb2.Frustration.ValueType] = set() main_challenges: set[str] = set() for key, value in answer.items(): if key.startswith('challenge_') and value == 'Very challenging': frustration, main_challenge = _CHALLENGE_MAP[key.removeprefix( 'challenge_')] if frustration: frustrations.add(frustration) if main_challenge: main_challenges.add(main_challenge) return user_pb2.User( user_id=answer['id'], registered_at=_SURVEY_DATE, profile=user_profile_pb2.UserProfile( gender=_GENDER_MAP.get(answer['gender'], user_profile_pb2.UNKNOWN_GENDER), custom_gender=answer['gender'] if answer['gender'] not in _GENDER_MAP else '', highest_degree=_DEGREE_MAP[answer['educ']], year_of_birth=_SURVEY_YEAR - int(answer['age']), family_situation=_FAMILY_SITUATION_MAP[answer['familysituation']], frustrations=frustrations, ), projects=[ project_pb2.Project( city=None if not (state_name := answer['state']) else _find_state(state_name), target_job=(_find_job(answer['specificjobtitle']) if answer['specificjob'] == 'Yes' else None), training_fulfillment_estimate=( project_pb2.CURRENTLY_IN_TRAINING if answer['enrolled'] == 'Yes' else project_pb2.UNKNOWN_TRAINING_FULFILLMENT), has_clear_project=boolean_pb2.TRUE, original_self_diagnostic=diagnostic_pb2.SelfDiagnostic( category_id=''.join(sorted(main_challenges)) if len(main_challenges) == 1 else '', category_details=(','.join(sorted(main_challenges)) if len(main_challenges) > 1 else ''), status=(diagnostic_pb2.UNDEFINED_SELF_DIAGNOSTIC if not main_challenges else diagnostic_pb2.KNOWN_SELF_DIAGNOSTIC if len(main_challenges) == 1 else diagnostic_pb2.OTHER_SELF_DIAGNOSTIC), ), passionate_level=_PASSIONATE_LEVEL_MAP[ answer['specificjobmeaning']], ) ], )
def test_nps_bad_score(self) -> None: """Test NPS bad score.""" data = self._compute_user_data_for_nps( datetime.datetime(2018, 11, 20, 18, 29), 0, boolean_pb2.FALSE, diagnostic_pb2.SelfDiagnostic( status=diagnostic_pb2.UNKNOWN_SELF_DIAGNOSTIC)) self.assertEqual( { 'hasActionsIdea': 'FALSE', 'selfDiagnostic': { 'status': 'UNKNOWN_SELF_DIAGNOSTIC', }, 'score': 0, 'time': '2018-11-20T18:29:00Z', 'loveScore': -1, }, data.get('nps_response'))
def _compute_user_data_for_nps( self, responded_at: datetime.datetime, score: int, has_actions_idea: 'boolean_pb2.OptionalBool.V', self_diagnostic: diagnostic_pb2.SelfDiagnostic) -> dict[str, Any]: user = user_pb2.User() project = user.projects.add() project.original_self_diagnostic.CopyFrom( diagnostic_pb2.SelfDiagnostic( status=diagnostic_pb2.KNOWN_SELF_DIAGNOSTIC, category_id='stuck-market')) project.diagnostic.CopyFrom( diagnostic_pb2.Diagnostic(category_id='stuck-market')) response = user.net_promoter_score_survey_response response.responded_at.FromDatetime(responded_at) response.has_actions_idea = has_actions_idea response.nps_self_diagnostic.CopyFrom(self_diagnostic) response.score = score return self._compute_user_data(user)
def test_nps_crazy_score(self, mock_warning: mock.MagicMock) -> None: """NPS crazy score.""" data = self._compute_user_data_for_nps( datetime.datetime(2018, 11, 20, 18, 29), 200, boolean_pb2.FALSE, diagnostic_pb2.SelfDiagnostic( status=diagnostic_pb2.UNKNOWN_SELF_DIAGNOSTIC)) self.assertEqual( { 'hasActionsIdea': 'FALSE', 'selfDiagnostic': { 'status': 'UNKNOWN_SELF_DIAGNOSTIC', }, 'score': 200, 'time': '2018-11-20T18:29:00Z', }, data.get('nps_response')) mock_warning.assert_called_once_with('Cannot convert nps_score %s', 200)
def test_nps_self_diagnostic_category(self) -> None: """Test NPS self diagnostic with category Id.""" data = self._compute_user_data_for_nps( datetime.datetime(2018, 11, 20, 18, 29), 0, boolean_pb2.TRUE, diagnostic_pb2.SelfDiagnostic( status=diagnostic_pb2.KNOWN_SELF_DIAGNOSTIC, category_id='missing-diploma')) self.assertEqual( { 'hasActionsIdea': 'TRUE', 'selfDiagnostic': { 'categoryId': 'missing-diploma', 'hasChanged': 'changed_to_other', 'status': 'KNOWN_SELF_DIAGNOSTIC', }, 'score': 0, 'time': '2018-11-20T18:29:00Z', 'loveScore': -1, }, data.get('nps_response'))
def save_new_user(self, user: user_pb2.User, user_data: auth_pb2.AuthUserData) -> user_pb2.User: """Save a user with additional data.""" user.profile.locale = user_data.locale user.features_enabled.alpha = user_data.is_alpha user.features_enabled.exclude_from_analytics = user_data.is_alpha if _ENABLE_ACTION_PLAN or user_data.is_action_plan_enabled: user.features_enabled.action_plan = features_pb2.ACTIVE if user_data.HasField('ma_voie'): user.ma_voie.CopyFrom(user_data.ma_voie) _register_to_ma_voie(user_data.ma_voie) if user_data.HasField('original_self_diagnostic'): original_self_diagnostic = diagnostic_pb2.SelfDiagnostic() original_self_diagnostic.CopyFrom( user_data.original_self_diagnostic) user.projects.add( is_incomplete=True, original_self_diagnostic=original_self_diagnostic) return self._save_new_user(user)