Skip to content

Conversation

@pm-dimagi
Copy link
Contributor

@pm-dimagi pm-dimagi commented Feb 4, 2025

Product Description

Test cases to test the sorting of an table from the storage through SQL query

Technical Summary

Sort function take the field through which sorting need to be done and take value ASC or DESC to describe the order of the sorting

Safety Assurance

Safety story

there is the test case written so it is all passed so there is no safety threat in this

Automated test coverage

Local test cases written

QA Plan

Special deploy instructions

  • This PR can be deployed after merge with no further considerations.

Rollback instructions

  • This PR can be reverted after deploy with no further considerations.

Review

  • The set of people pinged as reviewers is appropriate for the level of risk of the change.

Duplicate PR

Automatically duplicate this PR as defined in contributing.md.

Summary by CodeRabbit

  • New Features

    • Introduced enhanced record retrieval capabilities that allow users to fetch data sorted by specified fields. Sorting can be done in ascending or descending order based on provided criteria, offering more flexible data management.
  • Tests

    • Updated automated tests to validate the new sorting functionality and ensure consistent, correct ordering of records in various scenarios.

@coderabbitai
Copy link

coderabbitai bot commented Feb 4, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This pull request adds a new method, getSortedRecordsForValues, to the IStorageUtilityIndexed interface and its implementation in the DummyIndexedStorageUtility class. The method accepts metadata field names, corresponding values, and an order-by string to return sorted records. Additionally, a helper method using reflection is introduced to retrieve field values, and the test suite is updated with a new array of Shoe instances and assertions to verify the sorted retrieval functionality.

Changes

File Change Summary
src/main/.../IStorageUtilityIndexed.java Added the method getSortedRecordsForValues(String[] metaFieldNames, Object[] values, String orderby) to the interface.
src/main/.../DummyIndexedStorageUtility.java Implemented getSortedRecordsForValues with Java reflection for ordering and introduced the private helper method getFieldValue for field retrieval.
src/test/.../IndexedStorageUtilityTests.java Added a new array fiveSortedSizesOfMensAddidas of Shoe objects, updated test methods to verify sorted records retrieval, and included storage writes for the new data.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Suite
    participant Interface as IStorageUtilityIndexed
    participant Dummy as DummyIndexedStorageUtility
    participant Reflect as Reflection (getFieldValue)

    Test->>Interface: call getSortedRecordsForValues(metaFieldNames, values, orderby)
    Interface->>Dummy: delegate call to getSortedRecordsForValues
    Dummy->>Reflect: retrieve field values for each record
    Reflect-->>Dummy: return field value
    Dummy-->>Test: return sorted record list
Loading

Poem

I'm a little rabbit with a code-filled heart,
Hopping through records and sorting each part.
Carrots of metadata, lined up just right,
Reflecting on fields in the soft moonlight.
I celebrate these changes with a joyful leap and delight!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 6

🧹 Nitpick comments (1)
src/test/java/org/javarosa/core/storage/IndexedStorageUtilityTests.java (1)

132-133: Add assertion message for better test failure diagnostics.

The assertion message incorrectly references "vans" instead of "adidas".

-        Assert.assertArrayEquals("Failed index match [brand,style][vans,mens]", fiveSortedSizesOfMensAddidas,matchedSortedRecords.toArray());
+        Assert.assertArrayEquals("Failed index match [brand,style][adidas,mens]", fiveSortedSizesOfMensAdidas,matchedSortedRecords.toArray());
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d9e7c69 and 92395b2.

📒 Files selected for processing (3)
  • src/main/java/org/javarosa/core/services/storage/IStorageUtilityIndexed.java (1 hunks)
  • src/main/java/org/javarosa/core/services/storage/util/DummyIndexedStorageUtility.java (2 hunks)
  • src/test/java/org/javarosa/core/storage/IndexedStorageUtilityTests.java (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: task-list-completed
  • GitHub Check: task-list-completed
  • GitHub Check: task-list-completed
  • GitHub Check: task-list-completed

@pm-dimagi
Copy link
Contributor Author

@coderabbitai I pushed a fix in commit f2a04ae, please review it

@coderabbitai
Copy link

coderabbitai bot commented Feb 4, 2025

@pm-dimagi The changes look good! You've made several improvements:

  1. Enhanced documentation and parameter validation
  2. Added proper null handling in the comparator
  3. Improved error handling
  4. Fixed the "Adidas" spelling
  5. Added more test coverage for ascending sort and invalid parameters

(`∇´ゞ)

🔗 Analysis chain
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Fetch the commit changes
git show f2a04ae77250dd9a01efcb27581957efec7dc44c

Length of output: 8288

✅ Actions performed

Comments resolved.

* they may need to clean up if the bulk read doesn't complete
*/

Vector<E> getSortedRecordsForValues(String[] metaFieldNames, Object[] values,String orderby);
Copy link
Contributor

Choose a reason for hiding this comment

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

Java docs are out of order

Copy link
Contributor

Choose a reason for hiding this comment

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

@pm-dimagi stil seems pending

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

uff! Java docs are still wrong for this method, they copy the text from another java doc which is not relevant to this method at all - "If the provided recordMap already contains entries for any ids, it is not
* required for them to be retrieved from storage again." .

Comment on lines 219 to 221
} catch (Exception e) {
// Log error and continue with no ordering
System.err.println("Failed to compare records: " + e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

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

we should not be catching generic exceptions

Copy link
Contributor

Choose a reason for hiding this comment

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

@pm-dimagi stil seems pending

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

the ask here was to remove the catch clause, not to rename the variable.

Vector<Shoe> matchedAscSortedRecords = storage.getSortedRecordsForValues(new String[]{Shoe.META_BRAND, Shoe.META_STYLE}, new String[]{"vans", "mens"},Shoe.META_SIZE+" ASC");
Assert.assertArrayEquals("Failed index match [brand,style][adidas,mens]", matchedAscSortedRecords.toArray(),fiveSizesOfMensVans);

Vector<Shoe> matchedAscSortedRecordsTest = storage.getSortedRecordsForValues(new String[]{Shoe.META_BRAND, Shoe.META_STYLE}, new String[]{"vans", "mens"},Shoe.META_SIZE+" ASSC");
Copy link
Contributor

Choose a reason for hiding this comment

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

this call should fail right given ASSC is not the right keyword ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revert the array is asc order as default value is ascending

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should be allowing for incorrrect keywords in this method and should do a strict match and else throw an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added in the new commit 9007023

@pm-dimagi pm-dimagi requested a review from shubham1g5 February 5, 2025 15:49
Vector<Shoe> matchedAscSortedRecordsWithoutKey = storage.getSortedRecordsForValues(new String[]{Shoe.META_BRAND, Shoe.META_STYLE}, new String[]{"vans", "mens"}, " DESC");
Assert.assertArrayEquals("Failed the sorted match [brand,style][vans,mens]", fiveSizesOfMensVans, fiveSizesOfMensVans);
}catch (IllegalArgumentException e){
Assert.assertArrayEquals(e.getMessage(), matchedAscSortedRecords.toArray(), fiveSizesOfMensVans);
Copy link
Contributor

Choose a reason for hiding this comment

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

why should the array be sorted in case of an exception ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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


try {
Vector<Shoe> matchedAscSortedRecordsWithoutKey = storage.getSortedRecordsForValues(new String[]{Shoe.META_BRAND, Shoe.META_STYLE}, new String[]{"vans", "mens"}, " DESC");
Assert.assertArrayEquals("Failed the sorted match [brand,style][vans,mens]", fiveSizesOfMensVans, fiveSizesOfMensVans);
Copy link
Contributor

Choose a reason for hiding this comment

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

looks incorrect as we are comparing the same array fiveSizesOfMensVans to itself

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pm-dimagi pm-dimagi requested a review from shubham1g5 February 11, 2025 10:21
Copy link
Contributor

@shubham1g5 shubham1g5 left a comment

Choose a reason for hiding this comment

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

Code formatting seems quite off, please format ANY NEW code in Android Studio with the code style files

new Shoe("vans", "mens", i + 5);
}

fiveSortedSizesOfMensAdidas = new Shoe[5];
Copy link
Contributor

Choose a reason for hiding this comment

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

as I see this is only used for matching an expected outcome of a test, so this must only be used in that particular test case and should not be written to db at all.

try {
Vector<Shoe> matchedDescSortedRecordsWithoutKey = storage.getSortedRecordsForValues(new String[]{Shoe.META_BRAND, Shoe.META_STYLE}, new String[]{"adidas", "mens"}, " DESC");
Assert.assertArrayEquals("Failed the sorted match [brand,style][adidas,mens]", matchedDescSortedRecordsWithoutKey.toArray(), fiveSortedSizesOfMensAdidas);
}catch (IllegalArgumentException e){
Copy link
Contributor

Choose a reason for hiding this comment

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

why catch anything here ?

Comment on lines 154 to 159
try {
Vector<Shoe> matchedAscSortedRecordsTest = storage.getSortedRecordsForValues(new String[]{Shoe.META_BRAND, Shoe.META_STYLE}, new String[]{"vans", "mens"}, Shoe.META_SIZE + " ASSC");
Assert.assertArrayEquals("Failed the sorted match [brand,style][adidas,mens]", matchedAscSortedRecordsTest.toArray(), fiveSizesOfMensVans);
}catch (IllegalArgumentException e){
System.out.println(e.getMessage());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

this code needs to verify the exception by following this pattern -

  Exception exception = assertThrows(IllegalArgumentException.class, () -> {
        // Code that should throw an exception
    });

    //  check the exception message
    assertEquals("Invalid argument", exception.getMessage());

@shubham1g5
Copy link
Contributor

@pm-dimagi The build seems to failing as well

Shoe[] fiveSizesOfMensVans;
Shoe[] fiveSortedSizesOfMensAdidas;

Shoe[] combinedCollection;
Copy link
Contributor

Choose a reason for hiding this comment

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

should not be global

}
}

void combineAllList(){
Copy link
Contributor

Choose a reason for hiding this comment

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

code formatting is still off in the method

shubham1g5
shubham1g5 previously approved these changes Feb 14, 2025
# Conflicts:
#	src/main/java/org/javarosa/core/services/storage/IStorageUtilityIndexed.java
#	src/main/java/org/javarosa/core/services/storage/util/DummyIndexedStorageUtility.java
#	src/test/java/org/javarosa/core/storage/IndexedStorageUtilityTests.java
@pm-dimagi pm-dimagi requested a review from shubham1g5 February 17, 2025 14:34
@shubham1g5
Copy link
Contributor

Closing since we are not maintaining data types correctly on Android database which results in sort like this doing a String sort and changing/correcting data types at this point will require a careful db migration which we want to avoid doing. We should revisit this though if in-memory sort on top of Sqlite becomes a bottleneck for any of the features.

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.

3 participants