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

chore: [#540] Add test cases #845

Merged
merged 4 commits into from
Feb 3, 2025
Merged

chore: [#540] Add test cases #845

merged 4 commits into from
Feb 3, 2025

Conversation

hwbrzzl
Copy link
Contributor

@hwbrzzl hwbrzzl commented Feb 2, 2025

📑 Description

goravel/goravel#540

Summary by CodeRabbit

  • New Features

    • Expanded testing coverage to include MySQL alongside PostgreSQL, enhancing compatibility in migrations, ORM operations, schema validations, and query executions.
  • Tests

    • Integrated robust MySQL test queries and streamlined test configurations to ensure reliable cross-database validations.
  • Chores

    • Updated and refined module dependencies and connection configurations for improved consistency and maintainability.

✅ Checks

  • Added test cases for my code

Copy link
Contributor

coderabbitai bot commented Feb 2, 2025

Walkthrough

This pull request integrates MySQL support across the project’s testing framework and schema management. It updates module dependencies in tests/go.mod, adds new MySQL dependencies, and revises several test suites to include MySQL test queries alongside PostgreSQL. The tests now use driver-specific mappings and remove obsolete driver constants. Additionally, the CreateTable method is updated to pass the driver’s grammar, and the table creation logic transitions from hardcoded SQL to a blueprint-based approach. Finally, the database connection setup is refactored with a new mysqlTestQuery function.

Changes

File(s) Change Summary
tests/go.mod Updated module dependencies: upgraded versions for goravel/framework and goravel/postgres, added dependency for goravel/mysql, and introduced indirect dependencies for MySQL drivers (go-sql-driver/mysql, gorm.io/driver/mysql).
tests/migrator_test.go, tests/orm_test.go, tests/query_test.go, tests/repository_test.go, tests/schema_test.go Added and integrated MySQL test queries into multiple test suites; updated driver mappings (using mysql.Name in schema tests) and removed redundant driver constants.
tests/query.go Modified CreateTable method to include an additional argument (r.Driver().Grammar()) when calling newTestTables.
tests/table.go Transitioned from hardcoded SQL creation statements to a schema blueprint approach by updating the testTables struct and modifying the newTestTables function to accept a grammar parameter.
tests/utils.go Refactored database connection handling by updating PostgreSQL connection naming and adding a new mysqlTestQuery function to set up MySQL connections with Docker container configurations.

Sequence Diagram(s)

sequenceDiagram
    participant TS as TestSuite
    participant MTQ as mysqlTestQuery
    participant TQ as TestQuery Instance
    participant NT as newTestTables
    participant BP as Blueprint
    participant G as Grammar

    TS->>MTQ: Call mysqlTestQuery(prefix, singular)
    MTQ->>TS: Return TestQuery instance
    TS->>TQ: Invoke CreateTable(testTables...)
    TQ->>NT: Call newTestTables(driver, r.Driver().Grammar())
    NT->>BP: Initialize blueprint for table creation
    BP->>G: Use grammar to generate SQL
    G-->>BP: Return SQL command
    BP-->>NT: Return SQL command
    NT-->>TQ: Return configured test tables
Loading

Possibly related PRs

  • feat: [#540] Remove SQL in show_command.go #825: The changes in the main PR involve updates to module dependencies including the addition of MySQL support, related to the PR that removes constants for database drivers and enhances the configuration structure.
  • feat: [#280] Implement Mysql driver #725: The changes in the main PR focus on updating dependencies related to MySQL and enhancing test coverage for MySQL functionality, related to the PR that implements a MySQL driver, both integrating MySQL support within the framework.

Suggested reviewers

  • devhaozi
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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

codecov bot commented Feb 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.16%. Comparing base (cbb806d) to head (84c7c2f).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #845   +/-   ##
=======================================
  Coverage   67.16%   67.16%           
=======================================
  Files         150      150           
  Lines       10442    10442           
=======================================
  Hits         7013     7013           
  Misses       3055     3055           
  Partials      374      374           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hwbrzzl hwbrzzl marked this pull request as ready for review February 3, 2025 03:32
@hwbrzzl hwbrzzl requested a review from a team as a code owner February 3, 2025 03:32
Copy link
Contributor

@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: 0

🧹 Nitpick comments (2)
tests/table.go (1)

54-62: Consider refactoring repeated blueprint logic.
All table creation methods follow a similar pattern (ID, timestamps, soft deletes). Extracting a shared helper to apply these repeated fields can improve maintainability.

 func (r *testTables) peoples() string {
 	blueprint := schema.NewBlueprint(nil, "", "peoples")
 	blueprint.Create()
-	blueprint.BigIncrements("id")
-	blueprint.Timestamps()
-	blueprint.SoftDeletes()
+	r.addDefaultFields(blueprint)
 	blueprint.String("body")

 	return blueprint.ToSql(r.grammar)[0]
 }

+func (r *testTables) addDefaultFields(blueprint *schema.Blueprint) {
+	blueprint.BigIncrements("id")
+	blueprint.Timestamps()
+	blueprint.SoftDeletes()
+}
tests/query_test.go (1)

13-13: Consider using a consistent import alias for postgres.

The postgres package is imported without an alias. For consistency with other database drivers and to make the code more maintainable, consider using an alias.

-import "github.com/goravel/postgres"
+import postgres "github.com/goravel/postgres"
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between cbb806d and ceb2ace.

⛔ Files ignored due to path filters (1)
  • tests/go.sum is excluded by !**/*.sum
📒 Files selected for processing (9)
  • tests/go.mod (3 hunks)
  • tests/migrator_test.go (1 hunks)
  • tests/orm_test.go (1 hunks)
  • tests/query.go (1 hunks)
  • tests/query_test.go (6 hunks)
  • tests/repository_test.go (1 hunks)
  • tests/schema_test.go (9 hunks)
  • tests/table.go (3 hunks)
  • tests/utils.go (5 hunks)
⏰ Context from checks skipped due to timeout of 300000ms (1)
  • GitHub Check: test / windows (1.23)
🔇 Additional comments (26)
tests/table.go (15)

3-6: No issues with import statements.
The newly introduced imports are consistent with the usage for blueprint-based table creation.


27-28: No issues with the new 'grammar' field.
This field is properly integrated and sets up the blueprint functionality for driver-specific SQL generation.


31-32: Constructor changes look good.
Accepting the grammar as a parameter cleanly injects driver-specific behavior.


65-73: Same suggestion applies here regarding duplicated blueprint logic.


76-84: Same suggestion applies here regarding duplicated blueprint logic.


87-97: Same suggestion applies here regarding duplicated blueprint logic.


100-110: Same suggestion applies here regarding duplicated blueprint logic.


113-123: Same suggestion applies here regarding duplicated blueprint logic.


126-135: Same suggestion applies here regarding duplicated blueprint logic.


138-147: Same suggestion applies here regarding duplicated blueprint logic.


150-159: Same suggestion applies here regarding duplicated blueprint logic.


162-172: Same suggestion applies here regarding duplicated blueprint logic.


175-185: Same suggestion applies here regarding duplicated blueprint logic.


188-197: Same suggestion applies here regarding duplicated blueprint logic.


200-207: Same suggestion applies here regarding duplicated blueprint logic.

tests/query.go (1)

40-40: Passing the driver's grammar aligns table creation with the correct SQL dialect.
This change ensures that generated statements remain consistent across different database drivers.

tests/repository_test.go (1)

23-27: Well-structured addition of 'mysqlTestQuery'.
Extending the driverToTestQuery map with MySQL ensures MySQL test coverage remains on par with PostgreSQL.

tests/orm_test.go (1)

34-38: LGTM! Good test setup for MySQL.

The MySQL test setup follows the same pattern as PostgreSQL, ensuring consistent test environment and proper isolation between database drivers.

tests/migrator_test.go (1)

25-30: LGTM! Good test coverage for MySQL migrations.

The MySQL migrator test setup properly mirrors the PostgreSQL configuration, enabling comprehensive testing of migrations across both database drivers.

tests/utils.go (1)

79-128: LGTM! Well-structured MySQL test query setup.

The MySQL test query setup is well implemented with:

  • Proper configuration of MySQL-specific settings
  • Consistent error handling
  • Good use of mocks for configuration
tests/schema_test.go (2)

227-232: LGTM! Good handling of MySQL timestamp behavior differences.

The test correctly handles the difference in timestamp behavior between MySQL and other databases, where MySQL updates the timestamp on update while others don't.


1631-1664: LGTM! Comprehensive MySQL enum testing.

The test suite thoroughly validates MySQL enum behavior:

  • Tests constraint enforcement
  • Verifies error handling for invalid values
  • Confirms successful insertion of valid values
tests/query_test.go (2)

34-36: LGTM! MySQL test query setup added.

The MySQL test query setup follows the same pattern as PostgreSQL, maintaining consistency in the test suite.


2090-2118: Verify SQLite exclusion for LockForUpdate test.

The test correctly excludes SQLite from LockForUpdate tests since SQLite doesn't support row-level locking. However, let's verify if there are any other database-specific features that need similar treatment.

✅ Verification successful

SQLite exclusion and additional driver-specific logic verified.
The test in tests/query_test.go correctly excludes SQLite for the LockForUpdate operations. Our search also revealed that any additional database-specific features (e.g., in schema and index tests within tests/schema_test.go) already include the appropriate guards, so no further exclusions for SQLite are needed.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for other database-specific features that might need driver checks.

# Look for potential database-specific features
rg -A 5 'if driver [!=]=' 

# Look for SQLite-specific exclusions
rg -A 5 'sqlite'

Length of output: 16604

tests/go.mod (2)

28-28: Verify MySQL driver versions.

The indirect dependencies for MySQL support have been added:

  • github.com/go-sql-driver/mysql v1.8.1
  • gorm.io/driver/mysql v1.5.7

Let's verify these are the latest stable versions and check for any security advisories.

Also applies to: 58-58

✅ Verification successful

MySQL driver versions verified and no security vulnerabilities found.

  • The MySQL driver for github.com/go-sql-driver/mysql is set to v1.8.1, which is the latest version available.
  • The GORM MySQL driver gorm.io/driver/mysql is at v1.5.7, matching the most recent stable release.
  • No security advisories were reported for github.com/go-sql-driver/mysql in the reviewed query.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check MySQL driver versions and security advisories.

# Check for latest versions
curl -s https://proxy.golang.org/github.com/go-sql-driver/mysql/@v/list
curl -s https://proxy.golang.org/gorm.io/driver/mysql/@v/list

# Check for security advisories
gh api graphql -f query='
{
  securityVulnerabilities(first: 5, ecosystem: GO, package: "github.com/go-sql-driver/mysql") {
    nodes {
      advisory {
        summary
        severity
        publishedAt
      }
      vulnerableVersionRange
      firstPatchedVersion {
        identifier
      }
    }
  }
}'

Length of output: 969


9-11: Verify compatibility of new framework and MySQL dependencies.

The addition of MySQL support introduces new dependencies. Let's verify their compatibility:

  1. Framework update to v1.15.2-0.20250202111117-cbb806d1e8de
  2. New MySQL dependency v0.0.0-20250202150522-4e41e0908557
✅ Verification successful

Compatibility Verified: Framework and MySQL Dependencies
The GitHub API queries returned null for both the framework and MySQL pseudo-versions, which is expected since pseudo-versions (with commit SHA and timestamps) do not have associated release records on GitHub. There are no indications from the queried metadata of any compatibility issues between the new dependencies. For additional certainty, please ensure the integration tests and CI pipelines are passing.

  • Framework version v1.15.2-0.20250202111117-cbb806d1e8de is a pseudo-version without a formal release.
  • MySQL dependency v0.0.0-20250202150522-4e41e0908557 is also a pseudo-version, showing no release metadata issues.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for compatibility issues between dependencies.

# Check if there are any reported issues with these versions
gh api graphql -f query='
{
  repository(owner: "goravel", name: "framework") {
    release(tagName: "v1.15.2-0.20250202111117-cbb806d1e8de") {
      description
      isPrerelease
    }
  }
}'

# Check MySQL driver compatibility
gh api graphql -f query='
{
  repository(owner: "goravel", name: "mysql") {
    release(tagName: "v0.0.0-20250202150522-4e41e0908557") {
      description
      isPrerelease
    }
  }
}'

Length of output: 465

Copy link
Contributor

@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: 0

🧹 Nitpick comments (1)
database/schema/schema.go (1)

35-38: LGTM! Consider adding a comment explaining the fallback behavior.

The fallback mechanism for schema initialization is a good addition that improves robustness, particularly for MySQL support. The implementation is clean and backwards compatible.

Consider adding a comment explaining why we fall back to Database when Schema is empty:

 	schema := driver.Config().Schema
+	// Fall back to Database when Schema is empty, which is typical for MySQL
 	if schema == "" {
 		schema = driver.Config().Database
 	}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ceb2ace and 84c7c2f.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (1)
  • database/schema/schema.go (1 hunks)
⏰ Context from checks skipped due to timeout of 300000ms (2)
  • GitHub Check: test / windows (1.23)
  • GitHub Check: test / windows (1.22)

@hwbrzzl hwbrzzl merged commit 702075c into master Feb 3, 2025
12 of 14 checks passed
@hwbrzzl hwbrzzl deleted the bowen/optimize-tests branch February 3, 2025 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant