There is no test that validates the hash function codes in constants.py and funcs.py against the authoritative upstream multicodec table.csv. go-multihash has a TestSpec that does this validation.
Problem
The HASH_CODES dict in constants.py and the Func IntEnum in funcs.py are manually maintained. There is no automated check that:
- Every multihash-tagged entry in the CSV is present
- The codes match the CSV
- The names match the CSV
- No extra entries exist
go-multihash validates against the spec:
func TestSpec(t *testing.T) {
file, _ := os.Open("spec/multicodec/table.csv")
// ... reads CSV, filters for tag == "multihash"
for code, name := range multihash.Codes {
expectedName, ok := expectedFunctions[code]
if !ok {
t.Errorf("multihash %q (%x) not defined in the spec", name, code)
}
}
}
Proposed Solution
-
Add the multicodec spec as a git submodule:
git submodule add https://github.com/multiformats/multicodec spec/multicodec
-
Create tests/test_spec.py:
import csv
from multihash.constants import HASH_CODES, CODE_HASHES
def test_spec_table_completeness():
"""Every multihash entry in table.csv should be in HASH_CODES."""
with open("spec/multicodec/table.csv") as f:
reader = csv.DictReader(f, skipinitialspace=True)
for row in reader:
if row["tag"].strip() != "multihash":
continue
name = row["name"].strip()
code = int(row["code"].strip(), 16)
assert name in HASH_CODES, f"Missing hash: {name} (0x{code:x})"
assert HASH_CODES[name] == code, f"Code mismatch for {name}"
Related
There is no test that validates the hash function codes in
constants.pyandfuncs.pyagainst the authoritative upstream multicodec table.csv. go-multihash has aTestSpecthat does this validation.Problem
The
HASH_CODESdict inconstants.pyand theFuncIntEnum infuncs.pyare manually maintained. There is no automated check that:go-multihash validates against the spec:
Proposed Solution
Add the multicodec spec as a git submodule:
Create
tests/test_spec.py:Related
spec_test.go