github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/util/locations.spec.ts (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 import { assert } from "chai"; 12 13 import { LocalityTier, LocalityTree } from "src/redux/localities"; 14 import { LocationTree } from "src/redux/locations"; 15 import { findMostSpecificLocation, findOrCalculateLocation } from "./locations"; 16 17 const nycLocality: LocalityTier[] = [ 18 { key: "region", value: "us-east-1" }, 19 { key: "city", value: "nyc" }, 20 ]; 21 22 describe("findMostSpecificLocation", function() { 23 it("returns null when location tree is empty", function() { 24 const locations: LocationTree = {}; 25 26 const location = findMostSpecificLocation(locations, nycLocality); 27 28 assert.equal(location, null); 29 }); 30 31 it("returns the location of a locality", function() { 32 const locations = { 33 region: { 34 "us-east-1": { 35 locality_key: "region", 36 locality_value: "us-east-1", 37 latitude: 12.3, 38 longitude: 45.6, 39 }, 40 }, 41 }; 42 43 const location = findMostSpecificLocation(locations, nycLocality); 44 45 assert.deepEqual(location, locations.region["us-east-1"]); 46 }); 47 48 it("finds the most specific location for a locality", function() { 49 const locations = { 50 region: { 51 "us-east-1": { 52 locality_key: "region", 53 locality_value: "us-east-1", 54 latitude: 12.3, 55 longitude: 45.6, 56 }, 57 }, 58 city: { 59 "nyc": { 60 locality_key: "city", 61 locality_value: "nyc", 62 latitude: 45.6, 63 longitude: 78.9, 64 }, 65 }, 66 }; 67 68 const location = findMostSpecificLocation(locations, nycLocality); 69 70 assert.deepEqual(location, locations.city.nyc); 71 }); 72 }); 73 74 describe("findOrCalculateLocation", function() { 75 describe("when locality has location", function() { 76 it("returns the locality's location", function() { 77 const locations = { 78 city: { 79 "nyc": { 80 locality_key: "region", 81 locality_value: "us-east-1", 82 latitude: 12.3, 83 longitude: 45.6, 84 }, 85 }, 86 }; 87 88 const locality: LocalityTree = { 89 localities: {}, 90 nodes: [], 91 tiers: nycLocality, 92 }; 93 94 const location = findOrCalculateLocation(locations, locality); 95 96 assert.deepEqual(location, locations.city.nyc); 97 }); 98 }); 99 100 describe("when locality doesn't have location", function() { 101 describe("when locality has nodes", function() { 102 it("returns null", function() { 103 const locations = { 104 region: { 105 "us-east-1": { 106 locality_key: "region", 107 locality_value: "us-east-1", 108 latitude: 12.3, 109 longitude: 45.6, 110 }, 111 }, 112 }; 113 114 const locality: LocalityTree = { 115 localities: {}, 116 nodes: [ 117 { 118 desc: { 119 node_id: 1, 120 locality: { 121 tiers: nycLocality, 122 }, 123 }, 124 }, 125 ], 126 tiers: nycLocality, 127 }; 128 129 const location = findOrCalculateLocation(locations, locality); 130 131 assert.equal(location, null); 132 }); 133 }); 134 135 describe("when locality has children without locations", function() { 136 it("returns null", function() { 137 const locations = {}; 138 139 const locality: LocalityTree = { 140 localities: { 141 city: { 142 nyc: { 143 localities: {}, 144 nodes: [], 145 tiers: nycLocality, 146 }, 147 }, 148 }, 149 nodes: [], 150 tiers: [nycLocality[0]], 151 }; 152 153 const location = findOrCalculateLocation(locations, locality); 154 155 assert.equal(location, null); 156 }); 157 }); 158 159 describe("when locality has children with locations", function() { 160 // TODO(couchand): actually test the centroid 161 it("returns their centroid", function() { 162 const locations = { 163 city: { 164 "nyc": { 165 locality_key: "region", 166 locality_value: "us-east-1", 167 latitude: 12.3, 168 longitude: 45.6, 169 }, 170 }, 171 }; 172 173 const locality: LocalityTree = { 174 localities: { 175 city: { 176 nyc: { 177 localities: {}, 178 nodes: [], 179 tiers: nycLocality, 180 }, 181 }, 182 }, 183 nodes: [], 184 tiers: [nycLocality[0]], 185 }; 186 187 const location = findOrCalculateLocation(locations, locality); 188 189 assert.equal(location.latitude, locations.city.nyc.latitude); 190 assert.equal(location.longitude, locations.city.nyc.longitude); 191 }); 192 }); 193 }); 194 });