def test_runsched_fetch_google_scores(self, mock_verify, mock_client): """ Evaluate parameters passed to fetch_google_scores and set_and_verify_google_dates functions in _run_sched_for_model_no_set_dates """ last_date = date.today() - timedelta(days=4) end_date = date.today() - timedelta(days=3) with self.app.app_context(): google_date = GoogleDate(1, last_date) google_date.save() model_score = ModelScore() model_score.flu_model_id = 1 model_score.score_date = end_date model_score.score_value = 0.5 model_score.region = 'e' model_score.save() google_term = GoogleTerm() google_term.id = 1 google_term.term = 'Term 1' google_term.save() flu_model_google_term = FluModelGoogleTerm() flu_model_google_term.flu_model_id = 1 flu_model_google_term.google_term_id = 1 flu_model_google_term.save() patched_api_client = mock_client.return_value score_calculator.runsched([1], self.app) patched_api_client.fetch_google_scores.assert_called_with( ['Term 1'], last_date, end_date) mock_verify.assert_called_with(1, [end_date])
def test_no_missing_google_range(self): """ Scenario: Test run function to calculate model score for 2 consecutive dates Given requested Google dates already exist for such dates And the model score exist for one date Then the model score for the missing date is stored """ with self.app.app_context(): google_date_1 = GoogleDate(1, date(2018, 1, 1)) google_date_1.save() google_date_2 = GoogleDate(1, date(2018, 1, 2)) google_date_2.save() model_function = ModelFunction() model_function.flu_model_id = 1 model_function.function_name = 'matlab_function' model_function.average_window_size = 1 model_function.has_confidence_interval = False model_function.save() model_score = ModelScore() model_score.flu_model_id = 1 model_score.score_date = date(2018, 1, 1) model_score.score_value = 0.5 model_score.region = 'e' model_score.save() with patch.multiple('scheduler.score_calculator', build_calculator=DEFAULT) as mock_dict: matlab_client = mock_dict[ 'build_calculator'].return_value = Mock() matlab_client.calculate_model_score.return_value = 1.0 result_before = ModelScore.query.filter_by( flu_model_id=1).all() self.assertListEqual(result_before, [model_score]) score_calculator.run(1, date(2018, 1, 1), date(2018, 1, 2)) result_after = ModelScore.query.filter_by(flu_model_id=1).all() self.assertEqual(len(result_after), 2)
def test_get_flu_model_for_model_id_and_dates(self): """ Scenario: Get model data and scores for model_id and dates """ with self.app.app_context(): flu_model = FluModel() flu_model.id = 1 flu_model.is_displayed = True flu_model.is_public = True flu_model.calculation_parameters = '' flu_model.name = 'Model 1' flu_model.source_type = 'google' flu_model.save() model_function = ModelFunction() model_function.flu_model_id = 1 model_function.has_confidence_interval = True model_function.function_name = 'Function name' model_function.average_window_size = 7 model_function.save() for i in range(1, 32): model_score = ModelScore() model_score.flu_model_id = 1 model_score.score_date = date(2018, 1, i) model_score.score_value = i / (10 + i) model_score.region = 'e' model_score.save() result = get_flu_model_for_model_id_and_dates( 1, date(2018, 1, 2), date(2018, 1, 31)) self.assertEqual(result[0]['average_score'], 0.5723146873461765) self.assertEqual(result[0]['start_date'], date(2018, 1, 2)) self.assertEqual(result[0]['end_date'], date(2018, 1, 31)) self.assertEqual(result[0]['name'], 'Model 1') self.assertEqual(result[0]['has_confidence_interval'], True) self.assertEqual(result[0]['id'], 1) self.assertEqual(len(result[1]), 30)
def set_model_score(model_id: int, score_date: date, score_value: float): """ Persists a model score entity """ model_score = ModelScore() model_score.flu_model_id = model_id model_score.region = 'e' # Default for Google data model_score.score_date = score_date model_score.score_value = score_value model_score.save()
def set_model_score_confidence_interval( model_id: int, score_date: date, score_value: float, confidence_interval: Tuple[float, float] ): """ Persists a model score entity including its confidence interval """ model_score = ModelScore() model_score.flu_model_id = model_id model_score.region = 'e' # Default for Google data model_score.score_date = score_date model_score.score_value = score_value model_score.confidence_interval_lower = confidence_interval[0] model_score.confidence_interval_upper = confidence_interval[1] model_score.save()
def test_runsched(self): """ Scenario: Test runsched function to calculate model scores Given a last score_date of date.today() - timedelta(days=5) Then score_calculator.run is called with a start date of date.today() - timedelta(days=3) And end date of date.today() - timedelta(days=4) """ with self.app.app_context(): model_score = ModelScore() model_score.flu_model_id = 1 model_score.score_date = date.today() - timedelta(days=5) model_score.score_value = 0.5 model_score.region = 'e' model_score.save() with patch( 'scheduler.score_calculator._run_sched_for_model_no_set_dates' ) as patched_run: score_calculator.runsched([1], self.app) patched_run.assert_called_with(1)
def test_get_existing_model_dates(self): """ Scenario: Get list of existing dates from ModelScore Given a ModelScore.flu_model_id value of 1 exists And ModelScore.score_date with values '2018-01-02', '2018-01-03', '2018-01-05' When model_id = 1, start = '2018-01-01', end = '2018-01-02' Then the list contains one tuple for date '2018-01-02' """ with self.app.app_context(): for day in (2, 3, 5): model_score = ModelScore() model_score.flu_model_id = 1 model_score.score_date = date(2018, 1, day) model_score.score_value = 0.1 + day model_score.region = 'e' model_score.save() result = get_existing_model_dates(1, date(2018, 1, 1), date(2018, 1, 2)) self.assertListEqual(result, [(date(2018, 1, 2), )])
def test_runsched_get_google_batch(self): """ Evaluate parameters passed to get_google_batch function in _run_sched_for_model_no_set_dates """ last_date = date.today() - timedelta(days=4) end_date = date.today() - timedelta(days=3) with self.app.app_context(): google_date = GoogleDate(1, last_date) google_date.save() model_score = ModelScore() model_score.flu_model_id = 1 model_score.score_date = end_date model_score.score_value = 0.5 model_score.region = 'e' model_score.save() with patch('scheduler.score_calculator.get_google_batch' ) as patched_call: patched_call.return_value = [] score_calculator.runsched([1], self.app) patched_call.assert_called_with(1, [(end_date, end_date)])
def test_runsched_google_scores_get_date_ranges(self): """ Evaluate parameters passed to get_date_ranges_google_score function within _run_sched_for_model_no_set_dates """ last_date = date.today() - timedelta(days=4) with self.app.app_context(): google_date = GoogleDate(1, last_date) google_date.save() model_score = ModelScore() model_score.flu_model_id = 1 model_score.score_date = date.today() - timedelta(days=3) model_score.score_value = 0.5 model_score.region = 'e' model_score.save() with patch( 'scheduler.score_calculator.get_date_ranges_google_score' ) as patched_call: patched_call.return_value = (None, None) score_calculator.runsched([1], self.app) patched_call.assert_called_with( 1, last_date + timedelta(days=1), date.today() - timedelta(days=3))
def test_get_default_model_30days(self): """ Scenario: Get the last 30 days of data of the default flu model """ with self.app.app_context(): flu_model = FluModel() flu_model.id = 1 flu_model.is_displayed = True flu_model.is_public = True flu_model.calculation_parameters = '' flu_model.name = 'Model 1' flu_model.source_type = 'google' flu_model.save() default_model = DefaultFluModel() default_model.flu_model_id = 1 model_function = ModelFunction() model_function.flu_model_id = 1 model_function.has_confidence_interval = True model_function.function_name = 'Function name' model_function.average_window_size = 7 model_function.save() DB.session.add(default_model) DB.session.commit() for i in range(1, 32): model_score = ModelScore() model_score.flu_model_id = 1 model_score.score_date = date(2018, 1, i) model_score.score_value = i / (10 + i) model_score.region = 'e' model_score.save() result = get_default_flu_model_30days() self.assertEqual(result[0]['average_score'], 0.5723146873461765) self.assertEqual(result[0]['start_date'], date(2018, 1, 2)) self.assertEqual(result[0]['end_date'], date(2018, 1, 31)) self.assertEqual(result[0]['name'], 'Model 1') self.assertEqual(result[0]['id'], 1) self.assertEqual(len(result[1]), 30)
def test_get_days_missing_model(self): """ Scenario: Get the number of days with missing model scores Given a FluModel with an id value of 1 exists And ModelScores with score dates '2018-01-02', '2018-01-03', '2018-01-05' When start = '2018-01-01' and end = '2018-01-05' Then dates missing are '2018-01-01', '2018-01-04' """ with self.app.app_context(): for day in (2, 3, 5): model_score = ModelScore() model_score.flu_model_id = 1 model_score.score_date = date(2018, 1, day) model_score.score_value = 0.1 + day model_score.region = 'e' model_score.save() result = get_dates_missing_model_score(1, date(2018, 1, 1), date(2018, 1, 5)) expected = [date(2018, 1, 1), date(2018, 1, 4)] self.assertListEqual(result, expected) result = get_dates_missing_model_score(1, date(2018, 1, 6), date(2018, 1, 7)) expected = [date(2018, 1, 6), date(2018, 1, 7)] self.assertListEqual(result, expected)