@@ -823,29 +823,34 @@ func TestExcludedFromSearch(t *testing.T) {
823
823
}
824
824
825
825
func TestSearchBypass (t * testing.T ) {
826
- t .Parallel ()
827
- testDB , release := acquire (t )
828
- defer release ()
829
- ctx := context .Background ()
830
- bypassDB := NewBypassingLicenseCheck (testDB .db )
831
-
832
- m := nonRedistributableModule ()
833
- MustInsertModule (ctx , t , bypassDB , m )
834
-
835
826
for _ , test := range []struct {
836
- db * DB
827
+ bypass bool
837
828
wantEmpty bool
838
829
}{
839
- {testDB , true },
840
- {bypassDB , false },
830
+ {false , true },
831
+ {true , false },
841
832
} {
842
- rs , err := test .db .Search (ctx , m .ModulePath , SearchOptions {MaxResults : 10 })
843
- if err != nil {
844
- t .Fatal (err )
845
- }
846
- if got := (rs [0 ].Synopsis == "" ); got != test .wantEmpty {
847
- t .Errorf ("bypass %t: got empty %t, want %t" , test .db == bypassDB , got , test .wantEmpty )
848
- }
833
+ t .Run (fmt .Sprintf ("bypass %t" , test .bypass ), func (t * testing.T ) {
834
+ t .Parallel ()
835
+ testDB , release := acquire (t )
836
+ defer release ()
837
+ ctx := context .Background ()
838
+
839
+ if test .bypass {
840
+ testDB = NewBypassingLicenseCheck (testDB .db )
841
+ }
842
+
843
+ m := nonRedistributableModule ()
844
+ MustInsertModule (ctx , t , testDB , m )
845
+
846
+ rs , err := testDB .Search (ctx , m .ModulePath , SearchOptions {MaxResults : 10 })
847
+ if err != nil {
848
+ t .Fatal (err )
849
+ }
850
+ if got := (rs [0 ].Synopsis == "" ); got != test .wantEmpty {
851
+ t .Errorf ("got empty %t, want %t" , got , test .wantEmpty )
852
+ }
853
+ })
849
854
}
850
855
}
851
856
@@ -1219,90 +1224,99 @@ func TestUpdateSearchDocumentsImportedByCount(t *testing.T) {
1219
1224
}
1220
1225
1221
1226
func TestGetPackagesForSearchDocumentUpsert (t * testing.T ) {
1222
- t .Parallel ()
1223
- testDB , release := acquire (t )
1224
- defer release ()
1225
- ctx := context .Background ()
1227
+ for _ , test := range []struct { bypass bool }{{false }, {true }} {
1228
+ t .Run (fmt .Sprintf ("bypass %t" , test .bypass ), func (t * testing.T ) {
1229
+ ctx := context .Background ()
1226
1230
1227
- moduleA := sample .Module ("mod.com" , "v1.2.3" ,
1228
- "A" , "A/notinternal" , "A/internal" , "A/internal/B" )
1231
+ moduleA := sample .Module ("mod.com" , "v1.2.3" ,
1232
+ "A" , "A/notinternal" , "A/internal" , "A/internal/B" )
1229
1233
1230
- // moduleA.Units[1] is mod.com/A.
1231
- moduleA .Units [1 ].Readme = & internal.Readme {
1232
- Filepath : sample .ReadmeFilePath ,
1233
- Contents : sample .ReadmeContents ,
1234
- }
1235
- // moduleA.Units[2] is mod.com/A/notinternal.
1236
- moduleA .Units [2 ].Readme = & internal.Readme {
1237
- Filepath : sample .ReadmeFilePath ,
1238
- Contents : sample .ReadmeContents ,
1239
- }
1240
- moduleN := nonRedistributableModule ()
1241
- bypassDB := NewBypassingLicenseCheck (testDB .db )
1242
- for _ , m := range []* internal.Module {moduleA , moduleN } {
1243
- MustInsertModule (ctx , t , bypassDB , m )
1244
- }
1234
+ // moduleA.Units[1] is mod.com/A.
1235
+ moduleA .Units [1 ].Readme = & internal.Readme {
1236
+ Filepath : sample .ReadmeFilePath ,
1237
+ Contents : sample .ReadmeContents ,
1238
+ }
1239
+ // moduleA.Units[2] is mod.com/A/notinternal.
1240
+ moduleA .Units [2 ].Readme = & internal.Readme {
1241
+ Filepath : sample .ReadmeFilePath ,
1242
+ Contents : sample .ReadmeContents ,
1243
+ }
1244
+ moduleN := nonRedistributableModule ()
1245
1245
1246
- // We are asking for all packages in search_documents updated before now, which is
1247
- // all the non-internal packages.
1248
- got , err := testDB .GetPackagesForSearchDocumentUpsert (ctx , time .Now (), 10 )
1249
- if err != nil {
1250
- t .Fatal (err )
1251
- }
1252
- sort .Slice (got , func (i , j int ) bool { return got [i ].PackagePath < got [j ].PackagePath })
1253
- want := []UpsertSearchDocumentArgs {
1254
- {
1255
- PackagePath : moduleN .ModulePath ,
1256
- ModulePath : moduleN .ModulePath ,
1257
- Version : "v1.2.3" ,
1258
- ReadmeFilePath : "" ,
1259
- ReadmeContents : "" ,
1260
- Synopsis : "" ,
1261
- },
1262
- {
1263
- PackagePath : "mod.com/A" ,
1264
- ModulePath : "mod.com" ,
1265
- Version : "v1.2.3" ,
1266
- ReadmeFilePath : "README.md" ,
1267
- ReadmeContents : "readme" ,
1268
- Synopsis : sample .Doc .Synopsis ,
1269
- },
1270
- {
1271
- PackagePath : "mod.com/A/notinternal" ,
1272
- ModulePath : "mod.com" ,
1273
- Version : "v1.2.3" ,
1274
- ReadmeFilePath : "README.md" ,
1275
- ReadmeContents : "readme" ,
1276
- Synopsis : sample .Doc .Synopsis ,
1277
- },
1278
- }
1279
- if diff := cmp .Diff (want , got ); diff != "" {
1280
- t .Fatalf ("testDB.GetPackagesForSearchDocumentUpsert mismatch(-want +got):\n %s" , diff )
1281
- }
1246
+ testDB , release := acquire (t )
1247
+ defer release ()
1282
1248
1283
- // Reading with license bypass should return the non-redistributable fields.
1284
- got , err = bypassDB .GetPackagesForSearchDocumentUpsert (ctx , time .Now (), 10 )
1285
- if err != nil {
1286
- t .Fatal (err )
1287
- }
1288
- if len (got ) == 0 {
1289
- t .Fatal ("len(got)==0" )
1290
- }
1291
- sort .Slice (got , func (i , j int ) bool { return got [i ].PackagePath < got [j ].PackagePath })
1292
- gm := got [0 ]
1293
- for _ , got := range []string {gm .ReadmeFilePath , gm .ReadmeContents , gm .Synopsis } {
1294
- if got == "" {
1295
- t .Errorf ("got empty field, want non-empty" )
1296
- }
1297
- }
1249
+ if test .bypass {
1250
+ testDB = NewBypassingLicenseCheck (testDB .db )
1251
+ }
1252
+ MustInsertModule (ctx , t , testDB , moduleA )
1253
+ MustInsertModule (ctx , t , testDB , moduleN )
1298
1254
1299
- // pkgPaths should be an empty slice, all packages were inserted more recently than yesterday.
1300
- got , err = testDB .GetPackagesForSearchDocumentUpsert (ctx , time .Now ().Add (- 24 * time .Hour ), 10 )
1301
- if err != nil {
1302
- t .Fatal (err )
1303
- }
1304
- if len (got ) != 0 {
1305
- t .Fatalf ("expected testDB.GetPackagesForSearchDocumentUpsert to return an empty slice; got %v" , got )
1255
+ // We are asking for all packages in search_documents updated before now, which is
1256
+ // all the non-internal packages.
1257
+ got , err := testDB .GetPackagesForSearchDocumentUpsert (ctx , time .Now (), 10 )
1258
+ if err != nil {
1259
+ t .Fatal (err )
1260
+ }
1261
+ sort .Slice (got , func (i , j int ) bool { return got [i ].PackagePath < got [j ].PackagePath })
1262
+ wantNonRedistributable := UpsertSearchDocumentArgs {
1263
+ PackagePath : moduleN .ModulePath ,
1264
+ ModulePath : moduleN .ModulePath ,
1265
+ Version : "v1.2.3" ,
1266
+ ReadmeFilePath : "" ,
1267
+ ReadmeContents : "" ,
1268
+ Synopsis : "" ,
1269
+ }
1270
+ if test .bypass {
1271
+ // If we inserted with bypass mode, these exist in the database
1272
+ // and will be returned by search.
1273
+ wantNonRedistributable .ReadmeFilePath = "README.md"
1274
+ wantNonRedistributable .ReadmeContents = "readme"
1275
+ wantNonRedistributable .Synopsis = sample .Doc .Synopsis
1276
+ }
1277
+ want := []UpsertSearchDocumentArgs {
1278
+ wantNonRedistributable ,
1279
+ {
1280
+ PackagePath : "mod.com/A" ,
1281
+ ModulePath : "mod.com" ,
1282
+ Version : "v1.2.3" ,
1283
+ ReadmeFilePath : "README.md" ,
1284
+ ReadmeContents : "readme" ,
1285
+ Synopsis : sample .Doc .Synopsis ,
1286
+ },
1287
+ {
1288
+ PackagePath : "mod.com/A/notinternal" ,
1289
+ ModulePath : "mod.com" ,
1290
+ Version : "v1.2.3" ,
1291
+ ReadmeFilePath : "README.md" ,
1292
+ ReadmeContents : "readme" ,
1293
+ Synopsis : sample .Doc .Synopsis ,
1294
+ },
1295
+ }
1296
+ if diff := cmp .Diff (want , got ); diff != "" {
1297
+ t .Fatalf ("testDB.GetPackagesForSearchDocumentUpsert mismatch(-want +got):\n %s" , diff )
1298
+ }
1299
+
1300
+ // Reading with license bypass should return the non-redistributable fields.
1301
+ if test .bypass {
1302
+ sort .Slice (got , func (i , j int ) bool { return got [i ].PackagePath < got [j ].PackagePath })
1303
+ gm := got [0 ]
1304
+ for _ , got := range []string {gm .ReadmeFilePath , gm .ReadmeContents , gm .Synopsis } {
1305
+ if got == "" {
1306
+ t .Errorf ("got empty field, want non-empty" )
1307
+ }
1308
+ }
1309
+
1310
+ // pkgPaths should be an empty slice, all packages were inserted more recently than yesterday.
1311
+ got , err = testDB .GetPackagesForSearchDocumentUpsert (ctx , time .Now ().Add (- 24 * time .Hour ), 10 )
1312
+ if err != nil {
1313
+ t .Fatal (err )
1314
+ }
1315
+ if len (got ) != 0 {
1316
+ t .Fatalf ("expected testDB.GetPackagesForSearchDocumentUpsert to return an empty slice; got %v" , got )
1317
+ }
1318
+ }
1319
+ })
1306
1320
}
1307
1321
}
1308
1322
0 commit comments