Use betamax cassettes to record & replay requests to PyPI

This commit is contained in:
Karolina Surma
2021-09-14 16:55:32 +02:00
parent 6281df9766
commit 8e3d0e9645
5 changed files with 22 additions and 13 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install -r requirements.txt
python -m pip install pytest pytest-regressions
python -m pip install pytest pytest-regressions betamax
- name: Run pytest
run: |
python -m pytest -vv
+3 -2
View File
@@ -37,11 +37,12 @@ python conf2spec.py -f tests/test_config/pyp2spec_click.conf
Test framework used in the project is pytest.
The tests use pytest-regresssions extension used to generate spec files to compare.
The tests use betamax to record API calls and replay them from the prerecorded cassettes.
To run the tests, run following commands:
```
python -m pip install pytest pytest-regresssions
python -m pytest
python -m pip install pytest pytest-regresssions betamax
python -m pytest -vv
```
+7 -4
View File
@@ -7,9 +7,11 @@ import requests
import tomli_w
def get_pypi_metadata(package_name):
def get_pypi_metadata(package_name, session=None):
pypi_index = f'https://pypi.org/pypi/{package_name}/json'
response = requests.get(pypi_index)
s = session or requests.Session()
response = s.get(pypi_index)
return response.json()
@@ -95,7 +97,8 @@ def is_url(package):
def create_config_contents(package, description=None, release=None,
message=None, email=None, name=None, version=None, summary=None, date=None):
message=None, email=None, name=None, version=None, summary=None, date=None,
session=None):
"""Use `package_data` to create the whole config contents.
Return contents.
"""
@@ -109,7 +112,7 @@ def create_config_contents(package, description=None, release=None,
# `package` is not a URL -> it's a package name, look for it on PyPI
if not is_url(package):
package_data = get_pypi_metadata(package)
package_data = get_pypi_metadata(package, session)
pypi_name = package_data["info"]["name"]
contents["pypi_name"] = pypi_name
File diff suppressed because one or more lines are too long
+10 -6
View File
@@ -1,10 +1,14 @@
# import config
import pyp2conf
import betamax
import tomli
import pyp2conf
def test_config_is_valid():
with betamax.Betamax.configure() as config:
config.cassette_library_dir = 'tests/fixtures/cassettes'
def test_config_is_valid(betamax_session):
package = "aionotion"
config = pyp2conf.create_config_contents(
package=package,
@@ -15,11 +19,11 @@ def test_config_is_valid():
name="Package Manager",
version="2.0.3",
summary="Python library for Notion Home Monitoring",
date="Fri Jun 04 2021"
date="Fri Jun 04 2021",
session=betamax_session,
)
with open(f"tests/test_configs/pyp2spec_{package}.conf", "rb") as config_file:
loaded_contents = tomli.load(config_file)
assert config == loaded_contents
# assert config.ConfigFile.validate_contents(contents)