guibranco / CrispyWaffle

🧰 🛠️ Crispy Waffle project - toolkit for .NET projects
https://guibranco.github.io/CrispyWaffle/
MIT License
21 stars 20 forks source link

Add CouchDB support and related tests #544

Closed Mohammad-Haris closed 3 weeks ago

Mohammad-Haris commented 3 weeks ago

User description

Resolves #499


Before the change?

No support for CouchDB cache.

After the change?

Added support for CouchDB support.

Pull request checklist

Does this introduce a breaking change?



Description


Changes walkthrough 📝

Relevant files
Enhancement
CouchDBCacheRepository.cs
Add CouchDB cache repository implementation                           

Src/CrispyWaffle.CouchDB/CouchDBCacheRepository.cs
  • Implemented CouchDBCacheRepository for CouchDB support.
  • Added methods for CRUD operations on CouchDB documents.
  • Introduced exception handling and logging for database operations.
  • +455/-0 
    CouchDoc.cs
    Define CouchDB document structure                                               

    Src/CrispyWaffle.CouchDB/DTOs/CouchDoc.cs
  • Created CouchDoc class as a base for CouchDB documents.
  • Added properties for Key, SubKey, ExpiresAt, and TTL.
  • +41/-0   
    Documentation
    ICacheRepository.cs
    Improve ICacheRepository documentation                                     

    Src/CrispyWaffle/Cache/ICacheRepository.cs
  • Updated interface documentation for clarity.
  • Minor formatting changes for consistency.
  • +32/-12 
    Tests
    CouchDBCacheRepositoryTests.cs
    Implement unit tests for CouchDB cache                                     

    Tests/CrispyWaffle.Tests/Cache/CouchDBCacheRepositoryTests.cs
  • Added unit tests for CouchDBCacheRepository.
  • Tested CRUD operations and TTL functionality.
  • +151/-0 
    Configuration changes
    CrispyWaffle.sln
    Update solution to include CouchDB project                             

    CrispyWaffle.sln - Added `CrispyWaffle.CouchDB` project to the solution.
    +7/-0     
    CrispyWaffle.CouchDB.csproj
    Define CouchDB project configuration                                         

    Src/CrispyWaffle.CouchDB/CrispyWaffle.CouchDB.csproj
  • Created project file for CrispyWaffle.CouchDB.
  • Added dependencies for CouchDB.NET.
  • +20/-0   
    appveyor.yml
    Modify CI configuration for CouchDB                                           

    appveyor.yml - Updated AppVeyor configuration to include CouchDB installation.
    +3/-1     

    Summary by CodeRabbit

    coderabbitai[bot] commented 3 weeks ago

    Walkthrough

    The pull request introduces a new project, CrispyWaffle.CouchDB, to the solution, enhancing the existing functionality by implementing a caching mechanism using CouchDB. Key components include a repository for managing cached documents, project configuration, and dependencies. Additionally, unit tests for the new repository class are included, along with updates to various configuration files to support the integration of CouchDB.

    Changes

    File Change Summary
    CrispyWaffle.sln Added a new project entry for CrispyWaffle.CouchDB and updated global configurations.
    Directory.Packages.props Introduced package version declaration for CouchDB.NET version 1.2.2.
    Src/CrispyWaffle.CouchDB/CouchDBCacheRepository.cs Added CouchDBCacheRepository class with CRUD operations and exception handling.
    Src/CrispyWaffle.CouchDB/CrispyWaffle.CouchDB.csproj Defined project configuration and dependencies for CrispyWaffle.CouchDB.
    Src/CrispyWaffle.CouchDB/DTOs/CouchDoc.cs Introduced CouchDoc class for document persistence in CouchDB.
    Src/CrispyWaffle/Cache/ICacheRepository.cs Enhanced documentation for methods in ICacheRepository.
    Src/CrispyWaffle/Log/Adapters/RollingTextFileLogAdapter.cs Modified logging method documentation to focus on exception logging.
    Tests/CrispyWaffle.Tests/Cache/CouchDBCacheRepositoryTests.cs Added unit tests for CouchDBCacheRepository functionalities.
    Tests/CrispyWaffle.Tests/CrispyWaffle.Tests.csproj Added project reference to CrispyWaffle.CouchDB.
    appveyor.yml Added commands to automate the installation of Apache CouchDB during build.

    Assessment against linked issues

    Objective Addressed Explanation
    Add a new project, CrispyWaffle.CouchDB, with caching features (##499)
    Implement the ICacheRepository interface (##499)

    🐰 In the garden where bunnies hop,
    A CouchDB cache, we’ve made it pop!
    With documents stored, and tests in place,
    Our code now dances, a happy embrace.
    So let’s celebrate, with carrots and cheer,
    For CrispyWaffle’s growth, we hold dear! 🥕✨


    Recent review details **Configuration used: CodeRabbit UI** **Review profile: CHILL**
    Commits Files that changed from the base of the PR and between c3f11a54252b139343681f553ef7a48ac1c23d7b and 8e58e1775e882fff1a7fb56b7274f64617cb5580.
    Files selected for processing (1) * Directory.Packages.props (1 hunks)
    Files skipped from review as they are similar to previous changes (1) * Directory.Packages.props
    --- 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 - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
    Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `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.` - `@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. ### 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](https://docs.coderabbit.ai/guides/configure-coderabbit) 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](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
    penify-dev[bot] commented 3 weeks ago

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5] 4, because the PR introduces a significant amount of new code, including a new repository implementation and associated tests. The complexity of the CouchDB interactions and the need to ensure proper integration with existing caching mechanisms adds to the review effort.
    🧪 Relevant tests Yes
    ⚡ Possible issues Exception Handling: The exception handling in the CouchDBCacheRepository methods could be improved. Currently, exceptions are logged but not always rethrown, which may lead to silent failures.
    Performance Concerns: The use of synchronous calls (e.g., `Task.WaitAll`) in methods like `Clear` and `SetSpecific` could lead to performance bottlenecks. Consider using asynchronous patterns throughout.
    🔒 Security concerns Sensitive information exposure: The connection credentials (username and password) are hardcoded in the test class. This could lead to security vulnerabilities if the code is exposed. It's recommended to use environment variables or secure configuration management for sensitive data.
    gitguardian[bot] commented 3 weeks ago

    ⚠️ GitGuardian has uncovered 2 secrets following the scan of your pull request.

    Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

    Since your pull request originates from a forked repository, GitGuardian is not able to associate the secrets uncovered with secret incidents on your GitGuardian dashboard. Skipping this check run and merging your pull request will create secret incidents on your GitGuardian dashboard.

    🔎 Detected hardcoded secrets in your pull request
    | GitGuardian id | GitGuardian status | Secret | Commit | Filename | | | -------------- | ------------------ | ------------------------------ | ---------------- | --------------- | -------------------- | | [-](https://dashboard.gitguardian.com/workspace/7014/incidents/secrets) | - | Username Password | c3f11a54252b139343681f553ef7a48ac1c23d7b | Tests/CrispyWaffle.Tests/Cache/CouchDBCacheRepositoryTests.cs | [View secret](https://github.com/guibranco/CrispyWaffle/commit/c3f11a54252b139343681f553ef7a48ac1c23d7b#diff-076c4d09b760fa0f650865df471b6e4a45db6cd16f4e9fdd3f6fcdd712dab0a8L20) | | [-](https://dashboard.gitguardian.com/workspace/7014/incidents/secrets) | - | Generic Password | c3f11a54252b139343681f553ef7a48ac1c23d7b | appveyor.yml | [View secret](https://github.com/guibranco/CrispyWaffle/commit/c3f11a54252b139343681f553ef7a48ac1c23d7b#diff-92ab9a36df5d8e9f7076f2fdec59492d1ac2d9cf27ea046767a7fc4d542ef3dcL38) |
    🛠 Guidelines to remediate hardcoded secrets
    1. Understand the implications of revoking this secret by investigating where it is used in your code. 2. Replace and store your secrets safely. [Learn here](https://blog.gitguardian.com/secrets-api-management?utm_source=product&utm_medium=GitHub_checks&utm_campaign=check_run_comment) the best practices. 3. Revoke and [rotate these secrets](https://docs.gitguardian.com/secrets-detection/secrets-detection-engine/detectors/generics/username_password#revoke-the-secret?utm_source=product&utm_medium=GitHub_checks&utm_campaign=check_run_comment). 4. If possible, [rewrite git history](https://blog.gitguardian.com/rewriting-git-history-cheatsheet?utm_source=product&utm_medium=GitHub_checks&utm_campaign=check_run_comment). Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data. To avoid such incidents in the future consider - following these [best practices](https://blog.gitguardian.com/secrets-api-management/?utm_source=product&utm_medium=GitHub_checks&utm_campaign=check_run_comment) for managing and storing secrets including API keys and other credentials - install [secret detection on pre-commit](https://docs.gitguardian.com/ggshield-docs/integrations/git-hooks/pre-commit?utm_source=product&utm_medium=GitHub_checks&utm_campaign=check_run_comment) to catch secret before it leaves your machine and ease remediation.

    🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

    penify-dev[bot] commented 3 weeks ago

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Performance
    Replace blocking calls with asynchronous patterns for better performance ___ **Use asynchronous programming patterns instead of blocking calls like Wait() to improve
    performance and responsiveness.** [Src/CrispyWaffle.CouchDB/CouchDBCacheRepository.cs [218]](https://github.com/guibranco/CrispyWaffle/pull/544/files#diff-daf96ace932b8fe5fe112599a46db4a465e547190761e2c50b3fe544fc97fcb2R218-R218) ```diff -db.DeleteAsync(doc).Wait(); +await db.DeleteAsync(doc); ```
    Suggestion importance[1-10]: 9 Why: This suggestion addresses a significant performance issue by replacing a blocking call with an asynchronous one, which is crucial for responsiveness in applications.
    9
    Replace synchronous calls on asynchronous methods to avoid potential deadlocks ___ **Avoid using .Result on asynchronous calls to prevent deadlocks in certain synchronization
    contexts.** [Src/CrispyWaffle.CouchDB/CouchDBCacheRepository.cs [425]](https://github.com/guibranco/CrispyWaffle/pull/544/files#diff-daf96ace932b8fe5fe112599a46db4a465e547190761e2c50b3fe544fc97fcb2R425-R425) ```diff -if (!_couchClient.GetDatabasesNamesAsync().Result.Contains(dbName)) +var dbNames = await _couchClient.GetDatabasesNamesAsync(); +if (!dbNames.Contains(dbName)) ```
    Suggestion importance[1-10]: 9 Why: This suggestion is critical as it addresses a potential deadlock issue by replacing synchronous calls with asynchronous patterns, which is essential for maintaining application stability.
    9
    Security
    Sanitize the database name to prevent injection issues ___ **Ensure that the database name is sanitized to prevent potential injection attacks or
    errors.** [Src/CrispyWaffle.CouchDB/CouchDBCacheRepository.cs [422]](https://github.com/guibranco/CrispyWaffle/pull/544/files#diff-daf96ace932b8fe5fe112599a46db4a465e547190761e2c50b3fe544fc97fcb2R422-R422) ```diff -dbName = $"{typeof(T).Name.ToLowerInvariant()}s"; +dbName = $"{Regex.Replace(typeof(T).Name.ToLowerInvariant(), @"[^a-z0-9]", "")}s"; ```
    Suggestion importance[1-10]: 8 Why: This suggestion improves security by sanitizing the database name, which is important to prevent potential injection attacks.
    8
    Best practice
    Replace equality assertions with a more descriptive assertion method ___ **Consider using Assert.Equal instead of Assert.True for equality checks to provide clearer
    test failure messages.** [Tests/CrispyWaffle.Tests/Cache/CouchDBCacheRepositoryTests.cs [34]](https://github.com/guibranco/CrispyWaffle/pull/544/files#diff-076c4d09b760fa0f650865df471b6e4a45db6cd16f4e9fdd3f6fcdd712dab0a8R34-R34) ```diff -Assert.True(doc.Key == docDB.Key); +Assert.Equal(doc.Key, docDB.Key); ```
    Suggestion importance[1-10]: 8 Why: Using `Assert.Equal` provides clearer failure messages, which can help in diagnosing test failures more effectively.
    8
    Use specific exception types for better error handling ___ **Consider using a more specific exception type instead of the general Exception to improve
    error handling.** [Src/CrispyWaffle.CouchDB/CouchDBCacheRepository.cs [50]](https://github.com/guibranco/CrispyWaffle/pull/544/files#diff-daf96ace932b8fe5fe112599a46db4a465e547190761e2c50b3fe544fc97fcb2R50-R50) ```diff -catch (Exception e) +catch (CouchDBException e) ```
    Suggestion importance[1-10]: 6 Why: While using specific exception types can enhance error handling, the impact of this change is relatively minor compared to performance and security improvements.
    6
    Rename test methods to follow a consistent naming convention for clarity ___ **Use a consistent naming convention for test methods to improve readability and
    maintainability.** [Tests/CrispyWaffle.Tests/Cache/CouchDBCacheRepositoryTests.cs [26]](https://github.com/guibranco/CrispyWaffle/pull/544/files#diff-076c4d09b760fa0f650865df471b6e4a45db6cd16f4e9fdd3f6fcdd712dab0a8R26-R26) ```diff -public void GetAndSetCouchDocTest() +public void Should_Set_And_Get_CouchDoc_Correctly() ```
    Suggestion importance[1-10]: 5 Why: Consistent naming conventions for test methods improve code readability, but this change is more about style than functionality.
    5
    Possible issue
    Add existence checks before removing documents to prevent exceptions ___ **Ensure that the Remove and RemoveSpecific methods are called only after verifying that the
    document exists to avoid potential exceptions.** [Tests/CrispyWaffle.Tests/Cache/CouchDBCacheRepositoryTests.cs [36]](https://github.com/guibranco/CrispyWaffle/pull/544/files#diff-076c4d09b760fa0f650865df471b6e4a45db6cd16f4e9fdd3f6fcdd712dab0a8R36-R36) ```diff -_repo.Remove(doc.Key); +if (_repo.Get(doc.Key) != null) { _repo.Remove(doc.Key); } ```
    Suggestion importance[1-10]: 7 Why: Adding existence checks before removal can prevent exceptions, enhancing the robustness of the tests.
    7
    Maintainability
    Implement cleanup logic in the Dispose method to maintain test isolation ___ **Consider adding a cleanup step in the Dispose method to ensure that any created documents
    are removed after tests run.** [Tests/CrispyWaffle.Tests/Cache/CouchDBCacheRepositoryTests.cs [138]](https://github.com/guibranco/CrispyWaffle/pull/544/files#diff-076c4d09b760fa0f650865df471b6e4a45db6cd16f4e9fdd3f6fcdd712dab0a8R138-R138) ```diff +// Cleanup logic to remove any test documents +_repo.Clear(); _repo?.Dispose(); ```
    Suggestion importance[1-10]: 6 Why: Implementing cleanup logic helps maintain test isolation, which is important for reliable test outcomes.
    6
    AppVeyorBot commented 3 weeks ago

    :white_check_mark: Build CrispyWaffle 8.1.350 completed (commit https://github.com/guibranco/CrispyWaffle/commit/87d9856bf7 by @Mohammad-Haris)

    github-actions[bot] commented 3 weeks ago

    Infisical secrets check: :white_check_mark: No secrets leaked!

    Scan results:

    5:26PM INF scanning for exposed secrets...
    5:26PM INF 529 commits scanned.
    5:26PM INF scan completed in 679ms
    5:26PM INF no leaks found
    
    github-advanced-security[bot] commented 3 weeks ago

    This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

    github-advanced-security[bot] commented 3 weeks ago

    This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

    AppVeyorBot commented 3 weeks ago

    :white_check_mark: Build CrispyWaffle 8.1.351 completed (commit https://github.com/guibranco/CrispyWaffle/commit/254485a082 by @guibranco)

    guibranco commented 3 weeks ago

    @Mohammad-Haris thanks!

    Mohammad-Haris commented 3 weeks ago

    Any feedback. I did it in patches due to some other engagements so might have missed some things.

    guibranco commented 3 weeks ago

    No, no, all good. I need to add more tests to it and fix the pipeline in GH now, but it's okay. I will need to add some more secrets to the repository here.

    Great job BTW, I appreciate that! 🚀