aboutsummaryrefslogtreecommitdiff
path: root/internal/postgres/version_map_test.go
blob: 3cbc325cda47cf8f5bc1ee821e846204ba3cbab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package postgres

import (
	"context"
	"fmt"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"golang.org/x/pkgsite/internal"
	"golang.org/x/pkgsite/internal/testing/sample"
	"golang.org/x/pkgsite/internal/version"
)

func TestReadAndWriteVersionMap(t *testing.T) {
	t.Parallel()
	testDB, release := acquire(t)
	defer release()
	ctx := context.Background()

	m := sample.Module("golang.org/x/tools", sample.VersionString, "go/packages")
	MustInsertModule(ctx, t, testDB, m)

	vm := &internal.VersionMap{
		ModulePath:       m.ModulePath,
		RequestedVersion: "master",
		ResolvedVersion:  "v1.0.0",
		Status:           200,
	}
	if err := testDB.UpsertVersionMap(ctx, vm); err != nil {
		t.Fatal(err)
	}

	got, err := testDB.GetVersionMap(ctx, vm.ModulePath, vm.RequestedVersion)
	if err != nil {
		t.Fatal(err)
	}
	if diff := cmp.Diff(vm, got, cmpopts.IgnoreFields(internal.VersionMap{}, "UpdatedAt")); diff != "" {
		t.Fatalf("t.Errorf(ctx, %q, %q) mismatch (-want +got):\n%s", vm.ModulePath, vm.RequestedVersion, diff)
	}
}
func TestUpsertVersionMap(t *testing.T) {
	t.Parallel()
	testDB, release := acquire(t)
	defer release()
	ctx := context.Background()

	upsertAndVerifyVersionMap := func(vm *internal.VersionMap) {
		err := testDB.UpsertVersionMap(ctx, vm)
		if err != nil {
			t.Fatal(err)
		}
		got, err := testDB.GetVersionMap(ctx, vm.ModulePath, vm.RequestedVersion)
		if err != nil {
			t.Fatal(err)
		}
		if diff := cmp.Diff(vm, got, cmpopts.IgnoreFields(internal.VersionMap{}, "UpdatedAt")); diff != "" {
			t.Fatalf("t.Errorf(ctx, %q, %q) mismatch (-want +got):\n%s",
				vm.ModulePath, vm.RequestedVersion, diff)
		}
	}

	vm := &internal.VersionMap{
		ModulePath:       "github.com/module",
		RequestedVersion: "master",
		ResolvedVersion:  "",
		Status:           404,
		Error:            "not found",
	}
	upsertAndVerifyVersionMap(vm)

	vm.ResolvedVersion = "v1.0.0"
	vm.Status = 200
	upsertAndVerifyVersionMap(vm)
}

func TestGetVersionMapsWithNon2xxStatus(t *testing.T) {
	t.Parallel()
	testDB, release := acquire(t)
	defer release()
	ctx := context.Background()

	tests := []struct {
		path   string
		status int
	}{
		{"github.com/a/b", 200},
		{"github.com/a/c", 290},
		{"github.com/a/d", 400},
		{"github.com/a/e", 440},
		{"github.com/a/f", 490},
		{"github.com/a/g", 491},
		{"github.com/a/h", 500},
	}
	var paths []string
	want := map[string]bool{}
	for _, test := range tests {
		paths = append(paths, test.path)
		want[test.path] = true
		if err := testDB.UpsertVersionMap(ctx, &internal.VersionMap{
			ModulePath:       test.path,
			RequestedVersion: version.Latest,
			ResolvedVersion:  sample.VersionString,
			GoModPath:        test.path,
			Status:           test.status,
		}); err != nil {
			t.Fatal(err)
		}
	}
	vms, err := testDB.GetVersionMaps(ctx, paths, version.Latest)
	if err != nil {
		t.Fatal(err)
	}

	got := map[string]bool{}
	for _, vm := range vms {
		got[vm.ModulePath] = true
	}
	if fmt.Sprint(want) != fmt.Sprint(got) {
		t.Fatalf("got = \n%v\nwant =\n%v", got, want)
	}
}