Expand test suite with adapter and DB coverage#41
Expand test suite with adapter and DB coverage#41wagnerdevocelot wants to merge 4 commits intomainfrom
Conversation
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR expands test coverage and adds performance benchmarks for the logging and legacy DB adapters.
- Adds unit tests for
LegacyDBAdapterclient, token, session, and JWT methods - Adds unit tests for
LoggingAdaptermetrics and error metrics - Adds an end-to-end integration test simulating the OAuth2 code flow against the legacy DB
- Introduces benchmarks for token creation on both adapters and adds the SQLite driver dependency
Reviewed Changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| logging_adapter_test.go | Unit tests for LoggingAdapter metrics and error metrics |
| db_adapter_test.go | Unit tests for LegacyDBAdapter client, token, session, and JWT methods |
| integration_legacy_db_test.go | Integration test simulating full OAuth2 flow against the legacy DB |
| benchmark_test.go | Benchmarks for token creation on LoggingAdapter and LegacyDBAdapter |
| go.mod | Adds github.com/mattn/go-sqlite3 dependency |
Comments suppressed due to low confidence (2)
logging_adapter_test.go:192
- [nitpick] TestLoggingAdapterErrorMetrics only verifies the
GetClienterror metric. Consider adding cases for other methods to ensure all error metrics are incremented as expected.
fs := newFailingStore(map[string]bool{"GetClient": true})
db_adapter_test.go:40
- [nitpick] The client tests only validate ID and RedirectURIs. Consider adding assertions for other fields like Scopes and Audience to ensure full client data is persisted and retrieved.
client := &fosite.DefaultClient{ID: "c1", Secret: []byte("secret"), RedirectURIs: []string{"http://localhost"}}
| ) | ||
|
|
||
| // failingStore wraps InMemoryStore and allows forcing errors for selected methods. | ||
| type failingStore struct { |
There was a problem hiding this comment.
[nitpick] The failingStore implements many methods with identical error-checking boilerplate. Consider extracting a shared helper or decorator to reduce duplication and improve maintainability.
| t.Fatalf("token request err: %v", err) | ||
| } | ||
| var tokenResp map[string]interface{} | ||
| json.NewDecoder(resToken.Body).Decode(&tokenResp) |
There was a problem hiding this comment.
Decoding JSON without checking the returned error can mask failures. Consider capturing and validating the error from Decode before using the parsed data.
| json.NewDecoder(resToken.Body).Decode(&tokenResp) | |
| if err := json.NewDecoder(resToken.Body).Decode(&tokenResp); err != nil { | |
| t.Fatalf("failed to decode token response: %v", err) | |
| } |
| json.NewDecoder(resToken.Body).Decode(&tokenResp) | ||
| resToken.Body.Close() | ||
|
|
||
| access := tokenResp["access_token"].(string) |
There was a problem hiding this comment.
A direct type assertion on tokenResp["access_token"] will panic if the key is missing or not a string. Use a safe assertion and handle the case where the assertion fails.
| access := tokenResp["access_token"].(string) | |
| access, ok := tokenResp["access_token"].(string) | |
| if !ok { | |
| t.Fatalf("access_token missing or not a string: %v", tokenResp) | |
| } |
| req := []byte("data") | ||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| adapter.CreateToken(b.Context(), "access_token", "sig"+strconv.Itoa(i), "c1", req) |
There was a problem hiding this comment.
The benchmark for LegacyDBCreateToken inserts into a table that grows without bound, skewing results. Consider truncating the table or resetting the database between iterations to maintain consistent measurements.
| adapter.CreateToken(b.Context(), "access_token", "sig"+strconv.Itoa(i), "c1", req) | |
| adapter.CreateToken(b.Context(), "access_token", "sig"+strconv.Itoa(i), "c1", req) | |
| if _, err := db.Exec("DELETE FROM tokens"); err != nil { | |
| b.Fatal(err) | |
| } |
| adapter := NewLoggingAdapter(NewInMemoryStore()) | ||
| req := &fosite.Request{} | ||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { |
There was a problem hiding this comment.
[nitpick] Repeated calls to CreateToken on the same LoggingAdapter and InMemoryStore accumulate state across iterations. Consider resetting or recreating the store inside the loop to isolate performance characteristics.
| adapter := NewLoggingAdapter(NewInMemoryStore()) | |
| req := &fosite.Request{} | |
| b.ResetTimer() | |
| for i := 0; i < b.N; i++ { | |
| req := &fosite.Request{} | |
| b.ResetTimer() | |
| for i := 0; i < b.N; i++ { | |
| adapter := NewLoggingAdapter(NewInMemoryStore()) |
Summary
Testing
go test ./... -vgo test -bench=. -run=NONEhttps://chatgpt.com/codex/tasks/task_e_687443f066ac832881de289ac38519fa