X Tutup
Skip to content

[test-improver] Improve tests for config package (GuardPolicyConfig)#1656

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/guard-policy-coverage-377eabceddd300e2
Draft

[test-improver] Improve tests for config package (GuardPolicyConfig)#1656
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/guard-policy-coverage-377eabceddd300e2

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Mar 7, 2026

Test Improvements: guard_policy_test.go

File Analyzed

  • Test File: internal/config/guard_policy_test.go (new file)
  • Implementation: internal/config/guard_policy.go
  • Package: internal/config

Why This File?

The guard_policy.go file provides the GuardPolicyConfig type — a type-safe wrapper around the raw map[string]interface{} guard policies. It has 5 public methods (NewGuardPolicyConfig, GetPolicy, HasPolicy, IsEmpty, GetGuardPolicies) that had zero direct unit test coverage.

The existing config_guardpolicies_test.go file tests guard policy config loading end-to-end (TOML/JSON parsing), but the individual methods of GuardPolicyConfig were never tested in isolation. This is significant because:

  • GetPolicy has a type assertion (.(map[string]interface{})) that can silently return nil for wrong types
  • HasPolicy delegates to GetPolicy, so untested type assertion paths affect both
  • IsEmpty checks len() which behaves differently for nil vs empty maps

Improvements Made

1. New Test File: internal/config/guard_policy_test.go

Added 357 lines of comprehensive unit tests organized into 6 test functions:

TestNewGuardPolicyConfig — 3 sub-tests:

  • nil policies creates non-nil config with empty state
  • ✅ Non-nil policies stored and accessible
  • ✅ Empty map creates non-nil, non-empty config

TestGuardPolicyConfig_GetPolicy — 9 table-driven cases:

  • ✅ Returns policy for existing service
  • ✅ Returns nil for missing service
  • ✅ Returns nil when policies is nil
  • ✅ Returns nil when policies map is empty
  • ✅ Returns nil for non-map value (string, covering type assertion path)
  • ✅ Returns nil for nil value in map
  • ✅ Returns nil for empty string service key
  • ✅ Returns correct policy for one of multiple services
  • ✅ Verifies returned policy field values match expected

TestGuardPolicyConfig_HasPolicy — 6 table-driven cases:

  • ✅ Returns true for existing service with map value
  • ✅ Returns false for missing service
  • ✅ Returns false for nil policies
  • ✅ Returns false for non-map value
  • ✅ Returns false for nil value in map
  • ✅ Multi-service registry returns correct service

TestGuardPolicyConfig_IsEmpty — 5 table-driven cases:

  • nil policies → empty
  • ✅ Empty map → empty
  • ✅ Single entry → not empty
  • ✅ Multiple entries → not empty
  • ✅ Entry with nil value → not empty (key exists)

TestServerConfig_GetGuardPolicies — 4 sub-tests:

  • nil GuardPolicies field returns empty config
  • ✅ Non-nil GuardPolicies returns populated config with correct values
  • ✅ Empty GuardPolicies returns empty config
  • ✅ Multi-service access: HasPolicy, GetPolicy work correctly for all services

TestGuardPolicyConfig_GetPolicy_TypeAssertions — 5 sub-tests:

  • ✅ Boolean value returns nil (type assertion branch)
  • ✅ Integer value returns nil (type assertion branch)
  • ✅ Slice value returns nil (type assertion branch)
  • ✅ Nested map returns valid policy
  • ✅ Empty map value returns non-nil empty policy (HasPolicy returns true)

2. Coverage

Function Before After
NewGuardPolicyConfig 0% 100%
GetPolicy 0% 100%
HasPolicy 0% 100%
IsEmpty 0% 100%
GetGuardPolicies 0% 100%

3. Test Quality

  • ✅ Table-driven tests for multi-case functions
  • ✅ Descriptive sub-test names (t.Run)
  • ✅ Testify assert for non-fatal checks, require for preconditions
  • ✅ Covers all branches including type assertion failures
  • ✅ Tests both nil and empty map edge cases for correct behavior

Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests

Generated by Test Improver

Add comprehensive unit tests for the GuardPolicyConfig type in
guard_policy.go which had no direct test coverage:

- TestNewGuardPolicyConfig: nil/empty/non-nil policies handling
- TestGuardPolicyConfig_GetPolicy: service lookup, type assertions,
  nil handling, non-map values
- TestGuardPolicyConfig_HasPolicy: existence checks for all cases
- TestGuardPolicyConfig_IsEmpty: empty/non-empty detection
- TestServerConfig_GetGuardPolicies: helper method integration
- TestGuardPolicyConfig_GetPolicy_TypeAssertions: boolean, integer,
  slice, and empty map values

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

0 participants

X Tutup