internal/postgres: use MustInsertModuleLatest

For most internal/postgres tests, update latest-version information
when we insert a module.

That is more like what actually happens on the worker, and some
features now rely on it, so we should do it everywhere.

Some tests still use a custom go.mod file, so we can't switch to
MustInsertModuleLatest for those, because it uses a minimal go.mod
file.

Change-Id: Ie5030aee4ed9c837931fddb8757e37bf6373a8c7
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/307871
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
diff --git a/internal/postgres/delete_test.go b/internal/postgres/delete_test.go
index f0e5fc7..71b14d6 100644
--- a/internal/postgres/delete_test.go
+++ b/internal/postgres/delete_test.go
@@ -26,7 +26,7 @@
 		"v0.0.0-20190904010203-89fb59e2e920",
 	}
 	for _, v := range versions {
-		MustInsertModule(ctx, t, testDB, sample.Module(sample.ModulePath, v, ""))
+		MustInsertModuleLatest(ctx, t, testDB, sample.Module(sample.ModulePath, v, ""))
 	}
 	if err := testDB.DeletePseudoversionsExcept(ctx, sample.ModulePath, pseudo1); err != nil {
 		t.Fatal(err)
diff --git a/internal/postgres/details_test.go b/internal/postgres/details_test.go
index 3ba376c..7075284 100644
--- a/internal/postgres/details_test.go
+++ b/internal/postgres/details_test.go
@@ -73,7 +73,7 @@
 			defer release()
 
 			for _, v := range test.modules {
-				MustInsertModule(ctx, t, testDB, v)
+				MustInsertModuleLatest(ctx, t, testDB, v)
 			}
 
 			gotModules, err := testDB.GetNestedModules(ctx, test.path)
@@ -120,7 +120,7 @@
 		},
 	}
 	for _, m := range test.modules {
-		MustInsertModule(ctx, t, testDB, m)
+		MustInsertModuleLatest(ctx, t, testDB, m)
 	}
 	if err := testDB.InsertExcludedPrefix(ctx, "cloud.google.com/go/storage", "postgres", "test"); err != nil {
 		t.Fatal(err)
@@ -185,7 +185,7 @@
 	for _, test := range testCases {
 		t.Run(test.name, func(t *testing.T) {
 			for _, v := range test.modules {
-				MustInsertModule(ctx, t, testDB, v)
+				MustInsertModuleLatest(ctx, t, testDB, v)
 			}
 
 			gotVI, err := testDB.GetModuleInfo(ctx, test.path, test.version)
@@ -270,7 +270,7 @@
 			defer release()
 
 			for _, v := range testModules {
-				MustInsertModule(ctx, t, testDB, v)
+				MustInsertModuleLatest(ctx, t, testDB, v)
 			}
 
 			gotImportedBy, err := testDB.GetImportedBy(ctx, test.path, test.modulePath, 100)
diff --git a/internal/postgres/insert_module_test.go b/internal/postgres/insert_module_test.go
index a5ec926..66ec5c1 100644
--- a/internal/postgres/insert_module_test.go
+++ b/internal/postgres/insert_module_test.go
@@ -80,9 +80,9 @@
 			testDB, release := acquire(t)
 			defer release()
 
-			MustInsertModule(ctx, t, testDB, test.module)
+			MustInsertModuleLatest(ctx, t, testDB, test.module)
 			// Test that insertion of duplicate primary key won't fail.
-			MustInsertModule(ctx, t, testDB, test.module)
+			MustInsertModuleLatest(ctx, t, testDB, test.module)
 			checkModule(ctx, t, testDB, test.module)
 		})
 	}
@@ -158,7 +158,7 @@
 			mod.IsRedistributable = false
 			mod.Units[0].IsRedistributable = false
 
-			MustInsertModule(ctx, t, db, mod)
+			MustInsertModuleLatest(ctx, t, db, mod)
 
 			// New model
 			u, err := db.GetUnit(ctx, newUnitMeta(mod.ModulePath, mod.ModulePath, mod.Version), internal.AllFields)
@@ -190,7 +190,7 @@
 	m := sample.Module("upsert.org", "v1.2.3", "dir/p")
 
 	// Insert the module.
-	MustInsertModule(ctx, t, testDB, m)
+	MustInsertModuleLatest(ctx, t, testDB, m)
 	// Change the module, and re-insert.
 	m.IsRedistributable = !m.IsRedistributable
 	lic := *m.Licenses[0]
@@ -198,7 +198,7 @@
 	sample.ReplaceLicense(m, &lic)
 	m.Units[0].Readme.Contents += " and more"
 
-	MustInsertModule(ctx, t, testDB, m)
+	MustInsertModuleLatest(ctx, t, testDB, m)
 	// The changes should have been saved.
 	checkModule(ctx, t, testDB, m)
 }
@@ -303,7 +303,7 @@
 			Contents: []byte(`Lorem Ipsum`),
 		},
 	}
-	MustInsertModule(ctx, t, testDB, m)
+	MustInsertModuleLatest(ctx, t, testDB, m)
 	u, err := testDB.GetUnit(ctx, newUnitMeta(m.ModulePath, m.ModulePath, m.Version), internal.AllFields)
 	if err != nil {
 		t.Fatal(err)
@@ -338,7 +338,7 @@
 		seriesPath:  "github.com/user/repo/path",
 	}
 
-	MustInsertModule(ctx, t, testDB, v)
+	MustInsertModuleLatest(ctx, t, testDB, v)
 	query := `
 	SELECT
 		sort_version, series_path
@@ -450,7 +450,7 @@
 
 	v := sample.DefaultModule()
 
-	MustInsertModule(ctx, t, testDB, v)
+	MustInsertModuleLatest(ctx, t, testDB, v)
 	if _, err := testDB.GetModuleInfo(ctx, v.ModulePath, v.Version); err != nil {
 		t.Fatal(err)
 	}
@@ -513,7 +513,7 @@
 		t.Fatal(err)
 	}
 	m := sample.Module(mvs.ModulePath, okVersion, "p")
-	MustInsertModule(ctx, t, testDB, m)
+	MustInsertModuleLatest(ctx, t, testDB, m)
 	if _, _, found := GetFromSearchDocuments(ctx, t, testDB, m.Packages()[0].Path); found {
 		t.Fatal("found package after inserting")
 	}
diff --git a/internal/postgres/licenses_test.go b/internal/postgres/licenses_test.go
index 9ebcd86..380eabf 100644
--- a/internal/postgres/licenses_test.go
+++ b/internal/postgres/licenses_test.go
@@ -44,8 +44,8 @@
 
 	ctx, cancel := context.WithTimeout(context.Background(), testTimeout*5)
 	defer cancel()
-	MustInsertModule(ctx, t, testDB, testModule)
-	MustInsertModule(ctx, t, testDB, stdlibModule)
+	MustInsertModuleLatest(ctx, t, testDB, testModule)
+	MustInsertModuleLatest(ctx, t, testDB, stdlibModule)
 	for _, test := range []struct {
 		err                                 error
 		name, fullPath, modulePath, version string
@@ -142,7 +142,7 @@
 		})
 	}
 
-	MustInsertModule(ctx, t, testDB, testModule)
+	MustInsertModuleLatest(ctx, t, testDB, testModule)
 
 	var moduleID int
 	query := `
@@ -176,7 +176,7 @@
 
 	// Insert with non-redistributable license contents.
 	m := nonRedistributableModule()
-	MustInsertModule(ctx, t, bypassDB, m)
+	MustInsertModuleLatest(ctx, t, bypassDB, m)
 
 	// check reads and the second license in the module and compares it with want.
 	check := func(bypass bool, want *licenses.License) {
diff --git a/internal/postgres/path_test.go b/internal/postgres/path_test.go
index 61f0cfd..7241887 100644
--- a/internal/postgres/path_test.go
+++ b/internal/postgres/path_test.go
@@ -72,7 +72,7 @@
 			for _, mv := range test.modvers {
 				mod, ver, _ := parseModuleVersionPackage(mv)
 				m := sample.Module(mod, ver, suffix)
-				MustInsertModule(ctx, t, testDB, m)
+				MustInsertModuleLatest(ctx, t, testDB, m)
 			}
 			t.Run("module", func(t *testing.T) {
 				check(test.v1ModulePath, test.wantModulePath)
diff --git a/internal/postgres/search_test.go b/internal/postgres/search_test.go
index f627c81..8d63567 100644
--- a/internal/postgres/search_test.go
+++ b/internal/postgres/search_test.go
@@ -297,7 +297,7 @@
 			testDB, release := acquire(t)
 			defer release()
 			for _, m := range test.modules {
-				MustInsertModule(ctx, t, testDB, m)
+				MustInsertModuleLatest(ctx, t, testDB, m)
 			}
 			if _, err := testDB.UpdateSearchDocumentsImportedByCount(ctx); err != nil {
 				t.Fatal(err)
@@ -385,7 +385,7 @@
 			defer release()
 			modules := importGraph("foo.com/A", "", 0)
 			for _, v := range modules {
-				MustInsertModule(ctx, t, testDB, v)
+				MustInsertModuleLatest(ctx, t, testDB, v)
 			}
 			if _, err := testDB.UpdateSearchDocumentsImportedByCount(ctx); err != nil {
 				t.Fatal(err)
@@ -550,7 +550,7 @@
 					pkg.Licenses = sample.LicenseMetadata()
 					m := sample.Module(modulePath, sample.VersionString)
 					sample.AddUnit(m, pkg)
-					MustInsertModule(ctx, t, testDB, m)
+					MustInsertModuleLatest(ctx, t, testDB, m)
 				}
 
 				if test.limit < 1 {
@@ -606,7 +606,7 @@
 		v.Packages()[0].IsRedistributable = m.redist
 		v.IsRedistributable = m.redist
 		v.HasGoMod = m.hasGoMod
-		MustInsertModule(ctx, t, testDB, v)
+		MustInsertModuleLatest(ctx, t, testDB, v)
 	}
 
 	for method, searcher := range searchers {
@@ -640,7 +640,7 @@
 	// Insert a module with two packages.
 	const domain = "exclude.com"
 	sm := sample.Module(domain, "v1.2.3", "pkg", "ex/clude")
-	MustInsertModule(ctx, t, testDB, sm)
+	MustInsertModuleLatest(ctx, t, testDB, sm)
 	// Exclude a prefix that matches one of the packages.
 	if err := testDB.InsertExcludedPrefix(ctx, domain+"/ex", "no user", "no reason"); err != nil {
 		t.Fatal(err)
@@ -669,7 +669,7 @@
 	bypassDB := NewBypassingLicenseCheck(testDB.db)
 
 	m := nonRedistributableModule()
-	MustInsertModule(ctx, t, bypassDB, m)
+	MustInsertModuleLatest(ctx, t, bypassDB, m)
 
 	for _, test := range []struct {
 		db        *DB
@@ -703,7 +703,7 @@
 			FilePath: "pkg/LICENSE.md",
 		},
 	})
-	MustInsertModule(ctx, t, testDB, m)
+	MustInsertModuleLatest(ctx, t, testDB, m)
 	got, err := testDB.Search(ctx, m.ModulePath, 10, 0, 100)
 	if err != nil {
 		t.Fatal(err)
@@ -791,9 +791,7 @@
 		m := sample.Module(sample.ModulePath, version, "A")
 		m.HasGoMod = gomod
 		m.Packages()[0].Documentation[0].Synopsis = "syn-" + version
-		lmv := addLatest(ctx, t, testDB, m.ModulePath, m.Version, "")
-		MustInsertModule(ctx, t, testDB, m)
-		MustInsertModuleLMV(ctx, t, testDB, m, lmv)
+		MustInsertModuleLatest(ctx, t, testDB, m)
 	}
 
 	const v1 = "v1.0.0"
@@ -846,7 +844,7 @@
 	for _, hasGoMod := range []bool{true, false} {
 		m := sample.Module(fmt.Sprintf("foo.com/%t", hasGoMod), "v1.2.3", "bar")
 		m.HasGoMod = hasGoMod
-		MustInsertModule(ctx, t, testDB, m)
+		MustInsertModuleLatest(ctx, t, testDB, m)
 	}
 
 	for _, hasGoMod := range []bool{true, false} {
@@ -877,7 +875,7 @@
 		for _, imp := range imports {
 			pkg.Imports = append(pkg.Imports, fmt.Sprintf("mod.com/%s/%[1]s", imp))
 		}
-		MustInsertModule(ctx, t, db, m)
+		MustInsertModuleLatest(ctx, t, db, m)
 		return m
 	}
 
@@ -984,7 +982,7 @@
 		// It should not get inserted into search_documents.
 		mAlt := sample.Module(alternativeModulePath, "v1.0.0", "A")
 		mAlt.Packages()[0].Imports = []string{"B"}
-		MustInsertModule(ctx, t, testDB, mAlt)
+		MustInsertModuleLatest(ctx, t, testDB, mAlt)
 		// Although B is imported by two packages, only one is in search_documents, so its
 		// imported-by count is 1.
 		updateImportedByCount(testDB)
@@ -1015,7 +1013,7 @@
 	moduleN := nonRedistributableModule()
 	bypassDB := NewBypassingLicenseCheck(testDB.db)
 	for _, m := range []*internal.Module{moduleA, moduleN} {
-		MustInsertModule(ctx, t, bypassDB, m)
+		MustInsertModuleLatest(ctx, t, bypassDB, m)
 	}
 
 	// We are asking for all packages in search_documents updated before now, which is
@@ -1154,7 +1152,7 @@
 	}
 	insert := func(m module) {
 		sm := sample.Module(m.path, m.version, m.pkg)
-		MustInsertModule(ctx, t, testDB, sm)
+		MustInsertModuleLatest(ctx, t, testDB, sm)
 	}
 
 	check := func(m module) {
diff --git a/internal/postgres/stdlib_test.go b/internal/postgres/stdlib_test.go
index ce5f219..32f41ec 100644
--- a/internal/postgres/stdlib_test.go
+++ b/internal/postgres/stdlib_test.go
@@ -46,7 +46,7 @@
 		for _, p := range m.Packages() {
 			p.Imports = nil
 		}
-		MustInsertModule(ctx, t, testDB, m)
+		MustInsertModuleLatest(ctx, t, testDB, m)
 	}
 
 	got, err := testDB.GetStdlibPathsWithSuffix(ctx, "json")
diff --git a/internal/postgres/symbol_test.go b/internal/postgres/symbol_test.go
index 0ea7b79..82634bf 100644
--- a/internal/postgres/symbol_test.go
+++ b/internal/postgres/symbol_test.go
@@ -38,7 +38,7 @@
 		sample.Type,
 	}
 	mod.Packages()[0].Documentation[0].API = api
-	MustInsertModule(ctx, t, testDB, mod)
+	MustInsertModuleLatest(ctx, t, testDB, mod)
 
 	got, err := collectStrings(ctx, testDB.db, `SELECT name FROM symbol_names;`)
 	if err != nil {
@@ -86,7 +86,7 @@
 		sample.Type,
 	}
 	mod.Packages()[0].Documentation[0].API = api
-	MustInsertModule(ctx, t, testDB, mod)
+	MustInsertModuleLatest(ctx, t, testDB, mod)
 
 	compareUnitSymbols(ctx, t, testDB, mod.Packages()[0].Path, mod.ModulePath, mod.Version,
 		map[internal.BuildContext][]*internal.Symbol{internal.BuildContextAll: api})
@@ -136,9 +136,9 @@
 	mod12 := moduleWithSymbols(t, "v1.2.0", []*internal.Symbol{&typB})
 
 	// Insert most recent, then oldest, then middle version.
-	MustInsertModule(ctx, t, testDB, mod12)
-	MustInsertModule(ctx, t, testDB, mod10)
-	MustInsertModule(ctx, t, testDB, mod11)
+	MustInsertModuleLatest(ctx, t, testDB, mod12)
+	MustInsertModuleLatest(ctx, t, testDB, mod10)
+	MustInsertModuleLatest(ctx, t, testDB, mod11)
 
 	createwant := func(docs []*internal.Documentation) map[internal.BuildContext][]*internal.Symbol {
 		want := map[internal.BuildContext][]*internal.Symbol{}
@@ -254,9 +254,9 @@
 	mod12.Packages()[0].Documentation = docs2
 
 	// Insert most recent, then oldest, then middle version.
-	MustInsertModule(ctx, t, testDB, mod12)
-	MustInsertModule(ctx, t, testDB, mod10)
-	MustInsertModule(ctx, t, testDB, mod11)
+	MustInsertModuleLatest(ctx, t, testDB, mod12)
+	MustInsertModuleLatest(ctx, t, testDB, mod10)
+	MustInsertModuleLatest(ctx, t, testDB, mod11)
 
 	createwant := func(docs []*internal.Documentation) map[internal.BuildContext][]*internal.Symbol {
 		want := map[internal.BuildContext][]*internal.Symbol{}
diff --git a/internal/postgres/test_helper.go b/internal/postgres/test_helper.go
index a47b788..a10dcbd 100644
--- a/internal/postgres/test_helper.go
+++ b/internal/postgres/test_helper.go
@@ -303,7 +303,7 @@
 		},
 	} {
 		m := sample.Module(data.modulePath, data.version, data.suffixes...)
-		MustInsertModule(ctx, t, testDB, m)
+		MustInsertModuleLatest(ctx, t, testDB, m)
 	}
 
 }
diff --git a/internal/postgres/unit_test.go b/internal/postgres/unit_test.go
index df03128..0e0e437 100644
--- a/internal/postgres/unit_test.go
+++ b/internal/postgres/unit_test.go
@@ -332,7 +332,7 @@
 		m := sample.Module(testModule.module, testModule.version, testModule.packageSuffix)
 		makeModuleNonRedistributable(m)
 
-		MustInsertModule(ctx, t, bypassDB, m)
+		MustInsertModuleLatest(ctx, t, bypassDB, m)
 		requested := m.Version
 		if testModule.isMaster {
 			requested = "master"
@@ -711,7 +711,7 @@
 		Filepath: "PKG_README.md",
 		Contents: "pkg readme",
 	}
-	MustInsertModule(ctx, t, testDB, m)
+	MustInsertModuleLatest(ctx, t, testDB, m)
 
 	// Add a module that has documentation for two Go build contexts.
 	m = sample.Module("a.com/twodoc", "v1.2.3", "p")
@@ -721,7 +721,7 @@
 		sample.Documentation("windows", "amd64", `package p; var W int`),
 	}
 	pkg.Documentation = docs2
-	MustInsertModule(ctx, t, testDB, m)
+	MustInsertModuleLatest(ctx, t, testDB, m)
 
 	for _, test := range []struct {
 		name, path, modulePath, version string
@@ -893,7 +893,7 @@
 	m := sample.DefaultModule()
 	m.IsRedistributable = false
 	m.Packages()[0].IsRedistributable = false
-	MustInsertModule(ctx, t, testDB, m)
+	MustInsertModuleLatest(ctx, t, testDB, m)
 }
 
 func TestGetUnitFieldSet(t *testing.T) {
@@ -910,7 +910,7 @@
 	// Add a module that has READMEs in a directory and a package.
 	m := sample.Module("a.com/m", "v1.2.3", "dir/p")
 	m.Packages()[0].Readme = readme
-	MustInsertModule(ctx, t, testDB, m)
+	MustInsertModuleLatest(ctx, t, testDB, m)
 
 	cleanFields := func(u *internal.Unit, fields internal.FieldSet) {
 		// Add/remove fields based on the FieldSet specified.
@@ -1034,7 +1034,7 @@
 	bypassDB := NewBypassingLicenseCheck(testDB.db)
 
 	m := nonRedistributableModule()
-	MustInsertModule(ctx, t, bypassDB, m)
+	MustInsertModuleLatest(ctx, t, bypassDB, m)
 
 	for _, test := range []struct {
 		db        *DB
diff --git a/internal/postgres/version_map_test.go b/internal/postgres/version_map_test.go
index 1532e72..4b8e082 100644
--- a/internal/postgres/version_map_test.go
+++ b/internal/postgres/version_map_test.go
@@ -23,7 +23,7 @@
 	defer cancel()
 
 	m := sample.Module("golang.org/x/tools", sample.VersionString, "go/packages")
-	MustInsertModule(ctx, t, testDB, m)
+	MustInsertModuleLatest(ctx, t, testDB, m)
 
 	vm := &internal.VersionMap{
 		ModulePath:       m.ModulePath,
diff --git a/internal/postgres/version_test.go b/internal/postgres/version_test.go
index 33ffe5d..325678a 100644
--- a/internal/postgres/version_test.go
+++ b/internal/postgres/version_test.go
@@ -434,7 +434,7 @@
 		t.Fatal(err)
 	}
 	for _, v := range []string{"v2.0.0+incompatible", "v1.4.0-pre", "v1.3.0", "v1.2.0", "v1.1.0"} {
-		MustInsertModule(ctx, t, testDB, sample.Module(modulePath, v, "pkg"))
+		MustInsertModuleLatest(ctx, t, testDB, sample.Module(modulePath, v, "pkg"))
 	}
 
 	for _, test := range []struct {
diff --git a/internal/postgres/versionstate_test.go b/internal/postgres/versionstate_test.go
index 103a6aa..b04b423 100644
--- a/internal/postgres/versionstate_test.go
+++ b/internal/postgres/versionstate_test.go
@@ -223,7 +223,7 @@
 			defer release()
 
 			if test.insertModuleBeforeMVS && test.shouldInsertModule {
-				MustInsertModule(ctx, t, testDB, m)
+				MustInsertModuleLatest(ctx, t, testDB, m)
 			}
 
 			mvsu := &ModuleVersionStateForUpsert{
@@ -250,7 +250,7 @@
 			}
 
 			if !test.insertModuleBeforeMVS && test.shouldInsertModule {
-				MustInsertModule(ctx, t, testDB, m)
+				MustInsertModuleLatest(ctx, t, testDB, m)
 			}
 
 			if !test.shouldInsertModule {
@@ -349,7 +349,7 @@
 			if test.modulesHasRow {
 				m := sample.Module(modulePath, version, "")
 				m.HasGoMod = test.modulesValue
-				MustInsertModule(ctx, t, testDB, m)
+				MustInsertModuleLatest(ctx, t, testDB, m)
 			}
 
 			got, err := testDB.HasGoMod(ctx, modulePath, version)