Skip to content

Commit

Permalink
move from green to pytest for unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
coreone committed Jan 6, 2025
1 parent fac7a5f commit 30908b9
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 311 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ jobs:
uses: broadinstitute/shared-workflows/.github/workflows/[email protected]
with:
python_package_name: cert_manager
run_coverage: false
test_runner: pytest
2 changes: 0 additions & 2 deletions .green

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ To start a development environment, you should be able to just run the `dev.bash
./dev.bash
```

The first time you run the script, it should build the [Docker][4] image and then drop you into the container's shell. The directory where you cloned this repository should be volume mounted in to `/working`, which should also be the current working directory. From there, you can make changes as you see fit. Tests can be run from the `/working` directory by simply typing `green` as [green][5] has been setup to with the correct parameters.
The first time you run the script, it should build the [Docker][4] image and then drop you into the container's shell. The directory where you cloned this repository should be volume mounted in to `/working`, which should also be the current working directory. From there, you can make changes as you see fit. Tests can be run from the `/working` directory by simply typing `pytest` as [pytest][5] has been setup to with the correct parameters.

## Changelog

Expand Down Expand Up @@ -164,5 +164,5 @@ git push --tags
[2]: https://sectigo.com/ "Sectigo"
[3]: https://python-poetry.org/ "Poetry"
[4]: https://www.docker.com/ "Docker"
[5]: https://github.com/CleanCut/green "green"
[5]: https://docs.pytest.org/en/stable/ "pytest"
[6]: https://pypi.org/project/bump2version/ "bump2version"
523 changes: 234 additions & 289 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ license = "BSD-3-Clause"
name = "cert_manager"
readme = "README.md"
repository = "https://github.com/broadinstitute/python-cert_manager.git"
requires-python = ">=3.9,<3.12.1"
requires-python = ">=3.9,<4.0.0"
version = "2.4.0"

[tool.poetry.dependencies]
Expand All @@ -25,11 +25,11 @@ toml = "^0.10.2"
bump2version = "^1.0.1"
coverage = "^7.6.10"
fixtures = "^4.2.2"
green = "^4.0.2"
mock = "^5.1.0"
responses = "^0.25.3"
testtools = "^2.7.2"
yamllint = "^1.35.1"
pytest = "^8.3.4"

[tool.ruff]
fixable = ["ALL"]
Expand Down
30 changes: 16 additions & 14 deletions tests/test_acme.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import responses
from requests.exceptions import HTTPError
from responses.matchers import json_params_matcher
from testtools import TestCase

from cert_manager.acme import ACMEAccount, ACMEAccountCreationResponseError
Expand Down Expand Up @@ -164,8 +165,8 @@ def test_init_param(self):
api_url = self.get_api_url(api_version=version)

# Setup the mocked response
responses.add(responses.GET, api_url, json=self.valid_response,
status=200, match_querystring=False)
responses.matchers.query_string_matcher = False
responses.add(responses.GET, api_url, json=self.valid_response, status=200)

acme = ACMEAccount(client=self.client, api_version=version)
data = acme.all(self.org_id)
Expand All @@ -182,8 +183,8 @@ def test_init_param(self):
def test_bad_http(self):
"""Raise an HTTPError exception if acme accounts cannot be retrieved from the API."""
# Setup the mocked response
responses.add(responses.GET, self.api_url, json=self.error_response,
status=404, match_querystring=False)
responses.matchers.query_string_matcher = False
responses.add(responses.GET, self.api_url, json=self.error_response, status=404)

acme = ACMEAccount(client=self.client)
self.assertRaises(HTTPError, acme.all, self.org_id)
Expand All @@ -196,8 +197,8 @@ def test_bad_http(self):
def test_cached(self):
"""Return all the data, but should not query the API twice."""
# Setup the mocked response, refrain from matching the query string
responses.add(responses.GET, self.api_url, json=self.valid_response,
status=200, match_querystring=False)
responses.matchers.query_string_matcher = False
responses.add(responses.GET, self.api_url, json=self.valid_response, status=200)

acme = ACMEAccount(client=self.client)
acme.all(self.org_id)
Expand All @@ -215,8 +216,8 @@ def test_cached(self):
def test_forced(self):
"""Return all the data, but should query the API twice."""
# Setup the mocked response, refrain from matching the query string
responses.add(responses.GET, self.api_url, json=self.valid_response,
status=200, match_querystring=False)
responses.matchers.query_string_matcher = False
responses.add(responses.GET, self.api_url, json=self.valid_response, status=200)

acme = ACMEAccount(client=self.client)
acme.all(self.org_id, force=True)
Expand Down Expand Up @@ -257,8 +258,9 @@ def generic_test(self):
)
]
# Setup the mocked response
responses.matchers.query_string_matcher = False
responses.add(
responses.GET, self.api_url, json=valid_response, status=200, match_querystring=False
responses.GET, self.api_url, json=valid_response, status=200,
)
acme = ACMEAccount(client=self.client)
data = list(acme.find(self.org_id, **params))
Expand Down Expand Up @@ -400,7 +402,7 @@ def test_create_success(self, acme_id, response_headers, args, request_params):
"""Return the created ACME ID."""
# Setup the mocked response
responses.add(responses.POST, self.api_url, headers=response_headers,
match=[responses.json_params_matcher(request_params)],
match=[json_params_matcher(request_params)],
status=201)

acme = ACMEAccount(client=self.client)
Expand All @@ -414,7 +416,7 @@ def test_create_failure_http_error(self, _, __, args, request_params):
"""Raise an exception if the ACME Account creation fails with an http error."""
# Setup the mocked response
responses.add(responses.POST, self.api_url, json=self.error_response,
match=[responses.json_params_matcher(request_params)],
match=[json_params_matcher(request_params)],
status=400)

acme = ACMEAccount(client=self.client)
Expand All @@ -428,7 +430,7 @@ def test_create_failure_http_status_unexpected(self, _, __, args,
"""Raise an exception if the ACME Account creation fails with unexpected http code."""
# Setup the mocked response
responses.add(responses.POST, self.api_url, json=self.error_response,
match=[responses.json_params_matcher(request_params)],
match=[json_params_matcher(request_params)],
status=200) # unexpected status

acme = ACMEAccount(client=self.client)
Expand All @@ -444,7 +446,7 @@ def test_create_failure_missing_location_header(self, _, response_headers,
# Setup the mocked response
responses.add(responses.POST, self.api_url, json=self.error_response,
headers=response_headers,
match=[responses.json_params_matcher(request_params)],
match=[json_params_matcher(request_params)],
status=201)

acme = ACMEAccount(client=self.client)
Expand All @@ -460,7 +462,7 @@ def test_create_failure_acme_id_not_found(self, _, response_headers, args,
# Setup the mocked response
responses.add(responses.POST, self.api_url, json=self.error_response,
headers=response_headers,
match=[responses.json_params_matcher(request_params)],
match=[json_params_matcher(request_params)],
status=201)

acme = ACMEAccount(client=self.client)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ def test_params(self):
"""Add passed parameters."""
# Setup the mocked response
json_data = {"some": "data"}
responses.add(responses.GET, self.test_url, json=json_data, status=200,
match_querystring=False)
responses.matchers.query_string_matcher = False
responses.add(responses.GET, self.test_url, json=json_data, status=200)

# Call the function with extra parameters
params = {"key": "value"}
Expand Down

0 comments on commit 30908b9

Please sign in to comment.