diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fc64a1a3..78ddf94b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,7 +2,7 @@ # For more information see: https://docs.github.com/en/enterprise-cloud@latest/actions/automating-builds-and-tests/building-and-testing-java-with-gradle -name: Java CI with Gradle +name: CI on: pull_request: @@ -10,7 +10,7 @@ on: - main jobs: - run-unit-tests: + run-efsity-unit-tests: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -39,3 +39,37 @@ jobs: - name: Run spotless check run: ./gradlew spotlessCheck working-directory: efsity + + run-importer-unit-tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Generate Importer Report + run: | + python3 -m venv venv + source venv/bin/activate + python3 -m pip install --upgrade pip + pip install -r requirements.txt + pip install pytest-cov + pytest --doctest-modules --junitxml=coverage.xml --cov=. --cov-report=xml --cov-report=html + coverage xml + ls -al + working-directory: importer + + run-cleaner-unit-tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Generate Cleaner Report + run: | + python3 -m venv venv + source venv/bin/activate + python3 -m pip install --upgrade pip + pip install -r requirements.txt + pip install pytest-cov + pytest --doctest-modules --junitxml=coverage.xml --cov=. --cov-report=xml --cov-report=html + coverage xml + ls -al + working-directory: cleaner diff --git a/cleaner/README.md b/cleaner/README.md index 4204069b..c740fa5c 100644 --- a/cleaner/README.md +++ b/cleaner/README.md @@ -17,3 +17,20 @@ The following are command options for running the script 3. Create a `config.py` file. The `sample_config.py` is an example of what this should look like. Populate it with the right credentials 4. Run script - `python3 main.py --resource_type Locations --parameter lastUpdated --value lt2023-03-01` 5. You can turn on logging by passing a `--log_level` to the command line as `info`, `debug` or `error`. + +## To test + +To run all tests +```console +$ pytest +``` +To run specific tests +```console +$ pytest path/to/test_file.py::TestClass::test_function +``` + +To run tests and generate a coverage report +```console +$ pytest --junitxml=coverage.html --cov=importer --cov-report=html +``` +The coverage report `coverage.html` will be at the working directory \ No newline at end of file diff --git a/cleaner/config.py b/cleaner/config.py new file mode 100644 index 00000000..e69de29b diff --git a/cleaner/requirements.txt b/cleaner/requirements.txt index 23327ec7..3ca82874 100644 --- a/cleaner/requirements.txt +++ b/cleaner/requirements.txt @@ -2,4 +2,5 @@ click==8.1.3 oauthlib==3.2.2 requests==2.31.0 requests-oauthlib==1.3.1 -urllib3==2.0.3 \ No newline at end of file +urllib3==2.0.3 +pytest==7.4.2 diff --git a/cleaner/test_main.py b/cleaner/test_main.py new file mode 100644 index 00000000..927d3154 --- /dev/null +++ b/cleaner/test_main.py @@ -0,0 +1,41 @@ +import json +import unittest +from unittest.mock import patch +from main import post_request, build_payload + + +class TestMainFunctions(unittest.TestCase): + + @patch('main.config') + @patch('main.requests.post') + @patch('main.OAuth2Session') + def test_post_request_successful(self, oauth_session, mock_post, + mock_config): + oauth_session.fetch_token.return_value = "token" + mock_config.client_id = 1 + mock_config.client_secret = "client_secret" + mock_config.username = "username" + mock_config.password = "password" + mock_config.access_token_url = "access_token_url" + + mock_response = mock_post.return_value + mock_response.status_code = 200 + mock_response.text = "Success" + result = post_request("POST", "payload", "url") + self.assertEqual(result.status_code, 200) + + def test_build_payload(self): + resource_ids = ["1"] + resource_type = "ResourceType" + payload = build_payload(resource_ids, resource_type) + + self.assertTrue( + '{"resourceType": "Bundle", "type": "transaction", "entry": [' + in payload) + payload = json.loads(payload) + self.assertDictEqual(payload['entry'][0]['request'], + {'method': 'DELETE', 'url': 'ResourceType/1'}) + + +if __name__ == '__main__': + unittest.main() diff --git a/importer/README.md b/importer/README.md index 9413045c..77c5f986 100644 --- a/importer/README.md +++ b/importer/README.md @@ -37,6 +37,23 @@ and then posts them to the API for creation See example csvs in the csv folder +## To test + +To run all tests +```console +$ pytest +``` +To run specific tests +```console +$ pytest path/to/test_file.py::TestClass::test_function +``` + +To run tests and generate a coverage report +```console +$ pytest --junitxml=coverage.html --cov=importer --cov-report=html +``` +The coverage report `coverage.html` will be at the working directory + ## How to use it ### 1. Create locations in bulk diff --git a/importer/config.py b/importer/config.py new file mode 100644 index 00000000..e69de29b diff --git a/importer/requirements.txt b/importer/requirements.txt index e37a4686..548dd05c 100644 --- a/importer/requirements.txt +++ b/importer/requirements.txt @@ -3,4 +3,5 @@ oauthlib==3.2.2 requests==2.31.0 requests-oauthlib==1.3.1 urllib3==2.0.3 -backoff==2.2.1 \ No newline at end of file +backoff==2.2.1 +pytest==7.4.2 diff --git a/importer/test_main.py b/importer/test_main.py new file mode 100644 index 00000000..0af0ec9a --- /dev/null +++ b/importer/test_main.py @@ -0,0 +1,43 @@ +import json +import unittest +from main import ( + read_csv, + build_payload, + build_org_affiliation, + extract_matches +) + + +class TestMain(unittest.TestCase): + def test_read_csv(self): + csv_file = "csv/users.csv" + records = read_csv(csv_file) + self.assertIsInstance(records, list) + self.assertEqual(len(records), 3) + + def test_build_payload_organizations(self): + csv_file = "csv/organizations/organizations_full.csv" + resource_list = read_csv(csv_file) + payload = build_payload( + "organizations", + resource_list, + "json_payloads/organizations_payload.json" + ) + payload_obj = json.loads(payload) + self.assertIsInstance(payload_obj, dict) + self.assertEqual(payload_obj["resourceType"], "Bundle") + self.assertEqual(len(payload_obj['entry']), 3) + + def test_build_org_affiliation(self): + csv_file = "csv/organizations/organization_locations.csv" + resource_list = read_csv(csv_file) + resources = extract_matches(resource_list) + payload = build_org_affiliation(resources, resource_list) + payload_obj = json.loads(payload) + self.assertIsInstance(payload_obj, dict) + self.assertEqual(payload_obj["resourceType"], "Bundle") + self.assertEqual(len(payload_obj['entry']), 2) + + +if __name__ == "__main__": + unittest.main()