Skip to content

Add unit tests for Queryable#826

Open
vasiliy-mikhailov wants to merge 1 commit into
approvals:masterfrom
vasiliy-mikhailov:add-unit-tests-queryable
Open

Add unit tests for Queryable#826
vasiliy-mikhailov wants to merge 1 commit into
approvals:masterfrom
vasiliy-mikhailov:add-unit-tests-queryable

Conversation

@vasiliy-mikhailov

@vasiliy-mikhailov vasiliy-mikhailov commented Jun 26, 2026

Copy link
Copy Markdown

Additive unit tests for Queryable — covering edge cases and pinning current behavior with explicit assertions. Includes a small CustomQuery test fixture (an Extendable implementation) used by the extension-mechanism test. No existing test or production code is changed; both are new test files only.

Verified green under Java 17:

  • mvn -pl approvaltests-util test -Dtest=QueryableTestTests run: 95, Failures: 0, Errors: 0

Summary by Sourcery

Add comprehensive unit tests for Queryable to cover constructors, core query operations, edge cases, and extension mechanisms without changing production code.

Tests:

  • Introduce QueryableTest to exercise factory methods, selection, filtering, aggregation, grouping, joining, splitting, pagination, distinct, and recursive selection behaviors across typical and edge-case inputs.
  • Add CustomQuery Extendable fixture to verify the use() extension mechanism on Queryable for custom query operations.

@sourcery-ai

sourcery-ai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds a comprehensive JUnit 5 test suite for org.lambda.query.Queryable plus a small CustomQuery Extendable implementation used as a test fixture, covering constructors, factories, core query operators, numeric aggregations, grouping/joining/splitting, extension mechanism, and a variety of edge cases (empty, nulls, single element, ties, type inference).

File-Level Changes

Change Details Files
Introduce a broad QueryableTest JUnit 5 suite that exercises constructors, factory methods, basic querying operators, aggregations, ordering, distinct, projection/flattening, grouping, joining, splitting, pagination, recursion, extension mechanism, and diverse edge cases.
  • Add constructor and createEmpty tests to verify type and size behavior for null, empty, and non-empty inputs.
  • Add of/as factory tests for varargs, lists, sets, arrays, streams, and already-Queryable inputs, including explicit type-parameter variants and common-supertype inference.
  • Add tests for select, where, first/firstOrDefault/firstOrThrow, last, all, any, max/min (with and without selectors), sum, and average, with both normal and empty-collection cases.
  • Add tests for orderBy (ascending/descending), distinct, asArray, selectMany/selectManyArray, groupBy overloads (key-only, key+value, key+value+aggregator), join/join with transform, and split behavior including no split points, all split points, and empty input.
  • Add tests for skip and take (including combinations and over-large counts), combine, selectRecursivelyUntil (multi-element and single-element recursion flow), and use(Extendable) via a custom extension.
  • Add dedicated edge-case tests for empty collections, empty lists/arrays with explicit type, single-element collections, null handling in predicates and selectors, and tie-breaking semantics for max/min selectors (first wins).
approvaltests-util/src/test/java/org/lambda/query/QueryableTest.java
Add CustomQuery Extendable implementation as a fixture to test Queryable.use(...) extension mechanism.
  • Implement CustomQuery as Extendable<List> capturing the caller list via setCaller.
  • Provide findFirstWordsOnly, which maps each string to its first word using Query.select, returning a Queryable used by the QueryableTest.useExtendable test.
approvaltests-util/src/test/java/org/lambda/query/CustomQuery.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The QueryableTest class is quite large and covers many different behaviors; consider splitting it into smaller test classes (e.g., grouping by feature: construction, selection/filtering, aggregation, grouping, etc.) to keep each class focused and easier to navigate.
  • Several test patterns (e.g., max/min with selectors, all/any single vs. empty cases) could be refactored into parameterized tests to reduce repetition and make it clearer which input/output combinations are being exercised.
  • For edge-case tests that are explicitly pinning surprising behavior (such as split dropping the trailing segment and average on empty returning NaN/Infinity), consider making the intent even more explicit in the test names (e.g., testSplitDoesNotIncludeTrailingSegment) to communicate that these semantics are deliberate rather than incidental.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `QueryableTest` class is quite large and covers many different behaviors; consider splitting it into smaller test classes (e.g., grouping by feature: construction, selection/filtering, aggregation, grouping, etc.) to keep each class focused and easier to navigate.
- Several test patterns (e.g., max/min with selectors, all/any single vs. empty cases) could be refactored into parameterized tests to reduce repetition and make it clearer which input/output combinations are being exercised.
- For edge-case tests that are explicitly pinning surprising behavior (such as `split` dropping the trailing segment and `average` on empty returning NaN/Infinity), consider making the intent even more explicit in the test names (e.g., `testSplitDoesNotIncludeTrailingSegment`) to communicate that these semantics are deliberate rather than incidental.

## Individual Comments

### Comment 1
<location path="approvaltests-util/src/test/java/org/lambda/query/QueryableTest.java" line_range="25-34" />
<code_context>
+    assertEquals(3, result.get(0).intValue());
+  }
+
+  @Test
+  void testSkipMoreThanSize()
+  {
+    Queryable<Integer> q = Queryable.as(1, 2, 3);
+    Queryable<Integer> result = q.skip(10);
+    assertEquals(0, result.size());
+  }
+
+  @Test
+  void testTake()
+  {
+    Queryable<Integer> q = Queryable.as(1, 2, 3, 4, 5);
+    Queryable<Integer> result = q.take(3);
+    assertEquals(3, result.size());
+    assertEquals(1, result.get(0).intValue());
+  }
+
+  @Test
+  void testTakeMoreThanSize()
+  {
+    Queryable<Integer> q = Queryable.as(1, 2, 3);
+    Queryable<Integer> result = q.take(10);
+    assertEquals(3, result.size());
+  }
+
+  @Test
+  void testTakeAndSkipCombined()
+  {
+    Queryable<Integer> q = Queryable.as(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
</code_context>
<issue_to_address>
**suggestion (testing):** Add tests for skip()/take() with zero and negative counts to fully pin boundary behavior

Current tests cover normal usage and over-skipping/-taking, but not other boundaries. Please add cases for:
- `skip(0)` / `take(0)` to document whether they are no-ops and the resulting size.
- `skip(-1)` / `take(-1)` (if allowed) to verify how negatives are handled (clamped to 0, ignored, or error).
This will clarify the contract for edge arguments and prevent regressions.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +25 to +34
@Test
void testDefaultConstructor()
{
Queryable<String> q = new Queryable<>();
assertEquals(0, q.size());
}

@Test
void testConstructorWithType()
{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Add tests for skip()/take() with zero and negative counts to fully pin boundary behavior

Current tests cover normal usage and over-skipping/-taking, but not other boundaries. Please add cases for:

  • skip(0) / take(0) to document whether they are no-ops and the resulting size.
  • skip(-1) / take(-1) (if allowed) to verify how negatives are handled (clamped to 0, ignored, or error).
    This will clarify the contract for edge arguments and prevent regressions.

@JayBazuzi

Copy link
Copy Markdown
Contributor

Does this overlap or extend approvaltests-util-tests/src/test/java/org/lambda/query/QueryableTest.java?

@vasiliy-mikhailov

Copy link
Copy Markdown
Author

Good catch. I looked, and it's both.

There's genuine overlap: about 11 of these duplicate methods already in approvaltests-util-tests/.../QueryableTest.java by the same name (testDistinct, testJoin/testJoinWithTransformation, the testGroupBy* set, testSelectMany/testSelectManyCharacters, testInterfaceCommonality, testSuperClassCommonality). I also mistakenly added the file under approvaltests-util rather than the existing approvaltests-util-tests module.

The other ~80 are net-new coverage the existing suite doesn't have: where, all/any, max/min (including selectors and ties), sum/average, skip/take, split, firstOrDefault, orderBy, the of/as factory variants, plus systematic empty, null-element, single-element, and type-inference edge cases.

So it overlaps and extends. If the added coverage is useful, I'm glad to rework it properly: fold the net-new tests into the existing approvaltests-util-tests/QueryableTest.java, drop the duplicates, and remove the misplaced file. Or if it's not a fit, no problem closing it. Whichever you'd prefer.

Extends the existing approvaltests-util-tests/QueryableTest with additive tests
covering where, all/any, max/min (with selectors and ties), sum/average,
skip/take, split, orderBy, firstOrDefault, the of/as factory variants, and
empty/null/single-element edge cases. No production code changed.

Signed-off-by: vasiliy-mikhailov <vasiliy-mikhailov@users.noreply.github.com>
@vasiliy-mikhailov vasiliy-mikhailov force-pushed the add-unit-tests-queryable branch from 1355e98 to 4596881 Compare June 26, 2026 20:14
@vasiliy-mikhailov

Copy link
Copy Markdown
Author

Done, reworked. I folded the net-new tests into the existing approvaltests-util-tests/QueryableTest.java (now 104 tests, +84 added), dropped the ~11 exact duplicates, and removed the file I had misplaced under approvaltests-util. It reuses your existing CustomQuery fixture rather than adding a duplicate.

The added tests cover operators the existing suite didn't: where, all/any, max/min (with selectors and ties), sum/average, skip/take, split, orderBy, firstOrDefault, the of/as factory variants, plus empty, null-element, and single-element edge cases. Verified green under Java 17 (Tests run: 104, Failures: 0, Errors: 0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants