github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/graph/formats/sigmajs/sigmajs_test.go (about) 1 // Copyright ©2018 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package sigmajs 6 7 import ( 8 "encoding/json" 9 "io/ioutil" 10 "path/filepath" 11 "reflect" 12 "testing" 13 ) 14 15 var sigmajsExampleTests = []struct { 16 path string 17 wantNodes int 18 wantEdges int 19 wantGraph *Graph 20 wantAttributes map[string]bool 21 }{ 22 { 23 path: "geolocalized.json", 24 wantNodes: 17, 25 wantEdges: 35, 26 wantGraph: &Graph{ 27 Nodes: []Node{ 28 { 29 ID: "n1", 30 Attributes: map[string]interface{}{ 31 "label": "n1", 32 "longitude": 2.48, 33 "latitude": 50.93, 34 "size": "5.5", 35 "color": "rgb(1,179,255)", 36 }, 37 }, 38 { 39 ID: "n2", 40 Attributes: map[string]interface{}{ 41 "label": "n2", 42 "latitude": 50.88, 43 "longitude": 2.0, 44 "size": "5.0", 45 "color": "rgb(1,179,255)", 46 }, 47 }, 48 { 49 ID: "n4", 50 Attributes: map[string]interface{}{ 51 "label": "n4", 52 "latitude": 49.4, 53 "longitude": 0.19, 54 "size": "6.0", 55 "color": "rgb(1,179,255)", 56 }, 57 }, 58 { 59 ID: "n5", 60 Attributes: map[string]interface{}{ 61 "label": "n5", 62 "latitude": 48.49, 63 "longitude": -1.92, 64 "size": "6.0", 65 "color": "rgb(1,179,255)", 66 }, 67 }, 68 { 69 ID: "n6", 70 Attributes: map[string]interface{}{ 71 "label": "n6", 72 "latitude": 48.26, 73 "longitude": -4.38, 74 "size": "4.5", 75 "color": "rgb(1,179,255)", 76 }, 77 }, 78 { 79 ID: "n7", 80 Attributes: map[string]interface{}{ 81 "label": "n7", 82 "latitude": 47.15, 83 "longitude": -2.09, 84 "size": "6.5", 85 "color": "rgb(1,179,255)", 86 }, 87 }, 88 { 89 ID: "n8", 90 Attributes: map[string]interface{}{ 91 "label": "n8", 92 "latitude": 46.02, 93 "longitude": -1.04, 94 "size": "6.5", 95 "color": "rgb(1,179,255)", 96 }, 97 }, 98 { 99 ID: "n9", 100 Attributes: map[string]interface{}{ 101 "label": "n9", 102 "latitude": 43.22, 103 "longitude": -1.85, 104 "size": "5.0", 105 "color": "rgb(1,179,255)", 106 }, 107 }, 108 { 109 ID: "n10", 110 Attributes: map[string]interface{}{ 111 "label": "n10", 112 "latitude": 42.38, 113 "longitude": 3.18, 114 "color": "rgb(1,179,255)", 115 "size": "4.0", 116 }, 117 }, 118 { 119 ID: "n11", 120 Attributes: map[string]interface{}{ 121 "label": "n11", 122 "latitude": 43.47, 123 "longitude": 4.04, 124 "size": "5.5", 125 "color": "rgb(1,179,255)", 126 }, 127 }, 128 { 129 ID: "n12", 130 Attributes: map[string]interface{}{ 131 "label": "n12", 132 "latitude": 42.9, 133 "longitude": 6.59, 134 "size": "5.0", 135 "color": "rgb(1,179,255)", 136 }, 137 }, 138 { 139 ID: "n13", 140 Attributes: map[string]interface{}{ 141 "label": "n13", 142 "latitude": 43.62, 143 "longitude": 7.66, 144 "size": "6.0", 145 "color": "rgb(1,179,255)", 146 }, 147 }, 148 { 149 ID: "n14", 150 Attributes: map[string]interface{}{ 151 "label": "n14", 152 "latitude": 46.05, 153 "longitude": 6.19, 154 "size": "6.5", 155 "color": "rgb(1,179,255)", 156 }, 157 }, 158 { 159 ID: "n15", 160 Attributes: map[string]interface{}{ 161 "label": "n15", 162 "latitude": 47.43, 163 "longitude": 7.65, 164 "size": "6.0", 165 "color": "rgb(1,179,255)", 166 }, 167 }, 168 { 169 ID: "n16", 170 Attributes: map[string]interface{}{ 171 "label": "n16", 172 "latitude": 48.9, 173 "longitude": 8.32, 174 "size": "5.5", 175 "color": "rgb(1,179,255)", 176 }, 177 }, 178 { 179 ID: "n17", 180 Attributes: map[string]interface{}{ 181 "label": "n17", 182 "latitude": 49.83, 183 "longitude": 4.94, 184 "size": "6.5", 185 "color": "rgb(1,179,255)", 186 }, 187 }, 188 { 189 ID: "Paris", 190 Attributes: map[string]interface{}{ 191 "label": "Paris", 192 "latitude": 48.72, 193 "longitude": 2.46, 194 "size": "9.0", 195 "color": "rgb(1,179,255)", 196 }, 197 }, 198 }, 199 Edges: []Edge{ 200 {ID: "8", Source: "n1", Target: "Paris"}, 201 {ID: "7", Source: "n2", Target: "n4"}, 202 {ID: "28", Source: "n4", Target: "n1"}, 203 {ID: "30", Source: "n4", Target: "n7"}, 204 {ID: "26", Source: "n5", Target: "n1"}, 205 {ID: "27", Source: "n5", Target: "n2"}, 206 {ID: "0", Source: "n6", Target: "n5"}, 207 {ID: "29", Source: "n7", Target: "n5"}, 208 {ID: "1", Source: "n7", Target: "n8"}, 209 {ID: "17", Source: "n7", Target: "Paris"}, 210 {ID: "10", Source: "n8", Target: "n13"}, 211 {ID: "18", Source: "n8", Target: "Paris"}, 212 {ID: "15", Source: "n9", Target: "n8"}, 213 {ID: "34", Source: "n10", Target: "n9"}, 214 {ID: "31", Source: "n10", Target: "n11"}, 215 {ID: "11", Source: "n11", Target: "n13"}, 216 {ID: "13", Source: "n11", Target: "n14"}, 217 {ID: "32", Source: "n12", Target: "n10"}, 218 {ID: "12", Source: "n12", Target: "n11"}, 219 {ID: "23", Source: "n12", Target: "n13"}, 220 {ID: "33", Source: "n13", Target: "n10"}, 221 {ID: "25", Source: "n13", Target: "n14"}, 222 {ID: "14", Source: "n14", Target: "n9"}, 223 {ID: "5", Source: "n14", Target: "n17"}, 224 {ID: "19", Source: "n14", Target: "Paris"}, 225 {ID: "6", Source: "n15", Target: "n8"}, 226 {ID: "22", Source: "n15", Target: "n16"}, 227 {ID: "20", Source: "n15", Target: "Paris"}, 228 {ID: "4", Source: "n16", Target: "n15"}, 229 {ID: "24", Source: "n16", Target: "Paris"}, 230 {ID: "9", Source: "n17", Target: "n7"}, 231 {ID: "21", Source: "n17", Target: "n17"}, 232 {ID: "2", Source: "Paris", Target: "n4"}, 233 {ID: "3", Source: "Paris", Target: "n17"}, 234 {ID: "16", Source: "Paris", Target: "Paris"}, 235 }, 236 }, 237 }, 238 { 239 path: "arctic.json", 240 wantNodes: 1715, 241 wantEdges: 6676, 242 wantAttributes: map[string]bool{ 243 "label": true, 244 "x": true, 245 "y": true, 246 "color": true, 247 "size": true, 248 "attributes": true, 249 "attributes.nodedef": true, 250 }, 251 }, 252 } 253 254 func TestUnmarshal(t *testing.T) { 255 for _, test := range sigmajsExampleTests { 256 data, err := ioutil.ReadFile(filepath.Join("testdata", test.path)) 257 if err != nil { 258 t.Errorf("failed to read %q: %v", test.path, err) 259 continue 260 } 261 var got Graph 262 err = json.Unmarshal(data, &got) 263 if err != nil { 264 t.Errorf("failed to unmarshal %q: %v", test.path, err) 265 continue 266 } 267 if len(got.Nodes) != test.wantNodes { 268 t.Errorf("unexpected result for order of %q: got:%d want:%d", test.path, len(got.Nodes), test.wantNodes) 269 } 270 if len(got.Edges) != test.wantEdges { 271 t.Errorf("unexpected result for size of %q: got:%d want:%d", test.path, len(got.Edges), test.wantEdges) 272 } 273 if test.wantGraph != nil && !reflect.DeepEqual(&got, test.wantGraph) { 274 t.Errorf("unexpected result for %q:\ngot:\n%#v\nwant:\n%#v", test.path, got, test.wantGraph) 275 } 276 if test.wantAttributes != nil { 277 var paths []string 278 for _, n := range got.Nodes { 279 paths = attrPaths(paths, "", n.Attributes) 280 } 281 gotAttrs := make(map[string]bool) 282 for _, p := range paths { 283 gotAttrs[p] = true 284 } 285 if !reflect.DeepEqual(gotAttrs, test.wantAttributes) { 286 t.Errorf("unexpected result for %q:\ngot:\n%#v\nwant:\n%#v", test.path, gotAttrs, test.wantAttributes) 287 } 288 } 289 } 290 } 291 292 func attrPaths(dst []string, prefix string, m map[string]interface{}) []string { 293 for k, v := range m { 294 path := prefix 295 if path != "" { 296 path += "." 297 } 298 if v, ok := v.(map[string]interface{}); ok { 299 dst = attrPaths(dst, path+k, v) 300 } 301 dst = append(dst, path+k) 302 } 303 return dst 304 } 305 306 func TestMarshal(t *testing.T) { 307 for _, test := range sigmajsExampleTests { 308 data, err := ioutil.ReadFile(filepath.Join("testdata", test.path)) 309 if err != nil { 310 t.Errorf("failed to read %q: %v", test.path, err) 311 continue 312 } 313 var want Graph 314 err = json.Unmarshal(data, &want) 315 if err != nil { 316 t.Errorf("failed to unmarshal %q: %v", test.path, err) 317 continue 318 } 319 marshaled, err := json.Marshal(want) 320 if err != nil { 321 t.Errorf("failed to unmarshal %q: %v", test.path, err) 322 continue 323 } 324 var got Graph 325 err = json.Unmarshal(marshaled, &got) 326 if err != nil { 327 t.Errorf("failed to unmarshal %q: %v", test.path, err) 328 continue 329 } 330 if !reflect.DeepEqual(got, want) { 331 t.Errorf("unexpected result for %q:\ngot:\n%#v\nwant:\n%#v", test.path, got, want) 332 } 333 } 334 }