From 4767a47772903a0e33c180a5f3dab6ea99834578 Mon Sep 17 00:00:00 2001 From: tmerlier Date: Thu, 2 Feb 2017 18:36:07 +0100 Subject: [PATCH 1/4] Add countries and territories --- forced_data/pays2016.tsv | 283 +++++++++++++++++++++++++++++++++++ lib/countries.js | 23 +++ lib/countryHelpers.js | 8 + lib/integration/countries.js | 107 +++++++++++++ lib/integration/index.js | 7 +- server.js | 27 +++- 6 files changed, 453 insertions(+), 2 deletions(-) create mode 100755 forced_data/pays2016.tsv create mode 100644 lib/countries.js create mode 100644 lib/countryHelpers.js create mode 100644 lib/integration/countries.js diff --git a/forced_data/pays2016.tsv b/forced_data/pays2016.tsv new file mode 100755 index 0000000..472c007 --- /dev/null +++ b/forced_data/pays2016.tsv @@ -0,0 +1,283 @@ +COG ACTUAL CAPAY CRPAY ANI LIBCOG LIBENR ANCNOM CODEISO2 CODEISO3 CODENUM3 +XXXXX 1 FRANCE RÉPUBLIQUE FRANÇAISE FR FRA 250 +XXXXX 3 GUADELOUPE DÉPARTEMENT DE LA GUADELOUPE GP GLP 312 +XXXXX 3 GUYANE DÉPARTEMENT DE LA GUYANE GF GUF 254 +XXXXX 3 MARTINIQUE DÉPARTEMENT DE LA MARTINIQUE MQ MTQ 474 +XXXXX 3 NOUVELLE-CALEDONIE NOUVELLE-CALÉDONIE NC NCL 540 +XXXXX 3 POLYNESIE FRANCAISE POLYNÉSIE FRANÇAISE PF PYF 258 +XXXXX 3 CLIPPERTON (ILE) CLIPPERTON (ÎLE) +XXXXX 3 SAINT-PIERRE-ET-MIQUELON COLLECTIVITÉ TERRITORIALE DE SAINT-PIERRE-ET-MIQUELON PM SPM 666 +XXXXX 3 TERRES AUSTRALES FRANCAISES TERRES AUSTRALES FRANÇAISES TF ATF 260 +XXXXX 3 WALLIS-ET-FUTUNA WALLIS-ET-FUTUNA WF WLF 876 +XXXXX 3 MAYOTTE DÉPARTEMENT DE MAYOTTE YT MYT 175 +XXXXX 3 SAINT-BARTHELEMY COLLECTIVITÉ D'OUTRE-MER DE SAINT-BARTHÉLEMY BL BLM 652 +XXXXX 3 SAINT-MARTIN COLLECTIVITÉ D'OUTRE-MER DE SAINT-MARTIN MF MAF 663 +XXXXX 3 LA REUNION DÉPARTEMENT DE LA RÉUNION RE REU 638 +99101 3 99102 FEROE (ILES) FÉROÉ (ÎLES) FO FRO 234 +99101 1 DANEMARK ROYAUME DU DANEMARK DK DNK 208 +99102 1 1944 ISLANDE RÉPUBLIQUE D'ISLANDE IS ISL 352 +99103 1 NORVEGE ROYAUME DE NORVÈGE NO NOR 578 +99103 3 SVALBARD et ILE JAN MAYEN SVALBARD ET ÎLE JAN MAYEN SJ SJM 744 +99103 3 BOUVET (ILE) BOUVET (ÎLE) BV BVT 074 +99104 1 SUEDE ROYAUME DE SUÈDE SE SWE 752 +99105 1 FINLANDE RÉPUBLIQUE DE FINLANDE FI FIN 246 +99106 1 99123 1991 ESTONIE RÉPUBLIQUE D'ESTONIE EE EST 233 +99107 1 99123 1991 LETTONIE RÉPUBLIQUE DE LETTONIE LV LVA 428 +99108 1 99123 1991 LITUANIE RÉPUBLIQUE DE LITUANIE LT LTU 440 +99109 1 1990 ALLEMAGNE RÉPUBLIQUE FÉDÉRALE D'ALLEMAGNE DE DEU 276 +99110 1 AUTRICHE RÉPUBLIQUE D'AUTRICHE AT AUT 040 +99111 1 BULGARIE RÉPUBLIQUE DE BULGARIE BG BGR 100 +99112 1 HONGRIE RÉPUBLIQUE DE HONGRIE HU HUN 348 +99113 1 LIECHTENSTEIN PRINCIPAUTÉ DE LIECHTENSTEIN LI LIE 438 +99114 1 ROUMANIE ROUMANIE RO ROM 642 +99115 2 TCHECOSLOVAQUIE TCHÉCOSLOVAQUIE +99116 1 99115 1992 TCHEQUE (REPUBLIQUE) RÉPUBLIQUE TCHÈQUE CZ CZE 203 +99117 1 99115 1992 SLOVAQUIE RÉPUBLIQUE SLOVAQUE SK SVK 703 +99118 1 99121 1992 BOSNIE-HERZEGOVINE RÉPUBLIQUE DE BOSNIE-HERZÉGOVINE BA BIH 070 +99119 1 99121 1991 CROATIE RÉPUBLIQUE DE CROATIE HR HRV 191 +99120 1 99121 2006 MONTENEGRO RÉPUBLIQUE DU MONTÉNÉGRO SERBIE-ET-MONTENEGRO ME MNE 499 +99121 1 2006 SERBIE RÉPUBLIQUE DE SERBIE SERBIE-ET-MONTENEGRO RS SRB 688 +99122 1 POLOGNE RÉPUBLIQUE DE POLOGNE PL POL 616 +99123 1 1991 RUSSIE FÉDÉRATION DE RUSSIE U.R.S.S. RU RUS 643 +99124 2 99208 TURQUIE D'EUROPE TURQUIE D'EUROPE +99125 1 ALBANIE RÉPUBLIQUE D'ALBANIE AL ALB 008 +99126 1 GRECE RÉPUBLIQUE HELLÉNIQUE GR GRC 300 +99127 1 ITALIE RÉPUBLIQUE ITALIENNE IT ITA 380 +99128 1 SAINT-MARIN RÉPUBLIQUE DE SAINT-MARIN SM SMR 674 +99129 1 VATICAN, ou SAINT-SIEGE ÉTAT DE LA CITÉ DU VATICAN VA VAT 336 +99130 1 ANDORRE PRINCIPAUTÉ D'ANDORRE AD AND 020 +99131 1 BELGIQUE ROYAUME DE BELGIQUE BE BEL 056 +99132 3 GUERNESEY GUERNESEY GG GGY 831 +99132 3 MAN (ILE) MAN (ÎLE) IM IMN 833 +99132 1 ROYAUME-UNI ROYAUME-UNI DE GRANDE-BRETAGNE ET D'IRLANDE DU NORD GB GBR 826 +99132 3 JERSEY JERSEY JE JEY 832 +99133 4 99132 GIBRALTAR GIBRALTAR GI GIB 292 +99134 1 ESPAGNE ROYAUME D'ESPAGNE ES ESP 724 +99135 3 ARUBA ARUBA AW ABW 533 +99135 1 PAYS-BAS ROYAUME DES PAYS-BAS HOLLANDE NL NLD 528 +99136 1 IRLANDE, ou EIRE IRLANDE IE IRL 372 +99137 1 LUXEMBOURG GRAND-DUCHÉ DE LUXEMBOURG LU LUX 442 +99138 1 MONACO PRINCIPAUTÉ DE MONACO MC MCO 492 +99139 1 PORTUGAL RÉPUBLIQUE PORTUGAISE PT PRT 620 +99140 1 SUISSE CONFÉDÉRATION SUISSE CH CHE 756 +99141 2 99109 REPUBLIQUE DEMOCRATIQUE ALLEMANDE RÉPUBLIQUE DÉMOCRATIQUE ALLEMANDE +99142 2 99109 REPUBLIQUE FEDERALE D'ALLEMAGNE RÉPUBLIQUE FÉDÉRALE D'ALLEMAGNE +99144 1 99133 1964 MALTE RÉPUBLIQUE DE MALTE MT MLT 470 +99145 1 99121 1991 SLOVENIE RÉPUBLIQUE DE SLOVÉNIE SI SVN 705 +99148 1 99123 1991 BIELORUSSIE RÉPUBLIQUE DE BIÉLORUSSIE BY BLR 112 +99151 1 99123 1991 MOLDAVIE RÉPUBLIQUE DE MOLDAVIE MD MDA 498 +99155 1 99123 1991 UKRAINE UKRAINE UA UKR 804 +99156 1 99121 1993 EX-REPUBLIQUE YOUGOSLAVE DE MACEDOINE EX-RÉPUBLIQUE YOUGOSLAVE DE MACÉDOINE MK MKD 807 +99157 1 99121 2008 KOSOVO RÉPUBLIQUE DU KOSOVO SERBIE XK +99201 1 ARABIE SAOUDITE ROYAUME D'ARABIE SAOUDITE SA SAU 682 +99202 2 99251 YEMEN (REPUBLIQUE ARABE DU) YÉMEN (RÉPUBLIQUE ARABE DU) +99203 1 IRAQ RÉPUBLIQUE D'IRAQ IQ IRQ 368 +99204 1 IRAN RÉPUBLIQUE ISLAMIQUE D'IRAN PERSE IR IRN 364 +99205 1 1943 LIBAN RÉPUBLIQUE LIBANAISE LB LBN 422 +99206 1 1944 SYRIE RÉPUBLIQUE ARABE SYRIENNE SY SYR 760 +99207 1 1948 ISRAEL ÉTAT D'ISRAËL IL ISR 376 +99208 1 TURQUIE RÉPUBLIQUE TURQUE TR TUR 792 +99209 2 99123 SIBERIE SIBÉRIE +99210 2 99123 TURKESTAN RUSSE TURKESTAN RUSSE +99211 2 99123 KAMTCHATKA KAMTCHATKA +99212 1 AFGHANISTAN ÉTAT ISLAMIQUE D'AFGHANISTAN AF AFG 004 +99213 1 1947 PAKISTAN RÉPUBLIQUE ISLAMIQUE DU PAKISTAN PK PAK 586 +99214 1 BHOUTAN ROYAUME DU BHOUTAN BT BTN 064 +99215 1 NEPAL ROYAUME DU NÉPAL NP NPL 524 +99216 1 CHINE RÉPUBLIQUE POPULAIRE DE CHINE CN CHN 156 +99217 1 JAPON JAPON JP JPN 392 +99218 2 99216 MANDCHOURIE MANDCHOURIE MANDCHOUKOUO +99219 1 THAILANDE ROYAUME DE THAÏLANDE SIAM TH THA 764 +99220 1 1946 PHILIPPINES RÉPUBLIQUE DES PHILIPPINES PH PHL 608 +99221 2 POSSESSIONS BRITANNIQUES AU PROCHE-ORIENT POSSESSIONS BRITANNIQUES AU PROCHE-ORIENT +99222 1 1946 JORDANIE ROYAUME HACHÉMITE DE JORDANIE TRANSJORDANIE JO JOR 400 +99223 3 99232 GOA GOA +99223 1 1947 INDE RÉPUBLIQUE DE L'INDE IN IND 356 +99224 1 1948 BIRMANIE UNION DE BIRMANIE MM MMR 104 +99225 1 1984 BRUNEI NEGARA BRUNEI DARUSSALAM BORNEO BRITANNIQUE BN BRN 096 +99226 1 1965 SINGAPOUR RÉPUBLIQUE DE SINGAPOUR ETABLS. DES DETROITS SG SGP 702 +99227 1 1957 MALAISIE MALAISIE MY MYS 458 +99228 2 99227 ETATS MALAIS NON FEDERES ÉTATS MALAIS NON FÉDÉRÉS +99229 1 1965 MALDIVES RÉPUBLIQUE DES MALDIVES ILES MALDIVES MV MDV 462 +99230 2 99132 99216 HONG-KONG HONG-KONG HK +99231 1 1945 INDONESIE RÉPUBLIQUE D'INDONÉSIE INDES NEER. ET PORT. ID IDN 360 +99232 2 99139 99216 MACAO MACAO MO +99233 2 99251 YEMEN DEMOCRATIQUE YÉMEN DÉMOCRATIQUE +99234 1 98503 1953 CAMBODGE ROYAUME DU CAMBODGE KAMPUCHEA DEM. KH KHM 116 +99235 1 99223 1948 SRI LANKA RÉPUBLIQUE DÉMOCRATIQUE SOCIALISTE DU SRI LANKA CEYLAN LK LKA 144 +99236 4 99217 1945 TAIWAN TAÏWAN FORMOSE TW TWN 158 +99237 2 99217 COREE CORÉE +99238 1 99237 1953 COREE (REPUBLIQUE POPULAIRE DEMOCRATIQUE DE) RÉPUBLIQUE POPULAIRE DÉMOCRATIQUE DE CORÉE KP PRK 408 +99239 1 99237 1945 COREE (REPUBLIQUE DE) RÉPUBLIQUE DE CORÉE KR KOR 410 +99240 1 99221 1961 KOWEIT ÉTAT DU KOWEÏT KW KWT 414 +99241 1 98505 1949 LAOS RÉPUBLIQUE DÉMOCRATIQUE POPULAIRE LAO LA LAO 418 +99242 1 99216 1945 MONGOLIE MONGOLIE MONGOLIE EXTERIEURE MN MNG 496 +99243 1 1976 VIET NAM RÉPUBLIQUE SOCIALISTE DU VIÊT NAM VN VNM 704 +99244 2 98506 99243 1954 VIET NAM DU NORD VIÊT NAM DU NORD TONKIN, ANNAM N. +99245 2 XXXXX 99243 1954 VIET NAM DU SUD VIÊT NAM DU SUD ANNAM S.,COCHINCHINE +99246 1 99213 1972 BANGLADESH RÉPUBLIQUE POPULAIRE DU BANGLADESH PAKISTAN ORIENTAL BD BGD 050 +99247 1 99221 1971 EMIRATS ARABES UNIS ÉMIRATS ARABES UNIS AE ARE 784 +99248 1 99221 1971 QATAR ÉTAT DU QATAR QA QAT 634 +99249 1 99221 1971 BAHREIN ÉTAT DE BAHREÏN BH BHR 048 +99250 1 99221 1971 OMAN SULTANAT D'OMAN MASCATE-ET-OMAN OM OMN 512 +99251 1 1990 YEMEN RÉPUBLIQUE DU YÉMEN YE YEM 887 +99252 1 99123 1991 ARMENIE RÉPUBLIQUE D'ARMÉNIE AM ARM 051 +99253 1 99123 1991 AZERBAIDJAN RÉPUBLIQUE AZERBAÏDJANAISE AZ AZE 031 +99254 1 99143 1960 CHYPRE RÉPUBLIQUE DE CHYPRE CY CYP 196 +99255 1 99123 1991 GEORGIE RÉPUBLIQUE DE GÉORGIE GE GEO 268 +99256 1 99123 1991 KAZAKHSTAN RÉPUBLIQUE DU KAZAKHSTAN KZ KAZ 398 +99257 1 99123 1991 KIRGHIZISTAN RÉPUBLIQUE KIRGHIZE KG KGZ 417 +99258 1 99123 1991 OUZBEKISTAN RÉPUBLIQUE D'OUZBÉKISTAN UZ UZB 860 +99259 1 99123 1991 TADJIKISTAN RÉPUBLIQUE DU TADJIKISTAN TJ TJK 762 +99260 1 99123 1991 TURKMENISTAN TURKMÉNISTAN TM TKM 795 +99261 1 99207 PALESTINE (Etat de) ÉTAT DE PALESTINE PALESTINE (Territoire de) PS PSE 275 +99262 1 99231 2002 TIMOR ORIENTAL RÉPUBLIQUE DÉMOCRATIQUE DU TIMOR ORIENTAL TL TLS 626 +99301 1 EGYPTE RÉPUBLIQUE ARABE D'ÉGYPTE EG EGY 818 +99302 1 LIBERIA RÉPUBLIQUE DU LIBERIA LR LBR 430 +99303 1 AFRIQUE DU SUD RÉPUBLIQUE D'AFRIQUE DU SUD UNION SUD-AFRICAINE ZA ZAF 710 +99304 1 1965 GAMBIE RÉPUBLIQUE DE GAMBIE GM GMB 270 +99305 2 CAMEROUN ET TOGO CAMEROUN ET TOGO +99306 4 99132 SAINTE HELENE, ASCENSION ET TRISTAN DA CUNHA SAINTE-HÉLÈNE, ASCENSION ET TRISTAN DA CUNHA SAINTE HELENE SH SHN 654 +99307 2 SOUDAN ANGLO-EGYPTIEN, KENYA, OUGANDA SOUDAN ANGLO-ÉGYPTIEN, KENYA, OUGANDA +99308 4 99132 OCEAN INDIEN (TERRITOIRE BRITANNIQUE DE L') OCÉAN INDIEN (TERRITOIRE BRITANNIQUE DE L') IO IOT 086 +99308 2 99309 ZANZIBAR ZANZIBAR +99309 1 1961 TANZANIE RÉPUBLIQUE UNIE DE TANZANIE TANGANYIKA, ZANZIBAR TZ TZA 834 +99310 1 1980 ZIMBABWE RÉPUBLIQUE DU ZIMBABWE RHODESIE DU SUD ZW ZWE 716 +99311 1 1990 NAMIBIE RÉPUBLIQUE DE NAMIBIE SUD-OUEST AFRICAIN NA NAM 516 +99312 1 1960 CONGO (REPUBLIQUE DEMOCRATIQUE) RÉPUBLIQUE DÉMOCRATIQUE DU CONGO ZAIRE CD COD 180 +99313 4 99314 99134 CANARIES (ILES) CANARIES (ÎLES) +99313 4 99134 PRESIDES PRÉSIDES +99313 4 99134 PROVINCES ESPAGNOLES D'AFRIQUE PROVINCES ESPAGNOLES D'AFRIQUE +99314 1 1968 GUINEE EQUATORIALE RÉPUBLIQUE DE GUINÉE ÉQUATORIALE GUINEE ESPAGNOLE GQ GNQ 226 +99315 1 ETHIOPIE RÉPUBLIQUE DÉMOCRATIQUE FÉDÉRALE D'ÉTHIOPIE ABYSSINIE ET ETH 231 +99316 1 1951 LIBYE JAMAHIRIYA ARABE LIBYENNE POPULAIRE ET SOCIALISTE CYRENAIQUE LY LBY 434 +99317 1 99315 1993 ERYTHREE ÉTAT D'ÉRYTHRÉE ER ERI 232 +99318 1 99308 1951 SOMALIE SOMALIE SO SOM 706 +99319 4 99139 ACORES, MADERE AÇORES, MADÈRE +99320 2 ILES PORTUGAISES DE L'OCEAN INDIEN ÎLES PORTUGAISES DE L'OCÉAN INDIEN +99321 1 99312 1962 BURUNDI RÉPUBLIQUE DU BURUNDI URUNDI BI BDI 108 +99322 1 99305 1961 CAMEROUN RÉPUBLIQUE DU CAMEROUN CM CMR 120 +99323 1 98303 1960 CENTRAFRICAINE (REPUBLIQUE) RÉPUBLIQUE CENTRAFRICAINE OUBANGUI-CHARI CF CAF 140 +99324 1 98302 1960 CONGO RÉPUBLIQUE DU CONGO CONGO-BRAZZAVILLE CG COG 178 +99325 2 99350 TANGER TANGER +99326 1 98204 1960 COTE D'IVOIRE RÉPUBLIQUE DE CÔTE D'IVOIRE CI CIV 384 +99327 1 98205 1960 BENIN RÉPUBLIQUE DU BÉNIN DAHOMEY BJ BEN 204 +99328 1 98301 1960 GABON RÉPUBLIQUE GABONAISE GA GAB 266 +99329 1 99304 1957 GHANA RÉPUBLIQUE DU GHANA COTE-DE-L'OR GH GHA 288 +99330 1 XXXXX 1958 GUINEE RÉPUBLIQUE DE GUINÉE GUINEE FRANCAISE GN GIN 324 +99331 1 98209 1960 BURKINA BURKINA FASO HAUTE-VOLTA BF BFA 854 +99332 1 99307 1963 KENYA RÉPUBLIQUE DU KENYA KE KEN 404 +99333 1 98401 1960 MADAGASCAR RÉPUBLIQUE DE MADAGASCAR MG MDG 450 +99334 1 99310 1964 MALAWI RÉPUBLIQUE DU MALAWI NYASSALAND MW MWI 454 +99335 1 98206 1960 MALI RÉPUBLIQUE DU MALI SOUDAN FRANCAIS ML MLI 466 +99336 1 98201 1960 MAURITANIE RÉPUBLIQUE ISLAMIQUE DE MAURITANIE MR MRT 478 +99337 1 98207 1960 NIGER RÉPUBLIQUE DU NIGER NE NER 562 +99338 1 99304 1960 NIGERIA RÉPUBLIQUE FÉDÉRALE DU NIGÉRIA NG NGA 566 +99339 1 99307 1962 OUGANDA RÉPUBLIQUE DE L'OUGANDA UG UGA 800 +99340 1 99312 1962 RWANDA RÉPUBLIQUE RWANDAISE RUANDA RW RWA 646 +99341 1 98202 1960 SENEGAL RÉPUBLIQUE DU SÉNÉGAL SN SEN 686 +99342 1 99304 1961 SIERRA LEONE RÉPUBLIQUE DE SIERRA LEONE SL SLE 694 +99343 1 99307 1956 SOUDAN RÉPUBLIQUE DU SOUDAN ANGLO-EGYPTIEN SD SDN 729 +99344 1 98304 1960 TCHAD RÉPUBLIQUE DU TCHAD TD TCD 148 +99345 1 99305 1960 TOGO RÉPUBLIQUE TOGOLAISE TG TGO 768 +99346 1 99310 1964 ZAMBIE RÉPUBLIQUE DE ZAMBIE RHODESIE DU NORD ZM ZMB 894 +99347 1 99310 1966 BOTSWANA RÉPUBLIQUE DU BOTSWANA BECHUANALAND BW BWA 072 +99348 1 99310 1966 LESOTHO ROYAUME DU LESOTHO BASUTOLAND LS LSO 426 +99349 1 99343 2011 SOUDAN DU SUD RÉPUBLIQUE DU SOUDAN DU SUD SS SSD 728 +99350 1 XXXXX 1956 MAROC ROYAUME DU MAROC MA MAR 504 +99351 1 XXXXX 1956 TUNISIE RÉPUBLIQUE TUNISIENNE TN TUN 788 +99352 1 XXXXX 1962 ALGERIE RÉPUBLIQUE ALGÉRIENNE DÉMOCRATIQUE ET POPULAIRE DZ DZA 012 +99389 4 SAHARA OCCIDENTAL SAHARA OCCIDENTAL EH ESH 732 +99390 1 99308 1968 MAURICE RÉPUBLIQUE DE MAURICE MU MUS 480 +99391 1 99310 1968 SWAZILAND ROYAUME DU SWAZILAND SZ SWZ 748 +99392 1 99319 1974 GUINEE-BISSAU RÉPUBLIQUE DE GUINÉE-BISSAU GUINEE PORTUGAISE GW GNB 624 +99393 1 99320 1975 MOZAMBIQUE RÉPUBLIQUE DU MOZAMBIQUE MZ MOZ 508 +99394 1 99319 1975 SAO TOME-ET-PRINCIPE RÉPUBLIQUE DÉMOCRATIQUE DE SAO TOMÉ-ET-PRINCIPE ST STP 678 +99395 1 99319 1975 ANGOLA RÉPUBLIQUE D'ANGOLA AO AGO 024 +99396 1 99319 1975 CAP-VERT RÉPUBLIQUE DU CAP-VERT CV CPV 132 +99397 1 98402 1975 COMORES RÉPUBLIQUE FÉDÉRALE ISLAMIQUE DES COMORES KM COM 174 +99398 1 99308 1976 SEYCHELLES RÉPUBLIQUE DES SEYCHELLES SC SYC 690 +99399 1 98406 1977 DJIBOUTI RÉPUBLIQUE DE DJIBOUTI T.F. AFARS ET ISSAS DJ DJI 262 +99401 1 CANADA CANADA CA CAN 124 +99402 2 99401 TERRE-NEUVE TERRE-NEUVE +99403 2 99401 LABRADOR LABRADOR +99404 3 99432 ALASKA ALASKA +99404 1 ETATS-UNIS ÉTATS-UNIS D'AMÉRIQUE US USA 840 +99405 1 MEXIQUE ÉTATS-UNIS DU MEXIQUE MX MEX 484 +99406 1 COSTA RICA RÉPUBLIQUE DU COSTA RICA CR CRI 188 +99407 1 CUBA RÉPUBLIQUE DE CUBA CU CUB 192 +99408 1 DOMINICAINE (REPUBLIQUE) RÉPUBLIQUE DOMINICAINE SAINT-DOMINGUE DO DOM 214 +99409 1 GUATEMALA RÉPUBLIQUE DU GUATEMALA GT GTM 320 +99410 1 HAITI RÉPUBLIQUE D'HAÏTI HT HTI 332 +99411 1 HONDURAS RÉPUBLIQUE DU HONDURAS HN HND 340 +99412 1 NICARAGUA RÉPUBLIQUE DU NICARAGUA NI NIC 558 +99413 1 PANAMA RÉPUBLIQUE DU PANAMA PA PAN 591 +99414 1 EL SALVADOR RÉPUBLIQUE DU SALVADOR SV SLV 222 +99415 1 ARGENTINE RÉPUBLIQUE ARGENTINE AR ARG 032 +99416 1 BRESIL RÉPUBLIQUE FÉDÉRATIVE DU BRÉSIL BR BRA 076 +99417 1 CHILI RÉPUBLIQUE DU CHILI CL CHL 152 +99418 1 BOLIVIE ÉTAT PLURINATIONAL DE BOLIVIE REPUBLIQUE DE BOLIVIE BO BOL 068 +99419 1 COLOMBIE RÉPUBLIQUE DE COLOMBIE CO COL 170 +99420 1 EQUATEUR RÉPUBLIQUE DE L'ÉQUATEUR EC ECU 218 +99421 1 PARAGUAY RÉPUBLIQUE DU PARAGUAY PY PRY 600 +99422 1 PEROU RÉPUBLIQUE DU PÉROU PE PER 604 +99423 1 URUGUAY RÉPUBLIQUE ORIENTALE DE L'URUGUAY UY URY 858 +99424 1 VENEZUELA RÉPUBLIQUE BOLIVARIENNE DU VENEZUELA REPUBLIQUE DU VENEZUELA VE VEN 862 +99425 4 99426 99132 BERMUDES BERMUDES BM BMU 060 +99425 4 99132 ANGUILLA ANGUILLA AI AIA 660 +99425 4 99426 99132 CAIMANES (ILES) CAÏMANES (ÎLES) KY CYM 136 +99425 4 99132 VIERGES BRITANNIQUES (ILES) VIERGES BRITANNIQUES (ÎLES) VG VGB 092 +99425 4 99132 TURKS ET CAIQUES (ILES) TURKS ET CAÏQUES (ÎLES) TC TCA 796 +99425 4 99132 TERRITOIRES DU ROYAUME-UNI AUX ANTILLES TERRITOIRES DU ROYAUME-UNI AUX ANTILLES +99425 4 99132 MONTSERRAT MONTSERRAT MS MSR 500 +99426 1 1962 JAMAIQUE JAMAÏQUE JM JAM 388 +99427 4 99132 MALOUINES, OU FALKLAND (ILES) MALOUINES, OU FALKLAND (ÎLES) FK FLK 238 +99427 4 99132 TERR. DU ROYAUME-UNI DANS L'ATLANTIQUE SUD TERR. DU ROYAUME-UNI DANS L'ATLANTIQUE_SUD +99427 4 99132 GEORGIE DU SUD ET LES ILES SANDWICH DU SUD GÉORGIE DU SUD ET LES ÎLES SANDWICH DU SUD GS SGS 239 +99428 1 1966 GUYANA RÉPUBLIQUE COOPÉRATIVE DE GUYANA GUYANE BRITANNIQUE GY GUY 328 +99429 1 1981 BELIZE BELIZE HONDURAS BRITANNIQUE BZ BLZ 084 +99430 4 99101 GROENLAND GROENLAND GL GRL 304 +99431 2 99135 ANTILLES NEERLANDAISES ANTILLES NÉERLANDAISES AN +99432 4 99404 PORTO RICO PORTO RICO PR PRI 630 +99432 4 99404 TERR. DES ETATS-UNIS D'AMERIQUE EN AMERIQUE TERR. DES ÉTATS-UNIS D'AMÉRIQUE EN AMÉRIQUE +99432 4 99404 VIERGES DES ETATS-UNIS (ILES) VIERGES DES ÉTATS-UNIS (ÎLES) VI VIR 850 +99433 1 99426 1962 TRINITE-ET-TOBAGO RÉPUBLIQUE DE TRINITÉ-ET-TOBAGO TT TTO 780 +99434 1 99425 1966 BARBADE BARBADE BB BRB 052 +99435 1 99425 1974 GRENADE GRENADE GD GRD 308 +99436 1 99425 1973 BAHAMAS COMMONWEALTH DES BAHAMAS ILES LUCAYES BS BHS 044 +99437 1 99431 1975 SURINAME RÉPUBLIQUE DU SURINAME GUYANE NEERLANDAISE SR SUR 740 +99438 1 99425 1978 DOMINIQUE COMMONWEALTH DE DOMINIQUE DM DMA 212 +99439 1 99425 1979 SAINTE-LUCIE SAINTE-LUCIE LC LCA 662 +99440 1 99425 1979 SAINT-VINCENT-ET-LES GRENADINES SAINT-VINCENT-ET-LES GRENADINES VC VCT 670 +99441 1 99425 1981 ANTIGUA-ET-BARBUDA ANTIGUA-ET-BARBUDA AG ATG 028 +99442 1 99425 1983 SAINT-CHRISTOPHE-ET-NIEVES FÉDÉRATION DE SAINT-CHRISTOPHE-ET-NIÉVÈS KN KNA 659 +99443 1 99431 2010 BONAIRE, SAINT EUSTACHE ET SABA BONAIRE, SAINT EUSTACHE ET SABA BQ BES 535 +99444 1 99431 2010 CURAÇAO CURAÇAO CW CUW 531 +99445 1 99431 2010 SAINT-MARTIN (PARTIE NEERLANDAISE) SAINT-MARTIN (PARTIE NÉERLANDAISE) SX SXM 534 +99501 3 99229 CHRISTMAS (ILE) CHRISTMAS (ÎLE) CX CXR 162 +99501 1 AUSTRALIE AUSTRALIE AU AUS 036 +99501 3 99229 COCOS ou KEELING (ILES) COCOS OU KEELING (ÎLES) CC CCK 166 +99501 3 HEARD ET MACDONALD (ILES) HEARD ET MACDONALD (ÎLES) HM HMD 334 +99501 3 99503 NORFOLK (ILE) NORFOLK (ÎLE) NF NFK 574 +99502 3 99503 COOK (ILES) COOK (ÎLES) CK COK 184 +99502 3 TOKELAU TOKELAU TK TKL 772 +99502 3 NIUE NIUE NU NIU 570 +99502 1 NOUVELLE-ZELANDE NOUVELLE-ZÉLANDE NZ NZL 554 +99503 4 99132 PITCAIRN (ILE) PITCAIRN (ÎLE) PN PCN 612 +99504 2 99404 HAWAII (ILES) HAWAII (ÎLES) +99505 4 99404 MARIANNES DU NORD (ILES) MARIANNES DU NORD (ÎLES) MP MNP 580 +99505 4 99220 99404 GUAM GUAM GU GUM 316 +99505 4 99404 SAMOA AMERICAINES SAMOA AMÉRICAINES AS ASM 016 +99505 4 99404 TERR. DES ETATS-UNIS D'AMERIQUE EN OCEANIE TERR. DES ÉTATS-UNIS D'AMÉRIQUE EN OCÉANIE +99506 1 99505 1962 SAMOA OCCIDENTALES ÉTAT INDÉPENDANT DES SAMOA OCCIDENTALES WS WSM 882 +99507 1 99501 1968 NAURU RÉPUBLIQUE DE NAURU NR NRU 520 +99508 1 99503 1970 FIDJI RÉPUBLIQUE DES FIDJI FJ FJI 242 +99509 1 99503 1970 TONGA ROYAUME DES TONGA TO TON 776 +99510 1 99501 1975 PAPOUASIE-NOUVELLE-GUINEE PAPOUASIE-NOUVELLE-GUINÉE PG PNG 598 +99511 1 99503 1978 TUVALU TUVALU ILES ELLICE TV TUV 798 +99512 1 99503 1978 SALOMON (ILES) ÎLES SALOMON ILES SALOMON DU SUD SB SLB 090 +99513 1 1979 KIRIBATI RÉPUBLIQUE DE KIRIBATI ILES GILBERT KI KIR 296 +99514 1 99503 1980 VANUATU RÉPUBLIQUE DE VANUATU NOUVELLES-HEBRIDES VU VUT 548 +99515 1 99505 1991 MARSHALL (ILES) RÉPUBLIQUE DES ÎLES MARSHALL MH MHL 584 +99516 1 99505 1991 MICRONESIE (ETATS FEDERES DE) ÉTATS FÉDÉRÉS DE MICRONÉSIE FM FSM 583 +99517 1 99404 1994 PALAOS (ILES) RÉPUBLIQUE DES ÎLES PALAOS BELAU PW PLW 585 diff --git a/lib/countries.js b/lib/countries.js new file mode 100644 index 0000000..a1811ed --- /dev/null +++ b/lib/countries.js @@ -0,0 +1,23 @@ +const SearchableCollection = require('./searchableCollection'); + + +const schema = { + nom: { + type: 'text', + queryWith: 'nom', + ref: 'code', + }, + code: { type: 'token', queryWith: 'code' }, +}; + +function getIndexedDb(options = {}) { + /* Source dataset */ + const countries = options.countries || require(options.countriesDbPath || '../data/countries.json'); + + const searchableCollection = new SearchableCollection(schema); + searchableCollection.load(countries); + + return searchableCollection; +} + +module.exports = { getIndexedDb }; diff --git a/lib/countryHelpers.js b/lib/countryHelpers.js new file mode 100644 index 0000000..e4769c0 --- /dev/null +++ b/lib/countryHelpers.js @@ -0,0 +1,8 @@ +const { initFields } = require('./helpers'); + +const initCountryFields = initFields({ + default: ['nom', 'code', 'iso2', 'iso3', 'territories'], + base: ['nom', 'code'], +}); + +module.exports = { initCountryFields }; diff --git a/lib/integration/countries.js b/lib/integration/countries.js new file mode 100644 index 0000000..0e26edd --- /dev/null +++ b/lib/integration/countries.js @@ -0,0 +1,107 @@ +const fs = require('fs'); +const parse = require('csv-parse'); +const t = require('through2').obj; +const iconv = require('iconv-lite'); +const JSONStream = require('JSONStream'); +const streamify = require('stream-array'); + +/* Initialisation */ +function init(ctx, next) { + ctx.countries = new Map(); + + ctx.getCountry = (code) => { + if (!ctx.countries.has(code)) { + const country = { code }; + ctx.countries.set(code, country); + } + return ctx.countries.get(code); + }; + + ctx.getTerritories = (country, srcPath, callBack) => { + country.territories = []; + + fs.createReadStream(srcPath) + .pipe(iconv.decodeStream('win1252')) + .on('error', next) + .pipe(parse({ delimiter: '\t', columns: true })) + .on('error', next) + .pipe(t((data, enc, cb) => { + if (data.COG === country.code && data.ACTUAL == 3) { + const territory = { nom: data.LIBCOG }; + country.territories.push(territory); + } + cb(); + })) + .on('error', next) + .on('finish', () => { + // if (territories.length) console.log(`Ajout de ${territories.length} territore pour ${code} !`); + callBack(); + }); + }; + + next(); +} + +/* Chargement des pays */ +function loadCountries(options = {}) { + return function (ctx, next) { + ctx.debug('Chargement du jeu de données pays2016 '); + let count = 0; + const srcPath = options.srcPath || __dirname + '/../../forced_data/pays2016.tsv'; + + fs.createReadStream(srcPath) + .pipe(iconv.decodeStream('win1252')) + .on('error', next) + .pipe(parse({ delimiter: '\t', columns: true })) + .on('error', next) + .pipe(t((data, enc, cb) => { + const code = data.COG; + if (code !== 'XXXXX' && data.ACTUAL !== 3) { + const country = ctx.getCountry(code); + country.nom = data.LIBCOG; + country.iso2 = data.CODEISO2; + country.iso3 = data.CODEISO3; + count++; + ctx.getTerritories(country, srcPath, cb); + } else { + cb(); + } + })) + .on('error', next) + .on('finish', () => { + ctx.debug('Nombre de pays chargées : %d', count); + next(); + }); + }; +} + +/* Sérialisation */ +function serialize(options = {}) { + return function (ctx, next) { + ctx.debug('Sérialisation des données'); + let count = 0; + + if (ctx.countries.size < 1) { + return next(new Error('No country')); + } else { + streamify(Array.from(ctx.countries.values())) + .on('error', next) + .pipe(t((dep, enc, cb) => { + count++; + cb(null, dep); + })) + .on('error', next) + .pipe(JSONStream.stringify()) + .on('error', next) + .pipe(fs.createWriteStream(options.destPath || __dirname + '/../../data/countries.json')) + .on('error', next) + .on('finish', () => { + ctx.debug('Nombre de pays écrits : %d', count); + next(); + }); + } + }; +} + +/* Exports */ +module.exports = { init, serialize, loadCountries }; diff --git a/lib/integration/index.js b/lib/integration/index.js index ae6b823..3a8806b 100644 --- a/lib/integration/index.js +++ b/lib/integration/index.js @@ -1,6 +1,7 @@ const departements = require('./departements') const communes = require('./communes') const regions = require('./regions') +const countries = require('./countries') const pipeline = require('./pipeline') /* Pipeline */ @@ -24,7 +25,11 @@ function integrate(done) { // Régions regions.init, regions.loadRegions(), - regions.serialize() + regions.serialize(), + // Pays + countries.init, + countries.loadCountries(), + countries.serialize(), ], done) } diff --git a/server.js b/server.js index 4b9a568..29e7784 100644 --- a/server.js +++ b/server.js @@ -4,10 +4,12 @@ const morgan = require('morgan') const {initCommuneFields, initCommuneFormat} = require('./lib/communeHelpers') const {initDepartementFields} = require('./lib/departementHelpers') const {initRegionFields} = require('./lib/regionHelpers') +const {initCountryFields} = require('./lib/countryHelpers') const {formatOne, initLimit} = require('./lib/helpers') const dbCommunes = require('./lib/communes').getIndexedDb() const dbDepartements = require('./lib/departements').getIndexedDb() const dbRegions = require('./lib/regions').getIndexedDb() +const dbCountries = require('./lib/countries').getIndexedDb() const {pick} = require('lodash') const app = express() @@ -19,7 +21,8 @@ app.use((req, res, next) => { req.db = { communes: dbCommunes, departements: dbDepartements, - regions: dbRegions + regions: dbRegions, + countries: dbCountries, } next() }) @@ -136,6 +139,28 @@ app.get('/regions/:code/departements', initLimit(), initDepartementFields, (req, } }) +/* Pays */ +app.get('/pays', initLimit(), initCountryFields, (req, res) => { + const query = pick(req.query, 'code', 'nom'); + + if (query.nom) req.fields.add('_score'); + + res.send( + dbCountries + .search(query) + .map(country => formatOne(req, country)) + ); +}); + +app.get('/pays/:code', initCountryFields, (req, res) => { + const countries = dbCountries.search({ code: req.params.code }); + if (countries.length === 0) { + res.sendStatus(404); + } else { + res.send(formatOne(req, countries[0])); + } +}); + /* Definition */ app.get('/definition.yml', (req, res) => { res.sendFile(__dirname + '/definition.yml') From 33b327c0c2ce80bc9eaf32948d927f59c3f83478 Mon Sep 17 00:00:00 2001 From: tmerlier Date: Thu, 2 Feb 2017 18:36:25 +0100 Subject: [PATCH 2/4] Add coutries test --- test/countries.js | 68 ++++++++ test/countryHelpers.js | 44 +++++ test/countryIntegration.js | 158 ++++++++++++++++++ .../countries-territories.tsv | 7 + test/integration-data/countries.tsv | 2 + 5 files changed, 279 insertions(+) create mode 100644 test/countries.js create mode 100644 test/countryHelpers.js create mode 100644 test/countryIntegration.js create mode 100644 test/integration-data/countries-territories.tsv create mode 100644 test/integration-data/countries.tsv diff --git a/test/countries.js b/test/countries.js new file mode 100644 index 0000000..fedbd93 --- /dev/null +++ b/test/countries.js @@ -0,0 +1,68 @@ +/* eslint-env mocha */ +const expect = require('expect.js'); +const countries = require('../lib/countries'); +const { cloneDeep } = require('lodash'); + +describe('countries', function () { + let db; + const country1 = { nom: 'one', code: '11' }; + const country2 = { nom: 'two', code: '22' }; + const country3 = { nom: 'three', code: '33' }; + + beforeEach(done => { + db = countries.getIndexedDb({ countries: [country1, country2, country3].map(cloneDeep) }); + done(); + }); + + describe('getIndexedDb()', function () { + describe('bad countries db path', function () { + it('should throw an error', function () { + expect(() => countries.getIndexedDb({ countriesDbPath: '_' })).to.throwError(); + }); + }); + }); + + describe('indexes', function () { + describe('indexes list', function () { + const db = countries.getIndexedDb({ countries: [] }); + [ + 'nom', + 'code', + ].forEach(index => { + it(`should contain '${index}' index`, () => { + expect(db._indexes).to.have.key(index); + }); + }); + }); + }); + + describe('search()', function () { + describe('No criteria', function () { + it('should return everything', function () { + expect(db.search()).to.eql([country1, country2, country3]); + }); + }); + describe('Simple matching criteria', function () { + it('should return an array with 1 country', function () { + expect(db.search({ code: '11' })).to.eql([country1]); + }); + }); + describe('Disjoint criteria', function () { + it('should return an empty array', function () { + expect(db.search({ nom: 'three', code: '22' })).to.eql([]); + }); + }); + describe('All criteria', function () { + it('should return an array with 1 country', function () { + expect(db.search({ nom: 'three', code: '33' })).to.eql([ + { nom: 'three', code: '33', _score: 1 }, + ]); + db.search({ nom: 'three', code: '33' }).forEach(reg => { + expect(reg).to.have.key('_score'); + expect(reg._score >= 0).to.be.ok(); + }); + }); + }); + }); + +}); diff --git a/test/countryHelpers.js b/test/countryHelpers.js new file mode 100644 index 0000000..b23a1cd --- /dev/null +++ b/test/countryHelpers.js @@ -0,0 +1,44 @@ +/* eslint-env mocha */ +const expect = require('expect.js'); +const { initCountryFields } = require('../lib/countryHelpers'); + +describe('countryHelpers', function () { + + describe('initCountryFields()', function () { + function runTestCase(reqParams, expectedFields, done) { + const req = { query: reqParams.query ? reqParams.query : {} }; + initCountryFields(req, undefined, function (err) { + expect(err).to.be(undefined); + expect(req.fields).to.be.a(Set); + expect(Array.from(req.fields).sort()).to.eql(expectedFields.sort()); + done(); + }); + } + + // 3 tests volontairement identiques en attendant les futures évolutions + it('empty request should return default fields', function (done) { + runTestCase( + {}, + ['nom', 'code', 'iso2', 'iso3', 'territories'], + done + ); + }); + + it('fields should be read from query', function (done) { + runTestCase( + { query: { fields: 'nom,code' } }, + ['nom', 'code'], + done + ); + }); + + it('`nom` and `code` should always be present', function (done) { + runTestCase( + { query: { fields: 'nom' } }, + ['nom', 'code'], + done + ); + }); + }); + +}); diff --git a/test/countryIntegration.js b/test/countryIntegration.js new file mode 100644 index 0000000..e13754b --- /dev/null +++ b/test/countryIntegration.js @@ -0,0 +1,158 @@ +/* eslint-env mocha */ +const { init, serialize, loadCountries } = require('../lib/integration/countries'); +const expect = require('expect.js'); + +describe('#integration countries', () => { + + describe('init()', () => { + let ctx; + beforeEach(done => { + ctx = {}; + init(ctx, done); + }); + + describe('Context setup', () => { + it('should set ctx.countries as an empty Map', () => { + expect(ctx.countries).to.be.a(Map); + expect(ctx.countries.size).to.be(0); + }); + it('should set ctx.getCountry function', + () => expect(ctx.getCountry).to.be.a(Function)); + it('should set ctx.getTerritories function', + () => expect(ctx.getTerritories).to.be.a(Function)); + }); + + describe('getCountry()', () => { + describe('New country', () => { + beforeEach(() => { + expect(ctx.countries.size).to.be(0); + }); + it('should return a country with given code', () => { + const country = ctx.getCountry('42'); + expect(country).to.be.an(Object); + expect(country).to.only.have.keys('code'); + expect(country.code).to.be('42'); + }); + it('should store the country', () => { + ctx.getCountry('21'); + expect(ctx.countries.size).to.be(1); + expect(ctx.countries.has('21')).to.be.ok(); + const country = ctx.countries.get('21'); + expect(country).to.only.have.keys('code'); + expect(country.code).to.be('21'); + }); + }); + + describe('Existing country', () => { + beforeEach(() => { + ctx.getCountry('11'); + expect(ctx.countries.size).to.be(1); + }); + it('should return a country with given code', () => { + const country = ctx.getCountry('11'); + expect(country).to.be.an(Object); + expect(country).to.only.have.keys('code'); + expect(country.code).to.be('11'); + }); + it('should have no impact on storage', () => { + ctx.getCountry('11'); + expect(ctx.countries.has('11')).to.be.ok(); + expect(ctx.countries.size).to.be(1); + }); + }); + }); + + describe('getTerritories()', () => { + describe('Country has no territory', () => { + it('should assign an empty array to country.territories', () => { + const country = ctx.getCountry('42'); + const srcPath = __dirname + '/integration-data/countries.tsv'; + ctx.getTerritories(country, srcPath, () => expect(country.territories).to.eql([])); + }); + }); + + describe('Country has several territories', () => { + it('should assign the 3 territories that have the same code as the country.', () => { + const country = ctx.getCountry('123456'); + const srcPath = __dirname + '/integration-data/countries-territories.tsv'; + ctx.getTerritories(country, srcPath, () => true); + expect(country.territories).to.eql([ + { nom: 'TEST1' }, + { nom: 'TEST2' }, + { nom: 'TEST3' }, + ]); + }); + }); + }); + }); + + describe('loadCountries()', () => { + let ctx; + let country; + beforeEach(() => { + country = { }; + ctx = { + debug: () => {}, + getCountry: code => { + country.code = code; + return country; + }, + getTerritories: (country, srcPath, callBack) => { + country.territories = []; + callBack() ; + }, + }; + }); + + describe('Processing a file containing 1 country', () => { + it('should store 1 country', done => { + loadCountries({ srcPath: __dirname + '/integration-data/countries.tsv' })(ctx, err => { + expect(err).to.be(undefined); + expect(country).to.eql({ + code: '123456', + nom: 'TEST', + iso2: 'TS', + iso3: 'TST', + territories: [], + }); + done(); + }); + }); + }); + }); + + describe('serialize()', () => { + describe('No country', () => { + it('should throw an error', done => { + const ctx = { debug: () => {}, countries: new Map() }; + serialize()(ctx, err => { + expect(err).to.be.an(Error); + expect(err.message).to.be('No country'); + done(); + }); + }); + }); + + describe('One country', () => { + const path = '../data/test-serialize-country.json'; + it('should generate a JSON file with one country inside', done => { + const ctx = { debug: () => {}, countries: new Map([ + ['42', { + code: '42', + nom: 'Test', + }], + ]) }; + serialize({ destPath: __dirname + '/' + path })(ctx, err => { + expect(err).to.be(undefined); + const countries = require(path); + expect(countries).to.have.length(1); + expect(countries[0]).to.eql({ + code: '42', + nom: 'Test', + }); + done(); + }); + }); + }); + }); +}); diff --git a/test/integration-data/countries-territories.tsv b/test/integration-data/countries-territories.tsv new file mode 100644 index 0000000..2a5f90a --- /dev/null +++ b/test/integration-data/countries-territories.tsv @@ -0,0 +1,7 @@ +COG ACTUAL CAPAY CRPAY ANI LIBCOG LIBENR ANCNOM CODEISO2 CODEISO3 CODENUM3 +123456 1 TEST TEST TS TST 250 +123456 3 TEST1 TEST1 T1 TS1 251 +123456 3 TEST2 TEST2 T2 TS2 252 +123456 3 TEST3 TEST3 T3 TS3 253 +654321 2 OTHER OTHER OT OTH 254 +615243 1 TRAP TRAP TP TRP 255 diff --git a/test/integration-data/countries.tsv b/test/integration-data/countries.tsv new file mode 100644 index 0000000..ae5bc73 --- /dev/null +++ b/test/integration-data/countries.tsv @@ -0,0 +1,2 @@ +COG ACTUAL CAPAY CRPAY ANI LIBCOG LIBENR ANCNOM CODEISO2 CODEISO3 CODENUM3 +123456 1 TEST TEST TS TST 250 From 01ebf2088344cd4d1b0e5bb8e1185f467d14940d Mon Sep 17 00:00:00 2001 From: tmerlier Date: Mon, 6 Feb 2017 17:17:51 +0100 Subject: [PATCH 3/4] Add ISO 3166-1 code and improuved territories loading --- lib/countryHelpers.js | 2 +- lib/integration/countries.js | 46 +++++---- lib/integration/index.js | 1 + test/countryHelpers.js | 2 +- test/countryIntegration.js | 94 ++++++++++++------- .../countries-territories.tsv | 1 - 6 files changed, 93 insertions(+), 53 deletions(-) diff --git a/lib/countryHelpers.js b/lib/countryHelpers.js index e4769c0..1a94d09 100644 --- a/lib/countryHelpers.js +++ b/lib/countryHelpers.js @@ -1,7 +1,7 @@ const { initFields } = require('./helpers'); const initCountryFields = initFields({ - default: ['nom', 'code', 'iso2', 'iso3', 'territories'], + default: ['nom', 'code', 'iso2', 'iso3', 'num', 'territories'], base: ['nom', 'code'], }); diff --git a/lib/integration/countries.js b/lib/integration/countries.js index 0e26edd..f6e85ce 100644 --- a/lib/integration/countries.js +++ b/lib/integration/countries.js @@ -11,14 +11,21 @@ function init(ctx, next) { ctx.getCountry = (code) => { if (!ctx.countries.has(code)) { - const country = { code }; + const country = { code, territories: new Set() }; ctx.countries.set(code, country); } return ctx.countries.get(code); }; - ctx.getTerritories = (country, srcPath, callBack) => { - country.territories = []; + next(); +} + +/* Chargement des territoires */ +function loadTerritories(options = {}) { + return function (ctx, next) { + ctx.debug('Chargement du jeu de données pays2016 '); + let count = 0; + const srcPath = options.srcPath || __dirname + '/../../forced_data/pays2016.tsv'; fs.createReadStream(srcPath) .pipe(iconv.decodeStream('win1252')) @@ -26,20 +33,25 @@ function init(ctx, next) { .pipe(parse({ delimiter: '\t', columns: true })) .on('error', next) .pipe(t((data, enc, cb) => { - if (data.COG === country.code && data.ACTUAL == 3) { - const territory = { nom: data.LIBCOG }; - country.territories.push(territory); + const code = data.COG; + if (data.ACTUAL === '3') { + const territory = { + nom: data.LIBCOG, + iso2: data.CODEISO2, + iso3: data.CODEISO2, + num: data.CODENUM3, + }; + ctx.getCountry(code).territories.add(territory); + count++; } cb(); })) .on('error', next) .on('finish', () => { - // if (territories.length) console.log(`Ajout de ${territories.length} territore pour ${code} !`); - callBack(); + ctx.debug('Nombre de territoires chargées : %d', count); + next(); }); }; - - next(); } /* Chargement des pays */ @@ -56,16 +68,15 @@ function loadCountries(options = {}) { .on('error', next) .pipe(t((data, enc, cb) => { const code = data.COG; - if (code !== 'XXXXX' && data.ACTUAL !== 3) { + if (code !== 'XXXXX' && data.ACTUAL !== '3') { const country = ctx.getCountry(code); country.nom = data.LIBCOG; country.iso2 = data.CODEISO2; country.iso3 = data.CODEISO3; + country.num = data.CODENUM3; count++; - ctx.getTerritories(country, srcPath, cb); - } else { - cb(); } + cb(); })) .on('error', next) .on('finish', () => { @@ -86,9 +97,10 @@ function serialize(options = {}) { } else { streamify(Array.from(ctx.countries.values())) .on('error', next) - .pipe(t((dep, enc, cb) => { + .pipe(t((country, enc, cb) => { + country.territories = Array.from(country.territories).sort(); count++; - cb(null, dep); + cb(null, country); })) .on('error', next) .pipe(JSONStream.stringify()) @@ -104,4 +116,4 @@ function serialize(options = {}) { } /* Exports */ -module.exports = { init, serialize, loadCountries }; +module.exports = { init, serialize, loadCountries, loadTerritories }; diff --git a/lib/integration/index.js b/lib/integration/index.js index 3a8806b..ec212ea 100644 --- a/lib/integration/index.js +++ b/lib/integration/index.js @@ -29,6 +29,7 @@ function integrate(done) { // Pays countries.init, countries.loadCountries(), + countries.loadTerritories(), countries.serialize(), ], done) } diff --git a/test/countryHelpers.js b/test/countryHelpers.js index b23a1cd..ac6b66f 100644 --- a/test/countryHelpers.js +++ b/test/countryHelpers.js @@ -19,7 +19,7 @@ describe('countryHelpers', function () { it('empty request should return default fields', function (done) { runTestCase( {}, - ['nom', 'code', 'iso2', 'iso3', 'territories'], + ['nom', 'code', 'iso2', 'iso3', 'num', 'territories'], done ); }); diff --git a/test/countryIntegration.js b/test/countryIntegration.js index e13754b..ec17c59 100644 --- a/test/countryIntegration.js +++ b/test/countryIntegration.js @@ -1,5 +1,5 @@ /* eslint-env mocha */ -const { init, serialize, loadCountries } = require('../lib/integration/countries'); +const { init, serialize, loadCountries, loadTerritories } = require('../lib/integration/countries'); const expect = require('expect.js'); describe('#integration countries', () => { @@ -16,10 +16,6 @@ describe('#integration countries', () => { expect(ctx.countries).to.be.a(Map); expect(ctx.countries.size).to.be(0); }); - it('should set ctx.getCountry function', - () => expect(ctx.getCountry).to.be.a(Function)); - it('should set ctx.getTerritories function', - () => expect(ctx.getTerritories).to.be.a(Function)); }); describe('getCountry()', () => { @@ -30,7 +26,8 @@ describe('#integration countries', () => { it('should return a country with given code', () => { const country = ctx.getCountry('42'); expect(country).to.be.an(Object); - expect(country).to.only.have.keys('code'); + expect(country).to.have.keys('code'); + expect(country).to.have.keys('territories'); expect(country.code).to.be('42'); }); it('should store the country', () => { @@ -38,7 +35,8 @@ describe('#integration countries', () => { expect(ctx.countries.size).to.be(1); expect(ctx.countries.has('21')).to.be.ok(); const country = ctx.countries.get('21'); - expect(country).to.only.have.keys('code'); + expect(country).to.have.keys('code'); + expect(country).to.have.keys('territories'); expect(country.code).to.be('21'); }); }); @@ -51,7 +49,8 @@ describe('#integration countries', () => { it('should return a country with given code', () => { const country = ctx.getCountry('11'); expect(country).to.be.an(Object); - expect(country).to.only.have.keys('code'); + expect(country).to.have.keys('code'); + expect(country).to.have.keys('territories'); expect(country.code).to.be('11'); }); it('should have no impact on storage', () => { @@ -61,29 +60,6 @@ describe('#integration countries', () => { }); }); }); - - describe('getTerritories()', () => { - describe('Country has no territory', () => { - it('should assign an empty array to country.territories', () => { - const country = ctx.getCountry('42'); - const srcPath = __dirname + '/integration-data/countries.tsv'; - ctx.getTerritories(country, srcPath, () => expect(country.territories).to.eql([])); - }); - }); - - describe('Country has several territories', () => { - it('should assign the 3 territories that have the same code as the country.', () => { - const country = ctx.getCountry('123456'); - const srcPath = __dirname + '/integration-data/countries-territories.tsv'; - ctx.getTerritories(country, srcPath, () => true); - expect(country.territories).to.eql([ - { nom: 'TEST1' }, - { nom: 'TEST2' }, - { nom: 'TEST3' }, - ]); - }); - }); - }); }); describe('loadCountries()', () => { @@ -97,7 +73,7 @@ describe('#integration countries', () => { country.code = code; return country; }, - getTerritories: (country, srcPath, callBack) => { + loadTerritories: (country, srcPath, callBack) => { country.territories = []; callBack() ; }, @@ -113,7 +89,7 @@ describe('#integration countries', () => { nom: 'TEST', iso2: 'TS', iso3: 'TST', - territories: [], + num: '250', }); done(); }); @@ -121,6 +97,56 @@ describe('#integration countries', () => { }); }); + describe('loadTerritories()', () => { + describe('Processing a file containing a relation with known COG code', () => { + it('should associate territories to the country', done => { + const country = { code: '123456', territories: new Set() }; + const ctx = { + countries: { has: () => true }, + debug: () => {}, + getCountry: () => country, + }; + + loadTerritories({ srcPath: __dirname + '/integration-data/countries-territories.tsv' })(ctx, err => { + expect(err).to.be(undefined); + expect(Array.from(country.territories)).to.eql( + [ + { + 'iso2': 'T1', + 'iso3': 'T1', + 'nom': 'TEST1', + 'num': '251', + }, + { + 'iso2': 'T2', + 'iso3': 'T2', + 'nom': 'TEST2', + 'num': '252', + }, + ]); + done(); + }); + }); + }); + + describe('Country has no territories', () => { + it('should not associate any territory', done => { + const country = { code: '654321', territories: new Set() }; + const ctx = { + countries: { has: () => true }, + debug: () => {}, + getCountry: (code) => code === country.code ? country : { territories: new Set() }, + }; + + loadTerritories({ srcPath: __dirname + '/integration-data/countries-territories.tsv' })(ctx, err => { + expect(err).to.be(undefined); + expect(Array.from(country.territories)).to.eql([]); + done(); + }); + }); + }); + }); + describe('serialize()', () => { describe('No country', () => { it('should throw an error', done => { @@ -140,6 +166,7 @@ describe('#integration countries', () => { ['42', { code: '42', nom: 'Test', + territories: new Set(), }], ]) }; serialize({ destPath: __dirname + '/' + path })(ctx, err => { @@ -149,6 +176,7 @@ describe('#integration countries', () => { expect(countries[0]).to.eql({ code: '42', nom: 'Test', + territories: [], }); done(); }); diff --git a/test/integration-data/countries-territories.tsv b/test/integration-data/countries-territories.tsv index 2a5f90a..5180609 100644 --- a/test/integration-data/countries-territories.tsv +++ b/test/integration-data/countries-territories.tsv @@ -2,6 +2,5 @@ COG ACTUAL CAPAY CRPAY ANI LIBCOG LIBENR ANCNOM CODEISO2 CODEISO3 CODENUM3 123456 1 TEST TEST TS TST 250 123456 3 TEST1 TEST1 T1 TS1 251 123456 3 TEST2 TEST2 T2 TS2 252 -123456 3 TEST3 TEST3 T3 TS3 253 654321 2 OTHER OTHER OT OTH 254 615243 1 TRAP TRAP TP TRP 255 From 80f83c84eb415f87100b20c23beb45e2d7c3f346 Mon Sep 17 00:00:00 2001 From: tmerlier Date: Mon, 2 Jul 2018 18:02:46 +0200 Subject: [PATCH 4/4] fix linter --- lib/countries.js | 19 ++- lib/countryHelpers.js | 8 +- lib/integration/countries.js | 129 ++++++++++--------- lib/integration/index.js | 2 +- server.js | 18 +-- test/countries.js | 113 ++++++++--------- test/countryHelpers.js | 52 ++++---- test/countryIntegration.js | 239 +++++++++++++++++------------------ 8 files changed, 287 insertions(+), 293 deletions(-) diff --git a/lib/countries.js b/lib/countries.js index a1811ed..533d47c 100644 --- a/lib/countries.js +++ b/lib/countries.js @@ -1,23 +1,22 @@ -const SearchableCollection = require('./searchableCollection'); - +const SearchableCollection = require('./searchableCollection') const schema = { nom: { type: 'text', queryWith: 'nom', - ref: 'code', + ref: 'code' }, - code: { type: 'token', queryWith: 'code' }, -}; + code: {type: 'token', queryWith: 'code'} +} function getIndexedDb(options = {}) { /* Source dataset */ - const countries = options.countries || require(options.countriesDbPath || '../data/countries.json'); + const countries = options.countries || require(options.countriesDbPath || '../data/countries.json') - const searchableCollection = new SearchableCollection(schema); - searchableCollection.load(countries); + const searchableCollection = new SearchableCollection(schema) + searchableCollection.load(countries) - return searchableCollection; + return searchableCollection } -module.exports = { getIndexedDb }; +module.exports = {getIndexedDb} diff --git a/lib/countryHelpers.js b/lib/countryHelpers.js index 1a94d09..57c6eb5 100644 --- a/lib/countryHelpers.js +++ b/lib/countryHelpers.js @@ -1,8 +1,8 @@ -const { initFields } = require('./helpers'); +const {initFields} = require('./helpers') const initCountryFields = initFields({ default: ['nom', 'code', 'iso2', 'iso3', 'num', 'territories'], - base: ['nom', 'code'], -}); + base: ['nom', 'code'] +}) -module.exports = { initCountryFields }; +module.exports = {initCountryFields} diff --git a/lib/integration/countries.js b/lib/integration/countries.js index f6e85ce..e09300b 100644 --- a/lib/integration/countries.js +++ b/lib/integration/countries.js @@ -1,119 +1,118 @@ -const fs = require('fs'); -const parse = require('csv-parse'); -const t = require('through2').obj; -const iconv = require('iconv-lite'); -const JSONStream = require('JSONStream'); -const streamify = require('stream-array'); +const fs = require('fs') +const parse = require('csv-parse') +const t = require('through2').obj +const iconv = require('iconv-lite') +const JSONStream = require('JSONStream') +const streamify = require('stream-array') /* Initialisation */ function init(ctx, next) { - ctx.countries = new Map(); + ctx.countries = new Map() - ctx.getCountry = (code) => { + ctx.getCountry = code => { if (!ctx.countries.has(code)) { - const country = { code, territories: new Set() }; - ctx.countries.set(code, country); + const country = {code, territories: new Set()} + ctx.countries.set(code, country) } - return ctx.countries.get(code); - }; + return ctx.countries.get(code) + } - next(); + next() } /* Chargement des territoires */ function loadTerritories(options = {}) { return function (ctx, next) { - ctx.debug('Chargement du jeu de données pays2016 '); - let count = 0; - const srcPath = options.srcPath || __dirname + '/../../forced_data/pays2016.tsv'; + ctx.debug('Chargement du jeu de données pays2016 ') + let count = 0 + const srcPath = options.srcPath || __dirname + '/../../forced_data/pays2016.tsv' fs.createReadStream(srcPath) .pipe(iconv.decodeStream('win1252')) .on('error', next) - .pipe(parse({ delimiter: '\t', columns: true })) + .pipe(parse({delimiter: '\t', columns: true})) .on('error', next) .pipe(t((data, enc, cb) => { - const code = data.COG; + const code = data.COG if (data.ACTUAL === '3') { const territory = { nom: data.LIBCOG, iso2: data.CODEISO2, iso3: data.CODEISO2, - num: data.CODENUM3, - }; - ctx.getCountry(code).territories.add(territory); - count++; + num: data.CODENUM3 + } + ctx.getCountry(code).territories.add(territory) + count++ } - cb(); + cb() })) .on('error', next) .on('finish', () => { - ctx.debug('Nombre de territoires chargées : %d', count); - next(); - }); - }; + ctx.debug('Nombre de territoires chargées : %d', count) + next() + }) + } } /* Chargement des pays */ function loadCountries(options = {}) { return function (ctx, next) { - ctx.debug('Chargement du jeu de données pays2016 '); - let count = 0; - const srcPath = options.srcPath || __dirname + '/../../forced_data/pays2016.tsv'; + ctx.debug('Chargement du jeu de données pays2016 ') + let count = 0 + const srcPath = options.srcPath || __dirname + '/../../forced_data/pays2016.tsv' fs.createReadStream(srcPath) .pipe(iconv.decodeStream('win1252')) .on('error', next) - .pipe(parse({ delimiter: '\t', columns: true })) + .pipe(parse({delimiter: '\t', columns: true})) .on('error', next) .pipe(t((data, enc, cb) => { - const code = data.COG; + const code = data.COG if (code !== 'XXXXX' && data.ACTUAL !== '3') { - const country = ctx.getCountry(code); - country.nom = data.LIBCOG; - country.iso2 = data.CODEISO2; - country.iso3 = data.CODEISO3; - country.num = data.CODENUM3; - count++; + const country = ctx.getCountry(code) + country.nom = data.LIBCOG + country.iso2 = data.CODEISO2 + country.iso3 = data.CODEISO3 + country.num = data.CODENUM3 + count++ } - cb(); + cb() })) .on('error', next) .on('finish', () => { - ctx.debug('Nombre de pays chargées : %d', count); - next(); - }); - }; + ctx.debug('Nombre de pays chargées : %d', count) + next() + }) + } } /* Sérialisation */ function serialize(options = {}) { return function (ctx, next) { - ctx.debug('Sérialisation des données'); - let count = 0; + ctx.debug('Sérialisation des données') + let count = 0 if (ctx.countries.size < 1) { - return next(new Error('No country')); - } else { - streamify(Array.from(ctx.countries.values())) - .on('error', next) - .pipe(t((country, enc, cb) => { - country.territories = Array.from(country.territories).sort(); - count++; - cb(null, country); - })) - .on('error', next) - .pipe(JSONStream.stringify()) - .on('error', next) - .pipe(fs.createWriteStream(options.destPath || __dirname + '/../../data/countries.json')) - .on('error', next) - .on('finish', () => { - ctx.debug('Nombre de pays écrits : %d', count); - next(); - }); + return next(new Error('No country')) } - }; + streamify([...ctx.countries.values()]) + .on('error', next) + .pipe(t((country, enc, cb) => { + country.territories = [...country.territories].sort() + count++ + cb(null, country) + })) + .on('error', next) + .pipe(JSONStream.stringify()) + .on('error', next) + .pipe(fs.createWriteStream(options.destPath || __dirname + '/../../data/countries.json')) + .on('error', next) + .on('finish', () => { + ctx.debug('Nombre de pays écrits : %d', count) + next() + }) + } } /* Exports */ -module.exports = { init, serialize, loadCountries, loadTerritories }; +module.exports = {init, serialize, loadCountries, loadTerritories} diff --git a/lib/integration/index.js b/lib/integration/index.js index ec212ea..6fc3a4c 100644 --- a/lib/integration/index.js +++ b/lib/integration/index.js @@ -30,7 +30,7 @@ function integrate(done) { countries.init, countries.loadCountries(), countries.loadTerritories(), - countries.serialize(), + countries.serialize() ], done) } diff --git a/server.js b/server.js index 29e7784..0b63eaf 100644 --- a/server.js +++ b/server.js @@ -22,7 +22,7 @@ app.use((req, res, next) => { communes: dbCommunes, departements: dbDepartements, regions: dbRegions, - countries: dbCountries, + countries: dbCountries } next() }) @@ -141,25 +141,25 @@ app.get('/regions/:code/departements', initLimit(), initDepartementFields, (req, /* Pays */ app.get('/pays', initLimit(), initCountryFields, (req, res) => { - const query = pick(req.query, 'code', 'nom'); + const query = pick(req.query, 'code', 'nom') - if (query.nom) req.fields.add('_score'); + if (query.nom) req.fields.add('_score') res.send( dbCountries .search(query) .map(country => formatOne(req, country)) - ); -}); + ) +}) app.get('/pays/:code', initCountryFields, (req, res) => { - const countries = dbCountries.search({ code: req.params.code }); + const countries = dbCountries.search({code: req.params.code}) if (countries.length === 0) { - res.sendStatus(404); + res.sendStatus(404) } else { - res.send(formatOne(req, countries[0])); + res.send(formatOne(req, countries[0])) } -}); +}) /* Definition */ app.get('/definition.yml', (req, res) => { diff --git a/test/countries.js b/test/countries.js index fedbd93..e0cafec 100644 --- a/test/countries.js +++ b/test/countries.js @@ -1,68 +1,67 @@ /* eslint-env mocha */ -const expect = require('expect.js'); -const countries = require('../lib/countries'); -const { cloneDeep } = require('lodash'); +const expect = require('expect.js') +const countries = require('../lib/countries') +const {cloneDeep} = require('lodash') -describe('countries', function () { - let db; - const country1 = { nom: 'one', code: '11' }; - const country2 = { nom: 'two', code: '22' }; - const country3 = { nom: 'three', code: '33' }; +describe('countries', () => { + let db + const country1 = {nom: 'one', code: '11'} + const country2 = {nom: 'two', code: '22'} + const country3 = {nom: 'three', code: '33'} beforeEach(done => { - db = countries.getIndexedDb({ countries: [country1, country2, country3].map(cloneDeep) }); - done(); - }); + db = countries.getIndexedDb({countries: [country1, country2, country3].map(cloneDeep)}) + done() + }) - describe('getIndexedDb()', function () { - describe('bad countries db path', function () { - it('should throw an error', function () { - expect(() => countries.getIndexedDb({ countriesDbPath: '_' })).to.throwError(); - }); - }); - }); + describe('getIndexedDb()', () => { + describe('bad countries db path', () => { + it('should throw an error', () => { + expect(() => countries.getIndexedDb({countriesDbPath: '_'})).to.throwError() + }) + }) + }) - describe('indexes', function () { - describe('indexes list', function () { - const db = countries.getIndexedDb({ countries: [] }); + describe('indexes', () => { + describe('indexes list', () => { + const db = countries.getIndexedDb({countries: []}); [ 'nom', - 'code', + 'code' ].forEach(index => { it(`should contain '${index}' index`, () => { - expect(db._indexes).to.have.key(index); - }); - }); - }); - }); + expect(db._indexes).to.have.key(index) + }) + }) + }) + }) - describe('search()', function () { - describe('No criteria', function () { - it('should return everything', function () { - expect(db.search()).to.eql([country1, country2, country3]); - }); - }); - describe('Simple matching criteria', function () { - it('should return an array with 1 country', function () { - expect(db.search({ code: '11' })).to.eql([country1]); - }); - }); - describe('Disjoint criteria', function () { - it('should return an empty array', function () { - expect(db.search({ nom: 'three', code: '22' })).to.eql([]); - }); - }); - describe('All criteria', function () { - it('should return an array with 1 country', function () { - expect(db.search({ nom: 'three', code: '33' })).to.eql([ - { nom: 'three', code: '33', _score: 1 }, - ]); - db.search({ nom: 'three', code: '33' }).forEach(reg => { - expect(reg).to.have.key('_score'); - expect(reg._score >= 0).to.be.ok(); - }); - }); - }); - }); - -}); + describe('search()', () => { + describe('No criteria', () => { + it('should return everything', () => { + expect(db.search()).to.eql([country1, country2, country3]) + }) + }) + describe('Simple matching criteria', () => { + it('should return an array with 1 country', () => { + expect(db.search({code: '11'})).to.eql([country1]) + }) + }) + describe('Disjoint criteria', () => { + it('should return an empty array', () => { + expect(db.search({nom: 'three', code: '22'})).to.eql([]) + }) + }) + describe('All criteria', () => { + it('should return an array with 1 country', () => { + expect(db.search({nom: 'three', code: '33'})).to.eql([ + {nom: 'three', code: '33', _score: 1} + ]) + db.search({nom: 'three', code: '33'}).forEach(reg => { + expect(reg).to.have.key('_score') + expect(reg._score >= 0).to.be.ok() + }) + }) + }) + }) +}) diff --git a/test/countryHelpers.js b/test/countryHelpers.js index ac6b66f..9995d04 100644 --- a/test/countryHelpers.js +++ b/test/countryHelpers.js @@ -1,44 +1,42 @@ /* eslint-env mocha */ -const expect = require('expect.js'); -const { initCountryFields } = require('../lib/countryHelpers'); +const expect = require('expect.js') +const {initCountryFields} = require('../lib/countryHelpers') -describe('countryHelpers', function () { - - describe('initCountryFields()', function () { - function runTestCase(reqParams, expectedFields, done) { - const req = { query: reqParams.query ? reqParams.query : {} }; - initCountryFields(req, undefined, function (err) { - expect(err).to.be(undefined); - expect(req.fields).to.be.a(Set); - expect(Array.from(req.fields).sort()).to.eql(expectedFields.sort()); - done(); - }); +describe('countryHelpers', () => { + describe('initCountryFields()', () => { + const runTestCase = (reqParams, expectedFields, done) => { + const req = {query: reqParams.query ? reqParams.query : {}} + initCountryFields(req, undefined, err => { + expect(err).to.be(undefined) + expect(req.fields).to.be.a(Set) + expect([...req.fields].sort()).to.eql(expectedFields.sort()) + done() + }) } // 3 tests volontairement identiques en attendant les futures évolutions - it('empty request should return default fields', function (done) { + it('empty request should return default fields', done => { runTestCase( {}, ['nom', 'code', 'iso2', 'iso3', 'num', 'territories'], done - ); - }); + ) + }) - it('fields should be read from query', function (done) { + it('fields should be read from query', done => { runTestCase( - { query: { fields: 'nom,code' } }, + {query: {fields: 'nom,code'}}, ['nom', 'code'], done - ); - }); + ) + }) - it('`nom` and `code` should always be present', function (done) { + it('`nom` and `code` should always be present', done => { runTestCase( - { query: { fields: 'nom' } }, + {query: {fields: 'nom'}}, ['nom', 'code'], done - ); - }); - }); - -}); + ) + }) + }) +}) diff --git a/test/countryIntegration.js b/test/countryIntegration.js index ec17c59..2a593d3 100644 --- a/test/countryIntegration.js +++ b/test/countryIntegration.js @@ -1,186 +1,185 @@ /* eslint-env mocha */ -const { init, serialize, loadCountries, loadTerritories } = require('../lib/integration/countries'); -const expect = require('expect.js'); +const {init, serialize, loadCountries, loadTerritories} = require('../lib/integration/countries') +const expect = require('expect.js') describe('#integration countries', () => { - describe('init()', () => { - let ctx; + let ctx beforeEach(done => { - ctx = {}; - init(ctx, done); - }); + ctx = {} + init(ctx, done) + }) describe('Context setup', () => { it('should set ctx.countries as an empty Map', () => { - expect(ctx.countries).to.be.a(Map); - expect(ctx.countries.size).to.be(0); - }); - }); + expect(ctx.countries).to.be.a(Map) + expect(ctx.countries.size).to.be(0) + }) + }) describe('getCountry()', () => { describe('New country', () => { beforeEach(() => { - expect(ctx.countries.size).to.be(0); - }); + expect(ctx.countries.size).to.be(0) + }) it('should return a country with given code', () => { - const country = ctx.getCountry('42'); - expect(country).to.be.an(Object); - expect(country).to.have.keys('code'); - expect(country).to.have.keys('territories'); - expect(country.code).to.be('42'); - }); + const country = ctx.getCountry('42') + expect(country).to.be.an(Object) + expect(country).to.have.keys('code') + expect(country).to.have.keys('territories') + expect(country.code).to.be('42') + }) it('should store the country', () => { - ctx.getCountry('21'); - expect(ctx.countries.size).to.be(1); - expect(ctx.countries.has('21')).to.be.ok(); - const country = ctx.countries.get('21'); - expect(country).to.have.keys('code'); - expect(country).to.have.keys('territories'); - expect(country.code).to.be('21'); - }); - }); + ctx.getCountry('21') + expect(ctx.countries.size).to.be(1) + expect(ctx.countries.has('21')).to.be.ok() + const country = ctx.countries.get('21') + expect(country).to.have.keys('code') + expect(country).to.have.keys('territories') + expect(country.code).to.be('21') + }) + }) describe('Existing country', () => { beforeEach(() => { - ctx.getCountry('11'); - expect(ctx.countries.size).to.be(1); - }); + ctx.getCountry('11') + expect(ctx.countries.size).to.be(1) + }) it('should return a country with given code', () => { - const country = ctx.getCountry('11'); - expect(country).to.be.an(Object); - expect(country).to.have.keys('code'); - expect(country).to.have.keys('territories'); - expect(country.code).to.be('11'); - }); + const country = ctx.getCountry('11') + expect(country).to.be.an(Object) + expect(country).to.have.keys('code') + expect(country).to.have.keys('territories') + expect(country.code).to.be('11') + }) it('should have no impact on storage', () => { - ctx.getCountry('11'); - expect(ctx.countries.has('11')).to.be.ok(); - expect(ctx.countries.size).to.be(1); - }); - }); - }); - }); + ctx.getCountry('11') + expect(ctx.countries.has('11')).to.be.ok() + expect(ctx.countries.size).to.be(1) + }) + }) + }) + }) describe('loadCountries()', () => { - let ctx; - let country; + let ctx + let country beforeEach(() => { - country = { }; + country = {} ctx = { debug: () => {}, getCountry: code => { - country.code = code; - return country; + country.code = code + return country }, loadTerritories: (country, srcPath, callBack) => { - country.territories = []; - callBack() ; - }, - }; - }); + country.territories = [] + callBack() + } + } + }) describe('Processing a file containing 1 country', () => { it('should store 1 country', done => { - loadCountries({ srcPath: __dirname + '/integration-data/countries.tsv' })(ctx, err => { - expect(err).to.be(undefined); + loadCountries({srcPath: __dirname + '/integration-data/countries.tsv'})(ctx, err => { + expect(err).to.be(undefined) expect(country).to.eql({ code: '123456', nom: 'TEST', iso2: 'TS', iso3: 'TST', - num: '250', - }); - done(); - }); - }); - }); - }); + num: '250' + }) + done() + }) + }) + }) + }) describe('loadTerritories()', () => { describe('Processing a file containing a relation with known COG code', () => { it('should associate territories to the country', done => { - const country = { code: '123456', territories: new Set() }; + const country = {code: '123456', territories: new Set()} const ctx = { - countries: { has: () => true }, + countries: {has: () => true}, debug: () => {}, - getCountry: () => country, - }; + getCountry: () => country + } - loadTerritories({ srcPath: __dirname + '/integration-data/countries-territories.tsv' })(ctx, err => { - expect(err).to.be(undefined); - expect(Array.from(country.territories)).to.eql( + loadTerritories({srcPath: __dirname + '/integration-data/countries-territories.tsv'})(ctx, err => { + expect(err).to.be(undefined) + expect([...country.territories]).to.eql( [ { - 'iso2': 'T1', - 'iso3': 'T1', - 'nom': 'TEST1', - 'num': '251', + iso2: 'T1', + iso3: 'T1', + nom: 'TEST1', + num: '251' }, { - 'iso2': 'T2', - 'iso3': 'T2', - 'nom': 'TEST2', - 'num': '252', - }, - ]); - done(); - }); - }); - }); + iso2: 'T2', + iso3: 'T2', + nom: 'TEST2', + num: '252' + } + ]) + done() + }) + }) + }) describe('Country has no territories', () => { it('should not associate any territory', done => { - const country = { code: '654321', territories: new Set() }; + const country = {code: '654321', territories: new Set()} const ctx = { - countries: { has: () => true }, + countries: {has: () => true}, debug: () => {}, - getCountry: (code) => code === country.code ? country : { territories: new Set() }, - }; + getCountry: code => code === country.code ? country : {territories: new Set()} + } - loadTerritories({ srcPath: __dirname + '/integration-data/countries-territories.tsv' })(ctx, err => { - expect(err).to.be(undefined); - expect(Array.from(country.territories)).to.eql([]); - done(); - }); - }); - }); - }); + loadTerritories({srcPath: __dirname + '/integration-data/countries-territories.tsv'})(ctx, err => { + expect(err).to.be(undefined) + expect([...country.territories]).to.eql([]) + done() + }) + }) + }) + }) describe('serialize()', () => { describe('No country', () => { it('should throw an error', done => { - const ctx = { debug: () => {}, countries: new Map() }; + const ctx = {debug: () => {}, countries: new Map()} serialize()(ctx, err => { - expect(err).to.be.an(Error); - expect(err.message).to.be('No country'); - done(); - }); - }); - }); + expect(err).to.be.an(Error) + expect(err.message).to.be('No country') + done() + }) + }) + }) describe('One country', () => { - const path = '../data/test-serialize-country.json'; + const path = '../data/test-serialize-country.json' it('should generate a JSON file with one country inside', done => { - const ctx = { debug: () => {}, countries: new Map([ + const ctx = {debug: () => {}, countries: new Map([ ['42', { code: '42', nom: 'Test', - territories: new Set(), - }], - ]) }; - serialize({ destPath: __dirname + '/' + path })(ctx, err => { - expect(err).to.be(undefined); - const countries = require(path); - expect(countries).to.have.length(1); + territories: new Set() + }] + ])} + serialize({destPath: __dirname + '/' + path})(ctx, err => { + expect(err).to.be(undefined) + const countries = require(path) + expect(countries).to.have.length(1) expect(countries[0]).to.eql({ code: '42', nom: 'Test', - territories: [], - }); - done(); - }); - }); - }); - }); -}); + territories: [] + }) + done() + }) + }) + }) + }) +})