-
Notifications
You must be signed in to change notification settings - Fork 4
api/v1: public stats and recent reversals endpoints #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ZukwiZ
wants to merge
7
commits into
master
Choose a base branch
from
feat/public-stats-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9be036d
api/v1: public stats and recent reversals endpoints
byskov 3b30881
api/v1: address stats review feedback
byskov 8440a0d
repository: track Steam ID searches for stats summary
byskov 8652b50
Harden DailyCounts GROUP BY to positional form
byskov a1d8b76
Order recent reversals feed by reversed_at DESC, id DESC
byskov b6aa57f
Address PR #69 review feedback from zedimytch
cursoragent 897fd8e
Fix reversal list ordering regression from OrderBy refactor
byskov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| package reversals | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "reverse-watch/domain/models" | ||
| "reverse-watch/domain/models/constants" | ||
| "reverse-watch/errors" | ||
| "reverse-watch/internal/testutil" | ||
| "reverse-watch/middleware" | ||
| "reverse-watch/repository/factory" | ||
| "reverse-watch/secret" | ||
| "reverse-watch/util" | ||
|
|
||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| func buildRecentHandlerStack(t *testing.T) (http.Handler, *gorm.DB) { | ||
| t.Helper() | ||
|
|
||
| db := testutil.NewTestDB(t) | ||
| keygen := secret.NewKeyGenerator(constants.EnvironmentDevelopment) | ||
| f, err := factory.NewFactoryWithConfig(&factory.Config{ | ||
| PrivateDB: db, | ||
| PublicDB: db, | ||
| KeyGen: keygen, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("NewFactoryWithConfig(): %v", err) | ||
| } | ||
|
|
||
| handler := http.HandlerFunc(listRecentHandler) | ||
| return middleware.FactoryMiddleware(f)(handler), db | ||
| } | ||
|
|
||
| func TestListRecentHandler(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| handler, db := buildRecentHandlerStack(t) | ||
|
|
||
| base := models.Epoch + 1000 | ||
|
|
||
| // 5 rows. The feed orders by reversed_at DESC, id DESC. CreatedAt is set | ||
| // in ascending id order (i.e. ingest order) to prove the feed does NOT use | ||
| // id/ingest order. Row id=3 is expunged and must be excluded. Row id=5 is a | ||
| // backfill case: it has the highest id but the oldest reversed_at, so it | ||
| // must sort last rather than first. Rows id=1 and id=4 share a reversed_at | ||
| // to exercise the id DESC tiebreaker (id=4 must come before id=1). | ||
| testutil.Insert(t, db, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 1, CreatedAt: base + 100}, | ||
| SteamID: models.SteamID(76561197960287930), | ||
| MarketplaceSlug: "csfloat", | ||
| ReversedAt: base + 100, | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 2, CreatedAt: base + 200}, | ||
| SteamID: models.SteamID(76561197960287931), | ||
| MarketplaceSlug: "csfloat", | ||
| ReversedAt: base + 500, | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 3, CreatedAt: base + 300}, | ||
| SteamID: models.SteamID(76561197960287932), | ||
| MarketplaceSlug: "csfloat", | ||
| ReversedAt: base + 900, | ||
| ExpungedAt: util.Ptr(base + 400), | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 4, CreatedAt: base + 500}, | ||
| SteamID: models.SteamID(76561197960287933), | ||
| MarketplaceSlug: "csfloat", | ||
| ReversedAt: base + 100, | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 5, CreatedAt: base + 600}, | ||
| SteamID: models.SteamID(76561197960287934), | ||
| MarketplaceSlug: "csfloat", | ||
| ReversedAt: base + 50, | ||
| }, | ||
| ) | ||
|
|
||
| r := httptest.NewRequest(http.MethodGet, "/recent", nil) | ||
| w := httptest.NewRecorder() | ||
| handler.ServeHTTP(w, r) | ||
|
|
||
| resp := w.Result() | ||
| if resp.StatusCode != http.StatusOK { | ||
| t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK) | ||
| } | ||
|
|
||
| var body listRecentResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
| t.Fatalf("decode: %v", err) | ||
| } | ||
|
|
||
| wantSteamIDs := []models.SteamID{ | ||
| 76561197960287931, // id=2, reversed_at=base+500 | ||
| 76561197960287933, // id=4, reversed_at=base+100 (id tiebreaker over id=1) | ||
| 76561197960287930, // id=1, reversed_at=base+100 | ||
| 76561197960287934, // id=5, reversed_at=base+50 (backfill: high id, oldest) | ||
| } | ||
| if len(body.Data) != len(wantSteamIDs) { | ||
| t.Fatalf("len(data) = %d, want %d", len(body.Data), len(wantSteamIDs)) | ||
| } | ||
| for i, want := range wantSteamIDs { | ||
| if body.Data[i].SteamID != want { | ||
| t.Errorf("data[%d].SteamID = %d, want %d", i, body.Data[i].SteamID, want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestListRecentHandler_RespectsLimit(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| handler, db := buildRecentHandlerStack(t) | ||
|
|
||
| base := models.Epoch + 1000 | ||
| testutil.Insert(t, db, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 1, CreatedAt: base + 100}, | ||
| SteamID: models.SteamID(76561197960287930), | ||
| MarketplaceSlug: "csfloat", | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 2, CreatedAt: base + 200}, | ||
| SteamID: models.SteamID(76561197960287931), | ||
| MarketplaceSlug: "csfloat", | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 3, CreatedAt: base + 300}, | ||
| SteamID: models.SteamID(76561197960287932), | ||
| MarketplaceSlug: "csfloat", | ||
| }, | ||
| ) | ||
|
|
||
| r := httptest.NewRequest(http.MethodGet, "/recent?limit=2", nil) | ||
| w := httptest.NewRecorder() | ||
| handler.ServeHTTP(w, r) | ||
|
|
||
| resp := w.Result() | ||
| if resp.StatusCode != http.StatusOK { | ||
| t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK) | ||
| } | ||
| var body listRecentResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
| t.Fatalf("decode: %v", err) | ||
| } | ||
| if len(body.Data) != 2 { | ||
| t.Errorf("len(data) = %d, want 2", len(body.Data)) | ||
| } | ||
| } | ||
|
|
||
| func TestListRecentHandler_InvalidLimit(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| handler, _ := buildRecentHandlerStack(t) | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| limit string | ||
| }{ | ||
| {name: "zero", limit: "0"}, | ||
| {name: "negative", limit: "-1"}, | ||
| {name: "overMax", limit: "101"}, | ||
| {name: "nonNumeric", limit: "abc"}, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| r := httptest.NewRequest(http.MethodGet, "/recent?limit="+tc.limit, nil) | ||
| w := httptest.NewRecorder() | ||
| handler.ServeHTTP(w, r) | ||
|
|
||
| resp := w.Result() | ||
| if resp.StatusCode != http.StatusBadRequest { | ||
| t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusBadRequest) | ||
| } | ||
| var body errors.Error | ||
| if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
| t.Fatalf("decode: %v", err) | ||
| } | ||
| if body.Details != "limit must be between 1 and 100" { | ||
| t.Errorf("details = %q, want %q", body.Details, "limit must be between 1 and 100") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestListRecentHandler_ResponseShape(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| handler, db := buildRecentHandlerStack(t) | ||
|
|
||
| base := models.Epoch + 1000 | ||
| testutil.Insert(t, db, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 1, CreatedAt: base + 100}, | ||
| SteamID: models.SteamID(76561197960287930), | ||
| MarketplaceSlug: "csfloat", | ||
| ReversedAt: base + 50, | ||
| }, | ||
| ) | ||
|
|
||
| r := httptest.NewRequest(http.MethodGet, "/recent", nil) | ||
| w := httptest.NewRecorder() | ||
| handler.ServeHTTP(w, r) | ||
|
|
||
| // Decode as raw JSON to assert the exact wire shape (especially steam_id as a string). | ||
| var raw struct { | ||
| Data []map[string]interface{} `json:"data"` | ||
| } | ||
| if err := json.NewDecoder(w.Result().Body).Decode(&raw); err != nil { | ||
| t.Fatalf("decode: %v", err) | ||
| } | ||
| if len(raw.Data) != 1 { | ||
| t.Fatalf("len(data) = %d, want 1", len(raw.Data)) | ||
| } | ||
| row := raw.Data[0] | ||
|
|
||
| expectedKeys := []string{"marketplace_slug", "steam_id", "reversed_at"} | ||
| for _, k := range expectedKeys { | ||
| if _, ok := row[k]; !ok { | ||
| t.Errorf("missing key %q in response", k) | ||
| } | ||
| } | ||
| for k := range row { | ||
| ok := false | ||
| for _, want := range expectedKeys { | ||
| if k == want { | ||
| ok = true | ||
| break | ||
| } | ||
| } | ||
| if !ok { | ||
| t.Errorf("unexpected key %q in response", k) | ||
| } | ||
| } | ||
|
|
||
| steamIDValue, ok := row["steam_id"].(string) | ||
| if !ok { | ||
| t.Errorf("steam_id should be a JSON string, got %T", row["steam_id"]) | ||
| } | ||
| if steamIDValue != "76561197960287930" { | ||
| t.Errorf("steam_id = %q, want %q", steamIDValue, "76561197960287930") | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.