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

View File

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

View File

@ -1,27 +1,35 @@
# pytest-api # 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 ## 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: 2. Create your virtual env:
```shell ```shell
python3 -m venv venv python3 -m venv venv
``` ```
3. Activate the environment: 3. Activate the environment:
```shell ```shell
source venv/bin/activate source venv/bin/activate
``` ```
On Windows: On Windows:
```shell ```shell
.\venv\Scripts\activate .\venv\Scripts\activate
``` ```
4. Install requirements (pip will come from your venv) 4. Install requirements (pip will come from your venv)
```shell ```shell
pip install -r requirements.txt 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 =================================================================================================== ================================================================================================== 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 (.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): def api_client(call_dict, verify_cert=False, oauth=False):
method = call_dict["method"] method = call_dict["method"]
url = call_dict["url"] url = call_dict["url"]
headers = call_dict["headers"] headers = call_dict["headers"]
@ -35,10 +34,9 @@ def api_client(call_dict, verify_cert=False, oauth=False):
except RequestException as e: except RequestException as e:
print(f"Request failed. Method: {method}, URL: {url}", file=sys.stderr) print(f"Request failed. Method: {method}, URL: {url}", file=sys.stderr)
print(f"Error details: {str(e)}", 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: if response.status_code == 200:
return response.json() return response.json()
else:
return response.status_code, response.reason
return response.status_code, response.reason

View File

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

View File

@ -1,8 +1,8 @@
import json 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' ENV = 'qa'
cfg = get_cfg(ENV) cfg = get_cfg(ENV)

View File

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

View File

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

View File

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

View File

@ -1,15 +1,13 @@
import os
import json import json
import os
CWD = os.path.dirname(os.path.realpath(__file__)) CWD = os.path.dirname(os.path.realpath(__file__))
#### def get_expected_response(pkg, test_name):
# HELPERS
####
def get_expected_response(test_name):
expected_response_file = test_name + ".json" 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) expected_response = json.load(file)
return expected_response return expected_response

View File

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

View File

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

View File

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

View File

@ -1,8 +1,12 @@
import json 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: # NOTE:
@ -13,7 +17,7 @@ from tests.oauth_service.helpers import get_expected_response
@pytest.mark.get @pytest.mark.get
@pytest.mark.role @pytest.mark.role
def test_datadelivery_role_get(request, role_get_call): 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) actual_response = api_client(role_get_call, oauth=True)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4) 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.get
@pytest.mark.redaction @pytest.mark.redaction
def test_datadelivery_acms_redaction_get(request, acms_redaction_get_call): 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) actual_response = api_client(acms_redaction_get_call, oauth=True)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4) 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.get
@pytest.mark.client_application @pytest.mark.client_application
def test_datadelivery_client_applications_get(request, client_applications_get_call): 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) actual_response = api_client(client_applications_get_call, oauth=True)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4) 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.get
@pytest.mark.endpoint @pytest.mark.endpoint
def test_datadelivery_endpoint_get(request, endpoint_get_call): 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) actual_response = api_client(endpoint_get_call, oauth=True)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4) 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.get
@pytest.mark.redaction @pytest.mark.redaction
def test_datadelivery_redaction_type_get(request, redaction_type_get_call): 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) actual_response = api_client(redaction_type_get_call, oauth=True)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4) 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 import pytest
from apiclient.config import get_cfg
from apiclient.config import get_cfg
CFG = get_cfg('gw', force_refresh=True) 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 json
import pytest import pytest
from apiclient.client import api_client 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 @pytest.mark.rules
def test_get_creatures(request, get_creatures_call): 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) actual_response = api_client(get_creatures_call, oauth=False)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4) assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4)
@pytest.mark.rules @pytest.mark.rules
def test_get_creature(request, get_creature_call): 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) actual_response = api_client(get_creature_call, oauth=False)
assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4) assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4)