code cleanup for pylint
All checks were successful
Pylint / build (3.12) (push) Successful in 9s
execute / build (3.12) (push) Successful in 25s

This commit is contained in:
Greg Gauthier 2024-07-22 19:26:27 +01:00
parent c6a87dff12
commit a727c58cf3
20 changed files with 467 additions and 466 deletions

View File

@ -1,6 +1,6 @@
name: execute
on: [push]
on: [ push ]
env:
BUILD_TYPE: Release
@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-gitea
strategy:
matrix:
python-version: ["3.12"]
python-version: [ "3.12" ]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}

View File

@ -1,13 +1,13 @@
name: Pylint
on: [push]
on: [ push ]
jobs:
build:
runs-on: ubuntu-gitea
strategy:
matrix:
python-version: ["3.12"]
python-version: [ "3.12" ]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}

View File

@ -1,27 +1,35 @@
# pytest-api
This is a proof-of-concept project, showing how PyTest could be used in combination with a custom API client, to quickly and easily build api tests for a data-delivery service that is deployed and active on an existing environment.
This is a proof-of-concept project, showing how PyTest could be used in combination with a custom API client, to quickly
and easily build api tests for a data-delivery service that is deployed and active on an existing environment.
## Instructions
1. Add a `.env` file to your local copy of the repo. By default, this demo project will look for `.env.qa` in the root of the project. You can find a template to base this on, in `apiclient/env_template`. Info on how to set the values in that file can be found by asking Greg.
1. Add a `.env` file to your local copy of the repo. By default, this demo project will look for `.env.qa` in the root
of the project. You can find a template to base this on, in `apiclient/env_template`. Info on how to set the values
in that file can be found by asking Greg.
2. Create your virtual env:
```shell
python3 -m venv venv
```
3. Activate the environment:
```shell
source venv/bin/activate
```
On Windows:
```shell
.\venv\Scripts\activate
```
4. Install requirements (pip will come from your venv)
```shell
pip install -r requirements.txt
```
@ -70,7 +78,8 @@ PASSED tests/test_datadelivery.py::test_datadelivery_redaction_type_get
================================================================================================== 5 passed, 5 warnings in 17.20s ===================================================================================================
```
PyTest is configured to show the top 25 test durations, as well as the pass/fail status of all the tests. The tests have been marked with various tags, to allow for granular test selection. To see all the available markers:
PyTest is configured to show the top 25 test durations, as well as the pass/fail status of all the tests. The tests have
been marked with various tags, to allow for granular test selection. To see all the available markers:
```
(.venv) PS C:\Users\GregGauthier\Projects\local\pytest-api> pytest --markers

View File

@ -6,7 +6,6 @@ from requests_oauthlib import OAuth2Session
def api_client(call_dict, verify_cert=False, oauth=False):
method = call_dict["method"]
url = call_dict["url"]
headers = call_dict["headers"]
@ -35,10 +34,9 @@ def api_client(call_dict, verify_cert=False, oauth=False):
except RequestException as e:
print(f"Request failed. Method: {method}, URL: {url}", file=sys.stderr)
print(f"Error details: {str(e)}", file=sys.stderr)
exit(e.response.status_code)
sys.exit(e.response.status_code)
if response.status_code == 200:
return response.json()
else:
return response.status_code, response.reason
return response.status_code, response.reason

View File

@ -1,7 +1,7 @@
import os
from dotenv import dotenv_values
from types import MappingProxyType
from dotenv import dotenv_values
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

View File

@ -1,8 +1,8 @@
import json
from apiclient.config import get_cfg
from apiclient.client import api_client
from apiclient.oauth_helper import get_legacy_token
from apiclient.client import api_client
from apiclient.config import get_cfg
from apiclient.oauth_helper import get_legacy_token
ENV = 'qa'
cfg = get_cfg(ENV)

View File

@ -1,7 +1,9 @@
import sys
from oauthlib.oauth2 import LegacyApplicationClient, OAuth2Error
from requests_oauthlib import OAuth2Session
from apiclient.config import get_cfg
import sys
def get_legacy_token(env):
@ -22,6 +24,6 @@ def get_legacy_token(env):
)
except OAuth2Error as e:
print("OAuth2 Error: ", str(e), file=sys.stderr)
exit(e.status_code)
sys.exit(e.status_code)
return token

View File

@ -3,3 +3,4 @@ requests~=2.32.3
requests-oauthlib~=2.0.0
python-dotenv~=1.0.1
pytest~=8.2.2
pylint~=3.2.6

View File

@ -17,4 +17,3 @@ def reset_oauth_session():
oauth_session.cookies.clear()
yield oauth_session
oauth_session.close()

View File

@ -1,15 +1,13 @@
import os
import json
import os
CWD = os.path.dirname(os.path.realpath(__file__))
####
# HELPERS
####
def get_expected_response(test_name):
def get_expected_response(pkg, test_name):
expected_response_file = test_name + ".json"
with open(os.path.join(CWD, 'expected_responses', expected_response_file), 'r') as file:
with open(
os.path.join(CWD, pkg, 'expected_responses', expected_response_file),
'r', encoding="utf8") as file:
expected_response = json.load(file)
return expected_response

View File

@ -1,8 +1,9 @@
# pylint: disable=line-too-long,unused-argument
import pytest
from apiclient.config import get_cfg
from apiclient.oauth_helper import get_legacy_token
ENV = 'qa'
CFG = get_cfg(ENV, force_refresh=True)
application_json = {'Content-Type': 'application/json'}

View File

@ -1,42 +1,42 @@
[
{
"ClientId" : "e72cdfde-957a-4f65-9f5e-c7b18e95d8dc",
"ClientSecret" : "f4ca195c-989c-48e6-a002-b1fb7b3c03fd",
"IsActive" : 1,
"Name" : "testcase",
"date_of_generation" : "2024-04-25 04:48:47",
"id" : 1,
"last_connection" : null,
"organization_id" : 16
"ClientId": "e72cdfde-957a-4f65-9f5e-c7b18e95d8dc",
"ClientSecret": "f4ca195c-989c-48e6-a002-b1fb7b3c03fd",
"IsActive": 1,
"Name": "testcase",
"date_of_generation": "2024-04-25 04:48:47",
"id": 1,
"last_connection": null,
"organization_id": 16
},
{
"ClientId" : "b0dd74c2-9f39-45fc-a52e-b1130ffddbb5",
"ClientSecret" : "71b2ab0e-d3fa-40e2-a40e-2088ae716a46",
"IsActive" : 1,
"Name" : "Test",
"date_of_generation" : "2024-05-20 07:22:31",
"id" : 2,
"last_connection" : null,
"organization_id" : 1
"ClientId": "b0dd74c2-9f39-45fc-a52e-b1130ffddbb5",
"ClientSecret": "71b2ab0e-d3fa-40e2-a40e-2088ae716a46",
"IsActive": 1,
"Name": "Test",
"date_of_generation": "2024-05-20 07:22:31",
"id": 2,
"last_connection": null,
"organization_id": 1
},
{
"ClientId" : "be1a0686-fed2-4d79-a04c-c473c6c3e22e",
"ClientSecret" : "ddfdaee7-1fe5-4f62-964c-cb2b8e8397e1",
"IsActive" : 1,
"Name" : "Test3",
"date_of_generation" : "2024-05-20 10:58:35",
"id" : 3,
"last_connection" : null,
"organization_id" : 1
"ClientId": "be1a0686-fed2-4d79-a04c-c473c6c3e22e",
"ClientSecret": "ddfdaee7-1fe5-4f62-964c-cb2b8e8397e1",
"IsActive": 1,
"Name": "Test3",
"date_of_generation": "2024-05-20 10:58:35",
"id": 3,
"last_connection": null,
"organization_id": 1
},
{
"ClientId" : "37f7afc6-91a1-4c2d-9696-0e8db6fc1bc6",
"ClientSecret" : "40106ff7-7feb-4869-b4c9-eeed5dee2f36",
"IsActive" : 1,
"Name" : "test4",
"date_of_generation" : "2024-05-20 11:14:16",
"id" : 4,
"last_connection" : null,
"organization_id" : 1
"ClientId": "37f7afc6-91a1-4c2d-9696-0e8db6fc1bc6",
"ClientSecret": "40106ff7-7feb-4869-b4c9-eeed5dee2f36",
"IsActive": 1,
"Name": "test4",
"date_of_generation": "2024-05-20 11:14:16",
"id": 4,
"last_connection": null,
"organization_id": 1
}
]

View File

@ -1,22 +1,22 @@
[
{
"description" : "RAW A717 Redaction Type",
"name" : "RAWA717",
"redactiontype_id" : 1
"description": "RAW A717 Redaction Type",
"name": "RAWA717",
"redactiontype_id": 1
},
{
"description" : "RAW A767 Redaction Type",
"name" : "RAWA767",
"redactiontype_id" : 2
"description": "RAW A767 Redaction Type",
"name": "RAWA767",
"redactiontype_id": 2
},
{
"description" : "CSV Redaction Type",
"name" : "CSV",
"redactiontype_id" : 3
"description": "CSV Redaction Type",
"name": "CSV",
"redactiontype_id": 3
},
{
"description" : "PARQUET Redaction Type",
"name" : "PARQUET",
"redactiontype_id" : 4
"description": "PARQUET Redaction Type",
"name": "PARQUET",
"redactiontype_id": 4
}
]

View File

@ -1,8 +1,12 @@
import json
import pytest
from apiclient.client import api_client
from tests.oauth_service.helpers import get_expected_response
import pytest
from apiclient.client import api_client
from tests.helpers import get_expected_response
pkg_name = __package__.split('.')[1]
####
# NOTE:
@ -13,7 +17,7 @@ from tests.oauth_service.helpers import get_expected_response
@pytest.mark.get
@pytest.mark.role
def test_datadelivery_role_get(request, role_get_call):
expected_response = get_expected_response(request.node.name)
expected_response = get_expected_response(pkg_name, request.node.name)
actual_response = api_client(role_get_call, oauth=True)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4)
@ -21,7 +25,7 @@ def test_datadelivery_role_get(request, role_get_call):
@pytest.mark.get
@pytest.mark.redaction
def test_datadelivery_acms_redaction_get(request, acms_redaction_get_call):
expected_response = get_expected_response(request.node.name)
expected_response = get_expected_response(pkg_name,request.node.name)
actual_response = api_client(acms_redaction_get_call, oauth=True)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4)
@ -29,7 +33,7 @@ def test_datadelivery_acms_redaction_get(request, acms_redaction_get_call):
@pytest.mark.get
@pytest.mark.client_application
def test_datadelivery_client_applications_get(request, client_applications_get_call):
expected_response = get_expected_response(request.node.name)
expected_response = get_expected_response(pkg_name, request.node.name)
actual_response = api_client(client_applications_get_call, oauth=True)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4)
@ -37,7 +41,7 @@ def test_datadelivery_client_applications_get(request, client_applications_get_c
@pytest.mark.get
@pytest.mark.endpoint
def test_datadelivery_endpoint_get(request, endpoint_get_call):
expected_response = get_expected_response(request.node.name)
expected_response = get_expected_response(pkg_name, request.node.name)
actual_response = api_client(endpoint_get_call, oauth=True)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4)
@ -45,6 +49,6 @@ def test_datadelivery_endpoint_get(request, endpoint_get_call):
@pytest.mark.get
@pytest.mark.redaction
def test_datadelivery_redaction_type_get(request, redaction_type_get_call):
expected_response = get_expected_response(request.node.name)
expected_response = get_expected_response(pkg_name, request.node.name)
actual_response = api_client(redaction_type_get_call, oauth=True)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4)

View File

@ -1,6 +1,7 @@
# pylint: disable=line-too-long,unused-argument
import pytest
from apiclient.config import get_cfg
from apiclient.config import get_cfg
CFG = get_cfg('gw', force_refresh=True)

View File

@ -1,15 +0,0 @@
import os
import json
CWD = os.path.dirname(os.path.realpath(__file__))
####
# HELPERS
####
def get_expected_response(test_name):
expected_response_file = test_name + ".json"
with open(os.path.join(CWD, 'expected_responses', expected_response_file), 'r') as file:
expected_response = json.load(file)
return expected_response

View File

@ -1,19 +1,22 @@
import json
import pytest
from apiclient.client import api_client
from tests.open_service.helpers import get_expected_response
from tests.helpers import get_expected_response
pkg_name = __package__.split('.')[1]
@pytest.mark.rules
def test_get_creatures(request, get_creatures_call):
expected_response = get_expected_response(request.node.name)
expected_response = get_expected_response(pkg_name, request.node.name)
actual_response = api_client(get_creatures_call, oauth=False)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4)
@pytest.mark.rules
def test_get_creature(request, get_creature_call):
expected_response = get_expected_response(request.node.name)
expected_response = get_expected_response(pkg_name, request.node.name)
actual_response = api_client(get_creature_call, oauth=False)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4)