Add defensive checks and tests for single-array JSON spatial query responses#206
Conversation
|
WalkthroughA defensive check was added to two functions handling spatial queries to prevent crashes when unexpected single-array JSON responses are received instead of nested arrays. Corresponding test cases were introduced to verify correct behavior when the response structure varies, particularly for edge cases outside designated areas. Changes
Sequence Diagram(s)sequenceDiagram
participant TestSuite
participant HelperFunction
participant GeoJSONResponse
TestSuite->>HelperFunction: Call with centroid (edge case)
HelperFunction->>GeoJSONResponse: Query for spatial area inclusion
GeoJSONResponse-->>HelperFunction: Return single array or nested array
HelperFunction->>HelperFunction: Check if response is non-empty array
alt Non-empty and nested
HelperFunction->>TestSuite: Return true
else Empty or single array
HelperFunction->>TestSuite: Return false
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Assessment against linked issues
Assessment against linked issues: Out-of-scope changesNo out-of-scope changes identified. Possibly related PRs
Suggested reviewers
Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## development #206 +/- ##
============================================
Coverage 94.04% 94.04%
============================================
Files 85 85
Lines 12628 12632 +4
Branches 1290 1291 +1
============================================
+ Hits 11876 11880 +4
Misses 750 750
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
fdm-calculator/src/norms/nl/2025/dierlijke-mest-gebruiksnorm.test.ts (1)
83-83: Fix typo in test description.There's a spelling error: "responce" should be "response".
- it("should return the default norm value for derogation outside Grondwaterbeschermingsgebied and inside NV-gebied, but with single array responce (see #205)", async () => { + it("should return the default norm value for derogation outside Grondwaterbeschermingsgebied and inside NV-gebied, but with single array response (see #205)", async () => {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
fdm-calculator/src/norms/nl/2025/dierlijke-mest-gebruiksnorm.test.ts(1 hunks)fdm-calculator/src/norms/nl/2025/dierlijke-mest-gebruiksnorm.ts(1 hunks)fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.test.ts(1 hunks)fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.ts(1 hunks)
🧰 Additional context used
🧠 Learnings (4)
fdm-calculator/src/norms/nl/2025/dierlijke-mest-gebruiksnorm.test.ts (2)
Learnt from: SvenVw
PR: #156
File: fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.ts:295-303
Timestamp: 2025-07-21T12:06:07.070Z
Learning: Functions in the fdm-calculator with "NL2025" in their names are specifically designed for Netherlands 2025 agricultural norms calculation and hardcoded 2025 dates are appropriate in this context, as different years would have separate calculation modules.
Learnt from: SvenVw
PR: #9
File: fdm-data/src/cultivations/index.test.ts:57-59
Timestamp: 2024-11-27T12:15:36.425Z
Learning: In fdm-data/src/cultivations/index.test.ts, the fdm object created by drizzle does not have an .end() method. Cleanup code should not attempt to call fdm.end();.
fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.ts (2)
Learnt from: SvenVw
PR: #156
File: fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.ts:295-303
Timestamp: 2025-07-21T12:06:07.070Z
Learning: Functions in the fdm-calculator with "NL2025" in their names are specifically designed for Netherlands 2025 agricultural norms calculation and hardcoded 2025 dates are appropriate in this context, as different years would have separate calculation modules.
Learnt from: SvenVw
PR: #67
File: fdm-app/app/routes/farm.create.$b_id_farm.fields.$b_id.tsx:601-610
Timestamp: 2025-01-31T15:05:14.310Z
Learning: The updateField function in fdm-core has optional parameters after fdm and b_id. The TypeScript definitions might show 8 required parameters due to a potential version mismatch.
fdm-calculator/src/norms/nl/2025/dierlijke-mest-gebruiksnorm.ts (3)
Learnt from: SvenVw
PR: #156
File: fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.ts:295-303
Timestamp: 2025-07-21T12:06:07.070Z
Learning: Functions in the fdm-calculator with "NL2025" in their names are specifically designed for Netherlands 2025 agricultural norms calculation and hardcoded 2025 dates are appropriate in this context, as different years would have separate calculation modules.
Learnt from: SvenVw
PR: #9
File: fdm-data/src/cultivations/index.test.ts:57-59
Timestamp: 2024-11-27T12:15:36.425Z
Learning: In fdm-data/src/cultivations/index.test.ts, the fdm object created by drizzle does not have an .end() method. Cleanup code should not attempt to call fdm.end();.
Learnt from: SvenVw
PR: #9
File: fdm-data/src/cultivations/catalogues/brp.ts:9-19
Timestamp: 2024-11-27T10:28:18.683Z
Learning: When importing JSON data like brp.json that is fully typed and controlled within the codebase, and TypeScript's static analysis is sufficient, additional runtime validation may not be necessary.
fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.test.ts (1)
Learnt from: SvenVw
PR: #156
File: fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.ts:295-303
Timestamp: 2025-07-21T12:06:07.070Z
Learning: Functions in the fdm-calculator with "NL2025" in their names are specifically designed for Netherlands 2025 agricultural norms calculation and hardcoded 2025 dates are appropriate in this context, as different years would have separate calculation modules.
🧬 Code Graph Analysis (2)
fdm-calculator/src/norms/nl/2025/dierlijke-mest-gebruiksnorm.test.ts (1)
fdm-calculator/src/norms/nl/2025/dierlijke-mest-gebruiksnorm.ts (1)
getNL2025DierlijkeMestGebruiksNorm(73-102)
fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.test.ts (1)
fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.ts (1)
isFieldInNVGebied(25-52)
🔇 Additional comments (4)
fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.test.ts (1)
24-28: LGTM! Good test coverage for the single array response edge case.This test case properly verifies the defensive check added to handle issue #205, ensuring that
isFieldInNVGebiedreturnsfalsewhen the API returns a single array response for coordinates outside the NV Gebied. The test follows the established pattern and includes a helpful reference to the issue number.fdm-calculator/src/norms/nl/2025/dierlijke-mest-gebruiksnorm.ts (1)
35-40: Excellent defensive programming to handle API response variations.The added check prevents runtime errors when the external API returns a single array instead of the expected nested array structure. This implementation correctly handles the edge case described in issue #205 by:
- Verifying the response array has elements before accessing nested properties
- Safely accessing
json[0][0]only when the array is non-empty- Returning
falsewhen the expected nested structure is not presentThe solution is consistent with the similar fix applied to
isFieldInNVGebied.fdm-calculator/src/norms/nl/2025/dierlijke-mest-gebruiksnorm.test.ts (1)
84-96: LGTM! Good integration test for the defensive check.This test case effectively verifies that the defensive logic added to handle single array responses works correctly within the full norm calculation flow. The expected values (190 for derogation in NV Gebied) align with the business logic, and the test includes a helpful reference to issue #205.
fdm-calculator/src/norms/nl/2025/stikstofgebruiksnorm.ts (1)
41-47: Excellent defensive check implementation addressing issue #205.The added length validation prevents runtime errors when the external API returns a single array instead of the expected nested array structure. This implementation:
- Safely checks array length before accessing nested elements
- Only attempts to access
json[0][0]when the array is non-empty- Returns
falsewhen the expected nested feature is not presentThis change complements the identical fix in
isFieldInGWGBGebiedand provides consistent error handling across both geographical query functions.
BoraIneviNMI
left a comment
There was a problem hiding this comment.
Looks good. I have checked it and the issue seems to be fixed now.
Summary by CodeRabbit
Closes #205