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

Feat: Add query param support and Made Neo4jOps singleton! #9

Merged
merged 12 commits into from
May 22, 2024

Conversation

amindadgar
Copy link
Member

@amindadgar amindadgar commented May 22, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a function to load Neo4j credentials from environment variables.
    • Added a Query class for defining Neo4j query schemas.
  • Bug Fixes

    • Removed redundant environment variables from the Neo4j service configuration.
  • Enhancements

    • Updated Neo4jOps class to enforce a singleton pattern and refactored methods for better query handling.
    • Improved query handling by accepting Query objects instead of strings.
  • Tests

    • Added integration tests for running single and multiple queries with Neo4jOps.
    • Added unit tests for loading and validating Neo4j credentials.
  • Versioning

    • Updated the package version from "1.0.2" to "2.0.0".

Copy link

coderabbitai bot commented May 22, 2024

Warning

Rate Limit Exceeded

@amindadgar has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 8 minutes and 27 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between e5c6a45 and 2319c26.

Walkthrough

The recent update ushers in significant improvements to the tc-neo4j-lib package. Notable changes include removing Sentry environment variables from docker-compose.test.yml, upgrading the package version to "2.0.0", and implementing a singleton pattern in Neo4jOps. Additionally, a new Query class is introduced to handle Neo4j queries, accompanied by thorough tests for the enhanced functionalities.

Changes

File Path Change Summary
docker-compose.test.yml Removed SENTRY_DSN and SENTRY_ENV env variables from neo4j service config.
setup.py Updated package version to "2.0.0" from "1.0.2".
tc_neo4j_lib/__init__.py Added import for Query from schema module.
tc_neo4j_lib/credentials.py Introduced load_neo4j_credentials() function for loading Neo4j credentials.
tc_neo4j_lib/neo4j_ops.py Enforced singleton pattern in Neo4jOps, refactored connection method, updated _run_query to accept kwargs, renamed store_data_neo4j.
tc_neo4j_lib/schema.py Added Query class to define schema for Neo4j queries.
tests/integration/test_run_queries.py Added integration tests for Neo4j connection and query execution.
tests/unit/test_credentials.py Added unit tests for loading and validating Neo4j credentials.

🐰✨ In the realm of code, a rabbit's delight,
Queries now dance in the Neo4j light.
Sentry bids adieu, credentials take flight,
A singleton rises, version two's in sight.
With tests to guide us through the night,
Our code's future is shining bright. 🌟


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

This was linked to issues May 22, 2024
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: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 0de5a35 and 8f588a8.
Files selected for processing (8)
  • docker-compose.test.yml (1 hunks)
  • setup.py (1 hunks)
  • tc_neo4j_lib/init.py (1 hunks)
  • tc_neo4j_lib/credentials.py (1 hunks)
  • tc_neo4j_lib/neo4j_ops.py (4 hunks)
  • tc_neo4j_lib/schema.py (1 hunks)
  • tests/integration/test_run_queries.py (1 hunks)
  • tests/unit/test_credentials.py (1 hunks)
Files skipped from review due to trivial changes (2)
  • docker-compose.test.yml
  • setup.py
Additional Context Used
Ruff (1)
tc_neo4j_lib/neo4j_ops.py (1)

4-4: neo4j.Driver imported but unused

Additional comments not posted (5)
tc_neo4j_lib/__init__.py (1)

3-3: The import of Query is correctly placed and enhances the usability of the library by exposing the Query class directly.

tests/unit/test_credentials.py (1)

6-19: The unit tests for credential loading are comprehensive, checking both the presence of necessary keys and the non-null values, which is crucial for the robustness of the credentials management.

tc_neo4j_lib/credentials.py (1)

6-45: The load_neo4j_credentials function is well-implemented with robust error handling for missing environment variables. However, there's a small typo in the documentation: "Parameters" should be corrected to "Parameters".

- Parameters
+ Parameters

Likely invalid or redundant comment.

tests/integration/test_run_queries.py (1)

6-56: The integration tests are comprehensive, covering the connection to Neo4j and the execution of single and multiple queries. This ensures that the Neo4jOps class functions as expected in a real-world scenario.

tc_neo4j_lib/neo4j_ops.py (1)

Line range hint 11-112: The refactoring of the Neo4jOps class, including the implementation of the singleton pattern and the enhancements to the _run_query and run_queries_in_batch methods, are well-executed. The error handling in _run_query is robust, appropriately logging different types of Neo4j errors.

Comment on lines 4 to 17
class Query:
def __init__(self, query: str, parameters: dict[str, Any]) -> None:
"""
A schema for passing neo4j query

Paramters
------------
query : str
the actual query to pass to neo4j driver
parameters : dict[str, Any]
the neo4j parameters for the query itself
"""
self.query = query
self.parameters = parameters
Copy link

Choose a reason for hiding this comment

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

The Query class is well-defined with clear documentation. However, there's a small typo in the documentation: "Paramters" should be corrected to "Parameters".

- Paramters
+ Parameters

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
class Query:
def __init__(self, query: str, parameters: dict[str, Any]) -> None:
"""
A schema for passing neo4j query
Paramters
------------
query : str
the actual query to pass to neo4j driver
parameters : dict[str, Any]
the neo4j parameters for the query itself
"""
self.query = query
self.parameters = parameters
class Query:
def __init__(self, query: str, parameters: dict[str, Any]) -> None:
"""
A schema for passing neo4j query
Parameters
------------
query : str
the actual query to pass to neo4j driver
parameters : dict[str, Any]
the neo4j parameters for the query itself
"""
self.query = query
self.parameters = parameters

tc_neo4j_lib/neo4j_ops.py Show resolved Hide resolved
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: 2

Outside diff range and nitpick comments (2)
tc_neo4j_lib/neo4j_ops.py (2)

Line range hint 47-61: Enhance error handling in _run_query.

The error handling in _run_query could be enhanced by re-raising exceptions after logging them, allowing higher-level methods to handle or log them as necessary. This would provide more flexibility in how errors are managed throughout the application.

-        except TransientError as err:
-            logging.error("Neo4j transient error!")
-            logging.error(f"Code: {err.code}, message: {err.message}")
-        except DatabaseError as err:
-            logging.error("Neo4J database error")
-            logging.error(f"Code: {err.code}, message: {err.message}")
-        except ClientError as err:
-            logging.error("Neo4j Client Error!")
-            logging.error(f"Code: {err.code}, message: {err.message}")
+        except (TransientError, DatabaseError, ClientError) as err:
+            logging.error(f"Neo4j error: {err.code}, message: {err.message}")
+            raise

Line range hint 72-113: Optimize batch processing in run_queries_in_batch.

The method run_queries_in_batch modifies the queries list directly within the loop, which can lead to unexpected behavior and bugs. Consider using a different variable to store the slice for clarity and to avoid modifying the input list.

-                queries = queries[index : index + session_batch]
+                batch_queries = queries[index : index + session_batch]
+                # Use batch_queries in the subsequent code
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 8f588a8 and 35672f3.
Files selected for processing (2)
  • tc_neo4j_lib/credentials.py (1 hunks)
  • tc_neo4j_lib/neo4j_ops.py (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tc_neo4j_lib/credentials.py
Additional comments not posted (1)
tc_neo4j_lib/neo4j_ops.py (1)

18-22: Ensure proper singleton usage by raising an exception in the constructor.

The use of an exception to prevent multiple instantiations directly through the constructor is a good practice in implementing the singleton pattern. This ensures that the instance is controlled through the get_instance method.

tc_neo4j_lib/neo4j_ops.py Outdated Show resolved Hide resolved
tc_neo4j_lib/neo4j_ops.py Outdated Show resolved Hide resolved
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: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 35672f3 and e5c6a45.
Files selected for processing (2)
  • tc_neo4j_lib/credentials.py (1 hunks)
  • tc_neo4j_lib/neo4j_ops.py (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tc_neo4j_lib/credentials.py
Additional comments not posted (3)
tc_neo4j_lib/neo4j_ops.py (3)

14-24: Constructor logic correctly enforces singleton behavior and initializes database connection.


33-46: Database connection method correctly handles credential loading, driver creation, and connectivity verification.


Line range hint 48-72: Query execution method correctly handles parameters and various Neo4j errors.

tc_neo4j_lib/neo4j_ops.py Show resolved Hide resolved
tc_neo4j_lib/neo4j_ops.py Outdated Show resolved Hide resolved
we were assigning the variable to itself which data loss could happen.
@amindadgar amindadgar requested a review from cyri113 May 22, 2024 13:33
@cyri113 cyri113 merged commit 85f424a into main May 22, 2024
15 checks passed
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.

Add support for Neo4j query parameters Make Neo4jOps singletone
2 participants