github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/cat/txndata_test.go (about) 1 package cat 2 3 import ( 4 "encoding/json" 5 "testing" 6 ) 7 8 func TestTxnDataRoundTrip(t *testing.T) { 9 for _, test := range []struct { 10 input string 11 output string 12 txnData TxnDataHeader 13 }{ 14 { 15 input: `["guid",false]`, 16 output: `["guid",false,"",""]`, 17 txnData: TxnDataHeader{ 18 GUID: "guid", 19 TripID: "", 20 PathHash: "", 21 }, 22 }, 23 { 24 input: `["guid",false,"trip"]`, 25 output: `["guid",false,"trip",""]`, 26 txnData: TxnDataHeader{ 27 GUID: "guid", 28 TripID: "trip", 29 PathHash: "", 30 }, 31 }, 32 { 33 input: `["guid",false,null]`, 34 output: `["guid",false,"",""]`, 35 txnData: TxnDataHeader{ 36 GUID: "guid", 37 TripID: "", 38 PathHash: "", 39 }, 40 }, 41 { 42 input: `["guid",false,"trip",null]`, 43 output: `["guid",false,"trip",""]`, 44 txnData: TxnDataHeader{ 45 GUID: "guid", 46 TripID: "trip", 47 PathHash: "", 48 }, 49 }, 50 { 51 input: `["guid",false,"trip","hash"]`, 52 output: `["guid",false,"trip","hash"]`, 53 txnData: TxnDataHeader{ 54 GUID: "guid", 55 TripID: "trip", 56 PathHash: "hash", 57 }, 58 }, 59 } { 60 // Test unmarshalling. 61 txnData := &TxnDataHeader{} 62 if err := json.Unmarshal([]byte(test.input), txnData); err != nil { 63 t.Errorf("given %s: error expected to be nil; got %v", test.input, err) 64 } 65 66 if test.txnData.GUID != txnData.GUID { 67 t.Errorf("given %s: GUID expected to be %s; got %s", test.input, test.txnData.GUID, txnData.GUID) 68 } 69 70 if test.txnData.TripID != txnData.TripID { 71 t.Errorf("given %s: TripID expected to be %s; got %s", test.input, test.txnData.TripID, txnData.TripID) 72 } 73 74 if test.txnData.PathHash != txnData.PathHash { 75 t.Errorf("given %s: PathHash expected to be %s; got %s", test.input, test.txnData.PathHash, txnData.PathHash) 76 } 77 78 // Test marshalling. 79 data, err := json.Marshal(&test.txnData) 80 if err != nil { 81 t.Errorf("given %s: error expected to be nil; got %v", test.output, err) 82 } 83 84 if string(data) != test.output { 85 t.Errorf("given %s: unexpected JSON %s", test.output, string(data)) 86 } 87 } 88 } 89 90 func TestTxnDataUnmarshal(t *testing.T) { 91 // Test error cases where we get a generic error from the JSON package. 92 for _, input := range []string{ 93 // Basic malformed JSON test: beyond this, we're not going to unit test the 94 // Go standard library's JSON package. 95 ``, 96 } { 97 txnData := &TxnDataHeader{} 98 99 if err := json.Unmarshal([]byte(input), txnData); err == nil { 100 t.Errorf("given %s: error expected to be non-nil; got nil", input) 101 } 102 } 103 104 // Test error cases where the incorrect number of elements was provided. 105 for _, input := range []string{ 106 `[]`, 107 `[1]`, 108 } { 109 txnData := &TxnDataHeader{} 110 111 err := json.Unmarshal([]byte(input), txnData) 112 if _, ok := err.(errUnexpectedArraySize); !ok { 113 t.Errorf("given %s: error expected to be errUnexpectedArraySize; got %v", input, err) 114 } 115 } 116 117 // Test error cases where a specific variable is returned. 118 for _, tc := range []struct { 119 input string 120 err error 121 }{ 122 // Unexpected JSON types. 123 {`false`, errInvalidTxnDataJSON}, 124 {`true`, errInvalidTxnDataJSON}, 125 {`1234`, errInvalidTxnDataJSON}, 126 {`{}`, errInvalidTxnDataJSON}, 127 {`""`, errInvalidTxnDataJSON}, 128 129 // Invalid data types for each field in turn. 130 {`[false,false,"trip","hash"]`, errInvalidTxnDataGUID}, 131 {`["guid",false,0,"hash"]`, errInvalidTxnDataTripID}, 132 {`["guid",false,"trip",[]]`, errInvalidTxnDataPathHash}, 133 } { 134 txnData := &TxnDataHeader{} 135 136 if err := json.Unmarshal([]byte(tc.input), txnData); err != tc.err { 137 t.Errorf("given %s: error expected to be %v; got %v", tc.input, tc.err, err) 138 } 139 } 140 }