aboutsummaryrefslogtreecommitdiff
path: root/internal/api/api_test.go
diff options
context:
space:
mode:
authorEthan Lee <ethanalee@google.com>2026-03-11 21:20:21 +0000
committerGopher Robot <gobot@golang.org>2026-03-30 13:26:49 -0700
commit8514eebca6ca7b3213e879faa2a83c7e9ea6e181 (patch)
tree85db643baa162d5a828895d3655b8e1fd3f5c9af /internal/api/api_test.go
parent46b63f3ffddc9657bc2c45fb116dc49c0b381a34 (diff)
downloadgo-x-pkgsite-8514eebca6ca7b3213e879faa2a83c7e9ea6e181.tar.xz
internal/api: implement package imported-by endpoint
- Implement ServePackageImportedBy handler and GetImportedBy and GetImportedByCount datasource methods. Change-Id: I8c4cc65fbff7172eaf48e5426e4f3f41c82bd38e Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/754865 Reviewed-by: Jonathan Amsterdam <jba@google.com> kokoro-CI: kokoro <noreply+kokoro@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ethan Lee <ethanalee@google.com>
Diffstat (limited to 'internal/api/api_test.go')
-rw-r--r--internal/api/api_test.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/internal/api/api_test.go b/internal/api/api_test.go
index 90373b66..a0ded1d2 100644
--- a/internal/api/api_test.go
+++ b/internal/api/api_test.go
@@ -708,6 +708,66 @@ func TestServePackageSymbols(t *testing.T) {
}
}
+func TestServePackageImportedBy(t *testing.T) {
+ ctx := context.Background()
+ ds := fakedatasource.New()
+
+ const (
+ pkgPath = "example.com/pkg"
+ modulePath = "example.com"
+ version = "v1.0.0"
+ )
+
+ ds.MustInsertModule(ctx, &internal.Module{
+ ModuleInfo: internal.ModuleInfo{ModulePath: modulePath, Version: version},
+ Units: []*internal.Unit{
+ {UnitMeta: internal.UnitMeta{Path: pkgPath, ModuleInfo: internal.ModuleInfo{ModulePath: modulePath, Version: version}}},
+ {
+ UnitMeta: internal.UnitMeta{Path: "example.com/other", ModuleInfo: internal.ModuleInfo{ModulePath: modulePath, Version: version}},
+ Imports: []string{pkgPath},
+ },
+ },
+ })
+
+ for _, test := range []struct {
+ name string
+ url string
+ wantStatus int
+ wantCount int
+ }{
+ {
+ name: "all imported by",
+ url: "/v1/imported-by/example.com/pkg?version=v1.0.0",
+ wantStatus: http.StatusOK,
+ wantCount: 1,
+ },
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ r := httptest.NewRequest("GET", test.url, nil)
+ w := httptest.NewRecorder()
+
+ err := ServePackageImportedBy(w, r, ds)
+ if err != nil && w.Code != test.wantStatus {
+ t.Fatalf("ServePackageImportedBy returned error: %v", err)
+ }
+
+ if w.Code != test.wantStatus {
+ t.Errorf("status = %d, want %d", w.Code, test.wantStatus)
+ }
+
+ if test.wantStatus == http.StatusOK {
+ var got PackageImportedBy
+ if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
+ t.Fatalf("json.Unmarshal: %v", err)
+ }
+ if len(got.ImportedBy.Items) != test.wantCount {
+ t.Errorf("count = %d, want %d", len(got.ImportedBy.Items), test.wantCount)
+ }
+ }
+ })
+ }
+}
+
// unmarshalResponse unmarshals an API response into either
// a *T or an *Error.
func unmarshalResponse[T any](data []byte) (any, error) {