github.com/google/osv-scalibr@v0.4.1/clients/internal/pypi/pypi_test.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package pypi 16 17 import ( 18 "encoding/json" 19 "testing" 20 ) 21 22 func TestYankedUnmarshalJSON(t *testing.T) { 23 tests := []struct { 24 name string 25 json string 26 want Yanked 27 wantErr bool 28 }{ 29 { 30 name: "boolean_false", 31 json: `false`, 32 want: Yanked{Value: false}, 33 }, 34 { 35 name: "boolean_true", 36 json: `true`, 37 want: Yanked{Value: true}, 38 }, 39 { 40 name: "string_reason", 41 json: `"security issue"`, 42 want: Yanked{Value: true}, 43 }, 44 { 45 name: "invalid type", 46 json: `123`, 47 wantErr: true, 48 }, 49 } 50 for _, tt := range tests { 51 t.Run(tt.name, func(t *testing.T) { 52 var got Yanked 53 err := json.Unmarshal([]byte(tt.json), &got) 54 if (err != nil) != tt.wantErr { 55 t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) 56 } 57 if got != tt.want { 58 t.Errorf("UnmarshalJSON() got = %v, want %v", got, tt.want) 59 } 60 }) 61 } 62 }