Skip to content

refactor(db): Extract function PickOne#1160

Merged
MR2011 merged 2 commits intomainfrom
mikrzyz/pick-one
Apr 10, 2026
Merged

refactor(db): Extract function PickOne#1160
MR2011 merged 2 commits intomainfrom
mikrzyz/pick-one

Conversation

@michalkrzyz
Copy link
Copy Markdown
Collaborator

Extract test helper function PickOne to minimize slips with double access to list. This refactor also increase test readability and removes test direct random dependency.

On-behalf-of: SAP Michal Krzyz michal.krzyz@sap.com

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.

What type of PR is this? (check all applicable)

  • 🍕 Feature
  • 🐛 Bug Fix
  • 📝 Documentation Update
  • 🎨 Style
  • 🧑‍💻 Code Refactor
  • 🔥 Performance Improvements
  • ✅ Test
  • 🤖 Build
  • 🔁 CI
  • 📦 Chore (Release)
  • ⏩ Revert

Related Tickets & Documents

  • Related Issue # (issue)
  • Closes # (issue)
  • Fixes # (issue)

Remove if not applicable

Added tests?

  • 👍 yes
  • 🙅 no, because they aren't needed
  • 🙋 no, because I need help
  • Separate ticket for tests # (issue/pr)

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

Added to documentation?

  • 📜 README.md
  • 🤝 Documentation pages updated
  • 🙅 no documentation needed
  • (if applicable) generated OpenAPI docs for CRD changes

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copilot AI review requested due to automatic review settings April 9, 2026 06:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extracts a generic PickOne helper function to refactor test code and eliminate direct dependencies on math/rand in test files. The refactoring replaces all occurrences of the pattern list[rand.Intn(len(list))] with calls to test.PickOne(list), improving test readability and reducing the risk of accidental double-access slips to collections.

Changes:

  • Introduces a generic PickOne function in the test package that encapsulates random element selection
  • Refactors 12 database test files to use the new helper function instead of inline rand.Intn calls
  • Modifies receiver types in entity.go from pointer to value receivers for As* conversion methods to accommodate the return value semantics of the PickOne function

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated no comments.

Show a summary per file
File Description
internal/database/mariadb/test/common.go Adds the generic PickOne function and imports math/rand into the test package
internal/database/mariadb/user_test.go Replaces 6 instances of rand.Intn with test.PickOne and removes unused import
internal/database/mariadb/support_group_test.go Replaces 4 instances of rand.Intn with test.PickOne and removes unused import
internal/database/mariadb/service_test.go Replaces 10 instances of rand.Intn with test.PickOne and removes unused import
internal/database/mariadb/remediation_test.go Replaces 10 instances of rand.Intn with test.PickOne and removes unused import
internal/database/mariadb/patch_test.go Replaces 6 instances of rand.Intn with test.PickOne and removes unused import
internal/database/mariadb/issue_variant_test.go Replaces 10 instances of rand.Intn with test.PickOne and removes unused import
internal/database/mariadb/issue_test.go Replaces 15 instances of rand.Intn with test.PickOne and removes unused import
internal/database/mariadb/issue_repository_test.go Replaces 3 instances of rand.Intn with test.PickOne and removes unused import
internal/database/mariadb/issue_match_test.go Replaces 16 instances of rand.Intn with test.PickOne and removes unused import
internal/database/mariadb/component_version_test.go Replaces 10 instances of rand.Intn with test.PickOne
internal/database/mariadb/component_test.go Replaces 2 instances of rand.Intn with test.PickOne and removes unused import
internal/database/mariadb/component_instance_test.go Replaces 5 instances of rand.Intn with test.PickOne and removes unused import
internal/database/mariadb/entity.go Changes receiver types from pointer to value for 16 As* conversion methods

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Extract function PickOne to minimize errors with double access to list

On-behalf-of: SAP Michal Krzyz <michal.krzyz@sap.com>
Signed-off-by: Michal Krzyz <michalkrzyz@gmail.com>
@MR2011 MR2011 merged commit 6c180e6 into main Apr 10, 2026
10 checks passed
@MR2011 MR2011 deleted the mikrzyz/pick-one branch April 10, 2026 12:24
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.

3 participants