Skip to content

Commit

Permalink
refactor: examples test (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored May 13, 2024
1 parent 5aa028b commit 6c4e601
Showing 1 changed file with 40 additions and 38 deletions.
78 changes: 40 additions & 38 deletions tests/test_package_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,47 +76,49 @@ def test_examples(example_name):
example = requests.get(f"{EXAMPLES_RAW_URL}/{example_name}/v3.json")
example_json = example.json()

if "invalid" not in example_name:
package = PackageManifest.model_validate(example_json)

# Hack to skip computed fiend in check.
for ct in (package.contract_types or {}).values():
ct.__dict__["method_identifiers"] = None

actual = package.model_dump(mode="json")
assert actual == example_json

# NOTE: Also make sure that the encoding is exactly the same (per EIP-2678)
actual = package.model_dump_json()
expected = example.text

for idx, (c1, c2) in enumerate(zip(actual, expected)):
# The following logic is because the strings being compared
# are very long and this more accurately pinpoints
# the failing section of the string, even on lower verbosity.
buffer = 20
start = max(0, idx - 10)
actual_end = min(idx + buffer, len(actual))
expected_end = min(idx + buffer, len(expected))
actual_prefix = actual[start:actual_end]
expected_prefix = expected[start:expected_end]
fail_msg = (
f"Differs at index: {idx}, "
f"Actual: '{actual_prefix}', "
f"Expected: '{expected_prefix}'"
)
assert c1 == c2, fail_msg

if package.sources:
for source_name, source in package.sources.items():
# NOTE: Per EIP-2678, "Checksum is only required if content is missing"
if not source.content:
assert source.content_is_valid(), f"Invalid checksum for '{source_name}'"

else:
if "invalid" in example_name:
with pytest.raises(ValidationError):
PackageManifest.model_validate(example_json).model_dump()

return

# Valid manifest test.
package = PackageManifest.model_validate(example_json)

# Hack to skip computed field in check.
for ct in (package.contract_types or {}).values():
ct.__dict__["method_identifiers"] = None

actual = package.model_dump(mode="json")
assert actual == example_json

# NOTE: Also make sure that the encoding is exactly the same (per EIP-2678)
actual = package.model_dump_json()
expected = example.text

for idx, (c1, c2) in enumerate(zip(actual, expected)):
# The following logic is because the strings being compared
# are very long and this more accurately pinpoints
# the failing section of the string, even on lower verbosity.
buffer = 20
start = max(0, idx - 10)
actual_end = min(idx + buffer, len(actual))
expected_end = min(idx + buffer, len(expected))
actual_prefix = actual[start:actual_end]
expected_prefix = expected[start:expected_end]
fail_msg = (
f"Differs at index: {idx}, "
f"Actual: '{actual_prefix}', "
f"Expected: '{expected_prefix}'"
)
assert c1 == c2, fail_msg

if package.sources:
for source_name, source in package.sources.items():
# NOTE: Per EIP-2678, "Checksum is only required if content is missing"
if not source.content:
assert source.content_is_valid(), f"Invalid checksum for '{source_name}'"


def test_open_zeppelin_contracts(oz_package):
for source_name, source in oz_package.sources.items():
Expand Down

0 comments on commit 6c4e601

Please sign in to comment.