aboutsummaryrefslogtreecommitdiff
path: root/internal/postgres/insert_module_test.go
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2021-01-13 11:05:02 -0500
committerJonathan Amsterdam <jba@google.com>2021-01-13 21:04:55 +0000
commit666af5b4380d053a018cfb903d6687ae10b59342 (patch)
tree6ae0f953f20bb9ff1b3e84ba40dbe8dc8f3b03fb /internal/postgres/insert_module_test.go
parent713673e316cf8990170a1a810ba49d9e15da2085 (diff)
downloadgo-x-pkgsite-666af5b4380d053a018cfb903d6687ae10b59342.tar.xz
internal/postgres: save and restore new licensecheck Coverage
Add a NewCoverage field internal/licenses.Metadata to support the upcoming change to the new licensecheck. When saving to the DB, save the NewCoverage field if it is populated; otherwise save the original Coverage field. When reading the JSON for the coverage field from the DB, distinguish old and new Coverage structs and populate one of the two fields of the licenses.Metadata value as appropriate. Change-Id: I5be5623fbaec668265e1e30a878864cf4aef2ae6 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/283652 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> Reviewed-by: Julie Qiu <julie@golang.org>
Diffstat (limited to 'internal/postgres/insert_module_test.go')
-rw-r--r--internal/postgres/insert_module_test.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/internal/postgres/insert_module_test.go b/internal/postgres/insert_module_test.go
index 3b2f429e..8921a652 100644
--- a/internal/postgres/insert_module_test.go
+++ b/internal/postgres/insert_module_test.go
@@ -18,6 +18,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
+ "github.com/google/licensecheck"
"github.com/google/safehtml"
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/database"
@@ -259,6 +260,45 @@ func TestInsertModuleErrors(t *testing.T) {
}
}
+func TestInsertModuleNewCoverage(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
+ defer cancel()
+ defer ResetTestDB(testDB, t)
+
+ m := sample.DefaultModule()
+ newCoverage := licensecheck.Coverage{
+ Percent: 100,
+ Match: []licensecheck.Match{{ID: "New", Start: 1, End: 10}},
+ }
+ m.Licenses = []*licenses.License{
+ {
+ Metadata: &licenses.Metadata{
+ Types: []string{sample.LicenseType},
+ FilePath: sample.LicenseFilePath,
+ NewCoverage: newCoverage,
+ },
+ Contents: []byte(`Lorem Ipsum`),
+ },
+ }
+ if err := testDB.InsertModule(ctx, m); err != nil {
+ t.Fatal(err)
+ }
+ u, err := testDB.GetUnit(ctx, &internal.UnitMeta{Path: m.ModulePath, ModulePath: m.ModulePath, Version: m.Version}, internal.AllFields)
+ if err != nil {
+ t.Fatal(err)
+ }
+ got := u.LicenseContents[0].Metadata
+ want := &licenses.Metadata{
+ Types: []string{"MIT"},
+ FilePath: sample.LicenseFilePath,
+ NewCoverage: newCoverage,
+ }
+ if !cmp.Equal(got, want) {
+ t.Errorf("\ngot %+v\nwant %+v", got, want)
+ }
+
+}
+
func TestPostgres_ReadAndWriteModuleOtherColumns(t *testing.T) {
// Verify that InsertModule correctly populates the columns in the versions
// table that are not in the ModuleInfo struct.