test: add test cases for missing products, zero distance, duplicate I…#231
Conversation
…Ds, and empty product lists in RoutingService
📝 WalkthroughWalkthroughFour new test cases extend coverage of ChangesTest Coverage Extension
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. 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. Review rate limit: 0/1 reviews remaining, refill in 60 minutes.Comment |
|
@coderabbitai cancel |
|
Tip For best results, initiate chat on the files or code changes.
[pause] |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/test/java/com/p2ps/service/RoutingServiceTest.java (2)
463-477: ⚡ Quick winMake zero-distance test deterministic about checkout handling.
assertEquals(0.0, totalDistanceMeters, ...)currently depends on implicit mock default behavior forqueryForObject. Explicitly force “no checkout” in this test so route-distance intent is unambiguous.Suggested patch
`@Test` `@SuppressWarnings`("unchecked") void calculateOptimalRoute_shouldHandleZeroDistanceImprovement() { when(jdbcTemplate.queryForList(anyString(), eq(String.class), anyDouble(), anyDouble())) .thenReturn(List.of(STORE_ID)); // User and product at the same location -> distance is 0 RoutingService.ProductLocation p1 = new RoutingService.ProductLocation(ITEM_1, "P1", 47.156, 27.587, 0.9); when(jdbcTemplate.query(anyString(), any(RowMapper.class), any(Object[].class))) .thenReturn(List.of(p1)); + when(jdbcTemplate.queryForObject(anyString(), any(RowMapper.class), anyString())) + .thenThrow(new org.springframework.dao.EmptyResultDataAccessException(1)); RoutingRequest request = new RoutingRequest(47.156, 27.587, List.of(ITEM_1), 0); RoutingResponse response = service.calculateOptimalRoute(request);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/com/p2ps/service/RoutingServiceTest.java` around lines 463 - 477, The test calculateOptimalRoute_shouldHandleZeroDistanceImprovement is nondeterministic because it relies on implicit defaults for jdbcTemplate.queryForObject (used by RoutingService.calculateOptimalRoute to check for an existing checkout); explicitly stub that call in the test to return the "no checkout" value your service expects (e.g., null/Optional.empty/false depending on the queryForObject signature) so the route distance logic is unambiguous and the assertion on response.getTotalDistanceMeters() remains deterministic.
481-498: ⚡ Quick winStrengthen duplicate-ID assertions to avoid false positives.
route.size() >= 2is too permissive and can pass even if duplicate handling changes unexpectedly. Add assertions on item occurrence and warnings for the duplicated ID.Suggested patch
RoutingResponse response = service.calculateOptimalRoute(request); assertEquals("success", response.getStatus()); - // Depending on implementation, we might have 1 product in route (unique) or 2. - // Current logic in getProductLocations doesn't de-duplicate, it just returns what the DB returns. - // If DB returns one row (because item_id is unique per store), then we have 1 product. - assertTrue(response.getRoute().size() >= 2); // user + at least one product + long item1Occurrences = response.getRoute().stream() + .filter(p -> ITEM_1.equals(p.getItemId())) + .count(); + assertEquals(1, item1Occurrences); + assertTrue(response.getWarnings().stream() + .noneMatch(w -> w.contains(ITEM_1) && w.contains("nu a fost gasit")));🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/com/p2ps/service/RoutingServiceTest.java` around lines 481 - 498, The test calculateOptimalRoute_shouldHandleDuplicateProductIds is too permissive; replace the generic assertTrue(response.getRoute().size() >= 2) with precise checks: using RoutingResponse.getRoute() assert the route includes the user/start node and at least one product node, then count occurrences of ITEM_1 in the route (e.g., stream/filter on route entries' productId) and assert the count matches expected behavior (for current getProductLocations behavior assert count == 1 if DB returns one row) and also assert that RoutingResponse.getWarnings() (or similar) contains a warning about the duplicated ID when duplicates are submitted; reference RoutingService.ProductLocation, getProductLocations, calculateOptimalRoute, RoutingRequest, and RoutingResponse to locate the code to update.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/test/java/com/p2ps/service/RoutingServiceTest.java`:
- Around line 463-477: The test
calculateOptimalRoute_shouldHandleZeroDistanceImprovement is nondeterministic
because it relies on implicit defaults for jdbcTemplate.queryForObject (used by
RoutingService.calculateOptimalRoute to check for an existing checkout);
explicitly stub that call in the test to return the "no checkout" value your
service expects (e.g., null/Optional.empty/false depending on the queryForObject
signature) so the route distance logic is unambiguous and the assertion on
response.getTotalDistanceMeters() remains deterministic.
- Around line 481-498: The test
calculateOptimalRoute_shouldHandleDuplicateProductIds is too permissive; replace
the generic assertTrue(response.getRoute().size() >= 2) with precise checks:
using RoutingResponse.getRoute() assert the route includes the user/start node
and at least one product node, then count occurrences of ITEM_1 in the route
(e.g., stream/filter on route entries' productId) and assert the count matches
expected behavior (for current getProductLocations behavior assert count == 1 if
DB returns one row) and also assert that RoutingResponse.getWarnings() (or
similar) contains a warning about the duplicated ID when duplicates are
submitted; reference RoutingService.ProductLocation, getProductLocations,
calculateOptimalRoute, RoutingRequest, and RoutingResponse to locate the code to
update.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 04922f74-28e0-4477-9361-d9c80d6c4fc9
📒 Files selected for processing (1)
src/test/java/com/p2ps/service/RoutingServiceTest.java
|



…Ds, and empty product lists in RoutingService
Summary by CodeRabbit
Release Notes