json.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * 测试 JSON 文件的完整性
  3. * @Author https://github.com/modood
  4. * @DateTime 2016-10-10 11:24
  5. */
  6. /* eslint no-labels: ["error", { "allowLoop": true }] */
  7. const assert = require('assert')
  8. const chinaDivision = require('..')
  9. const provinces = chinaDivision.provinces
  10. const cities = chinaDivision.cities
  11. const areas = chinaDivision.areas
  12. const streets = chinaDivision.streets
  13. const villages = chinaDivision.villages
  14. const pc = chinaDivision.pc
  15. const pcC = chinaDivision.pcC
  16. const pca = chinaDivision.pca
  17. const pcaC = chinaDivision.pcaC
  18. const pcas = chinaDivision.pcas
  19. const pcasC = chinaDivision.pcasC
  20. describe('中华人民共和国行政区划:', () => {
  21. it('“一级” 省级(省份、直辖市、自治区)数据', () => {
  22. const i = findElem(provinces, 'code', '11')
  23. assert(i !== -1)
  24. assert.equal(provinces[i].name, '北京市')
  25. })
  26. it('“二级” 地级(城市)数据', () => {
  27. const i = findElem(cities, 'code', '1401')
  28. assert(i !== -1)
  29. assert.equal(cities[i].name, '太原市')
  30. assert.equal(cities[i].provinceCode, '14')
  31. })
  32. it('“三级” 县级(区县)数据', () => {
  33. const i = findElem(areas, 'code', '120110')
  34. assert(i !== -1)
  35. assert.equal(areas[i].name, '东丽区')
  36. assert.equal(areas[i].cityCode, '1201')
  37. assert.equal(areas[i].provinceCode, '12')
  38. })
  39. it('“四级” 乡级(乡镇、街道)数据', () => {
  40. const i = findElem(streets, 'code', '441881124')
  41. assert(i !== -1)
  42. assert.equal(streets[i].name, '波罗镇')
  43. assert.equal(streets[i].areaCode, '441881')
  44. assert.equal(streets[i].cityCode, '4418')
  45. assert.equal(streets[i].provinceCode, '44')
  46. })
  47. it('“五级” 村级(村委会、居委会)数据', () => {
  48. const i = findElem(villages, 'code', '421303101216')
  49. assert(i !== -1)
  50. assert.equal(villages[i].name, '高庙村委会')
  51. assert.equal(villages[i].streetCode, '421303101')
  52. assert.equal(villages[i].areaCode, '421303')
  53. assert.equal(villages[i].cityCode, '4213')
  54. assert.equal(villages[i].provinceCode, '42')
  55. })
  56. })
  57. describe('联动数据', () => {
  58. it('“省份、城市” 二级联动数据', () => {
  59. assert.ok(pc['浙江省'].indexOf('杭州市') !== -1)
  60. assert.ok(pc['河南省'].indexOf('济源市') !== -1)
  61. assert.ok(pc['湖北省'].indexOf('仙桃市') !== -1)
  62. assert.ok(pc['新疆维吾尔自治区'].indexOf('铁门关市') !== -1)
  63. for (const p in pc) {
  64. if (['台湾省', '香港特别行政区', '澳门特别行政区'].indexOf(p) === -1 &&
  65. pc[p].length === 0) throw new Error(`数据:pc.json,${p}的城市列表为空!`)
  66. }
  67. let ok = false
  68. const t = [
  69. { code: '41', name: '河南省' },
  70. { code: '419001', name: '济源市' }
  71. ]
  72. loop:
  73. for (let i = 0; i < pcC.length; i++) {
  74. const p = pcC[i]
  75. if (['台湾省', '香港特别行政区', '澳门特别行政区'].indexOf(p.name) === -1 &&
  76. p.children.length === 0) throw new Error(`数据:pc-code.json,${p.name}的城市列表为空!`)
  77. if (p.code === t[0].code && p.name === t[0].name) {
  78. for (let j = 0; j < p.children.length; j++) {
  79. const c = p.children[j]
  80. if (c.code === t[1].code && c.name === t[1].name) {
  81. ok = true
  82. break loop
  83. }
  84. }
  85. }
  86. }
  87. if (!ok) throw new Error(`数据:pc-code.json,${t[0].name}${t[1].name}找不到!`)
  88. })
  89. it('“省份、城市、区县” 三级联动数据', () => {
  90. assert.ok(pca['云南省']['丽江市'].indexOf('古城区') !== -1)
  91. assert.ok(pca['海南省']['三沙市'].indexOf('南沙群岛') !== -1)
  92. assert.ok(pca['广东省']['中山市'].indexOf('横栏镇') !== -1)
  93. assert.ok(pca['广东省']['东莞市'].indexOf('常平镇') !== -1)
  94. assert.ok(pca['海南省']['儋州市'].indexOf('那大镇') !== -1)
  95. assert.ok(pca['甘肃省']['嘉峪关市'].indexOf('文殊镇') !== -1)
  96. for (const p in pca) {
  97. for (const c in pca[p]) {
  98. if (pca[p][c].length === 0) throw new Error(`数据:pca.json,${p}${c}的区县列表为空!`)
  99. }
  100. }
  101. let ok = false
  102. const t = [
  103. { code: '14', name: '山西省' },
  104. { code: '1405', name: '晋城市' },
  105. { code: '140524', name: '陵川县' }
  106. ]
  107. loop:
  108. for (let i = 0; i < pcaC.length; i++) {
  109. const p = pcaC[i]
  110. if (p.code === t[0].code && p.name === t[0].name) {
  111. for (let j = 0; j < p.children.length; j++) {
  112. const c = p.children[j]
  113. if (c.children.length === 0) throw new Error(`数据:pca-code.json,${p.name}${c.name}的区县列表为空!`)
  114. if (c.code === t[1].code && c.name === t[1].name) {
  115. for (let k = 0; k < c.children.length; k++) {
  116. const a = c.children[k]
  117. if (a.code === t[2].code && a.name === t[2].name) {
  118. ok = true
  119. break loop
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }
  126. if (!ok) throw new Error(`数据:pca-code.json,${t[0].anem}${t[1].name}${t[2].name}找不到!`)
  127. })
  128. it('“省份、城市、区县、乡镇” 四级联动数据', () => {
  129. assert.ok(pcas['广西壮族自治区']['玉林市']['容县'].indexOf('石头镇') !== -1)
  130. assert.ok(pcas['陕西省']['汉中市']['汉台区'].indexOf('七里街道') !== -1)
  131. assert.ok(pcas['海南省']['三沙市']['南沙群岛'].indexOf('永暑岛') !== -1)
  132. assert.ok(pcas['广东省']['中山市']['中山市'].indexOf('横栏镇') !== -1)
  133. assert.ok(pcas['广东省']['东莞市']['东莞市'].indexOf('常平镇') !== -1)
  134. assert.ok(pcas['海南省']['儋州市']['儋州市'].indexOf('那大镇') !== -1)
  135. assert.ok(pcas['甘肃省']['嘉峪关市']['嘉峪关市'].indexOf('文殊镇') !== -1)
  136. assert.ok(pcas['福建省']['泉州市']['金门县'].indexOf('金门县') !== -1)
  137. for (const p in pcas) {
  138. for (const c in pcas[p]) {
  139. for (const a in pcas[p][c]) {
  140. if (pcas[p][c][a].length === 0) throw new Error(`数据:pcas.json,${p}${c}${a}的乡镇列表为空!`)
  141. }
  142. }
  143. }
  144. let ok = false
  145. const t = [
  146. { code: '21', name: '辽宁省' },
  147. { code: '2102', name: '大连市' },
  148. { code: '210211', name: '甘井子区' },
  149. { code: '210211007', name: '泡崖街道' }
  150. ]
  151. loop:
  152. for (let i = 0; i < pcasC.length; i++) {
  153. const p = pcasC[i]
  154. if (p.code === t[0].code && p.name === t[0].name) {
  155. for (let j = 0; j < p.children.length; j++) {
  156. const c = p.children[j]
  157. if (c.code === t[1].code && c.name === t[1].name) {
  158. for (let k = 0; k < c.children.length; k++) {
  159. const a = c.children[k]
  160. if (a.children.length === 0) throw new Error(`数据:pcas-code.json,${p.name}${c.name}${a.name}的乡镇列表为空!`)
  161. if (a.code === t[2].code && a.name === t[2].name) {
  162. for (let l = 0; l < a.children.length; l++) {
  163. const s = a.children[l]
  164. if (s.code === t[3].code && s.name === t[3].name) {
  165. ok = true
  166. break loop
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. if (!ok) throw new Error(`数据:pcas-code.json,${t[0].anem}${t[1].name}${t[2].name}${t[3].name} 找不到!`)
  176. })
  177. it('“某省、某市、某县、某镇” 不存在', () => {
  178. assert.throws(() => pcas['某省']['某市']['某县'].indexOf('某镇') !== -1)
  179. })
  180. })
  181. function findElem (arrayToSearch, attr, val) {
  182. for (let i = 0; i < arrayToSearch.length; i++) {
  183. if (arrayToSearch[i][attr] === val) {
  184. return i
  185. }
  186. }
  187. return -1
  188. }