test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. var expect = require('chai').expect,
  2. Equality = require('../');
  3. describe('geojson-equality for Points', function() {
  4. var g1 = { "type": "Point", "coordinates": [30, 10] },
  5. g2 = { "type": "Point", "coordinates": [30, 10] },
  6. g3 = { "type": "Point", "coordinates": [30, 11] },
  7. g4 = { "type": "Point", "coordinates": [30, 10, 5]},
  8. g5 = { "type": "Point", "coordinates": [30, 10, 5]},
  9. eq = new Equality();
  10. it('are equal', function() {
  11. expect(eq.compare(g1,g2)).to.be.true;
  12. });
  13. it('are not equal', function() {
  14. expect(eq.compare(g1,g3)).to.be.false;
  15. });
  16. it('are not equal with different point dimensions', function() {
  17. expect(eq.compare(g1,g4)).to.be.false;
  18. });
  19. it('are equal with 3d points', function() {
  20. expect(eq.compare(g4,g5)).to.be.true;
  21. });
  22. });
  23. describe('geojson-equality for LineStrings', function() {
  24. var g1 = { "type": "LineString", "coordinates":
  25. [ [30, 10], [10, 30], [40, 40] ] },
  26. g2 = { "type": "LineString", "coordinates":
  27. [ [30, 10], [10, 30], [40, 40] ] };
  28. it('are equal', function() {
  29. var eq = new Equality();
  30. expect(eq.compare(g1,g2)).to.be.true;
  31. });
  32. var g3 = { "type": "LineString", "coordinates":
  33. [ [31, 10], [10, 30], [40, 40] ] };
  34. it('are not equal', function() {
  35. var eq = new Equality();
  36. expect(eq.compare(g1,g3)).to.be.false;
  37. });
  38. var g4 = { "type": "LineString", "coordinates":
  39. [ [40, 40], [10, 30], [30, 10]] };
  40. it('reverse direction, direction is not matched, so both are equal', function() {
  41. var eq = new Equality();
  42. expect(eq.compare(g1,g4)).to.be.true;
  43. });
  44. it('reverse direction, direction is matched, so both are not equal', function() {
  45. var eq = new Equality({direction: true});
  46. expect(eq.compare(g1,g4)).to.be.false;
  47. });
  48. });
  49. describe('geojson-equality for Polygons', function() {
  50. var g1 = { "type": "Polygon", "coordinates": [
  51. [[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]]
  52. ]};
  53. var g2 = { "type": "Polygon", "coordinates": [
  54. [[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]]
  55. ]};
  56. it('are equal', function() {
  57. var eq = new Equality();
  58. expect(eq.compare(g1,g2)).to.be.true;
  59. });
  60. var g3 = { "type": "Polygon", "coordinates": [
  61. [[30, 10], [41, 40], [20, 40], [10, 20], [30, 10]]
  62. ]};
  63. it('are not equal', function() {
  64. var eq = new Equality();
  65. expect(eq.compare(g1,g3)).to.be.false;
  66. });
  67. var g4 = { "type": "Polygon", "coordinates": [
  68. [[30,10], [10,20], [20,40], [40,40], [30,10]]
  69. ]};
  70. it('reverse direction, direction is not matched, so both are equal', function() {
  71. var eq = new Equality();
  72. expect(eq.compare(g1,g4)).to.be.true;
  73. });
  74. it('reverse direction, direction is matched, so both are not equal', function() {
  75. var eq = new Equality({direction: true});
  76. expect(eq.compare(g1,g4)).to.be.false;
  77. });
  78. var g5 = { "type": "Polygon", "coordinates": [
  79. [[10,20], [20,40], [40,40], [30,10], [10,20]]
  80. ]};
  81. it('reverse direction, diff start index, direction is not matched, so both are equal',
  82. function() {
  83. var eq = new Equality();
  84. expect(eq.compare(g1,g5)).to.be.true;
  85. });
  86. it('reverse direction, diff start index, direction is matched, so both are not equal',
  87. function() {
  88. var eq = new Equality({direction: true});
  89. expect(eq.compare(g1,g5)).to.be.false;
  90. });
  91. var gh1 = { "type": "Polygon", "coordinates": [
  92. [[45, 45], [15, 40], [10, 20], [35, 10],[45, 45]],
  93. [[20, 30], [35, 35], [30, 20], [20, 30]]
  94. ]};
  95. var gh2 = { "type": "Polygon", "coordinates": [
  96. [[35, 10], [45, 45], [15, 40], [10, 20], [35, 10]],
  97. [[20, 30], [35, 35], [30, 20], [20, 30]]
  98. ]};
  99. it('have holes too and diff start ind, direction is not matched, both are equal',
  100. function() {
  101. var eq = new Equality({direction: false});
  102. expect(eq.compare(gh1,gh2)).to.be.true;
  103. });
  104. it('have holes too and diff start ind, direction is matched, so both are not equal',
  105. function() {
  106. var eq = new Equality({direction: true});
  107. expect(eq.compare(gh1,gh2)).to.be.true;
  108. });
  109. var gprecision1 = { "type": "Polygon", "coordinates": [
  110. [[30, 10], [40.12345, 40.12345], [20, 40], [10, 20], [30, 10]]
  111. ]};
  112. var gprecision2 = { "type": "Polygon", "coordinates": [
  113. [[30, 10], [40.123389, 40.123378], [20, 40], [10, 20], [30, 10]]
  114. ]};
  115. it('after limiting precision, are equal', function() {
  116. var eq = new Equality({precision: 3});
  117. expect(eq.compare(gprecision1,gprecision2)).to.be.true;
  118. });
  119. it('with high precision, are not equal', function() {
  120. var eq = new Equality({precision: 10});
  121. expect(eq.compare(gprecision1,gprecision2)).to.be.false;
  122. });
  123. });
  124. describe ('geojson-equality for Feature', function() {
  125. it ('will not be equal with changed id', function() {
  126. var f1 = {"type": "Feature", "id": "id1"};
  127. var f2 = {"type": "Feature", "id": "id2"};
  128. var eq = new Equality();
  129. expect(eq.compare(f1, f2)).to.be.false;
  130. });
  131. it ('will not be equal with different count of properties', function() {
  132. var f1 = {"type": "Feature", "id": "id1", "properties": {"foo": "bar"}};
  133. var f2 = {"type": "Feature", "id": "id1", "properties": {"foo1": "bar", "foo2": "bar"}};
  134. var eq = new Equality();
  135. expect(eq.compare(f1, f2)).to.be.false;
  136. });
  137. it ('will not be equal with different keys in properties', function() {
  138. var f1 = {"type": "Feature", "id": "id1", "properties": {"foo1": "bar"}};
  139. var f2 = {"type": "Feature", "id": "id1", "properties": {"foo2": "bar"}};
  140. var eq = new Equality();
  141. expect(eq.compare(f1, f2)).to.be.false;
  142. });
  143. it ('will not be equal with different properties', function() {
  144. var f1 = {"type": "Feature", "id": "id1", "properties": {"foo": "bar1"}};
  145. var f2 = {"type": "Feature", "id": "id1", "properties": {"foo": "bar2"}};
  146. var eq = new Equality();
  147. expect(eq.compare(f1, f2)).to.be.false;
  148. });
  149. it ('will not be equal with different geometry', function() {
  150. var f1 = {
  151. "type": "Feature",
  152. "id": "id1",
  153. "properties": {"foo": "bar1"},
  154. "geometry": { "type": "Polygon", "coordinates": [
  155. [[30, 10], [41, 40], [20, 40], [10, 20], [30, 10]]
  156. ]}
  157. };
  158. var f2 = {
  159. "type": "Feature",
  160. "id": "id1",
  161. "properties": {"foo": "bar1"},
  162. "geometry": { "type": "Polygon", "coordinates": [
  163. [[40, 20], [31, 10], [30, 20], [30, 10], [10, 40]]
  164. ]}
  165. };
  166. var eq = new Equality();
  167. expect(eq.compare(f1, f2)).to.be.false;
  168. });
  169. it ('will be equal with nested properties', function() {
  170. var f1 = {"type": "Feature", "id": "id1", "properties": {"foo": {"bar": "baz"}},
  171. "geometry": {"type": "Point", "coordinates": [0, 1]}
  172. };
  173. var f2 = {"type": "Feature", "id": "id1", "properties": {"foo": {"bar": "baz"}},
  174. "geometry": {"type": "Point", "coordinates": [0, 1]}
  175. };
  176. var eq = new Equality();
  177. expect(eq.compare(f1, f2)).to.be.true;
  178. });
  179. it ('will not be equal with different nested properties', function() {
  180. var f1 = {"type": "Feature", "id": "id1", "properties": {"foo": {"bar": "baz"}},
  181. "geometry": {"type": "Point", "coordinates": [0, 1]}
  182. };
  183. var f2 = {"type": "Feature", "id": "id1", "properties": {"foo": {"bar": "baz2"}},
  184. "geometry": {"type": "Point", "coordinates": [0, 1]}
  185. };
  186. var eq = new Equality();
  187. expect(eq.compare(f1, f2)).to.be.false;
  188. });
  189. it ('will use a custom comparator if provided', function() {
  190. var f1 = {
  191. "type": "Feature",
  192. "id": "id1",
  193. "properties": {"foo_123": "bar"},
  194. "geometry": { "type": "Polygon", "coordinates": [
  195. [[40, 20], [31, 10], [30, 20], [30, 10], [10, 40]]
  196. ]}
  197. };
  198. var f2 = {
  199. "type": "Feature",
  200. "id": "id1",
  201. "properties": {"foo_456": "bar"},
  202. "geometry": { "type": "Polygon", "coordinates": [
  203. [[40, 20], [31, 10], [30, 20], [30, 10], [10, 40]]
  204. ]}
  205. };
  206. var eq = new Equality({objectComparator: function(obj1, obj2) {
  207. return ('foo_123' in obj1 && 'foo_456' in obj2);
  208. }});
  209. expect(eq.compare(f1, f2)).to.be.true;
  210. });
  211. it ('will not be equal if one has bbox and other not', function() {
  212. var f1 = {"type": "Feature", "id": "id1", "bbox": [1, 2, 3, 4]},
  213. f2 = {"type": "Feature", "id": "id1"},
  214. eq = new Equality();
  215. expect(eq.compare(f1, f2)).to.be.false;
  216. });
  217. it ('will not be equal if bboxes are not equal', function() {
  218. var f1 = {"type": "Feature", "id": "id1", "bbox": [1, 2, 3, 4]},
  219. f2 = {"type": "Feature", "id": "id1", "bbox": [1, 2, 3, 5]},
  220. eq = new Equality();
  221. expect(eq.compare(f1, f2)).to.be.false;
  222. });
  223. it ('equal features with bboxes', function() {
  224. var f1 = {
  225. "type": "Feature",
  226. "id": "id1",
  227. "properties": {"foo": "bar1"},
  228. "geometry": { "type": "Polygon", "coordinates": [
  229. [[30, 10], [41, 40], [20, 40], [10, 20], [30, 10]]
  230. ]},
  231. "bbox": [10, 10, 41, 40]
  232. };
  233. var f2 = {
  234. "type": "Feature",
  235. "id": "id1",
  236. "properties": {"foo": "bar1"},
  237. "geometry": { "type": "Polygon", "coordinates": [
  238. [[30, 10], [41, 40], [20, 40], [10, 20], [30, 10]]
  239. ]},
  240. "bbox": [10, 10, 41, 40]
  241. };
  242. var eq = new Equality();
  243. expect(eq.compare(f1, f2)).to.be.true;
  244. });
  245. it ('not equal features with equal bboxes', function() {
  246. var f1 = {
  247. "type": "Feature",
  248. "id": "id1",
  249. "properties": {"foo": "bar1"},
  250. "geometry": { "type": "Polygon", "coordinates": [
  251. [[30, 10], [41, 40], [20, 40], [10, 20], [30, 10]]
  252. ]},
  253. "bbox": [10, 10, 41, 40]
  254. };
  255. var f2 = {
  256. "type": "Feature",
  257. "id": "id1",
  258. "properties": {"foo": "bar1"},
  259. "geometry": { "type": "Polygon", "coordinates": [
  260. [[30, 10], [41, 40], [20, 40], [10, 20], [30, 1]]
  261. ]},
  262. "bbox": [10, 10, 41, 40]
  263. };
  264. var eq = new Equality();
  265. expect(eq.compare(f1, f2)).to.be.false;
  266. });
  267. });
  268. describe('geojson-equality for MultiPoints', function() {
  269. var g1 = { "type": "MultiPoint", "coordinates": [
  270. [0, 40], [40, 30], [20, 20], [30, 10]
  271. ]};
  272. var g2 = { "type": "MultiPoint", "coordinates": [
  273. [0, 40], [20, 20], [40, 30], [30, 10]
  274. ]};
  275. it('are equal', function() {
  276. var eq = new Equality();
  277. expect(eq.compare(g1,g2)).to.be.true;
  278. });
  279. var g3 = { "type": "MultiPoint", "coordinates": [
  280. [10, 40], [20, 20], [40, 30], [30, 10]
  281. ]};
  282. it('are not equal', function() {
  283. var eq = new Equality();
  284. expect(eq.compare(g1,g3)).to.be.false;
  285. });
  286. });
  287. describe('geojson-equality for MultiLineString', function() {
  288. var g1 = { "type": "MultiLineString", "coordinates": [
  289. [[30, 10], [10, 30], [40, 40]],
  290. [[0, 10], [10, 0], [40, 40]]
  291. ]};
  292. var g2 = { "type": "MultiLineString", "coordinates": [
  293. [[40, 40],[10, 30], [30, 10]],
  294. [[0, 10], [10, 0], [40, 40]]
  295. ]};
  296. it('reverse direction, direction is not matched, so both are equal',
  297. function() {
  298. var eq = new Equality();
  299. expect(eq.compare(g1,g2)).to.be.true;
  300. }
  301. );
  302. it('reverse direction, direction is matched, so both are not equal',
  303. function() {
  304. var eq = new Equality({direction: true});
  305. expect(eq.compare(g1,g2)).to.be.false;
  306. }
  307. );
  308. var g3 = { "type": "MultiLineString", "coordinates": [
  309. [[10, 10], [20, 20], [10, 40]],
  310. [[40, 40], [30, 30], [40, 20], [30, 10]] ] };
  311. it('both are not equal',
  312. function() {
  313. var eq = new Equality();
  314. expect(eq.compare(g1,g3)).to.be.false;
  315. }
  316. );
  317. });
  318. describe('geojson-equality for MultiPolygon', function() {
  319. var g1 = { "type": "MultiPolygon", "coordinates": [
  320. [[[30, 20], [45, 40], [10, 40], [30, 20]]],
  321. [[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]]
  322. ]};
  323. var g2 = { "type": "MultiPolygon", "coordinates": [
  324. [[[30, 20], [45, 40], [10, 40], [30, 20]]],
  325. [[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]]
  326. ]};
  327. it('both are equal', function() {
  328. var eq = new Equality();
  329. expect(eq.compare(g1,g2)).to.be.true;
  330. });
  331. var g3 = { "type": "MultiPolygon", "coordinates": [
  332. [[[30, 20], [45, 40], [10, 40], [30, 20]]],
  333. [[[15, 5], [400, 10], [10, 20], [5, 10], [15, 5]]]
  334. ]};
  335. it('both are not equal', function() {
  336. var eq = new Equality();
  337. expect(eq.compare(g1,g3)).to.be.false;
  338. });
  339. var gh1 = { "type": "MultiPolygon", "coordinates": [
  340. [[[40, 40], [20, 45], [45, 30], [40, 40]]],
  341. [
  342. [[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]],
  343. [[30, 20], [20, 15], [20, 25], [30, 20]],
  344. [[20, 10], [30, 10], [30, 15], [20, 10]]
  345. ]
  346. ]};
  347. var gh2 = { "type": "MultiPolygon", "coordinates": [
  348. [
  349. [[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]],
  350. [[20, 10], [30, 10], [30, 15], [20, 10]],
  351. [[30, 20], [20, 15], [20, 25], [30, 20]]
  352. ],
  353. [[[40, 40], [20, 45], [45, 30], [40, 40]]]
  354. ]};
  355. it('having holes, both are equal', function() {
  356. var eq = new Equality();
  357. expect(eq.compare(gh1,gh2)).to.be.true;
  358. });
  359. });