-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit tests for analytics API models and API end points (#2432)
* Added unit tests for analytics API models and API end points
- Loading branch information
1 parent
3d20650
commit 6c5808c
Showing
16 changed files
with
838 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Copyright © 2019 Province of British Columbia | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Tests to verify the survey result API end-point. | ||
Test-Suite to ensure that the survey result endpoint is working as expected. | ||
""" | ||
import json | ||
from http import HTTPStatus | ||
from faker import Faker | ||
from unittest.mock import patch | ||
|
||
from analytics_api.services.survey_result import SurveyResultService | ||
from analytics_api.utils.util import ContentType | ||
from tests.utilities.factory_scenarios import TestJwtClaims, TestRequestTypeOptionInfo | ||
from tests.utilities.factory_utils import ( | ||
factory_available_response_option_model, factory_engagement_model, factory_request_type_option_model, | ||
factory_response_type_option_model, factory_survey_model) | ||
|
||
fake = Faker() | ||
|
||
|
||
def test_get_survey_result_internal(client, mocker, session): # pylint:disable=unused-argument | ||
"""Assert that survey result can be fetched.""" | ||
engagement = factory_engagement_model() | ||
survey = factory_survey_model(engagement) | ||
available_response_option = factory_available_response_option_model(survey) | ||
factory_request_type_option_model(survey, available_response_option.request_key, | ||
TestRequestTypeOptionInfo.request_type_option3) | ||
factory_response_type_option_model(survey, available_response_option.request_key, | ||
available_response_option.value) | ||
|
||
# Mock the return value of get_survey_result method | ||
mocked_survey_result = {"data": "mocked_survey_result"} | ||
mocker.patch.object(SurveyResultService, 'get_survey_result', return_value=mocked_survey_result) | ||
|
||
token_str = json.dumps(TestJwtClaims.staff_admin_role.value) | ||
|
||
with patch("analytics_api.resources.survey_result._jwt.has_one_of_roles", return_value=True), \ | ||
patch("analytics_api.resources.survey_result.SurveyResultInternal.get") as mock_get: | ||
|
||
# Mock the get method of SurveyResultInternal to directly return the mocked data | ||
def mock_get(engagement_id): | ||
return mocked_survey_result | ||
|
||
mocker.patch("analytics_api.resources.survey_result.SurveyResultInternal.get", side_effect=mock_get) | ||
|
||
# Call the endpoint directly without involving authentication decorators | ||
rv = client.get(f'/api/surveyresult/{engagement.source_engagement_id}/internal', | ||
headers={'Authorization': 'Bearer ' + token_str}) | ||
|
||
# Check if the response status code is HTTPStatus.OK | ||
assert rv.status_code == HTTPStatus.OK | ||
|
||
|
||
def test_get_survey_result_public(client, session): # pylint:disable=unused-argument | ||
"""Assert that survey result can be fetched.""" | ||
engagement = factory_engagement_model() | ||
survey = factory_survey_model(engagement) | ||
available_response_option = factory_available_response_option_model(survey) | ||
factory_request_type_option_model(survey, available_response_option.request_key, | ||
TestRequestTypeOptionInfo.request_type_option2) | ||
factory_response_type_option_model(survey, available_response_option.request_key, | ||
available_response_option.value) | ||
|
||
rv = client.get(f'/api/surveyresult/{engagement.source_engagement_id}/public', | ||
content_type=ContentType.JSON.value) | ||
assert rv.status_code == HTTPStatus.OK | ||
|
||
rv = client.get(f'/api/surveyresult/{fake.random_int(min=11, max=99)}/public', | ||
content_type=ContentType.JSON.value) | ||
assert rv.status_code == HTTPStatus.NOT_FOUND | ||
|
||
with patch.object(SurveyResultService, 'get_survey_result', | ||
side_effect=KeyError('Test error')): | ||
rv = client.get(f'/api/surveyresult/{engagement.source_engagement_id}/public', | ||
content_type=ContentType.JSON.value) | ||
assert rv.status_code == HTTPStatus.INTERNAL_SERVER_ERROR | ||
|
||
with patch.object(SurveyResultService, 'get_survey_result', | ||
side_effect=ValueError('Test error')): | ||
rv = client.get(f'/api/surveyresult/{engagement.source_engagement_id}/public', | ||
content_type=ContentType.JSON.value) | ||
assert rv.status_code == HTTPStatus.INTERNAL_SERVER_ERROR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
analytics-api/tests/unit/models/test_available_response_option.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright © 2019 Province of British Columbia | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Tests to verify the User response detail API end-point. | ||
Test-Suite to ensure that the user response detail endpoint is working as expected. | ||
""" | ||
from http import HTTPStatus | ||
from analytics_api.utils.util import ContentType | ||
from datetime import datetime, timedelta | ||
from tests.utilities.factory_utils import ( | ||
factory_engagement_model, factory_user_response_detail_model, factory_survey_model) | ||
|
||
|
||
def test_get_user_responses_by_month(client, session): # pylint:disable=unused-argument | ||
"""Assert that user response detail by month can be fetched.""" | ||
eng = factory_engagement_model() | ||
survey_data = factory_survey_model(eng) | ||
user_response_detail = factory_user_response_detail_model(survey_data.id) | ||
from_date = (datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d') | ||
to_date = (datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d') | ||
rv = client.get(f'/api/responses/month/{user_response_detail.engagement_id}\ | ||
?&from_date={from_date}&to_date={to_date}', content_type=ContentType.JSON.value) | ||
assert rv.json[0].get('responses') == 1 | ||
assert rv.status_code == HTTPStatus.OK |
36 changes: 36 additions & 0 deletions
36
analytics-api/tests/unit/models/test_email_verification.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright © 2019 Province of British Columbia | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Tests for the Engagement model. | ||
Test suite to ensure that the Engagement model routines are working as expected. | ||
""" | ||
|
||
from analytics_api.models import EmailVerification as EmailVerificationModel | ||
from tests.utilities.factory_utils import factory_email_verification_model | ||
|
||
|
||
def test_get_email_verification_data_by_id(session): | ||
"""Assert that an email verification data can be created and fetched.""" | ||
email_verification = factory_email_verification_model() | ||
assert email_verification.id is not None | ||
retrieved_email_verification = EmailVerificationModel.find_by_id(email_verification.id) | ||
assert email_verification.participant_id == retrieved_email_verification.participant_id | ||
|
||
|
||
def test_get_email_verification_count(session): | ||
"""Test get count for email verification.""" | ||
email_verification = factory_email_verification_model() | ||
assert email_verification.id is not None | ||
email_verification_count = EmailVerificationModel.get_email_verification_count(email_verification.engagement_id) | ||
assert email_verification_count == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.