Closed jeffy-mathew closed 2 months ago
API Changes
no api changes detected
β±οΈ Estimated effort to review: 4 π΅π΅π΅π΅βͺ |
π§ͺ PR contains tests |
π No security concerns identified |
β‘ Key issues to review Code Duplication The test cases and structures in 'internal/policy/apply_test.go' are largely duplicated from the previous test suite in 'gateway/policy_test.go'. Consider refactoring to share common test utilities and data structures to reduce duplication and maintenance overhead. Missing Error Handling In the new test functions `TestService_Apply` and `BenchmarkService_Apply`, there is no handling or assertion for potential errors returned from the `service.Apply(sess)` method. This could lead to missed test failures during benchmarking and unit testing. |
Category | Suggestion | Score |
Best practice |
Use
___
**Instead of using | 9 |
Improve error handling in tests to ensure accurate reporting of failures___ **Consider handling the error fromservice.Apply(sess) directly in the test case to ensure that test failures are reported accurately.** [internal/policy/apply_test.go [1210-1212]](https://github.com/TykTechnologies/tyk/pull/6585/files#diff-5af7e299a6b0ce11e22f8aa4a01854b1151f4b54dccc68f0cd1cbedee5aed7c8R1210-R1212) ```diff if err := service.Apply(sess); err != nil { - assert.ErrorContains(t, err, tc.errMatch) + if !assert.ErrorContains(t, err, tc.errMatch) { + t.Errorf("Unexpected error: %v", err) + } return } ``` Suggestion importance[1-10]: 8Why: The suggestion enhances error handling by ensuring that unexpected errors are reported, which improves test reliability and debugging. | 8 | |
Performance |
Optimize the reversal of slices by using a manual loop instead of importing the
___
**Replace the use of | 7 |
Maintainability |
Improve code readability by using more descriptive variable names___ **Use a more descriptive variable name thantb for the testing object to enhance code readability.** [internal/policy/apply_test.go [177]](https://github.com/TykTechnologies/tyk/pull/6585/files#diff-5af7e299a6b0ce11e22f8aa4a01854b1151f4b54dccc68f0cd1cbedee5aed7c8R177-R177) ```diff -func testPrepareApplyPolicies(tb testing.TB) (*policy.Service, []testApplyPoliciesData) { +func testPrepareApplyPolicies(t testing.TB) (*policy.Service, []testApplyPoliciesData) { ``` Suggestion importance[1-10]: 5Why: While changing `tb` to `t` slightly improves readability, the improvement is minimal as both are common shorthand in Go testing. | 5 |
Failed conditions
C Reliability Rating on New Code (required β₯ A)
See analysis details on SonarCloud
Catch issues before they fail your Quality Gate with our IDE extension SonarLint
User description
Description
refactor apply policies tests to internal/policy
Related Issue
https://tyktech.atlassian.net/browse/TT-13175
Motivation and Context
How This Has Been Tested
Screenshots (if appropriate)
Types of changes
Checklist
PR Type
Tests
Description
gateway/policy_test.go
tointernal/policy/apply_test.go
.TestApplyPolicies
andBenchmarkApplyPolicies
fromgateway/policy_test.go
.TestService_Apply
andBenchmarkService_Apply
ininternal/policy/apply_test.go
.internal/policy/testdata/policies.json
for enhanced test coverage.Changes walkthrough π
policy_test.go
Remove apply policies test cases from gateway
gateway/policy_test.go
TestApplyPolicies
andBenchmarkApplyPolicies
functions.apply_test.go
Add apply policies test cases to internal/policy
internal/policy/apply_test.go
TestService_Apply
andBenchmarkService_Apply
functions.testPrepareApplyPolicies
.policies.json
Add new policy entries for testing
internal/policy/testdata/policies.json
acl_with_allowed_url
andrate_limit
policies.