github.com/influxdata/influxdb/v2@v2.7.6/variable_test.go (about) 1 package influxdb_test 2 3 import ( 4 "encoding/json" 5 "reflect" 6 "testing" 7 8 platform "github.com/influxdata/influxdb/v2" 9 platformtesting "github.com/influxdata/influxdb/v2/testing" 10 ) 11 12 var ( 13 variableTestID = "debac1e0deadbeef" 14 variableTestOrgID = "deadbeefdeadbeef" 15 ) 16 17 func TestVariable_UnmarshalJSON(t *testing.T) { 18 tests := []struct { 19 name string 20 json string 21 want platform.Variable 22 }{ 23 { 24 name: "with organization", 25 json: ` 26 { 27 "id": "debac1e0deadbeef", 28 "orgID": "deadbeefdeadbeef", 29 "name": "howdy", 30 "selected": [], 31 "arguments": { 32 "type": "constant", 33 "values": ["a", "b", "c", "d"] 34 } 35 } 36 `, 37 want: platform.Variable{ 38 ID: platformtesting.MustIDBase16(variableTestID), 39 OrganizationID: platformtesting.MustIDBase16(variableTestOrgID), 40 Name: "howdy", 41 Selected: make([]string, 0), 42 Arguments: &platform.VariableArguments{ 43 Type: "constant", 44 Values: platform.VariableConstantValues{"a", "b", "c", "d"}, 45 }, 46 }, 47 }, 48 { 49 name: "with constant arguments", 50 json: ` 51 { 52 "id": "debac1e0deadbeef", 53 "name": "howdy", 54 "selected": [], 55 "arguments": { 56 "type": "constant", 57 "values": ["a", "b", "c"] 58 } 59 } 60 `, 61 want: platform.Variable{ 62 ID: platformtesting.MustIDBase16(variableTestID), 63 Name: "howdy", 64 Selected: make([]string, 0), 65 Arguments: &platform.VariableArguments{ 66 Type: "constant", 67 Values: platform.VariableConstantValues{"a", "b", "c"}, 68 }, 69 }, 70 }, 71 { 72 name: "with map arguments", 73 json: ` 74 { 75 "id": "debac1e0deadbeef", 76 "name": "howdy", 77 "selected": [], 78 "arguments": { 79 "type": "map", 80 "values": { 81 "a": "A", 82 "b": "B" 83 } 84 } 85 } 86 `, 87 want: platform.Variable{ 88 ID: platformtesting.MustIDBase16(variableTestID), 89 Name: "howdy", 90 Selected: make([]string, 0), 91 Arguments: &platform.VariableArguments{ 92 Type: "map", 93 Values: platform.VariableMapValues{"a": "A", "b": "B"}, 94 }, 95 }, 96 }, 97 { 98 name: "with query arguments", 99 json: ` 100 { 101 "id": "debac1e0deadbeef", 102 "name": "howdy", 103 "selected": [], 104 "arguments": { 105 "type": "query", 106 "values": { 107 "query": "howdy", 108 "language": "flux" 109 } 110 } 111 } 112 `, 113 want: platform.Variable{ 114 ID: platformtesting.MustIDBase16(variableTestID), 115 Name: "howdy", 116 Selected: make([]string, 0), 117 Arguments: &platform.VariableArguments{ 118 Type: "query", 119 Values: platform.VariableQueryValues{ 120 Query: "howdy", 121 Language: "flux", 122 }, 123 }, 124 }, 125 }, 126 } 127 128 for _, tt := range tests { 129 t.Run(tt.name, func(t *testing.T) { 130 var m platform.Variable 131 132 err := json.Unmarshal([]byte(tt.json), &m) 133 if err != nil { 134 t.Fatalf("error unmarshalling json: %v", err) 135 } 136 137 if !reflect.DeepEqual(m, tt.want) { 138 t.Errorf("%q. got = %+v, want %+v", tt.name, m, tt.want) 139 } 140 }) 141 } 142 }