internal/api: group tests into one top-level function Call all tests from a single function, TestAPI. This prepares us to move the top-level part of the test to another package, removing the dependency on postgres. Change-Id: Ib58d464743ecd1beb19f3bc4c062f6a3bd35c06b Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/767883 kokoro-CI: kokoro <noreply+kokoro@google.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/internal/api/api.go b/internal/api/api.go index 09091c5..ddb8af2 100644 --- a/internal/api/api.go +++ b/internal/api/api.go
@@ -430,7 +430,7 @@ // ServeVulnerabilities handles requests for the v1 module vulnerabilities endpoint. // api:route /v1/vulns/{path} // api:desc Vulnerabilities of the module at {path}. -func ServeVulnerabilities(vc *vuln.Client) func(w http.ResponseWriter, r *http.Request, ds internal.DataSource) error { +func ServeVulnerabilities(vc *vuln.Client) func(w http.ResponseWriter, r *http.Request, _ internal.DataSource) error { return func(w http.ResponseWriter, r *http.Request, ds internal.DataSource) (err error) { defer derrors.Wrap(&err, "ServeVulnerabilities")
diff --git a/internal/api/api_test.go b/internal/api/api_test.go index a3ddf58..94308cc 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go
@@ -50,7 +50,7 @@ // which makes it hard to see test information. // Note: This function modifies global log state and should not // be used in tests running with t.Parallel(). -func setupTestDB(t *testing.T) *postgres.DB { +func setupTestDB(t *testing.T) internal.TestingDataSource { orig := log.GetLevel() t.Cleanup(func() { log.SetLevel(orig.String()) }) log.SetLevel("Info") @@ -69,12 +69,41 @@ return db } -func TestServePackage(t *testing.T) { +func TestAPI(t *testing.T) { t.Run("fake", func(t *testing.T) { - testServePackage(t, fakedatasource.New()) + testAPI(t, func(t *testing.T) internal.TestingDataSource { + return fakedatasource.New() + }) }) t.Run("db", func(t *testing.T) { - testServePackage(t, setupTestDB(t)) + testAPI(t, setupTestDB) + }) +} + +func testAPI(t *testing.T, newTestingDataSource func(t *testing.T) internal.TestingDataSource) { + t.Run("package", func(t *testing.T) { + testServePackage(t, newTestingDataSource(t)) + }) + t.Run("module", func(t *testing.T) { + testServeModule(t, newTestingDataSource(t)) + }) + t.Run("module versions", func(t *testing.T) { + testServeModuleVersions(t, newTestingDataSource(t)) + }) + t.Run("module packages", func(t *testing.T) { + testServeModulePackages(t, newTestingDataSource(t)) + }) + t.Run("search", func(t *testing.T) { + testServeSearch(t, newTestingDataSource(t)) + }) + t.Run("search pagination", func(t *testing.T) { + testServeSearchPagination(t, newTestingDataSource(t)) + }) + t.Run("package symbols", func(t *testing.T) { + testServePackageSymbols(t, newTestingDataSource(t)) + }) + t.Run("package imported by", func(t *testing.T) { + testServePackageImportedBy(t, newTestingDataSource(t)) }) } @@ -475,15 +504,6 @@ } } -func TestServeModule(t *testing.T) { - t.Run("fake", func(t *testing.T) { - testServeModule(t, fakedatasource.New()) - }) - t.Run("db", func(t *testing.T) { - testServeModule(t, setupTestDB(t)) - }) -} - func testServeModule(t *testing.T, ds internal.TestingDataSource) { const ( @@ -635,15 +655,6 @@ } } -func TestServeModuleVersions(t *testing.T) { - t.Run("fake", func(t *testing.T) { - testServeModuleVersions(t, fakedatasource.New()) - }) - t.Run("db", func(t *testing.T) { - testServeModuleVersions(t, setupTestDB(t)) - }) -} - func testServeModuleVersions(t *testing.T, ds internal.TestingDataSource) { ds.MustInsertModule(t, &internal.Module{ ModuleInfo: internal.ModuleInfo{ @@ -742,15 +753,6 @@ } } -func TestServeModulePackages(t *testing.T) { - t.Run("fake", func(t *testing.T) { - testServeModulePackages(t, fakedatasource.New()) - }) - t.Run("db", func(t *testing.T) { - testServeModulePackages(t, setupTestDB(t)) - }) -} - func testServeModulePackages(t *testing.T, ds internal.TestingDataSource) { const ( modulePath = "example.com" @@ -874,15 +876,6 @@ } } -func TestServeSearch(t *testing.T) { - t.Run("fake", func(t *testing.T) { - testServeSearch(t, fakedatasource.New()) - }) - t.Run("db", func(t *testing.T) { - testServeSearch(t, setupTestDB(t)) - }) -} - func testServeSearch(t *testing.T, ds internal.TestingDataSource) { ds.MustInsertModule(t, &internal.Module{ ModuleInfo: internal.ModuleInfo{ @@ -966,15 +959,6 @@ } } -func TestServeSearchPagination(t *testing.T) { - t.Run("fake", func(t *testing.T) { - testServeSearchPagination(t, fakedatasource.New()) - }) - t.Run("db", func(t *testing.T) { - testServeSearchPagination(t, setupTestDB(t)) - }) -} - func testServeSearchPagination(t *testing.T, ds internal.TestingDataSource) { for i := range 10 { pkgPath := "example.com/pkg" + strconv.Itoa(i) @@ -1062,15 +1046,6 @@ } } -func TestServePackageSymbols(t *testing.T) { - t.Run("fake", func(t *testing.T) { - testServePackageSymbols(t, fakedatasource.New()) - }) - t.Run("db", func(t *testing.T) { - testServePackageSymbols(t, setupTestDB(t)) - }) -} - func testServePackageSymbols(t *testing.T, ds internal.TestingDataSource) { const ( @@ -1217,15 +1192,6 @@ } } -func TestServePackageImportedBy(t *testing.T) { - t.Run("fake", func(t *testing.T) { - testServePackageImportedBy(t, fakedatasource.New()) - }) - t.Run("db", func(t *testing.T) { - testServePackageImportedBy(t, setupTestDB(t)) - }) -} - func testServePackageImportedBy(t *testing.T, ds internal.TestingDataSource) { const ( @@ -1315,6 +1281,8 @@ } func TestServeVulnerabilities(t *testing.T) { + // This test doesn't need to run against a Postgres DB, because + // vulnerabilities are not read from the database. vc, err := vuln.NewInMemoryClient([]*osv.Entry{ { ID: "VULN-1", @@ -1397,6 +1365,8 @@ } func TestCacheControl(t *testing.T) { + // This test doesn't need to run against a Postgres DB, because + // it's concerned only with headers. ds := fakedatasource.New() const modulePath = "example.com" for _, v := range []string{"v1.0.0", "master"} {