@@ -21,6 +21,77 @@ import (
21
21
"golang.org/x/pkgsite/internal/testing/sample"
22
22
)
23
23
24
+ func TestPostgres_GetModuleInfo (t * testing.T ) {
25
+ ctx , cancel := context .WithTimeout (context .Background (), testTimeout )
26
+ defer cancel ()
27
+
28
+ defer ResetTestDB (testDB , t )
29
+
30
+ testCases := []struct {
31
+ name , path , version string
32
+ modules []* internal.Module
33
+ wantIndex int // index into versions
34
+ wantErr error
35
+ }{
36
+ {
37
+ name : "version present" ,
38
+ path : "mod.1" ,
39
+ version : "v1.0.2" ,
40
+ modules : []* internal.Module {
41
+ sample .Module ("mod.1" , "v1.1.0" , sample .Suffix ),
42
+ sample .Module ("mod.1" , "v1.0.2" , sample .Suffix ),
43
+ sample .Module ("mod.1" , "v1.0.0" , sample .Suffix ),
44
+ },
45
+ wantIndex : 1 ,
46
+ },
47
+ {
48
+ name : "version not present" ,
49
+ path : "mod.2" ,
50
+ version : "v1.0.3" ,
51
+ modules : []* internal.Module {
52
+ sample .Module ("mod.2" , "v1.1.0" , sample .Suffix ),
53
+ sample .Module ("mod.2" , "v1.0.2" , sample .Suffix ),
54
+ sample .Module ("mod.2" , "v1.0.0" , sample .Suffix ),
55
+ },
56
+ wantErr : derrors .NotFound ,
57
+ },
58
+ {
59
+ name : "no versions" ,
60
+ path : "mod3" ,
61
+ version : "v1.2.3" ,
62
+ wantErr : derrors .NotFound ,
63
+ },
64
+ }
65
+
66
+ for _ , tc := range testCases {
67
+ t .Run (tc .name , func (t * testing.T ) {
68
+ for _ , v := range tc .modules {
69
+ if err := testDB .InsertModule (ctx , v ); err != nil {
70
+ t .Error (err )
71
+ }
72
+ }
73
+
74
+ gotVI , err := testDB .GetModuleInfo (ctx , tc .path , tc .version )
75
+ if err != nil {
76
+ if tc .wantErr == nil {
77
+ t .Fatalf ("got unexpected error %v" , err )
78
+ }
79
+ if ! errors .Is (err , tc .wantErr ) {
80
+ t .Fatalf ("got error %v, want Is(%v)" , err , tc .wantErr )
81
+ }
82
+ return
83
+ }
84
+ if tc .wantIndex >= len (tc .modules ) {
85
+ t .Fatal ("wantIndex too large" )
86
+ }
87
+ wantVI := & tc .modules [tc .wantIndex ].ModuleInfo
88
+ if diff := cmp .Diff (wantVI , gotVI , cmpopts .EquateEmpty (), cmp .AllowUnexported (source.Info {})); diff != "" {
89
+ t .Errorf ("mismatch (-want +got):\n %s" , diff )
90
+ }
91
+ })
92
+ }
93
+ }
94
+
24
95
func TestPostgres_GetVersionInfo_Latest (t * testing.T ) {
25
96
ctx , cancel := context .WithTimeout (context .Background (), testTimeout )
26
97
defer cancel ()
0 commit comments