Skip to content

unit tests for postgres database#1793

Draft
DmytroPI-dev wants to merge 1 commit intofeature/database-controllersfrom
unit-tests-for-postgres-database
Draft

unit tests for postgres database#1793
DmytroPI-dev wants to merge 1 commit intofeature/database-controllersfrom
unit-tests-for-postgres-database

Conversation

@DmytroPI-dev
Copy link

Description

Unit tests for postgresql/database/core

Key Changes

Add database_unit_test.go

Testing and Verification

The PR is for adding unit tests.

Related Issues

CPI-1915

PR Checklist

  • Code changes adhere to the project's coding standards.
  • Relevant unit and integration tests are included.
  • Documentation has been updated accordingly.
  • All tests pass locally.
  • The PR description follows the project's guidelines.

}

got := managedRoleOwners(managedFields)
want := map[string]string{
Copy link
Collaborator

@mploski mploski Mar 25, 2026

Choose a reason for hiding this comment

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

In general want should be a part of Arrange if we want to follow AAA pattern - this is general comment

Status: enterprisev4.PostgresClusterStatus{
Phase: func() *string {
v := string(ClusterReady)
return &v
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do we need this, maybe we can also levarage Ptr helper function strPtr(s string) *string


require.Len(t, got.retained, 2)
require.Len(t, got.deleted, 1)
assert.Equal(t, "payments", got.retained[0].Name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we levarage assert.ElementsMatch to avoid hardcoding the order?

Name: "primary-payments-admin",
Namespace: "dbs",
Annotations: map[string]string{annotationRetainedFrom: postgresDB.Name},
OwnerReferences: []metav1.OwnerReference{
Copy link
Collaborator

Choose a reason for hiding this comment

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

since you wan to test retained secret, shouldn't owner references be empty?

Copy link
Author

Choose a reason for hiding this comment

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

In database.go, in orphanSecret method, we have the following:

  1. Fetch the secret
  2. if secret.Annotations[annotationRetainedFrom] == postgresDB.Name, we continue immediately, and only otherwise it strips owner ref and add annotation.
    So for this specific test, the point is to verify the early-return path: if a secret is already marked retained, the helper should leave it alone. Leaving an owner reference in the fixture might be useful, as it proves the function did not mutate the object. If there would be empty owner ref, test would not show if helper truly skipped mutation.

}

newDBRepo := func(_ context.Context, host, dbName, password string) (DBRepo, error) {
assert.Equal(t, "rw.example.internal", host)
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need this asserts here? what is the benefit?

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.

2 participants