Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup testing framework - python modules #78

Merged
merged 6 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

# 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:
branches:
- main

jobs:
run-unit-tests:
run-efsity-unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions cleaner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Empty file added cleaner/config.py
Empty file.
3 changes: 2 additions & 1 deletion cleaner/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
urllib3==2.0.3
pytest==7.4.2
41 changes: 41 additions & 0 deletions cleaner/test_main.py
Original file line number Diff line number Diff line change
@@ -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()
17 changes: 17 additions & 0 deletions importer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added importer/config.py
Empty file.
3 changes: 2 additions & 1 deletion importer/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
backoff==2.2.1
pytest==7.4.2
43 changes: 43 additions & 0 deletions importer/test_main.py
Original file line number Diff line number Diff line change
@@ -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()