Add unit tests for Cube.coord_dims#7198
Conversation
|
From @SciTools/peloton: Hi @01luyicheng, thanks for the PR, we'll get this reviewed soon, in the mean time, we just need you to sign the CLA (linked here #7198 (comment)). |
|
Hi, the CLA is signed now - looking forward to the review. Thanks! |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7198 +/- ##
=======================================
Coverage 90.18% 90.18%
=======================================
Files 91 91
Lines 25102 25102
Branches 4706 4706
=======================================
Hits 22638 22638
Misses 1685 1685
Partials 779 779 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ESadek-MO
left a comment
There was a problem hiding this comment.
Hi @01luyicheng, thanks so much for tackling this!
I've got a couple of comments; the only problems I've found thus far are readability concerns, the code itself all looks great!
| self.x_coord = x_coord | ||
| self.aux_1d = aux_1d | ||
| self.aux_2d = aux_2d | ||
| self.factory = factory |
There was a problem hiding this comment.
See above.
| self.factory = factory |
| # Dependencies for a hybrid-height derived coordinate ("altitude"). | ||
| delta = AuxCoord(points=np.array([0, 1]), long_name="delta", units="m") | ||
| sigma = AuxCoord(points=np.array([0, 1]), long_name="sigma") | ||
| orography = AuxCoord( | ||
| np.arange(12).reshape(3, 4), units="m", long_name="orography" | ||
| ) | ||
| cube.add_aux_coord(delta, 0) | ||
| cube.add_aux_coord(sigma, 0) | ||
| cube.add_aux_coord(orography, (1, 2)) | ||
| factory = HybridHeightFactory(delta=delta, sigma=sigma, orography=orography) | ||
| cube.add_aux_factory(factory) |
There was a problem hiding this comment.
Since this is only used in one test, it possibly makes sense to move this to the test itself.
Normally, I'd avoid making tests unreadable, but I think in this case it's a clear enough set-up that it should be okay. Alternatively, I could see an argument for making this its own fixture.
| """Tests for :meth:`iris.cube.Cube.coord_dims`.""" | ||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _setup(self): |
There was a problem hiding this comment.
This setup is quite hard to read. I'd suggest breaking it up into code blocks of related bits, separated by lines.
For example, one structure could be:
- Set-up the cube
- Set-up the DimCoords (and add them to the cube)
- Set-up the AuxCoords (and add them to the cube)
- Set-up the hybrid heights AuxCoords, and add them to the cube, then set-up the factory. (If this stays in the setup at all)
- Assigned them all to class variables.
Address review feedback from @ESadek-MO: - Move hybrid-height set-up into the single test that uses it - Group _setup into clear blocks: cube → DimCoords → AuxCoords → assigns
|
Hi @ESadek-MO, Thanks so much for the review and the kind words! I've just pushed an update (commit 8d101a2) that addresses both points:
No test logic or assertions were changed. Let me know if you'd prefer the hybrid-height set-up extracted into its own fixture instead of inlined into the test; happy to switch it over. Thanks again for your time! |
|
Hi @ESadek-MO, just a gentle follow-up on the changes I pushed in 8d101a2 — the I'm still happy to extract the hybrid-height set-up into a fixture if you'd prefer that over inlining it in the test — just say the word and I'll switch it over. No rush at all on my end; thanks again for the review! |
Description
Adds specific unit tests for
Cube.coord_dims, addressing #4552.Cube.coord_dimspreviously had no direct unit tests inlib/iris/tests/unit/cube/test_Cube.py(the existingCoord.cube_dims/CellMeasure.cube_dims/AncillaryVariable.cube_dimstests only mock-verify that those methods delegate toCube.coord_dims, without exercisingcoord_dimsitself).This adds a
Test_coord_dimsclass covering the seven scenarios listed in the issue, each exercising a distinct branch of the method:test_dim_coordtest_equivalent_coord_not_instancetest_no_match_raisestest_string_nametest_aux_coord_multiple_dimstest_aux_factorytest_aux_coord_single_dimCloses #4552.
Checklist
changelog/(follow-up commit)Testing
All 7 new tests pass, and the full
test_Cube.pysuite shows no regressions:ruff checkandruff format --checkboth pass with no warnings.