chore(utilities): add tests for previously-uncovered helpers#2510
Open
mastermanas805 wants to merge 1 commit intosupabase:masterfrom
Open
chore(utilities): add tests for previously-uncovered helpers#2510mastermanas805 wants to merge 1 commit intosupabase:masterfrom
mastermanas805 wants to merge 1 commit intosupabase:masterfrom
Conversation
d2818fc to
4ec0d5c
Compare
internal/utilities/strings.go, context.go, and io.go held small pure-ish helpers but had no test coverage. Add focused unit tests so behaviour changes get caught: round-trip tests for StringValue and StringPtr; a context-key formatting check plus set/read/replace/derive cases for WithRequestID and GetRequestID; cancellation and completion cases for WaitForCleanup; and behaviour assertions for SafeClose covering the happy path, the error path, and io.NopCloser. Coverage for the package goes from 50.8% to 62.4% with no behaviour changes. Signed-off-by: Manas Srivastava <mastermanas805@gmail.com>
4ec0d5c to
bf5de18
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds unit tests for three small files in
internal/utilities/that had zero coverage:strings.go—StringValueandStringPtrcontext.go—contextKey.String,WithRequestID,GetRequestID,WaitForCleanupio.go—SafeCloseWhy
Coverage of
internal/utilities/was the lowest among utility-style packages in the module:Each of the helpers covered by this PR is a pure-ish function used widely across the API layer, so a behavior change here would propagate quickly. Locking the contracts in tests makes the next maintenance pass safer without changing any production code.
Coverage delta
internal/utilities(+11.6 percentage points, no production code touched.)
What's tested
strings.goStringValue(nil)andStringValue(&"")both return""StringValue(&s)returnssfor non-emptysStringPtr("")returnsnil;StringPtr(s)returns*sfor non-emptyStringPtris independent of the input variable (defends against future refactors that take the address of a closure-captured variable)StringValue(StringPtr(c)) == cround-trip across edge cases (empty, ASCII, whitespace, newline, multi-byte rune)context.gocontextKey.String()formatting (covers both populated and empty key)WithRequestID/GetRequestIDround-tripGetRequestIDreturns""when key absentcontext.WithCancel(defends the public-API guarantee that derived contexts see the parent value)WaitForCleanupreturns when the wait group completesWaitForCleanupreturns when the context is cancelled, even if the wait group never completes (covers the leak path)io.goSafeCloseinvokesClose()on the underlying closerSafeClosedoes not panic whenClose()returns an errorSafeClosedoes not panic onio.NopCloser(nil)How to verify (for reviewers)
The
-coverline should report 62.4%.Verification I ran locally
go test ./internal/utilities/ -count=1 -v— all new tests passgo test ./... -count=1 -p 1 -race— full module suite greengofmt -lon the three new files — emptygo vet ./...— emptyNotes
<file>_test.goper source file).WaitForCleanupis exercised end-to-end via real goroutines pluscontext.WithCancel, matching how the production callers use it. No mocking of channels.postgres.go,hibpcache.go,request.go::GetBodyBytes) need real Postgres errors / bloom-filter setup / multipart bodies and would each be its own PR.