def test_report_usage_to_coda_success(athlete, mock_call_json_responses, coda, settings): athletes = AthleteFactory.create_batch(5) for athlete in athletes: RouteFactory.create_batch(5, athlete=athlete) ActivityFactory.create_batch(5, athlete=athlete) response_mocks = [ { "url": coda["doc_url"], "response_file": "coda_doc.json", }, { "url": coda["table_url"], "response_file": "coda_table.json", }, { "url": coda["table_url"] + "/columns", "response_file": "coda_columns.json", }, { "url": coda["table_url"] + "/rows", "method": "post", "response_file": "coda_request.json", "status": 202, }, ] response = mock_call_json_responses(report_usage_to_coda, response_mocks) assert response == "Updated {} rows in Coda table at https://coda.io/d/{}".format( Athlete.objects.count(), settings.CODA_DOC_ID)
def test_import_strava_activity_streams_task_missing(athlete, mock_call_json_response): activity = ActivityFactory(athlete=athlete, streams=None) url = STRAVA_STREAMS_URL.format(str(activity.strava_id)) expected = "Streams not imported for activity {}".format(activity.strava_id) response = mock_call_json_response( import_strava_activity_streams_task, url, "missing_streams.json", strava_id=activity.strava_id, ) activity.refresh_from_db() assert activity.streams is None assert expected in response
def create_activities(): """ create Strava activities for existing activity types so that they don't get picked up for deletion """ for activity_type in ActivityType.objects.all(): ActivityFactory(activity_type=activity_type)
def test_train_activity_types(): count = ActivityType.objects.count() for activity_type in ActivityType.objects.all(): ActivityFactory(activity_type=activity_type) assert trained_message.format(count) in call_train_activity_types() for activity_type in ActivityType.objects.all(): assert not activity_type.model_score == 0.0
def test_process_events_deleted_activity(athlete, mock_call_not_found): activity = ActivityFactory() WebhookTransactionFactory( athlete_strava_id=athlete.strava_id, activity_strava_id=activity.strava_id ) call = process_strava_events url = STRAVA_API_BASE_URL + "activities/" + str(activity.strava_id) mock_call_not_found(call, url)
def test_import_strava_activities_streams_task(athlete, mocker): activities = ActivityFactory.create_batch(10, athlete=athlete, streams=None) activity_ids = [activity.strava_id for activity in activities] mock_task = mocker.patch( "homebytwo.routes.tasks.import_strava_activity_streams_task.run" ) import_strava_activities_streams_task(activity_ids) mock_task.assert_called
def test_import_strava_activity_streams_task_connection_error( athlete, mock_call_connection_error ): activity = ActivityFactory(athlete=athlete, streams=None) call = import_strava_activity_streams_task url = STRAVA_STREAMS_URL.format(activity.strava_id) response = mock_call_connection_error(call, url, strava_id=activity.strava_id) expected = "Streams for activity {} could not be retrieved from Strava".format( activity.strava_id ) assert expected in response
def test_report_usage_to_coda_failure(athlete, mock_call_json_responses, coda): RouteFactory(athlete=athlete) ActivityFactory(athlete=athlete) response_mocks = [ { "url": coda["doc_url"], "response_file": "coda_doc.json", }, { "url": coda["table_url"], "response_file": "coda_table.json", }, { "url": coda["table_url"] + "/columns", "response_file": "coda_missing_columns.json", }, ] with raises(ImproperlyConfigured): mock_call_json_responses(report_usage_to_coda, response_mocks)
def test_train_prediction_models_task(athlete): ActivityFactory.create_batch( 3, athlete=athlete, activity_type=ActivityTypeFactory(name="Run") ) ActivityFactory.create_batch( 2, athlete=athlete, activity_type=ActivityTypeFactory(name="Hike") ) ActivityFactory.create_batch( 1, athlete=athlete, activity_type=ActivityTypeFactory(name="Ride") ) response = train_prediction_models_task(athlete.id) expected = f"Prediction models trained for athlete: {athlete}." assert expected in response athlete_prediction_models = athlete.performances assert athlete_prediction_models.count() == 3 for model in athlete_prediction_models.all(): assert not model.model_score == 0.0
def test_train_activity_types_limit(): ActivityFactory(activity_type__name="Run") assert trained_message.format(1) in call_train_activity_types("Run", "--limit", 1) assert not ActivityType.objects.get(name="Run").model_score == 0.0
def test_train_activity_types_two_activities(): ActivityFactory(activity_type__name="Run") ActivityFactory(activity_type__name="Ride") assert trained_message.format(2) in call_train_activity_types("Run", "Ride") assert not ActivityType.objects.get(name="Run").model_score == 0.0 assert not ActivityType.objects.get(name="Ride").model_score == 0.0
def test_clean_up_activity_types_unsupported(create_activities): ActivityFactory(activity_type=ActivityTypeFactory(name=ActivityType.YOGA)) assert call_cleanup_activity_types() == delete_message.format(1, 1)
def test_update_activity_streams_from_strava_skip(athlete): activity = ActivityFactory(athlete=athlete, streams=None, skip_streams_import=True) expected = "Skipped importing streams for activity {}. ".format(activity.strava_id) response = import_strava_activity_streams_task(activity.strava_id) assert activity.streams is None assert expected in response