Repository navigation
Assert explain hash fields through assertProperties - #732
Conversation
eerxuan
left a comment
There was a problem hiding this comment.
Summary
Moving the hash reads out of helpers that raised RuntimeError and into one assertProperties call is the right fix. Every test still makes exactly one assertion, a failed explain is reported through the helper, and a missing hash on either side now fails the check instead of erroring before it. Two small issues in the new helper.
Critical Issues
- [Required]
explain_hash.py:hashes[key] = str(value)turns whatever explain returns into a string beforeNonEmptyStr()runs. So the "hash is a non-empty string" check can't fail on type: an int, a document or aBinaryhash would pass. Storevalueas is.Eq/Nestill compare correctly when both sides are strings.
Suggestions
- [Suggestion]
_lookuprepeats the dotted-path walk thatassertPropertiesalready does (_walk_pathinframework/assertions.py: dict keys plus numeric list indices). The one difference is that it returnsNoneinstead of the absent marker. So a hash that is present butnullis reported as "field is missing" rather than "got None". Returning a sentinel for absent, and keepingNoneas a value, would make that failure message accurate.
Questions
- [Nitpick]
explain_hash.pyis used only bycommands/explain/tests, but it lives insystem/diagnostic/utils/. Is it meant for other diagnostic commands? If not,commands/explain/utils/is the closest common level. - [Nitpick] The branch is 3 commits behind main. Please rebase before merge.
Positive Feedback
- The early return on a failed explain reports the server error once, not as two missing hashes.
- Keeping the
$out/$mergelayout as a second candidate path keeps those pipeline cases exact without adding a branch in each test.
|
🤖 Auto-triaged by documentdb-triage-tool. Applied: Reasoningcomponent from path globs (test-coverage); effort from diff stats (121+113 LOC, 3 files); LLM: Refactors explain hash test helpers so failures surface as proper assertion failures rather than RuntimeErrors, improving diagnostics across 168 test cases without changing expectations. If a label is wrong, remove it manually and ping |
The explain queryShapeHash / planCacheShapeHash tests pulled the hash out of the explain result in local helpers and raised RuntimeError when the field was missing or explain failed. That failure happened before any assertion helper ran, so an engine that does not report these fields failed every case with an exception instead of an assertion result. Replace the four helpers with explain_find / _explain_aggregate, which only return the raw explain result, and a shared assert_hash_relation helper in diagnostic/utils/explain_hash.py. It checks in a single assertProperties call that both hashes are non-empty strings and that they are equal (same shape) or different (different shape). An explain error is passed to assertProperties and fails there with the server error. Behavior change: a different-shape test now fails when the second explain has no hash, where before Ne passed on a missing field. Test expectations are otherwise unchanged; 168/168 still pass on MongoDB 8.2.4. Signed-off-by: Samar Farooqi <samafarc@amazon.com>
explain_hash.py is used only by the explain queryShapeHash / planCacheShapeHash tests, so move it from system/diagnostic/utils/ to system/diagnostic/commands/explain/utils/ and import it relatively, as the other command-local utils packages are. No behavior change. Signed-off-by: Samar Farooqi <samafarc@amazon.com>
assert_hash_relation stringified each hash before NonEmptyStr ran, so an int, document or Binary hash passed the check. Store the value as explain reported it; Eq / Ne still compare two string hashes the same way. Also drop the local _lookup helper and walk the dotted path with the framework's _walk_path, which returns the _FIELD_ABSENT sentinel for a missing field. _lookup returned None for a missing field, so a hash that was present but null was reported as "field is missing"; it is now reported as "got None". Signed-off-by: Samar Farooqi <samafarc@amazon.com>
598e494 to
995fb6e
Compare
|
Thanks @eerxuan, all addressed:
168/168 still pass on MongoDB 8.2.4, same as main. |
Summary
The explain hash tests (
test_explain_queryhash_shape.py,test_explain_queryhash_pipeline.py) readplanCacheShapeHash/queryShapeHashwith file-local helpers that raiseRuntimeErrorwhen explain fails or the field is missing. That error fires before any assertion runs, so on an engine that does not report these hashes, all 168 cases fail with a helper error instead of an assertion failure about the property under test.This change keeps the explain result raw and does every check through one
assertPropertiescall:system/diagnostic/commands/explain/utils/explain_hash.py(next to the only tests that use it) withassert_hash_relation(result_a, result_b, paths, same, msg). It asserts that both results carry a non-empty string hash at the given path (or the first of several candidate paths), then that the two hashes are equal (same=True) or differ (same=False).NonEmptyStr._walk_path, so a missing field and a present-but-null field are told apart: the first is reported as "field is missing", the second as "got None".assertProperties, so it is reported as an assertion failure with the server's error. The helper then returns, so anassertResultoverride provider (framework/override_hook.py) that handles that failure is consulted exactly once per test.get_*/_get_*helpers are replaced byexplain_find/_explain_aggregate, which only run explain and return the raw result.$out/$mergelayout (stages.0.$cursor.queryPlanner.planCacheShapeHash) is kept as a second candidate path.The expectations are unchanged. On MongoDB every case checks the same equal/different property as before. One behavior is stricter: previously a missing hash on the second query could not be told apart from a missing hash on the first. Now each missing hash is reported by name (
hash_a/hash_b), and aNecomparison never passes because a hash is absent.Testing
main.RuntimeErrorfrom the helpers. After it, every case fails insideassertProperties(156 missing-hashPROPERTY_MISMATCH, 12 explain errors carrying the server code), so the results can be judged per assertion.NonEmptyStr; a null hash reportsgot None; a missing hash reportsfield is missing; equal vs differ mismatches fail; the$out/$mergelayout resolves; an explain error fails throughassertProperties.pytest -m unit: 152/152 pass.Example failure on an engine without the field: