github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/supplyChain_test.go (about) 1 package openrtb_ext 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/prebid/openrtb/v20/openrtb2" 8 "github.com/prebid/prebid-server/v2/util/ptrutil" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestCloneSupplyChain(t *testing.T) { 13 testCases := []struct { 14 name string 15 schain *openrtb2.SupplyChain 16 schainCopy *openrtb2.SupplyChain // manual copy of above prebid object to verify against 17 mutator func(t *testing.T, schain *openrtb2.SupplyChain) // function to modify the prebid object 18 }{ 19 { 20 name: "Nil", // Verify the nil case 21 schain: nil, 22 schainCopy: nil, 23 mutator: func(t *testing.T, schain *openrtb2.SupplyChain) {}, 24 }, 25 { 26 name: "General", 27 schain: &openrtb2.SupplyChain{ 28 Complete: 2, 29 Nodes: []openrtb2.SupplyChainNode{ 30 { 31 SID: "alpha", 32 Name: "Johnny", 33 HP: ptrutil.ToPtr[int8](5), 34 Ext: json.RawMessage(`{}`), 35 }, 36 { 37 ASI: "Oh my", 38 Name: "Johnny", 39 HP: ptrutil.ToPtr[int8](5), 40 Ext: json.RawMessage(`{"samson"}`), 41 }, 42 }, 43 Ver: "v2.5", 44 Ext: json.RawMessage(`{"foo": "bar"}`), 45 }, 46 schainCopy: &openrtb2.SupplyChain{ 47 Complete: 2, 48 Nodes: []openrtb2.SupplyChainNode{ 49 { 50 SID: "alpha", 51 Name: "Johnny", 52 HP: ptrutil.ToPtr[int8](5), 53 Ext: json.RawMessage(`{}`), 54 }, 55 { 56 ASI: "Oh my", 57 Name: "Johnny", 58 HP: ptrutil.ToPtr[int8](5), 59 Ext: json.RawMessage(`{"samson"}`), 60 }, 61 }, 62 Ver: "v2.5", 63 Ext: json.RawMessage(`{"foo": "bar"}`), 64 }, 65 mutator: func(t *testing.T, schain *openrtb2.SupplyChain) { 66 schain.Nodes[0].SID = "beta" 67 schain.Nodes[1].HP = nil 68 schain.Nodes[0].Ext = nil 69 schain.Nodes = append(schain.Nodes, openrtb2.SupplyChainNode{SID: "Gamma"}) 70 schain.Complete = 0 71 schain.Ext = json.RawMessage(`{}`) 72 }, 73 }, 74 } 75 76 for _, test := range testCases { 77 t.Run(test.name, func(t *testing.T) { 78 clone := cloneSupplyChain(test.schain) 79 test.mutator(t, test.schain) 80 assert.Equal(t, test.schainCopy, clone) 81 }) 82 } 83 }