def test_get_scores_resolution(self): flumodel = FluModel() flumodel.name = 'Test Model' flumodel.is_public = True flumodel.is_displayed = True flumodel.source_type = 'google' flumodel.calculation_parameters = 'matlab_model,1' dates = [date(2018, 6, d) for d in range(1, 30)] datapoints = [] for d in dates: entry = ModelScore() entry.region = 'e' entry.score_date = d entry.calculation_timestamp = datetime.now() entry.score_value = '1.23' datapoints.append(entry) flumodel.model_scores = datapoints model_function = ModelFunction() model_function.id = 1 model_function.function_name = 'matlab_model' model_function.average_window_size = 1 model_function.flu_model_id = 1 model_function.has_confidence_interval = True with self.app.app_context(): flumodel.save() model_function.save() response = self.client().get( '/scores?id=1&startDate=2018-05-30&endDate=2018-06-30&resolution=week' ) result = response.get_json() self.assertEqual(len(result['model_data'][0]['data_points']), 4)
def test_get_rool(self): flumodel = FluModel() flumodel.name = 'Test Model' flumodel.is_public = True flumodel.is_displayed = True flumodel.source_type = 'google' flumodel.calculation_parameters = 'matlab_model,1' datapoint = ModelScore() datapoint.region = 'e' datapoint.score_date = date(2018, 6, 29) datapoint.calculation_timestamp = datetime.now() datapoint.score_value = 1.23 datapoint.confidence_interval_lower = 0.81 datapoint.confidence_interval_upper = 1.65 flumodel.model_scores = [datapoint] model_function = ModelFunction() model_function.id = 1 model_function.function_name = 'matlab_model' model_function.average_window_size = 1 model_function.flu_model_id = 1 model_function.has_confidence_interval = True default_model = DefaultFluModel() default_model.flu_model_id = 1 with self.app.app_context(): flumodel.save() model_function.save() DB.session.add(default_model) DB.session.commit() response = self.client().get('/') result = response.get_json() expected = { 'model_list': [{ 'id': 1, 'name': 'Test Model' }], 'rate_thresholds': {}, 'model_data': [{ 'id': 1, 'name': 'Test Model', 'average_score': 1.23, 'start_date': '2018-06-29', 'end_date': '2018-06-29', 'has_confidence_interval': True, 'data_points': [{ 'score_date': '2018-06-29', 'score_value': 1.23, 'confidence_interval_lower': 0.81, 'confidence_interval_upper': 1.65 }] }] } self.assertEqual(result, expected) self.assertEqual(response.status_code, 200)
def test_get_scores_for_model(self): flumodel = FluModel() flumodel.name = 'Test Model' flumodel.is_public = True flumodel.is_displayed = True flumodel.source_type = 'google' flumodel.calculation_parameters = 'matlab_model,1' dates = [date(2018, 3, 20), date(2018, 5, 20), date(2018, 6, 20)] datapoints = [] for d in dates: entry = ModelScore() entry.region = 'e' entry.score_date = d entry.calculation_timestamp = datetime.now() entry.score_value = '1.23' datapoints.append(entry) flumodel.model_scores = datapoints model_function = ModelFunction() model_function.id = 1 model_function.function_name = 'matlab_model' model_function.average_window_size = 1 model_function.flu_model_id = 1 model_function.has_confidence_interval = False with self.app.app_context(): flumodel.save() model_function.save() response = self.client().get( '/scores?id=1&startDate=2018-05-30&endDate=2018-06-30') result = response.get_json() expected = { 'model_data': [{ 'id': 1, 'name': 'Test Model', 'average_score': 1.23, 'has_confidence_interval': False, 'start_date': '2018-06-20', 'end_date': '2018-06-20', 'data_points': [{ 'score_date': '2018-06-20', 'score_value': 1.23, 'confidence_interval_lower': None, 'confidence_interval_upper': None }] }] } self.assertEqual(result, expected) self.assertEqual(response.status_code, 200) response = self.client().get( '/scores?id=1&startDate=2018-07-30&endDate=2018-06-30') self.assertEqual(response.status_code, 400)
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_twitter_enabled(self): """ Scenario: Test run function behaviour when the environment variable TWITTER_ENABLED is present """ with self.app.app_context(): google_date = GoogleDate(1, date.today() - timedelta(days=1)) google_date.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() with patch.multiple('scheduler.score_calculator', build_calculator=DEFAULT, build_message_client=DEFAULT) as mock_dict: matlab_client = mock_dict[ 'build_calculator'].return_value = Mock() matlab_client.calculate_model_score.return_value = 1.0 message_client = mock_dict[ 'build_message_client'].return_value = Mock() message_client.publish_model_score.return_value = None environ['TWITTER_ENABLED'] = 'True' environ['TWITTER_MODEL_ID'] = '1' with self.assertLogs(level='INFO') as logContext: score_calculator.run(1, google_date.score_date, google_date.score_date) self.assertListEqual(logContext.output, [ 'INFO:root:Google scores have already been collected for this time period', 'INFO:root:Latest ModelScore value sent to message queue' ]) result_after = ModelScore.query.filter_by(flu_model_id=1).all() self.assertEqual(len(result_after), 1)
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 test_get_scores_smoothing(self): flumodel = FluModel() flumodel.name = 'Test Model' flumodel.is_public = True flumodel.is_displayed = True flumodel.source_type = 'google' flumodel.calculation_parameters = 'matlab_model,1' dates = [date(2018, 6, d) for d in range(1, 30)] datapoints = [] for d in dates: entry = ModelScore() entry.region = 'e' entry.score_date = d entry.calculation_timestamp = datetime.now() entry.score_value = 1 + 10 / d.day datapoints.append(entry) flumodel.model_scores = datapoints model_function = ModelFunction() model_function.id = 1 model_function.function_name = 'matlab_model' model_function.average_window_size = 1 model_function.flu_model_id = 1 model_function.has_confidence_interval = True with self.app.app_context(): flumodel.save() model_function.save() response = self.client().get( '/scores?id=1&startDate=2018-06-10&endDate=2018-06-10&smoothing=3') result = response.get_json() expected = { 'model_data': [{ 'id': 1, 'name': 'Test Model', 'data_points': [{ 'score_value': 2.0067340067340065, 'score_date': '2018-06-10', 'confidence_interval_lower': 0.0, # should be None 'confidence_interval_upper': 0.0 # should be None }], 'has_confidence_interval': True, 'start_date': '2018-06-10', 'end_date': '2018-06-10', 'average_score': 2.0 }] } self.assertEqual(result, expected)
def test_get_model_function(self): """ Scenario: Get the model configuration parameters (name of Matlab function and window for calculation of moving averages """ with self.app.app_context(): model_function = ModelFunction() model_function.id = 1 model_function.flu_model_id = 1 model_function.function_name = 'matlab_function' model_function.average_window_size = 1 model_function.has_confidence_interval = True model_function.save() result = get_model_function(1) self.assertIsInstance(result, ModelFunction)
def test_csv(self): flumodel = FluModel() flumodel.id = 1 flumodel.name = 'Test Model' flumodel.is_public = True flumodel.is_displayed = True flumodel.source_type = 'google' flumodel.calculation_parameters = 'matlab_model,1' dates = [date(2018, 6, d) for d in range(1, 30)] datapoints = [] for d in dates: entry = ModelScore() entry.region = 'e' entry.score_date = d entry.calculation_timestamp = datetime.now() entry.score_value = 1 + 10 / d.day entry.confidence_interval_lower = 0 entry.confidence_interval_upper = 2 datapoints.append(entry) flumodel.model_scores = datapoints model_function = ModelFunction() model_function.id = 1 model_function.function_name = 'matlab_model' model_function.average_window_size = 1 model_function.flu_model_id = 1 model_function.has_confidence_interval = True with self.app.app_context(): flumodel.save() model_function.save() response = self.client().get( '/csv?id=1&tartDate=2018-06-01&endDate=2018-06-07&resolution=week' ) expected_header = r'attachment; filename=RawScores-\d{13}\.csv' expected_data = b'score_date,score_Test Model\r\n2018-06-03,4.333333333333334\r\n' self.assertEquals(response.data, expected_data) self.assertRegexpMatches(response.headers['Content-Disposition'], expected_header)
def test_api_returning_zero_values(self): """ Scenario: Test run function to attempt calculating model score for a specific date Given the Google API returns zero for the term temperature Then the retrieval of the scores and calculation of model scores is skipped And the requested data is not available on Google API And a warning log message is printed """ with self.app.app_context(): google_date_1 = GoogleDate(1, date.today() - timedelta(days=1)) google_date_1.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() 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() with patch.multiple( GoogleApiClient, fetch_google_scores=DEFAULT, is_returning_non_zero_for_temperature=DEFAULT ) as patch_dict, patch( 'scheduler.score_calculator.get_dates_missing_model_score' ) as model_score_ctx: with self.assertLogs(level='WARNING') as logContext: patch_dict['fetch_google_scores'].return_value = [] patch_dict[ 'is_returning_non_zero_for_temperature'].return_value = False model_score_ctx.return_value = False score_calculator.run(1, date.today(), date.today()) self.assertListEqual(logContext.output, [ 'WARNING:root:Google API has returned zero for the term temperature. Not fetching scores' ])
def test_failed_retrieval_of_google_scores(self): """ Scenario: Test run function to attempt calculating model score for a specific date Given the requested Google date is missing And the requested data is not available on Google API Then run function returns without calculating the score And an error log message is printed """ with self.app.app_context(): google_date_1 = GoogleDate(1, date.today() - timedelta(days=1)) google_date_1.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() 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() with patch.multiple(GoogleApiClient, fetch_google_scores=DEFAULT, is_returning_non_zero_for_temperature=DEFAULT ) as patch_dict: with self.assertLogs( level='ERROR') as logContext, self.assertRaises( RuntimeError) as errorCtx: patch_dict['fetch_google_scores'].return_value = [] patch_dict[ 'is_returning_non_zero_for_temperature'].return_value = True score_calculator.run(1, date.today(), date.today()) self.assertListEqual( logContext.output, ['ERROR:root:Retrieval of Google scores failed']) self.assertTrue( 'Retry call to Google API' in str(errorCtx.exception))
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_plink_smoothing(self): with self.app.app_context(): for idx in [1, 2]: flumodel = FluModel() flumodel.name = 'Test Model %d' % idx flumodel.is_public = True flumodel.is_displayed = True flumodel.source_type = 'google' flumodel.calculation_parameters = 'matlab_model,1' dates = [date(2018, 6, d) for d in range(1, 30)] datapoints = [] for d in dates: entry = ModelScore() entry.region = 'e' entry.score_date = d entry.calculation_timestamp = datetime.now() entry.score_value = 1.23 / d.day entry.confidence_interval_lower = 0.81 entry.confidence_interval_upper = 1.65 datapoints.append(entry) flumodel.model_scores = datapoints model_function = ModelFunction() model_function.id = idx model_function.function_name = 'matlab_model' model_function.average_window_size = 1 model_function.flu_model_id = idx model_function.has_confidence_interval = True flumodel.save() model_function.save() default_model = DefaultFluModel() default_model.flu_model_id = 1 rate_thresholds = RateThresholdSet() rate_thresholds.low_value = 0.1 rate_thresholds.medium_value = 0.2 rate_thresholds.high_value = 0.3 rate_thresholds.very_high_value = 0.4 rate_thresholds.valid_from = date(2010, 1, 1) rate_thresholds.save() DB.session.add(default_model) DB.session.commit() response = self.client().get( '/plink?id=1&id=2&startDate=2018-06-01&endDate=2018-06-05&smoothing=3' ) self.assertListEqual(response.get_json()['model_list'], [{ 'id': 1, 'name': 'Test Model 1' }, { 'id': 2, 'name': 'Test Model 2' }]) self.assertEqual( len(response.get_json()['model_data'][0]['data_points']), 5) self.assertEqual( len(response.get_json()['model_data'][1]['data_points']), 5) expected = { 'low_value': { 'label': 'Low epidemic rate', 'value': 0.1 }, 'medium_value': { 'label': 'Medium epidemic rate', 'value': 0.2 }, 'high_value': { 'label': 'High epidemic rate', 'value': 0.3 }, 'very_high_value': { 'label': 'Very high epidemic rate', 'value': 0.4 } } self.assertDictEqual(response.get_json()['rate_thresholds'], expected) list_expected = [0.252833, 0.321167, 0.444167, 0.751667, 0.922500] list_result = [ round(s['score_value'], 6) for s in response.get_json()['model_data'][0]['data_points'] ] self.assertListEqual(list_result, list_expected)
def test_get_twlink_current_link(self): flumodel = FluModel() flumodel.id = 1 flumodel.name = 'Test Model' flumodel.is_public = True flumodel.is_displayed = True flumodel.source_type = 'google' flumodel.calculation_parameters = 'matlab_model,1' flumodel.model_region_id = '1-e' dates = [date(2018, 6, d) for d in range(1, 30)] datapoints = [] for d in dates: entry = ModelScore() entry.region = 'e' entry.score_date = d entry.calculation_timestamp = datetime.now() entry.score_value = 1.23 entry.confidence_interval_lower = 0.81 entry.confidence_interval_upper = 1.65 datapoints.append(entry) flumodel.model_scores = datapoints model_function = ModelFunction() model_function.id = 1 model_function.function_name = 'matlab_model' model_function.average_window_size = 1 model_function.flu_model_id = 1 model_function.has_confidence_interval = True default_model = DefaultFluModel() default_model.flu_model_id = 1 rate_thresholds = RateThresholdSet() rate_thresholds.low_value = 0.1 rate_thresholds.medium_value = 0.2 rate_thresholds.high_value = 0.3 rate_thresholds.very_high_value = 0.4 rate_thresholds.valid_from = date(2010, 1, 1) with self.app.app_context(): flumodel.save() model_function.save() rate_thresholds.save() DB.session.add(default_model) DB.session.commit() response = self.client().get( '/twlink?id=1&start=2018-06-01&end=2018-06-10') self.assertListEqual(response.get_json()['model_list'], [{ 'id': 1, 'name': 'Test Model' }]) self.assertEqual( len(response.get_json()['model_data'][0]['data_points']), 10) expected = { 'low_value': { 'label': 'Low epidemic rate', 'value': 0.1 }, 'medium_value': { 'label': 'Medium epidemic rate', 'value': 0.2 }, 'high_value': { 'label': 'High epidemic rate', 'value': 0.3 }, 'very_high_value': { 'label': 'Very high epidemic rate', 'value': 0.4 } } self.assertDictEqual(response.get_json()['rate_thresholds'], expected)