ballerine icon indicating copy to clipboard operation
ballerine copied to clipboard

fix(test-int): fixing test concurrency attempts

Open alonp99 opened this issue 9 months ago • 5 comments

Type

enhancement, tests


Description

  • Introduced dynamic database URL configuration in PrismaService to enhance flexibility in database connections.
  • Updated test setup in nest-app-helper to allow passing a custom database URL, improving test isolation.
  • Integrated PostgreSqlContainer in transaction controller tests to manage database state more effectively, ensuring clean test environments.

Changes walkthrough

Relevant files
Enhancement
prisma.service.ts
Enhance PrismaService with Dynamic Datasource Configuration

services/workflows-service/src/prisma/prisma.service.ts

  • Added optional dbUrl parameter to PrismaService constructor.
  • Introduced dynamic datasource configuration based on dbUrl or
    environment variable.
  • +4/-1     
    nest-app-helper.ts
    Update Test Helper to Support Dynamic DB Configuration     

    services/workflows-service/src/test/helpers/nest-app-helper.ts

  • Added dbUrl parameter to initiateNestApp function to support dynamic
    database URLs.
  • Modified dependency injection to use a factory for PrismaService to
    include dbUrl.
  • +10/-1   
    Tests
    transaction.controller.external.intg.test.ts
    Integrate PostgreSQL Container in Transaction Controller Tests

    services/workflows-service/src/transaction/transaction.controller.external.intg.test.ts

  • Integrated PostgreSqlContainer for database interactions in tests.
  • Added setup and teardown processes for the PostgreSQL container.
  • +25/-5   

    PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Summary by CodeRabbit

    • New Features

      • Enhanced database connectivity options by allowing a custom database URL for service initialization.
      • Improved testing infrastructure with the integration of a Postgres container for more reliable external integration tests.
    • Refactor

      • Updated service constructors and initialization functions to support an optional database URL parameter, enhancing flexibility in database management.

    alonp99 avatar May 01 '24 23:05 alonp99

    ⚠️ No Changeset found

    Latest commit: a170bbe8a0efc598ac4b039f5e8a6ecdae889ce6

    Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

    This PR includes no changesets

    When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

    Click here to learn what changesets are, and how to add one.

    Click here if you're a maintainer who wants to add a changeset to this PR

    changeset-bot[bot] avatar May 01 '24 23:05 changeset-bot[bot]

    Walkthrough

    The updates primarily introduce flexibility in database connectivity across the workflows-service by allowing a dynamic database URL to be passed as an optional parameter. This change affects the instantiation of the PrismaService and the setup of the Nest application, facilitating easier configuration during testing or deployment in different environments.

    Changes

    File Path Change Summary
    .../src/prisma/prisma.service.ts Constructor in PrismaService now accepts an optional dbUrl parameter for database URL configuration.
    .../src/test/helpers/nest-app-helper.ts Updated initiateNestApp function to allow passing a dbUrl and modified the PrismaService provider to use this URL.
    .../transaction/transaction.controller...test.ts Enhanced integration tests with Postgres container setup and dynamic database URL handling.

    🐇🌟 In the land of code, where the data streams flow, A rabbit hopped in, with updates in tow. "Now pass the DB URL, dear," it said with a cheer, "For flexible connections, far or near!" With a twinkle in its eye, it made the code spry, Now every test and service can simply comply. 🌟🐇


    Recent Review Details

    Configuration used: CodeRabbit UI Review profile: CHILL

    Commits Files that changed from the base of the PR and between 3e7d2f8366df535fb843290f60f93538bf1ccfad and a170bbe8a0efc598ac4b039f5e8a6ecdae889ce6.
    Files selected for processing (3)
    • services/workflows-service/src/prisma/prisma.service.ts (1 hunks)
    • services/workflows-service/src/test/helpers/nest-app-helper.ts (2 hunks)
    • services/workflows-service/src/transaction/transaction.controller.external.intg.test.ts (2 hunks)
    Additional comments not posted (4)
    services/workflows-service/src/test/helpers/nest-app-helper.ts (1)

    57-62: The factory function for overriding the PrismaService provider is correctly implemented to accept the dbUrl. This allows each test instance to use a separate database, aligning with the PR's objectives to improve test isolation.

    services/workflows-service/src/prisma/prisma.service.ts (1)

    39-43: The constructor now correctly accepts a dbUrl parameter and uses it to configure the database connection dynamically. This change supports the PR's goal of allowing different instances of the application to connect to different databases for testing.

    services/workflows-service/src/transaction/transaction.controller.external.intg.test.ts (2)

    136-157: The setup for integration tests now correctly uses a PostgreSqlContainer to create a separate database for each test. This is a crucial enhancement for achieving test isolation and aligns with the PR's objectives.


    159-160: Proper cleanup of resources is ensured by closing the application and stopping the Postgres container in the afterAll hook. This is good practice for managing resources in integration tests.


    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.

    coderabbitai[bot] avatar May 01 '24 23:05 coderabbitai[bot]

    PR Description updated to latest commit (https://github.com/ballerine-io/ballerine/commit/a170bbe8a0efc598ac4b039f5e8a6ecdae889ce6)

    github-actions[bot] avatar May 01 '24 23:05 github-actions[bot]

    PR Review

    ⏱️ Estimated effort to review [1-5]

    3, because the PR involves multiple files and integrates new functionality with existing services, including dynamic database URL handling and container management for tests. The changes are moderate in complexity, involving dependency injection, environment configuration, and asynchronous operations.

    🧪 Relevant tests

    Yes

    🔍 Possible issues

    Possible Bug: The variable postgresContainer is redeclared in the beforeAll block of transaction.controller.external.intg.test.ts, which might lead to unexpected behavior or scope issues.

    🔒 Security concerns

    No

    Code feedback:
    relevant fileservices/workflows-service/src/transaction/transaction.controller.external.intg.test.ts
    suggestion      

    Remove the redeclaration of postgresContainer inside the beforeAll block to use the already declared variable at the higher scope. This avoids potential scope conflicts and makes the code cleaner. [important]

    relevant lineconst postgresContainer = await new PostgreSqlContainer('sibedge/postgres-plv8:15.3-3.1.7')


    ✨ Review tool usage guide:

    Overview: The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    github-actions[bot] avatar May 01 '24 23:05 github-actions[bot]

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Enhancement
    Validate the dbUrl parameter to ensure it is a valid URL.

    Consider validating the dbUrl parameter in the constructor to ensure it's a valid URL
    format before using it. This can prevent runtime errors related to database connection
    issues.

    services/workflows-service/src/prisma/prisma.service.ts [39-40]

     constructor(protected readonly logger: AppLoggerService, dbUrl?: string) {
    +  if (!dbUrl && !process.env.DB_URL) {
    +    throw new Error('Database URL is not provided.');
    +  }
       const datasources = { db: { url: dbUrl || process.env.DB_URL } };
     
    
    Best practice
    Add exception handling when overriding providers in the testing module.

    It's a good practice to handle potential exceptions when overriding providers in the
    testing module, especially when involving database connections.

    services/workflows-service/src/test/helpers/nest-app-helper.ts [57-61]

     .overrideProvider(PrismaService)
     .useFactory({
    -  factory: (logger: AppLoggerService) => new PrismaService(logger, dbUrl),
    +  factory: (logger: AppLoggerService) => {
    +    try {
    +      return new PrismaService(logger, dbUrl);
    +    } catch (error) {
    +      console.error('Failed to initialize PrismaService:', error);
    +      throw error;
    +    }
    +  },
       inject: [AppLoggerService],
     })
     
    
    Handle exceptions during the Postgres container setup and teardown.

    To ensure proper resource management, explicitly handle the potential exceptions during
    the container setup and teardown in the integration tests.

    services/workflows-service/src/transaction/transaction.controller.external.intg.test.ts [160]

    -await postgresContainer.stop();
    +try {
    +  await postgresContainer.stop();
    +} catch (error) {
    +  console.error('Failed to stop the Postgres container:', error);
    +}
     
    
    Add cleanup logic for the database in the test teardown.

    Consider adding cleanup logic for the database and any other resources used in the tests
    to prevent data leakage and ensure each test starts with a clean state.

    services/workflows-service/src/transaction/transaction.controller.external.intg.test.ts [159]

     await app.close(); // Ensure your app is properly closed to release all resources
    +await cleanupDatabase(); // Clean up the database to ensure no data leakage
     
    
    Bug
    Avoid redeclaration of the postgresContainer variable in the beforeAll block.

    Ensure that the postgresContainer variable is not redeclared in the beforeAll block, which
    can lead to confusion and potential bugs.

    services/workflows-service/src/transaction/transaction.controller.external.intg.test.ts [142-145]

    -const postgresContainer = await new PostgreSqlContainer('sibedge/postgres-plv8:15.3-3.1.7')
    +postgresContainer = await new PostgreSqlContainer('sibedge/postgres-plv8:15.3-3.1.7')
       .withDatabase(db)
       .withExposedPorts(5432)
       .start();
     
    

    ✨ Improve tool usage guide:

    Overview: The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    github-actions[bot] avatar May 01 '24 23:05 github-actions[bot]