github.com/influxdata/influxdb/v2@v2.7.6/dashboard_test.go (about) 1 package influxdb_test 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 platform "github.com/influxdata/influxdb/v2" 9 platformtesting "github.com/influxdata/influxdb/v2/testing" 10 ) 11 12 func TestView_MarshalJSON(t *testing.T) { 13 type args struct { 14 view platform.View 15 } 16 type wants struct { 17 json string 18 } 19 tests := []struct { 20 name string 21 args args 22 wants wants 23 }{ 24 { 25 name: "xy", 26 args: args{ 27 view: platform.View{ 28 ViewContents: platform.ViewContents{ 29 ID: platformtesting.MustIDBase16("f01dab1ef005ba11"), 30 Name: "XY widget", 31 }, 32 Properties: platform.XYViewProperties{ 33 Type: "xy", 34 }, 35 }, 36 }, 37 wants: wants{ 38 json: ` 39 { 40 "id": "f01dab1ef005ba11", 41 "name": "XY widget", 42 "properties": { 43 "shape": "chronograf-v2", 44 "queries": null, 45 "axes": null, 46 "type": "xy", 47 "staticLegend": {}, 48 "geom": "", 49 "colors": null, 50 "note": "", 51 "showNoteWhenEmpty": false, 52 "xColumn": "", 53 "generateXAxisTicks": null, 54 "xTotalTicks": 0, 55 "xTickStart": 0, 56 "xTickStep": 0, 57 "yColumn": "", 58 "generateYAxisTicks": null, 59 "yTotalTicks": 0, 60 "yTickStart": 0, 61 "yTickStep": 0, 62 "shadeBelow": false, 63 "position": "", 64 "timeFormat": "", 65 "hoverDimension": "", 66 "legendColorizeRows": false, 67 "legendHide": false, 68 "legendOpacity": 0, 69 "legendOrientationThreshold": 0 70 } 71 }`, 72 }, 73 }, 74 { 75 name: "geo", 76 args: args{ 77 view: platform.View{ 78 ViewContents: platform.ViewContents{ 79 ID: platformtesting.MustIDBase16("e21da111ef005b11"), 80 Name: "Circle Map", 81 }, 82 Properties: platform.GeoViewProperties{ 83 Type: platform.ViewPropertyTypeGeo, 84 Zoom: 2, 85 Center: platform.Datum{Lat: 50.4, Lon: 10.1}, 86 AllowPanAndZoom: true, 87 GeoLayers: []platform.GeoLayer{ 88 { 89 Type: "circleMap", 90 RadiusField: "radius", 91 ColorField: "color", 92 Radius: 12, 93 Blur: 20, 94 RadiusDimension: platform.Axis{ 95 Label: "Frequency", 96 Bounds: []string{"10", "20"}, 97 Suffix: "m", 98 }, 99 ColorDimension: platform.Axis{ 100 Label: "Severity", 101 Bounds: []string{"10.0", "40"}, 102 Suffix: "%", 103 }, 104 ViewColors: []platform.ViewColor{{ 105 Type: "min", 106 Hex: "#FF0000", 107 }, { 108 Hex: "#000000", 109 Value: 10, 110 }, { 111 Hex: "#FFFFFF", 112 Value: 40, 113 }}, 114 IntensityDimension: platform.Axis{ 115 Label: "Impact", 116 Prefix: "$", 117 }, 118 InterpolateColors: true, 119 TrackWidth: 2, 120 Speed: 1.0, 121 IsClustered: true, 122 }, 123 }, 124 Note: "Some more information", 125 }, 126 }, 127 }, 128 wants: wants{ 129 json: ` 130 { 131 "id": "e21da111ef005b11", 132 "name": "Circle Map", 133 "properties": { 134 "shape": "chronograf-v2", 135 "type": "geo", 136 "queries": null, 137 "center": { 138 "lat": 50.4, 139 "lon": 10.1 140 }, 141 "zoom": 2, 142 "mapStyle": "", 143 "allowPanAndZoom": true, 144 "detectCoordinateFields": false, 145 "colors": null, 146 "layers": [ 147 { 148 "type": "circleMap", 149 "radiusField": "radius", 150 "colorField": "color", 151 "intensityField": "", 152 "colors": [ 153 { 154 "id": "", 155 "type": "min", 156 "hex": "#FF0000", 157 "name": "", 158 "value": 0 159 }, 160 { 161 "id": "", 162 "type": "", 163 "hex": "#000000", 164 "name": "", 165 "value": 10 166 }, 167 { 168 "id": "", 169 "type": "", 170 "hex": "#FFFFFF", 171 "name": "", 172 "value": 40 173 } 174 ], 175 "radius": 12, 176 "blur": 20, 177 "radiusDimension": { 178 "bounds": [ 179 "10", 180 "20" 181 ], 182 "label": "Frequency", 183 "prefix": "", 184 "suffix": "m", 185 "base": "", 186 "scale": "" 187 }, 188 "colorDimension": { 189 "bounds": [ 190 "10.0", 191 "40" 192 ], 193 "label": "Severity", 194 "prefix": "", 195 "suffix": "%", 196 "base": "", 197 "scale": "" 198 }, 199 "intensityDimension": { 200 "bounds": null, 201 "label": "Impact", 202 "prefix": "$", 203 "suffix": "", 204 "base": "", 205 "scale": "" 206 }, 207 "interpolateColors": true, 208 "trackWidth": 2, 209 "speed": 1, 210 "randomColors": false, 211 "isClustered": true 212 } 213 ], 214 "note": "Some more information", 215 "showNoteWhenEmpty": false 216 } 217 }`, 218 }, 219 }, 220 } 221 222 for _, tt := range tests { 223 t.Run(tt.name, func(t *testing.T) { 224 b, err := json.MarshalIndent(tt.args.view, "", " ") 225 if err != nil { 226 t.Fatalf("error marshalling json") 227 } 228 229 eq, err := jsonEqual(string(b), tt.wants.json) 230 if err != nil { 231 t.Fatalf("error marshalling json %v", err) 232 } 233 if !eq { 234 t.Errorf("JSON did not match\nexpected:%s\ngot:\n%s\n", tt.wants.json, string(b)) 235 } 236 }) 237 } 238 } 239 240 func jsonEqual(s1, s2 string) (eq bool, err error) { 241 var o1, o2 interface{} 242 243 if err = json.Unmarshal([]byte(s1), &o1); err != nil { 244 return 245 } 246 if err = json.Unmarshal([]byte(s2), &o2); err != nil { 247 return 248 } 249 return cmp.Equal(o1, o2), nil 250 }