github.com/khulnasoft-lab/tunnel-db@v0.0.0-20231117205118-74e1113bd007/pkg/db/redhat_cpe_test.go (about) 1 package db_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 9 "github.com/khulnasoft-lab/tunnel-db/pkg/db" 10 "github.com/khulnasoft-lab/tunnel-db/pkg/dbtest" 11 ) 12 13 func TestConfig_GetRedHatCPEs(t *testing.T) { 14 tests := []struct { 15 name string 16 fixtures []string 17 repository string 18 want []int 19 wantErr string 20 }{ 21 { 22 name: "happy path", 23 fixtures: []string{"testdata/fixtures/redhat-cpe.yaml"}, 24 repository: "rhel-lb-for-rhel-6-server-eus-debug-rpms", 25 want: []int{1, 2}, 26 }, 27 { 28 name: "unknown cpe", 29 fixtures: []string{"testdata/fixtures/redhat-cpe.yaml"}, 30 repository: "unknown", 31 want: nil, 32 }, 33 { 34 name: "broken value", 35 fixtures: []string{"testdata/fixtures/redhat-cpe.yaml"}, 36 repository: "broken", 37 wantErr: "JSON unmarshal error", 38 }, 39 } 40 for _, tt := range tests { 41 t.Run(tt.name, func(t *testing.T) { 42 // Initialize DB for testing 43 dbtest.InitDB(t, tt.fixtures) 44 defer db.Close() 45 46 dbc := db.Config{} 47 got, err := dbc.RedHatRepoToCPEs(tt.repository) 48 49 if tt.wantErr != "" { 50 require.NotNil(t, err) 51 assert.Contains(t, err.Error(), tt.wantErr) 52 return 53 } 54 55 require.NoError(t, err) 56 57 // Compare 58 assert.Equal(t, tt.want, got) 59 }) 60 } 61 }