Skip to content

Commit

Permalink
Refactor and finalize utility tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eloquence committed Oct 4, 2023
1 parent 8bd3744 commit ac2db46
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,67 @@
get_relevant_poetry_dependencies,
get_poetry_hashes,
get_requirements_hashes,
get_requirements_from_poetry,
)

# These tests generally verify that our utility functions correctly parse the
# fixtures below. The Poetry fixture includes a development-only requirement
# (pytest) which should not be returned.
PYPROJECT_TOML_PATH = Path(__file__).parent / "fixtures/pyproject.toml"
POETRY_LOCK_PATH = Path(__file__).parent / "fixtures/poetry.lock"
REQUIREMENTS_TXT_PATH = Path(__file__).parent / "fixtures/requirements.txt"
EXPECTED_DEPENDENCIES = [
("beautifulsoup4", "4.12.2"),
("colorama", "0.4.6"),
("cowsay", "6.0"),
("soupsieve", "2.5"),
]
EXPECTED_KEYS = [f"{name}=={version}" for name, version in EXPECTED_DEPENDENCIES]


def test_get_requirements_names_and_versions():
expected_output = [
("beautifulsoup4", "4.12.2"),
("colorama", "0.4.6"),
("cowsay", "6.0"),
("soupsieve", "2.5"),
]
assert get_requirements_names_and_versions(REQUIREMENTS_TXT_PATH) == expected_output
assert (
get_requirements_names_and_versions(REQUIREMENTS_TXT_PATH)
== EXPECTED_DEPENDENCIES
)


def test_get_poetry_names_and_versions():
expected_output = [
("beautifulsoup4", "4.12.2"),
("colorama", "0.4.6"),
("cowsay", "6.0"),
("soupsieve", "2.5"),
]
output = get_poetry_names_and_versions(POETRY_LOCK_PATH, PYPROJECT_TOML_PATH)
assert output == expected_output
assert output == EXPECTED_DEPENDENCIES


def test_get_relevant_poetry_dependencies():
expected_output = ["colorama", "cowsay", "beautifulsoup4", "soupsieve"]
output = get_relevant_poetry_dependencies(PYPROJECT_TOML_PATH, POETRY_LOCK_PATH)
# Ensure we correctly ignore development-only dependencies
assert "pytest" not in output
assert sorted(output) == sorted(expected_output)


def test_get_poetry_hashes():
output = get_poetry_hashes(POETRY_LOCK_PATH, PYPROJECT_TOML_PATH)
expected_keys = [
"beautifulsoup4==4.12.2",
"colorama==0.4.6",
"cowsay==6.0",
"soupsieve==2.5",
]

assert list(output.keys()) == expected_keys
assert list(output.keys()) == EXPECTED_KEYS
assert all(len(output[dep]) > 0 for dep in output.keys())
assert all(all(len(hash) == 64 for hash in output[dep]) for dep in output.keys())


def test_get_requirements_hashes():
output = get_requirements_hashes(REQUIREMENTS_TXT_PATH)
expected_keys = [
"beautifulsoup4==4.12.2",
"colorama==0.4.6",
"cowsay==6.0",
"soupsieve==2.5",
]

assert list(output.keys()) == expected_keys
assert list(output.keys()) == EXPECTED_KEYS
assert all(len(output[dep]) > 0 for dep in output.keys())
assert all(all(len(hash) == 64 for hash in output[dep]) for dep in output.keys())


def test_get_requirements_from_poetry():
output = get_requirements_from_poetry(POETRY_LOCK_PATH, PYPROJECT_TOML_PATH)
output_lines = output.strip().split("\n")
assert len(output_lines) == len(EXPECTED_KEYS)
for line in output_lines:
package_name_and_version, *hashes = line.split()
assert package_name_and_version in EXPECTED_KEYS
assert all(hash_str.startswith("--hash=sha256:") for hash_str in hashes)


if __name__ == "__main__":
pytest.main([__file__])

0 comments on commit ac2db46

Please sign in to comment.