Skip to content

test: Add unit test for service/account/api/credits.go #5589

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gru-agent[bot]
Copy link

@gru-agent gru-agent bot commented May 12, 2025

Warning

[Regression Alert] The existing unit tests were failing. Gru has updated them, adding coverage and ensuring they pass. Please check if this behavior is expected.

Trigger Info

Trigger Type Triggered By Source Pull Request Assignment
PR Created bxy4543 5586 Detail

Summary

This PR introduces comprehensive unit tests for the getCreditsInfo function in the credits_test.go file. The key changes and additions include:

  1. Mock Implementation for DBClientInterface:

    • A mockDBClient struct is created to simulate database interactions, enabling isolated testing of the getCreditsInfo function.

    • Mock methods are implemented for GetSubscription, GetAccount, GetSubscriptionPlan, and GetAvailableCredits.

  2. Unit Tests for getCreditsInfo:

    • Success Scenarios:

      • Test_getCreditsInfo_success: Validates the function's behavior when both current and free plans are provided.

      • Test_getCreditsInfo_freePlan: Tests the function's behavior when the user is on a free plan.

    • Error Scenarios:

      • Test_getCreditsInfo_errorSubscription: Simulates a failure in fetching subscription information.

      • Test_getCreditsInfo_errorAccount: Simulates a failure in fetching account information.

      • Test_getCreditsInfo_errorPlan: Simulates a failure in fetching the current subscription plan.

      • Test_getCreditsInfo_errorFreePlan: Simulates a failure in fetching the free subscription plan.

      • Test_getCreditsInfo_errorAvailableCredits: Simulates a failure in fetching available credits.

  3. Global DBClient Patching:

    • The global dao.DBClient is temporarily replaced with the mock implementation during tests to ensure isolation and restore it afterward.
  4. Assertions and Validations:

    • Utilizes require and assert from testify for robust validation of expected outcomes and error handling.
  5. Retained Integration Test:

    • The original integration test for getCreditsInfo is preserved to ensure end-to-end functionality.

This PR enhances the test coverage for getCreditsInfo, ensuring its reliability across various scenarios, including edge cases and error conditions.

Coverage

The change in coverage value, such as: 0% -> 50%, indicates that the coverage was 0% before writing the tests and 50% after writing them.

https://web.dev/articles/ta-code-coverage

Type Change
lines unknown
statements unknown
functions unknown
branches unknown
source program

Test Statuses

Status Change
passed 0 -> 0
failed 0 -> 0
skipped 0 -> 0
error 0 -> 0
source program

Tip

You can @gru-agent and leave your feedback. TestGru will make adjustments based on your input

Tip

You can @gru-agent rebase to rebase the PR.

Tip

You can @gru-agent redo to reset or rebase before redoing the PR.

Tip

To modify the test code yourself, click here Edit Test Code

Copy link

🤖 Generated by lychee action

Summary

Status Count
🔍 Total 2066
✅ Successful 419
⏳ Timeouts 2
🔀 Redirected 0
👻 Excluded 1616
❓ Unknown 0
🚫 Errors 27

Errors per input

Errors in docs/5.0/docs/developer-guide/lifecycle-management/advanced-guide/dual-stack-cluster.md

Errors in docs/5.0/docs/developer-guide/lifecycle-management/reference/sealos/commands/inspect.md

Errors in docs/5.0/docs/developer-guide/lifecycle-management/advanced-guide/template-function.md

Errors in docs/5.0/i18n/zh-Hans/developer-guide/lifecycle-management/quick-start/deploy-kubernetes.md

Errors in docs/4.0/i18n/zh-Hans/self-hosting/sealos/QA.md

Errors in docs/website/README.md

Errors in docs/5.0/i18n/zh-Hans/developer-guide/lifecycle-management/advanced-guide/template-function.md

Errors in CONTRIBUTING.md

Errors in controllers/db/adminer/README_zh.md

Errors in docs/4.0/docs/guides/templates/submit-template.md

Errors in docs/4.0/i18n/zh-Hans/self-hosting/lifecycle-management/advanced-guide/dual-stack-cluster.md

Errors in docs/5.0/docs/developer-guide/lifecycle-management/quick-start/deploy-kubernetes.md

Errors in docs/4.0/docs/self-hosting/lifecycle-management/quick-start/deploy-kubernetes.md

Errors in docs/4.0/i18n/zh-Hans/guides/templates/submit-template.md

Errors in docs/5.0/i18n/zh-Hans/developer-guide/lifecycle-management/reference/sealos/commands/inspect.md

Errors in docs/4.0/docs/self-hosting/lifecycle-management/advanced-guide/dual-stack-cluster.md

Errors in controllers/db/adminer/README.md

Errors in docs/4.0/docs/self-hosting/lifecycle-management/reference/sealos/commands/inspect.md

Errors in docs/4.0/docs/self-hosting/lifecycle-management/advanced-guide/template-function.md

Errors in docs/5.0/i18n/zh-Hans/developer-guide/sealos/QA.md

Errors in docs/5.0/i18n/zh-Hans/developer-guide/lifecycle-management/advanced-guide/dual-stack-cluster.md

Errors in docs/4.0/i18n/zh-Hans/self-hosting/lifecycle-management/quick-start/deploy-kubernetes.md

Errors in docs/4.0/i18n/zh-Hans/self-hosting/lifecycle-management/reference/sealos/commands/inspect.md

Errors in docs/4.0/i18n/zh-Hans/self-hosting/lifecycle-management/advanced-guide/template-function.md

Full action output

Full Github Actions output

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