Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): fix filter bug #1276

Merged
merged 3 commits into from
Nov 25, 2024
Merged

fix(server): fix filter bug #1276

merged 3 commits into from
Nov 25, 2024

Conversation

hexaforce
Copy link
Contributor

@hexaforce hexaforce commented Nov 25, 2024

Overview

The query for cases where coresupport does not exist in the database is incorrect.

What I've done

What I haven't done

How I tested

It worked correctly after processing the data locally.

スクリーンショット 2024-11-25 13 49 58

スクリーンショット 2024-11-25 13 50 13

Which point I want you to review particularly

The test case was skipped because data without coresupport could not be created.

Memo

Summary by CodeRabbit

  • New Features

    • Enhanced asset retrieval logic to ensure only assets with core support are fetched.
    • Improved asset creation process in tests using a builder pattern for better structure and readability.
  • Bug Fixes

    • Streamlined filtering logic for asset queries, reducing complexity and improving performance.
  • Tests

    • Updated test functions to incorporate context management and repository interactions for better error handling and validation.

@hexaforce hexaforce requested a review from pyshx as a code owner November 25, 2024 04:45
Copy link

coderabbitai bot commented Nov 25, 2024

Walkthrough

The changes involve the FindByWorkspace method in the Asset struct located in server/internal/infrastructure/mongo/asset.go. The filtering logic has been simplified by replacing a complex $or condition for the coresupport field with a direct equality check for true values. The construction of the filter variable is now more straightforward, combining conditions without the use of the $or operator. The overall method structure remains unchanged, with no modifications to exported or public entity declarations.

Changes

File Path Change Summary
server/internal/infrastructure/mongo/asset.go Simplified the FindByWorkspace method's filter logic by replacing $or with a direct equality check for coresupport. Combined conditions for team and coresupport without using $or. No changes to method signatures or public entities.
server/e2e/gql_asset_test.go Updated TestGetAssets to utilize StartServerAndRepos, added context management, and improved asset creation and retrieval with new repository interactions.

Possibly related PRs

  • feat(server): query filter of core support #1231: This PR modifies project retrieval queries to ensure that only items with the coreSupport flag set to true are returned, which aligns with the changes made to the FindByWorkspace method in the main PR that also focuses on filtering based on coresupport.
  • feat(server): Add coreSupport to Asset #1246: This PR adds a coreSupport field to the Asset struct and modifies asset retrieval and creation logic to incorporate this field, directly relating to the changes made in the main PR's FindByWorkspace method.

Suggested reviewers

  • mkumbobeaty
  • pyshx

Poem

In the code where bunnies hop,
A filter change, we’ll never stop.
From $or to true, so neat and bright,
Simplified logic brings pure delight!
With every line, we dance and play,
Hopping through code, come what may! 🐇✨


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 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.

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

netlify bot commented Nov 25, 2024

Deploy Preview for reearth-web canceled.

Name Link
🔨 Latest commit d523288
🔍 Latest deploy log https://app.netlify.com/sites/reearth-web/deploys/674438a715568d00086e159b

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: 1

🧹 Outside diff range and nitpick comments (1)
server/internal/infrastructure/mongo/asset.go (1)

73-74: Consider adding a migration plan

If this is a breaking change in filtering behavior, existing assets without the coresupport field might need to be updated.

Consider:

  1. Adding a database migration to set a default value for existing records
  2. Documenting the change in behavior for other developers
  3. Implementing a gradual rollout strategy if this affects many assets
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 62ae80f and afd09e0.

📒 Files selected for processing (1)
  • server/internal/infrastructure/mongo/asset.go (1 hunks)
🔇 Additional comments (1)
server/internal/infrastructure/mongo/asset.go (1)

73-74: ⚠️ Potential issue

Verify the impact of this filtering change

The modification from an $or condition to a direct equality check significantly changes the filtering behavior:

  • Previously: matched if coresupport=true OR if the field was missing
  • Now: matches only if coresupport=true

This could potentially exclude assets that previously matched the filter if they don't have the coresupport field set.

Let's verify the impact:

Please clarify:

  1. Is this change intentional? The PR description mentions fixing a filter bug but doesn't specify the expected behavior.
  2. Are there any assets in the database without the coresupport field that should still be included in the results?
  3. Has this change been tested with existing data to ensure it doesn't break any functionality?
✅ Verification successful

Change in coresupport filtering is consistent with project implementation

The change in filtering behavior is intentional and aligns with the project repository implementation. The test data shows that:

  1. All test cases explicitly set the coresupport field (either true or false)
  2. The project repository also uses a direct coresupport: true filter in its implementation
  3. The CHANGELOG indicates this was an intentional feature addition (PR feat(server): add coreSupport flag to project #424)

The removal of the $or condition with $exists check is correct as the codebase consistently treats this as a required boolean field, not an optional one.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for assets that might be affected by this change
# Description: Look for any tests or code that relies on the previous behavior

# Check for test files that might need updates
rg -l "coresupport" --type go --glob "*_test.go"

# Look for any direct MongoDB queries in tests that might be affected
rg -A 5 '\$or.*coresupport' --type go

# Check for any documentation or comments about coresupport behavior
rg -i "coresupport" --type md

Length of output: 355


Script:

#!/bin/bash
# Let's check the test file and surrounding context for coresupport usage
rg -B 5 -A 5 "coresupport" server/internal/infrastructure/mongo/project_test.go

# Check the implementation of FindByWorkspace in project.go for comparison
ast-grep --pattern 'func (r *ProjectRepo) FindByWorkspace($$$) {
  $$$
}'

# Look for any other occurrences of coresupport in the codebase
rg -B 2 -A 2 "coresupport" --type go

Length of output: 5198

Comment on lines +73 to +74
"team": id.String(),
"coresupport": true,
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding test coverage for the filter behavior

The filter logic is critical for asset queries, but I don't see any tests being updated along with this change.

Consider adding test cases to verify:

  1. Assets with coresupport=true are included
  2. Assets with coresupport=false are excluded
  3. Assets without the coresupport field are excluded (new behavior)

Here's a suggested test structure:

func TestAsset_FindByWorkspace_CoreSupport(t *testing.T) {
    tests := []struct {
        name     string
        assets   []mongodoc.Asset
        expected int
    }{
        {
            name: "should return only assets with coresupport=true",
            assets: []mongodoc.Asset{
                {CoreSupport: true},
                {CoreSupport: false},
                {}, // missing coresupport field
            },
            expected: 1,
        },
    }
    // ... implement test logic
}

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: 1

🧹 Outside diff range and nitpick comments (3)
server/e2e/gql_asset_test.go (3)

4-15: Remove debug-related imports

The fmt package is only used for debug printing in the test. Consider removing both the import and the associated debug print statements for cleaner test code.


55-68: Enhance error assertions with descriptive messages

While error checking is present, consider adding descriptive messages to make test failures more informative:

-assert.Nil(t, err)
+assert.NoError(t, err, "failed to build asset")
-assert.Nil(t, err)
+assert.NoError(t, err, "failed to save asset")

76-78: Remove debug print statements

Remove the debug print loop as it's not providing any test value:

-for _, a := range as {
-    fmt.Printf("===== %v\n", a)
-}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between afd09e0 and 52f15c9.

📒 Files selected for processing (1)
  • server/e2e/gql_asset_test.go (2 hunks)
🔇 Additional comments (2)
server/e2e/gql_asset_test.go (2)

Line range hint 82-183: GraphQL operations look good!

The GraphQL operations are well-structured and properly include the coreSupport field in both mutations and queries.


Line range hint 81-183: Verify test coverage for the filter bug fix

While the test verifies that returned assets have coreSupport=true, let's ensure the test coverage is comprehensive:

✅ Verification successful

Test coverage for coreSupport filtering is comprehensive

The test file demonstrates thorough coverage of the coreSupport filtering functionality:

  • Creates assets with both true and false coreSupport values
  • Verifies the creation response contains correct coreSupport values
  • Tests the filtering by asserting that only assets with coreSupport=true are returned
  • Includes both file types (png, csv) in the test scenarios
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check test coverage for asset filtering logic

# Look for other test files that might need similar updates
fd -e go -e test.go . | xargs rg -l "coreSupport"

# Check for any TODOs or FIXMEs related to asset filtering
rg -i "todo|fixme|hack" -g "**/*asset*.go"

# Look for similar filter implementations that might need the same fix
ast-grep --pattern 'FindByWorkspace($$$) {
  $$$
}'

Length of output: 939


Script:

#!/bin/bash
# Let's check the actual implementation of asset filtering logic and its tests

# Check asset filtering implementation
ast-grep --pattern 'func ($$$) Filter($$$) {
  $$$
}'

# Look for test cases related to asset filtering
rg -A 5 -B 5 "coreSupport.*filter|filter.*coreSupport" server/pkg/asset/

# Check GraphQL resolver implementation for assets
rg -A 10 "type AssetResolver" server/internal/adapter/gql/

Length of output: 1089


Script:

#!/bin/bash
# Let's examine the test file more closely to understand the coverage

# Check the full test file content to understand test scenarios
cat server/e2e/gql_asset_test.go

# Look for asset query implementation
ast-grep --pattern 'func ($$$) Assets($$$) {
  $$$
}'

# Check for asset filtering in repository layer
rg -A 10 "type AssetRepository" server/pkg/asset/

Length of output: 4919

Comment on lines +69 to +75
f := int64(20)
as, _, err := r.Asset.FindByWorkspace(ctx, wID, repo.AssetFilter{
Pagination: usecasex.CursorPagination{
First: &f,
}.Wrap(),
})
assert.Nil(t, err)
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance test coverage for the filter bug fix

Given that this PR fixes a filter bug related to coreSupport, consider:

  1. Adding test cases that explicitly verify the fixed filtering behavior
  2. Testing edge cases with different coreSupport values
  3. Making the pagination limit configurable for better test flexibility
-f := int64(20)
+const defaultPageSize = int64(20)
+testCases := []struct {
+    name string
+    filter repo.AssetFilter
+    expectedCoreSupport bool
+    expectedCount int
+}{
+    {
+        name: "only core support assets",
+        filter: repo.AssetFilter{
+            Pagination: usecasex.CursorPagination{
+                First: &defaultPageSize,
+            }.Wrap(),
+        },
+        expectedCoreSupport: true,
+        expectedCount: 3,  // adjust based on your test data
+    },
+    // Add more test cases
+}

Committable suggestion skipped: line range outside the PR's diff.

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: 1

🧹 Outside diff range and nitpick comments (2)
server/e2e/gql_asset_test.go (2)

80-80: Consider using a named constant for pagination limit

The magic number 20 should be defined as a constant for better maintainability.

-f := int64(20)
+const defaultPageSize = int64(20)
+f := defaultPageSize

Line range hint 89-93: Consider enhancing GraphQL response validation

While the test verifies that returned assets have coreSupport=true, it could be more comprehensive:

  1. Verify the total count matches expected filtered results
  2. Verify other asset properties remain intact
  3. Add negative test cases to ensure assets with coreSupport=false are not returned
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 52f15c9 and d523288.

📒 Files selected for processing (1)
  • server/e2e/gql_asset_test.go (2 hunks)
🔇 Additional comments (4)
server/e2e/gql_asset_test.go (4)

4-4: LGTM: Import changes align with new functionality

The added imports support the new test functionality for direct DB operations and pagination.

Also applies to: 11-14


26-26: LGTM: Enhanced test setup with repository access

The change from StartServer to StartServerAndRepos appropriately provides repository access needed for direct DB operations.


54-88: 🛠️ Refactor suggestion

Enhance test coverage for filter bug fix

The test could be more explicit about verifying the filter bug fix. Consider:

  1. Adding test cases that explicitly verify different coreSupport scenarios
  2. Testing edge cases (null, undefined, false values)
  3. Adding descriptive comments about what each test case verifies

The previous review comment about enhancing test coverage is still relevant. Consider implementing the suggested test structure to make the test cases more explicit and maintainable.


81-85: Verify the filter implementation

Let's verify the actual filter implementation that was changed to fix the bug.

✅ Verification successful

Let me generate additional verification scripts to find the implementation of FindByWorkspace and understand the filter implementation.


Let me search for the actual implementation of the Asset repository to understand how the filter is applied.


Let me try one more search to find the asset domain model and filter implementation with corrected paths.


Based on the code analysis, I can now provide a final response about the filter implementation.

Filter implementation is working as expected

The FindByWorkspace implementation in the mongo Asset repository correctly applies the coreSupport filter:

var filter any = bson.M{
    "team": id.String(),
    "coresupport": true,
}

The test code in server/e2e/gql_asset_test.go is only using pagination filter, which is correctly passed to the repository layer. The coreSupport field is properly defined in the Asset model (server/pkg/asset/asset.go) and is being filtered at the database level.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find the implementation of FindByWorkspace method to verify the filter fix

# Search for the FindByWorkspace implementation
ast-grep --pattern 'func (*_) FindByWorkspace(ctx context.Context, $_, $_) ($_, $_, $_) {
  $$$
}'

# Search for any filter conditions related to coreSupport
rg -A 5 'coreSupport'

Length of output: 117189


Script:

#!/bin/bash
# Search for FindByWorkspace implementation in repository files
ast-grep --pattern 'func (*_) FindByWorkspace(ctx context.Context, $_, $filter) ($_, $_, $_) {
  $$$
}'

# Search for AssetFilter struct definition and usage
rg -A 10 "type AssetFilter struct"

# Search for any filter-related code in asset repository implementation
rg -A 5 "FindByWorkspace.*Filter"

Length of output: 5812


Script:

#!/bin/bash
# Search for the Asset repository implementation in mongo package
rg -A 20 "type Asset struct" server/internal/infrastructure/mongo/

# Search for the actual filter implementation in the mongo Asset repository
ast-grep --pattern 'func (*Asset) FindByWorkspace($ctx context.Context, $id accountdomain.WorkspaceID, $filter repo.AssetFilter) ($_, $_, $_) {
  $$$
}'

# Look for any Asset model definition to understand the fields
rg -A 10 "type Asset struct" server/internal/domain/

Length of output: 1834


Script:

#!/bin/bash
# Search for Asset model definition in all possible domain locations
rg -A 10 "type Asset struct" server/pkg/
rg -A 10 "type Asset struct" server/internal/

# Search for the mongo implementation of FindByWorkspace with correct path
rg -A 20 "func \(r \*Asset\) FindByWorkspace" server/internal/infrastructure/mongo/asset.go

# Search for any filter-related code in the asset package
rg -A 5 "Filter" server/internal/infrastructure/mongo/asset.go

Length of output: 6537

Comment on lines +80 to +87
f := int64(20)
as, _, err := r.Asset.FindByWorkspace(ctx, wID, repo.AssetFilter{
Pagination: usecasex.CursorPagination{
First: &f,
}.Wrap(),
})
assert.Nil(t, err)
assert.Equal(t, len(as), 3)
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical: Test assertion may be incorrect

The assertion assert.Equal(t, len(as), 3) expects all assets to be returned, but based on the PR objectives and AI summary, the filter should only return assets where coreSupport is true. This test might be masking the filter bug instead of verifying the fix.

Consider updating the assertion to verify only assets with coreSupport=true are returned:

-assert.Equal(t, len(as), 3)
+expectedCount := 1 // Only a2 has coreSupport=true
+assert.Equal(t, expectedCount, len(as))
+for _, a := range as {
+    assert.True(t, a.CoreSupport())
+}

Committable suggestion skipped: line range outside the PR's diff.

@hexaforce hexaforce merged commit 9527d75 into main Nov 25, 2024
16 checks passed
@hexaforce hexaforce deleted the fix/coresupport-filter-bug branch November 25, 2024 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants