diff --git a/.github/workflows/CHANGELOG.md b/.github/workflows/CHANGELOG.md index 0f64e27..61120ad 100644 --- a/.github/workflows/CHANGELOG.md +++ b/.github/workflows/CHANGELOG.md @@ -1,9 +1,25 @@ # Change Log -### 1.7.0 - TBC +## 1.7.0 - TBC ### Added - Add list of cities for each subdivision using the country-states-city API +- Added __version__ attribute to ISO3166_2() class + +## v1.6.1 - June 2024 + +### Added +- list_subdivisions endpoint added to API that returns list of all subdivision codes per country +- Unit tests for flag_url function in update_subdivisions script +- Separate function for extracting and parsing data attributes from RestCountries API +- Added raise_for_status error catcher for requests library + +### Changed +- Rotate user agent headers for any scripts using requests.get + +### Fixed +- Error in request URL for RestCountries API in update_subdivisions script +- Raise TypeError if invalid data type input to export_iso3166_2 function rather than system crashing ## v1.6.0 - June 2024 @@ -45,7 +61,8 @@ - If a custom subdivision object is input via its respective function, latLng attribute is set to 3d.p - Fixed syntax of some function parameters that can take multiple data types - Fixed parameter typing syntax for some function parameters that can be multiple data types - +- Error when adding a new subdivision, now will raise an error if the input parent code is invalid/not a country subdivision +- In get_flag_url function in update_subdivision script, you can pass in the full subdivision code or just the RHS of it ## v1.5.4 - March 2024 @@ -72,7 +89,6 @@ ## v1.4.0 - December 2023 - ### Added - readthedocs documentation added - Search for a particular subdivision via its subdivision name, finding the closest match using difflib libary @@ -81,7 +97,6 @@ ### Changed -- - Software package description updated - Changed software license to MIT - Remove subdivision_parent_codes function that returned this list of parent codes per subdivision diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index 89276af..d70fdd0 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -149,6 +149,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi pip3 install pytest pytest-cov codecov #generate codecov report using pytest diff --git a/API.md b/API.md index f5c774e..d47e644 100644 --- a/API.md +++ b/API.md @@ -14,7 +14,7 @@ The other endpoints available in the API are: * https://iso3166-2-api.vercel.app/api/subdivision/ * https://iso3166-2-api.vercel.app/api/name/ -Five paths/endpoints are available in the API - `/api/all`, `/api/alpha`, `/api/country_name`, `/api/subdivision` and `/api/name`. +Six paths/endpoints are available in the API - `/api/all`, `/api/alpha`, `/api/country_name`, `/api/subdivision`, `/api/name` and `/api/list_subdivisions`. * `/api/all`: get all of the ISO 3166 subdivision data for all countries. @@ -26,6 +26,8 @@ Five paths/endpoints are available in the API - `/api/all`, `/api/alpha`, `/api/ * `/api/name/`: get all of the ISO 3166 subdivision data for 1 or more ISO 3166-2 subdivision names, e.g `/api/name/Derry`. You can also input a comma separated list of subdivision name from the same or different countries and the data for each will be returned e.g `/api/name/Paris,Frankfurt,Rimini`. A closeness function is utilised to find the matching subdivision name, if no exact name match found then the most approximate subdivisions will be returned. Some subdivisions may have the same name, in this case each subdivision and its data will be returned e.g `/api/name/Saint George` (this example returns 5 subdivisions). This endpoint also has the likeness score (`?likeness=`) query string parameter that can be appended to the URL. This can be set between 1 - 100, representing a % of likeness to the input name the return subdivisions should be, e.g: a likeness score of 90 will return fewer potential matches whose name only match to a high degree compared to a score of 10 which will create a larger search space, thus returning more potential subdivision matches. A default likeness of 100 (exact match) is used, if no matching subdivision is found then this is reduced to 90. If an invalid subdivision name that doesn't match any is input then an error will be raised. +* `/api/list_subdivisions`: get list of all the subdivision codes for all countries. + * `/api`: main homepage and API documentation. A demo of the software and API is available [here][demo]. @@ -432,6 +434,45 @@ function getData() { var data = JSON.parse(this.response) ``` +Get all ISO 3166-2 subdivision codes for all countries +------------------------------------------------------ + +### Request +`GET /api/list_subdivisions/Tikistan` + + curl -i https://iso3166-2-api.vercel.app/api/list_subdivisions + +### Response + HTTP/2 200 + content-type: application/json + date: Sat, 03 Feb 2024 15:03:11 GMT + server: Vercel + content-length: + + {"AD":{"AD-02","AD-03"...}} + +### Python +```python +import requests + +request_url = "https://iso3166-2-api.vercel.app/api/list_subdivisions/" + +all_request = requests.get(request_url) +all_request.json() +``` + +### Javascript +```javascript +function getData() { + const response = + await fetch(`https://iso3166-updates.com/api/list_subdivisions`); + const data = await response.json() +} + +// Begin accessing JSON data here +var data = JSON.parse(this.response) +``` + [Back to top](#TOP) [demo]: https://colab.research.google.com/drive/1btfEx23bgWdkUPiwdwlDqKkmUp1S-_7U?usp=sharing \ No newline at end of file diff --git a/README.md b/README.md index e8c86a2..b2135c0 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ [![Documentation Status](https://readthedocs.org/projects/iso3166-2/badge/?version=latest)](https://iso3166-2.readthedocs.io/en/latest/?badge=latest) [![License: MIT](https://img.shields.io/github/license/amckenna41/iso3166-2)](https://opensource.org/licenses/MIT) [![Issues](https://img.shields.io/github/issues/amckenna41/iso3166-2)](https://github.com/amckenna41/iso3166-2/issues) -[![Size](https://img.shields.io/github/repo-size/amckenna41/iso3166-2)](https://github.com/amckenna41/iso3166-2) + diff --git a/ToDo.md b/ToDo.md index 0dc6971..84dec31 100644 --- a/ToDo.md +++ b/ToDo.md @@ -21,7 +21,6 @@ - [X] Reorder software metadata in setup.py to be in order of main func, create __description__ var. - [X] Add spcae in between ISO3166 - [X] Reorder subdivision codes in json output - alphabetically and or numerically. -- [ ] - [X] In unit tests, reorder subdivision names/codes arrays into alphabetcal/numerical order. - [X] In subdivisions in json, add link to flag in iso3166-flag-icons, if no flag then null, skip if status code != 200. - [X] Check for updates by calling export function and comparing new and old jsons. @@ -278,7 +277,7 @@ https://iso3166-updates.com/api/year/2020 : up-to-date. - [X] Double check not doing restcall for each subdivision and just once per country. - [X] In update_subdivisions, swap out row['country-code] for country_code var. - [X] In get_script, add more tests for subdivisions that are added/amended/deleted -- [ ] Read over update_subdiv script. +- [X] Read over update_subdiv script. - [X] For subdivisions whose subdivision codes have been updated in the update_subdivisions script, need to redo the script to get the flag URL e.g. GT-01, GT-02 ... KZ-11. Will need to rerun script to incorporate changes. - [X] Put flag icons get request code into its own function, used 3 times in get scripts. - [X] Add if __name__ == "main" for update_subdivisions, so it can be directly called from cmd. @@ -293,20 +292,19 @@ https://iso3166-updates.com/api/year/2020 : up-to-date. - [X] Move info about iso3166_2_scripts from main readme. - [X] For scripts dir, add usage examples including terminal and importing via module import. - [X] In update_subdivisions, raise error if no subdivision code input. -- [ ] Error when adding new subdivision data via cmd line/terminal - some attributes not being added to output object, error when importing parent_code parameter as None. +- [X] Error when adding new subdivision data via cmd line/terminal - some attributes not being added to output object, error when importing parent_code parameter as None: - [X] When adding new subdivision data via cmd line/terminal, latLng attribute may be a string of a list, change to array (copy jsom is original) -- [ ] When adding new subdivision data via cmd line/terminal, need to reorder attributes to normal order. +- [X] When adding new subdivision data via cmd line/terminal, need to reorder attributes to normal order. - [X] Add separate unit test instance for update_subdivisions script. - [X] Run bandit and package security check - [X] Raise error in update_subdivisons function when comma separated list of alpha codes input, only one should be input? Add unit tests - [X] In update_subdivisions, when making custom change to object, double check that custom latLng values are set to 3.dp. - [X] In cusom_subdivisions, when making custom change to object, double check that custom latLng values are set to 3.dp. -- [ ] In update_subdivisons, when adding new subdivison, validate parentCode, if it doesnt exist raise error, unless current code being added is same as parent code input. - [X] Add CHANGELOG.md - https://keepachangelog.com/en/1.0.0/ -- [ ] In test_iso3166_2 when testing custom_subdivision function, don't directly edit existing iso3166-2.json, create a hard copy and pass in the duplicate file name to function. +- [X] In test_iso3166_2 when testing custom_subdivision function, don't directly edit existing iso3166-2.json, create a hard copy and pass in the duplicate file name to function. - [X] In test_iso3166_2 test format of each subdivision code XX-Y, XX-YY, XX-YYY etc. - [X] Change flagUrl attribute to just flag? -- [ ] In custom_subdivisons function, add a flag/parameter whereby if set it creates a hard copy of the existing iso3166-2.json object so it is not directly overwritten. Issue when using the software and iso3166-2.json will not contain added custom subdivision. +- [X] In custom_subdivisons function, add a flag/parameter whereby if set it creates a hard copy of the existing iso3166-2.json object so it is not directly overwritten. Issue when using the software and iso3166-2.json will not contain added custom subdivision. - [ ] In test_get script, create a request session in setup and close session in tearDown to stop resource warning SSL socket no closed - https://stackoverflow.com/questions/48160728/resourcewarning-unclosed-socket-in-python-3-unit-test - [X] In iso3166_2.py add repr class function, add more info to __str__. Add unit tests - [X] In custom_subdivisions function, allow for the amendment of an exsting subdivision code, e.g changing its local name, flagUrl, latLng etc. Mention on readme that if erroneous data is found for an attribute/subdivision, you can raise an Issue but you could also implement it immediately into the object via the custom_subdivisions function. @@ -333,10 +331,22 @@ https://iso3166-updates.com/api/year/2020 : up-to-date. - [X] In custom_subdivision function, alpha_code and subdivision_code requried - [X] In update_subdivisons, alpha_code and subdivison not required because of subdivisons_csv param - [X] If custom path input to ISO3166_2(), add validation to see if path already has the full path to object in it, else append filepath to it. -- [ ] when packaging, download from testpypi, checking correct files/dirs are downloaded, before publishing to pypi. -- [ ] When adding restCountries keys to output, ensure they are in alphabetical order and appended to the end of the columns. +- [X] when packaging, download from testpypi, checking correct files/dirs are downloaded, before publishing to pypi. +- [X] When adding restCountries keys to output, ensure they are in alphabetical order and appended to the end of the columns. - [X] Rerun script with new flag atttribute, update filesize and execution time on readme. - [X] Read over docs +- [X] Remove multiple return data types. +- [X] Update supported python version >3.10 +- [X] Update 1.6.0 - 1.6.1 +- [X] In update_subdivisons, when adding new subdivison, validate parentCode, if it doesnt exist raise error, unless current code being added is same as parent code input. +- [X] When updating_subdivision, can add restcountries keys even if other objects don't have these keys +- [X] Test for get_flag_url function +- [X] In get_flag URL, if only RHS of subdivision code input to its parameter, prepend alpha code to it. +- [X] Issue with nested objects in RestCountries - new function that parses the desired attributes +- [X] In restcountries request, raise for status to catch 404 error +- [X] Test adding restcountries keys when amending a subdivision code. +- [X] For some nested restcountirwes keys, if it is a list, join into a string of comma separated. +- [X] Update user agent headers for get requests, rotate headers. Future Additions ---------------- @@ -368,4 +378,5 @@ Future Additions - [ ] Some sort of flag when creating a new class of the sw indicating that custom data is present in the object. - [ ] state_city_data parameter in get_all script that gets all the city data per subdivision from Country State City API using get request. - [X] Remove type Errors and just force data attribute types. +- [ ] Can I integrate AI with app? \ No newline at end of file diff --git a/docs/api.rst b/docs/api.rst index 7d70c19..6fff9bc 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -8,7 +8,7 @@ API .. subdivision name and code can be used to search for a sought subdivision. The main API endpoint is: `https://iso3166-2-api.vercel.app/api `_. This endpoint displays the API documentation and forms the -base URL for the 5 other endpoints. +base URL for the 6 other endpoints. The other endpoints available in the API are: @@ -17,8 +17,9 @@ The other endpoints available in the API are: * https://iso3166-2-api.vercel.app/api/subdivision/ * https://iso3166-2-api.vercel.app/api/country_name/ * https://iso3166-2-api.vercel.app/api/name/ +* https://iso3166-2-api.vercel.app/api/list_subdivisions -Five paths/endpoints are available in the API - `/api/all`, `/api/alpha`, `/api/subdivision`, `/api/country_name` and `/api/name`. +Five paths/endpoints are available in the API - `/api/all`, `/api/alpha`, `/api/subdivision`, `/api/country_name`, `/api/name` and `/api/list_subdivisions`. * `/api/all`: get all of the ISO 3166 subdivision data for all countries. @@ -28,8 +29,10 @@ Five paths/endpoints are available in the API - `/api/all`, `/api/alpha`, `/api/ * `/api/subdivision`: get all of the ISO 3166 subdivision data for 1 or more ISO 3166-2 subdivision codes, e.g `/api/subdivision/GB-ABD`. You can also input a comma separated list of subdivision codes from the same and or different countries and the data for each will be returned e.g `/api/subdivision/IE-MO,FI-17,RO-AG`. If the input subdivision code is not in the correct format then an error will be raised. Similarly if an invalid subdivision code that doesn't exist is input then an error will be raised. -* `/api/name/`: get all of the ISO 3166 subdivision data for 1 or more ISO 3166-2 subdivision names, e.g `/api/name/Derry`. You can also input a comma separated list of subdivision name from the same or different countries and the data for each will be returned e.g `/api/name/Paris,Frankfurt,Rimini`. A closeness function is utilized to find the matching subdivision name, if no exact name match found then the most approximate subdivisions will be returned. Some subdivisions may have the same name, in this case each subdivision and its data will be returned e.g `/api/name/Saint George` (this example returns 5 subdivisions). This endpoint also has the likeness score (`?likeness=`) query string parameter that can be appended to the URL. This can be set between 1 - 100, representing a % of likeness to the input name the return subdivisions should be, e.g: a likeness score of 90 will return fewer potential matches whose name only match to a high degree compared to a score of 10 which will create a larger search space, thus returning more potential subdivision matches. A default likeness of 100 (exact match) is used, if no matching subdivision is found then this is reduced to 90. If an invalid subdivision name that doesn't match any is input then an error will be raised. +* `/api/name`: get all of the ISO 3166 subdivision data for 1 or more ISO 3166-2 subdivision names, e.g `/api/name/Derry`. You can also input a comma separated list of subdivision name from the same or different countries and the data for each will be returned e.g `/api/name/Paris,Frankfurt,Rimini`. A closeness function is utilized to find the matching subdivision name, if no exact name match found then the most approximate subdivisions will be returned. Some subdivisions may have the same name, in this case each subdivision and its data will be returned e.g `/api/name/Saint George` (this example returns 5 subdivisions). This endpoint also has the likeness score (`?likeness=`) query string parameter that can be appended to the URL. This can be set between 1 - 100, representing a % of likeness to the input name the return subdivisions should be, e.g: a likeness score of 90 will return fewer potential matches whose name only match to a high degree compared to a score of 10 which will create a larger search space, thus returning more potential subdivision matches. A default likeness of 100 (exact match) is used, if no matching subdivision is found then this is reduced to 90. If an invalid subdivision name that doesn't match any is input then an error will be raised. +* `/api/list_subdivisions`: get list of all subdivision codes per country. + * `/api`: main homepage and API documentation. Get subdivision data for all countries @@ -223,4 +226,25 @@ curl:: .. |demo_link| raw:: html - here \ No newline at end of file + here + +Get list of all subdivision codes per country +--------------------------------------------- +Return a list of all ISO 3166-2 subdivision codes for each country. + +Python Requests: + +.. code-block:: python + + import requests + + base_url = "https://iso3166-2-api.vercel.app/api/list_subdivisions" + all_data = requests.get(base_url) + + all_data["DE"] #subdivision codes for Germany + all_data["OM"] #subdivision data for Oman + all_data["US"] #subdivision data for US + +curl:: + + $ curl -i https://iso3166-2-api.vercel.app/api/list_subdivisions \ No newline at end of file diff --git a/docs/index.rst b/docs/index.rst index 6974079..0889d69 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -45,12 +45,12 @@ There are 7 main data attributes available for each subdivision within the **iso Last Updated ============ -The ISO 3166-2 data was last updated on March 2023. A log of the latest ISO 3166-2 updates can be seen in the +The ISO 3166-2 data was last updated on **March 2024**. A log of the latest ISO 3166-2 updates can be seen in the |updates_md_link| file in the repository. .. |updates_md_link| raw:: html - UPDATES.md + UPDATES.md .. |license_repo_link| raw:: html diff --git a/iso3166_2/__init__.py b/iso3166_2/__init__.py index 1c46b3c..bf49b8a 100644 --- a/iso3166_2/__init__.py +++ b/iso3166_2/__init__.py @@ -2,7 +2,7 @@ #software metadata __name__ = 'iso3166-2' -__version__ = "1.6.0" +__version__ = "1.6.1" __description__ = "A lightweight Python package, and accompanying API, used to access all of the world's most up-to-date and accurate ISO 3166-2 subdivision data, including: name, local name, code, parent code, type, latitude/longitude and flag." __author__ = 'AJ McKenna, https://github.com/amckenna41' __authorEmail__ = 'amckenna41@qub.ac.uk' diff --git a/iso3166_2/iso3166_2.py b/iso3166_2/iso3166_2.py index 77def1d..9abe436 100644 --- a/iso3166_2/iso3166_2.py +++ b/iso3166_2/iso3166_2.py @@ -114,6 +114,7 @@ def __init__(self, country_code: str="", iso3166_2_filepath: str=""): self.country_code = country_code self.iso3166_2_filepath = iso3166_2_filepath self.iso3166_json_filename= "iso3166-2.json" + self.__version__ = "1.6.1" #get full path to default object self.iso3166_2_module_path = os.path.join(os.path.dirname(os.path.abspath(sys.modules[self.__module__].__file__)), self.iso3166_json_filename) diff --git a/iso3166_2_scripts/get_iso3166_2.py b/iso3166_2_scripts/get_iso3166_2.py index e671f0b..fa68895 100644 --- a/iso3166_2_scripts/get_iso3166_2.py +++ b/iso3166_2_scripts/get_iso3166_2.py @@ -3,13 +3,12 @@ import json import argparse import requests -import getpass -from importlib.metadata import metadata import pycountry import iso3166 import googlemaps from tqdm import tqdm import natsort +import random import warnings from collections import OrderedDict import pandas as pd @@ -20,11 +19,16 @@ except: from iso3166_2_scripts.update_subdivisions import * -#initialise version -__version__ = metadata('iso3166-2')['version'] - -#initalise User-agent header for requests library -USER_AGENT_HEADER = {'User-Agent': f"iso3166-2/{__version__} (https://github.com/amckenna41/iso3166-2; {getpass.getuser()})"} +#at each calling of script and requests.get, select random user agent from below list +user_agents = [ + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36", + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15"\ +] +USER_AGENT_HEADER = {'User-Agent': random.choice(user_agents)} #base URL for RestCountries API rest_countries_base_url = "https://restcountries.com/v3.1/" @@ -134,8 +138,8 @@ def convert_to_alpha2(alpha_code: str) -> str: return iso3166.countries_by_alpha3[alpha_code].alpha2 #raise error if input alpha code prameter is not of correct type - if not (TypeError(alpha_codes, str)): - raise ValueError(f"Alpha code input parameter not of correct type, got {type(alpha_codes)}.") + if not (isinstance(alpha_codes, str)): + raise TypeError(f"Alpha code input parameter not of correct type, got {type(alpha_codes)}.") #split multiple alpha codes into list, remove any whitespace, uppercase alpha_codes = alpha_codes.upper().replace(' ', '').split(',') @@ -274,12 +278,11 @@ def convert_to_alpha2(alpha_code: str) -> str: #create country key in json object all_country_data[alpha2] = {} - #make call to RestCountries api if additional rest countries attributes input + #make call to RestCountries api if additional rest countries attributes input, raise error if invalid request if (rest_countries_keys != ""): - try: - country_restcountries_data = requests.get(rest_countries_base_url + "alpha/" + alpha2, headers=USER_AGENT_HEADER, timeout=7).json() - except: - raise requests.exceptions.RequestException(f"Error thrown when trying to get the country-level data for {alpha2} using the RestCountries API at URL {rest_countries_base_url + 'alpha/' + alpha2}.") + country_restcountries_response = requests.get(rest_countries_base_url + "alpha/" + alpha2, headers=USER_AGENT_HEADER, timeout=12) + country_restcountries_response.raise_for_status() + country_restcountries_data = country_restcountries_response.json() #iterate over all countrys' subdivisions, assigning subdivision code, name, type, parent code and flag URL, where applicable for the json object for subd in all_subdivisions: diff --git a/iso3166_2_scripts/update_subdivisions.py b/iso3166_2_scripts/update_subdivisions.py index fb83984..778b5d7 100644 --- a/iso3166_2_scripts/update_subdivisions.py +++ b/iso3166_2_scripts/update_subdivisions.py @@ -3,19 +3,22 @@ import iso3166 import natsort from collections import OrderedDict -from importlib.metadata import metadata import argparse -import getpass +import random import pandas as pd import requests import numpy as np from datetime import datetime -#initialise version -__version__ = metadata('iso3166-2')['version'] - -#initalise User-agent header for requests library -USER_AGENT_HEADER = {'User-Agent': f"iso3166-2/{__version__} (https://github.com/amckenna41/iso3166-2; {getpass.getuser()})"} +#at each calling of script and requests.get, select random user agent from below list +user_agents = [ + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36", + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15" +] #base url for RestCountries URL rest_countries_base_url = "https://restcountries.com/v3.1/" @@ -217,7 +220,7 @@ def convert_to_alpha2(alpha_code: str) -> str: #round coordinates to 3d.p if applicable lat_lng[0], lat_lng[1] = round(float(lat_lng[0]), 3), round(float(lat_lng[1]), 3) - + #if no subdivision csv passed in and subdivision changes passed in directly to function parameters if (subdivision_csv == ""): @@ -275,7 +278,7 @@ def convert_to_alpha2(alpha_code: str) -> str: with open(os.path.join("iso3166-2-data-archive", "archive-iso3166-2_" + \ str(datetime.date(datetime.now())) + ".json"), 'w', encoding='utf-8') as output_json: json.dump(all_subdivision_data, output_json, ensure_ascii=False, indent=4) - + #delete subdivision if delete parameter set, raise error if code not found if (delete): if (subdivision_code in list(all_subdivision_data[alpha_code].keys())): @@ -291,7 +294,7 @@ def convert_to_alpha2(alpha_code: str) -> str: #raise error if current subdivision not found in list of codes if (current_subdivision_code not in list(all_subdivision_data[alpha_code].keys())): - raise ValueError(f"Subdivision code {subdivision_code} not found in country's list of codes:\n{list(all_subdivision_data[alpha_code].keys())}.") + raise ValueError(f"Subdivision code {current_subdivision_code} not found in country's list of codes:\n{list(all_subdivision_data[alpha_code].keys())}.") #raise error if new amended subdivision code does not have the same subdivision code prefix if (new_subdivision_code.split('-')[0] != current_subdivision_code.split('-')[0]): @@ -311,8 +314,7 @@ def convert_to_alpha2(alpha_code: str) -> str: #add rest countries key to object if applicable if (rest_countries_keys != ""): - for key in rest_countries_keys: - all_subdivision_data[alpha_code][new_subdivision_code][key] = all_subdivision_data[alpha_code][current_subdivision_code][key] + all_subdivision_data[alpha_code][new_subdivision_code] = parse_rest_countries(alpha_code, rest_countries_keys, all_subdivision_data[alpha_code][new_subdivision_code]) #order attributes of individual subdivision using natsort all_subdivision_data[alpha_code][new_subdivision_code] = dict(OrderedDict(natsort.natsorted(all_subdivision_data[alpha_code][new_subdivision_code].items()))) @@ -336,7 +338,7 @@ def convert_to_alpha2(alpha_code: str) -> str: if (lat_lng != None and not 'latLng' in exclude_default_keys): all_subdivision_data[alpha_code][subdivision_code]["latLng"] = lat_lng if (parent_code != "" and not 'parentCode' in exclude_default_keys): - if not ((parent_code != "") and (parent_code in list(all_subdivision_data[alpha_code].keys()) and (parent_code != subdivision_code))): + if not ((parent_code != "") and (parent_code in list(all_subdivision_data[alpha_code].keys())) and (parent_code != subdivision_code)): #*** raise ValueError(f"Parent code {parent_code} not found in list of subdivision codes:\n{list(all_subdivision_data[alpha_code].keys())}.") else: all_subdivision_data[alpha_code][subdivision_code]["parentCode"] = parent_code @@ -352,27 +354,20 @@ def convert_to_alpha2(alpha_code: str) -> str: if not ('type' in exclude_default_keys): new_subdivision_data["type"] = type_ if not ('parentCode' in exclude_default_keys): - new_subdivision_data["parentCode"] =parent_code + if (parent_code != ""): + #raise error if parent code not an existing subdivision code + if not (parent_code in list(all_subdivision_data[alpha_code].keys())) and (parent_code != subdivision_code): + raise ValueError(f"Parent code {parent_code} not found in list of subdivision codes:\n{list(all_subdivision_data[alpha_code].keys())}.") + new_subdivision_data["parentCode"] = parent_code if not ('latLng' in exclude_default_keys): new_subdivision_data["latLng"] = lat_lng if not ('flag' in exclude_default_keys): new_subdivision_data["flag"] = flag - - #iterate over each RestCountries key, if applicable, add attribute value to new object - for key in rest_countries_keys: - #using an iterable, check if existing subdivision exists for country code, pull its attribute values for new object - all_subdivision_data_iterable = iter(all_subdivision_data[alpha_code]) - if (next(all_subdivision_data_iterable, None) != None): - all_subdivision_data_iterable = iter(all_subdivision_data[alpha_code]) #reset iterable back to start of object - if (key in all_subdivision_data[alpha_code][next(all_subdivision_data_iterable)]): - all_subdivision_data_iterable = iter(all_subdivision_data[alpha_code]) #reset iterable back to start of object - new_subdivision_data[key] = all_subdivision_data[alpha_code][next(all_subdivision_data_iterable)][key] - #if subdivision not available for country code, use the RestCountries API to get the attribute values for new object - else: - #make call to RestCountries api if additional rest countries attributes input - country_restcountries_data = requests.get(rest_countries_base_url + "/alpha/" + all_subdivision_data[alpha_code], headers=USER_AGENT_HEADER).json() - new_subdivision_data[key] = country_restcountries_data[key] + #append restcountries attribute values to new subdivision + if (rest_countries_keys != ""): + new_subdivision_data = parse_rest_countries(alpha_code, rest_countries_keys, new_subdivision_data) + #add new subdivision object to main subdivision data object all_subdivision_data[alpha_code][subdivision_code] = new_subdivision_data @@ -447,9 +442,8 @@ def convert_to_alpha2(alpha_code: str) -> str: #add rest countries key to object if applicable if (rest_countries_keys != ""): - for key in rest_countries_keys: - all_subdivision_data[alpha_code][new_subdivision_code][key] = all_subdivision_data[alpha_code][subdivision_code][key] - + all_subdivision_data[alpha_code][new_subdivision_code] = parse_rest_countries(alpha_code, rest_countries_keys, all_subdivision_data[alpha_code][new_subdivision_code]) + #delete existing subdivision code data in object del all_subdivision_data[alpha_code][subdivision_code] @@ -478,7 +472,10 @@ def convert_to_alpha2(alpha_code: str) -> str: if (row['type'] != None and row['type'] != "" and not ('type' in exclude_default_keys)): all_subdivision_data[alpha_code][subdivision_code]['type'] = row['type'] if (row['parentCode'] != None and row['parentCode'] != "" and not ('parentCode' in exclude_default_keys)): - all_subdivision_data[alpha_code][subdivision_code]['parentCode'] = row['parentCode'] + if not ((row['parent_code'] in list(all_subdivision_data[alpha_code].keys())) and (row['parent_code'] != subdivision_code)): #validate parent code + raise ValueError(f"Parent code {row['parent_code']} not found in list of subdivision codes:\n{list(all_subdivision_data[alpha_code].keys())}.") + else: + all_subdivision_data[alpha_code][subdivision_code]['parentCode'] = row['parentCode'] if (row['flag'] != None and row['flag'] != "" and not ('flag' in exclude_default_keys)): all_subdivision_data[alpha_code][subdivision_code]['flag'] = row['flag'] if (row['latLng'] != None and row['latLng'] != "" and not ('latLng' in exclude_default_keys)): @@ -524,18 +521,24 @@ def convert_to_alpha2(alpha_code: str) -> str: if not ('flag' in exclude_default_keys): all_subdivision_data[alpha_code][row["code"]]["flag"] = row["flag"] - #iterate over each RestCountries key, if applicable, add attribute value to new object - for key in rest_countries_keys: - #using an iterable, check if existing subdivision exists for country code, pull its attribute values for new object - all_subdivision_data_iterable = iter(all_subdivision_data[alpha_code]) - if (next(all_subdivision_data_iterable, None) != None): - all_subdivision_data_iterable = iter(all_subdivision_data[alpha_code]) #reset iterable back to start of object - all_subdivision_data[alpha_code][subdivision_code][key] = all_subdivision_data[alpha_code][next(all_subdivision_data_iterable)][key] - #if subdivision not available for country code, use the RestCountries API to get the attribute values for new object - else: - #make call to RestCountries api if additional rest countries attributes input - country_restcountries_data = requests.get(rest_countries_base_url + "/alpha/" + all_subdivision_data[alpha_code], headers=USER_AGENT_HEADER).json() - all_subdivision_data[alpha_code][row["code"]][key] = country_restcountries_data[key] + #append restcountries attribute values to new subdivision + if (rest_countries_keys != ""): + all_subdivision_data[alpha_code][row["code"]] = parse_rest_countries(alpha_code, rest_countries_keys, all_subdivision_data[alpha_code][row["code"]]) + + # #iterate over each RestCountries key, if applicable, add attribute value to new object + # for key in rest_countries_keys: + # #using an iterable, check if existing subdivision exists for country code, pull its attribute values for new object + # all_subdivision_data_iterable = iter(all_subdivision_data[alpha_code]) + # if (next(all_subdivision_data_iterable, None) != None): + # all_subdivision_data_iterable = iter(all_subdivision_data[alpha_code]) #reset iterable back to start of object + # all_subdivision_data[alpha_code][subdivision_code][key] = all_subdivision_data[alpha_code][next(all_subdivision_data_iterable)][key] + # #if subdivision not available for country code, use the RestCountries API to get the attribute values for new object + # else: + # #make call to RestCountries api if additional rest countries attributes input, raise error if invalid request + # country_restcountries_response = requests.get(rest_countries_base_url + "alpha/" + all_subdivision_data[alpha_code], headers=USER_AGENT_HEADER) + # country_restcountries_response.raise_for_status() + # country_restcountries_data = country_restcountries_response.json() + # all_subdivision_data[alpha_code][row["code"]][key] = country_restcountries_data[key] #order attributes of individual subdivision using natsort all_subdivision_data[alpha_code][subdivision_code] = dict(OrderedDict(natsort.natsorted(all_subdivision_data[alpha_code][subdivision_code].items()))) @@ -617,23 +620,76 @@ def get_flag_icons_url(alpha2_code: str, subdivision_code: str) -> str|None: #list of flag file extensions in order of preference flag_file_extensions = ['.svg', '.png', '.jpeg', '.jpg', '.gif'] - + + #if only half of subdivision code input to its parameter, prepend the alpha-2 code to it to make it valid + if ('-' not in subdivision_code): + subdivision_code = alpha2_code + '-' + subdivision_code + #url to flag in iso3166-flag-icons repo alpha2_flag = flag_icons_base_url + alpha2_code + "/" + subdivision_code + #set user agent to a random agent in the list + USER_AGENT_HEADER = {'User-Agent': random.choice(user_agents)} + #verify that path on flag icons repo exists, if not set flag url value to None - if (requests.get(flag_icons_base_url + alpha2_code, headers=USER_AGENT_HEADER).status_code != 404): + if (requests.get(flag_icons_base_url + alpha2_code, headers=USER_AGENT_HEADER, timeout=15).status_code != 404): #iterate over all image extensions checking existence of flag in repo, if no flag with extensions found then set to null for extension in range(0, len(flag_file_extensions)): #if subdivision has a valid flag in flag icons repo set to its GitHub url, else set to None - if (requests.get(alpha2_flag + flag_file_extensions[extension], headers=USER_AGENT_HEADER).status_code != 404): + if (requests.get(alpha2_flag + flag_file_extensions[extension], headers=USER_AGENT_HEADER, timeout=15).status_code != 404): return alpha2_flag + flag_file_extensions[extension] elif (extension == 4): return None else: return None + +def parse_rest_countries(alpha_code: str, rest_countries_keys: list, new_subdivision_data: dict) -> dict: + """ + Extract and parse required data attributes from RestCountries API. Append each attribute's value to + subdivision object. + + Parameters + ========== + :alpha_code: str + ISO 3166-1 alpha-2 country code. + :rest_countries_keys: list + str of one or more comma separated additional attributes from the RestCountries API that can be + appended to each of the subdivisions in the output object. Here is the full list of accepted + fields/attributes: "idd", "carSigns", "carSide", "continents", "currencies", "languages", + "postalCode", "region", "startOfWeek", "subregion", "timezones", "tld". + :new_subdivision_data: dict + individual subdivision data object to be added or amended. + + Returns + ======= + :new_subdivision_data: dict: dict + individual subdivision data object with the applicable restcountries attribute values + appended to it. + """ + #set user agent to a random agent in the list + USER_AGENT_HEADER = {'User-Agent': random.choice(user_agents)} + + #use the RestCountries API to get the attribute values for country/subdivision + country_restcountries_response = requests.get(rest_countries_base_url + "alpha/" + alpha_code, headers=USER_AGENT_HEADER, timeout=15) + country_restcountries_response.raise_for_status() + country_restcountries_data = country_restcountries_response.json() + + #iterate over each RestCountries key, if applicable, add attribute values to new object + for key in rest_countries_keys: + if (key == "carSigns"): + new_subdivision_data[key] = country_restcountries_data[0]["car"]["signs"] + elif (key == "carSide"): + new_subdivision_data[key] = country_restcountries_data[0]["car"]["side"] + elif (key == "idd"): + new_subdivision_data[key] = "Root: " + str(country_restcountries_data[0]["idd"]["root"]) + ", Suffixes: " + str(country_restcountries_data[0]["idd"]["suffixes"]) + elif (key == "postalCode"): + new_subdivision_data[key] = "Format: " + str(country_restcountries_data[0]["postalCode"]["format"]) + ", Regex: " + str(country_restcountries_data[0]["postalCode"]["regex"]) + else: + new_subdivision_data[key] = country_restcountries_data[0][key] + + return new_subdivision_data if __name__ == '__main__': @@ -650,7 +706,7 @@ def get_flag_icons_url(alpha2_code: str, subdivision_code: str) -> str|None: help='ISO 3166-2 subdivision local name.') parser.add_argument('-type', '--type', type=str, required=False, default="", help='ISO 3166-2 subdivision type.') - parser.add_argument('-lat_lng', '--lat_lng', type=str, required=False, default="", + parser.add_argument('-lat_lng', '--lat_lng', type=str, required=False, default=None, help='ISO 3166-2 subdivision latitude/longitude.') parser.add_argument('-parent_code', '--parent_code', type=str, required=False, default="", help='ISO 3166-2 subdivision parent subdivision code.') diff --git a/pyproject.toml b/pyproject.toml index 02d79a1..bb7ba4b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "iso3166-2" -version = "1.6.0" +version = "1.6.1" description = "A lightweight Python package, and accompanying API, used to access all of the world's most up-to-date and accurate ISO 3166-2 subdivision data, including: name, local name, code, parent code, type, latitude/longitude and flag." authors = ["AJ McKenna "] maintainers = ["AJ McKenna "] @@ -15,10 +15,9 @@ classifiers = [ "License :: OSI Approved :: MIT License", "License :: Free For Educational Use", "Natural Language :: English", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", "Programming Language :: Python :: Implementation :: PyPy", "Programming Language :: Python :: 3 :: Only", "Topic :: Scientific/Engineering", @@ -30,7 +29,7 @@ packages = [{include = "iso3166_2"}] homepage = "https://iso3166-2-api.vercel.app/api" [tool.poetry.dependencies] -python = "^3.8" +python = "^3.9" iso3166 = "^2.1.1" natsort = "^8.4.0" thefuzz = "^0.22.1" diff --git a/tests/test_get_iso3166_2.py b/tests/test_get_iso3166_2.py index a56fac3..060c3db 100644 --- a/tests/test_get_iso3166_2.py +++ b/tests/test_get_iso3166_2.py @@ -3,6 +3,7 @@ import json import os import getpass +from importlib.metadata import metadata import shutil import unittest import warnings @@ -11,7 +12,6 @@ #ignore resource warnings warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning) -@unittest.skip("") class Get_ISO3166_2_Tests(unittest.TestCase): """ Test suite for testing get_iso3166_2.py script that pulls all the ISO 3166-2 @@ -59,7 +59,7 @@ def test_export_iso3166_2(self): test_alpha_error2 = "ABCDEF" test_alpha_error3 = 1234 #1.) - export_iso3166_2(alpha_codes=test_alpha_dk, export_folder=self.test_output_dir, export_filename=self.test_output_filename, verbose=0, export_csv=1) #Denmark + export_iso3166_2(alpha_codes=test_alpha_dk, export_folder=self.test_output_dir, export_filename=self.test_output_filename, verbose=0, export_csv=1,) #Denmark #open exported json with open(os.path.join(self.test_output_dir, self.test_output_filename + "-" + test_alpha_dk + ".json")) as output_json: @@ -419,8 +419,8 @@ def test_export_iso3166_restcountries(self): f"Expected attribute value to be root: +2, suffixes: 61, got {test_iso3166_2_mg_json[country][subd]['idd']}.") self.assertEqual(test_iso3166_2_mg_json[country][subd]["currencies"], {"MGA": {"name": "Malagasy ariary", "symbol": "Ar"}}, f"Expected attribute value to be MGA: name:Malagasy ariary, symbol:Ar, got {test_iso3166_2_mg_json[country][subd]['currencies']}.") - self.assertEqual(test_iso3166_2_mg_json[country][subd]["postalCode"], "Format: ###, Regex: ^(\d{3})$", - f"Expected attribute value to be Format: ###, Regex: ^(\d{3})$, got {test_iso3166_2_mg_json[country][subd]['postalCode']}.") + self.assertEqual(test_iso3166_2_mg_json[country][subd]["postalCode"], "Format: ###, Regex:" + r'^(\d{3})$', + f"Expected attribute value to be Format: ###, Regex: r'^(\d{3})$', got {test_iso3166_2_mg_json[country][subd]['postalCode']}.") #4.) with (self.assertRaises(ValueError)): export_iso3166_2(export_folder=self.test_output_dir, export_filename=self.test_output_filename, verbose=0, rest_countries_keys=test_rest_countries_keys_error1) #ZZ diff --git a/tests/test_iso3166_2.py b/tests/test_iso3166_2.py index e38e716..abfe23e 100644 --- a/tests/test_iso3166_2.py +++ b/tests/test_iso3166_2.py @@ -55,16 +55,16 @@ def setUp(self): def test_iso3166_2_metadata(self): """ Testing correct iso3166-2 software version and metadata. """ - # self.assertEqual(metadata('iso3166-2')['version'], "1.6.0", - # "iso3166-2 version is not correct, expected 1.6.0, got {}.".format(metadata('iso3166-2')['version'])) + # self.assertEqual(metadata('iso3166-2')['version'], "1.6.1", + # "iso3166-2 version is not correct, expected 1.6.1, got {}.".format(metadata('iso3166-2')['version'])) self.assertEqual(metadata('iso3166-2')['name'], "iso3166-2", "iso3166-2 software name is not correct, expected iso3166-2, got {}.".format(metadata('iso3166-2')['name'])) self.assertEqual(metadata('iso3166-2')['author'], "AJ McKenna", "iso3166-2 author is not correct, expected AJ McKenna, got {}.".format(metadata('iso3166-2')['author'])) self.assertEqual(metadata('iso3166-2')['author-email'], "amckenna41@qub.ac.uk", "iso3166-2 author email is not correct, expected amckenna41@qub.ac.uk, got {}.".format(metadata('iso3166-2')['author-email'])) - # self.assertEqual(metadata('iso3166-2')['summary'], "A lightweight Python package, and accompanying API, used to access all of the world's most up-to-date and accurate ISO 3166-2 subdivision data, including: name, local name, code, parent code, type, latitude/longitude and flag.", - # "iso3166-2 package summary is not correct, got: {}.".format(metadata('iso3166-2')['summary'])) + self.assertEqual(metadata('iso3166-2')['summary'], "A lightweight Python package, and accompanying API, used to access all of the world's most up-to-date and accurate ISO 3166-2 subdivision data, including: name, local name, code, parent code, type, latitude/longitude and flag.", + "iso3166-2 package summary is not correct, got: {}.".format(metadata('iso3166-2')['summary'])) self.assertEqual(metadata('iso3166-2')['keywords'], "iso,iso3166,beautifulsoup,python,pypi,countries,country codes,iso3166-2,iso3166-1,alpha-2,iso3166-updates,subdivisions,regions", "iso3166-2 keywords are not correct, got:\n{}.".format(metadata('iso3166-2')['keywords'])) self.assertEqual(metadata('iso3166-2')['home-page'], "https://iso3166-2-api.vercel.app/api", @@ -373,7 +373,7 @@ def test_custom_subdivision(self): #create hard copy of iso3166-2.json object for testing custom subdivison functionality on self.test_iso3166_2_copy = os.path.join(self.test_output_dir, "iso3166_2_copy.json") with open(self.test_iso3166_2_copy, "w") as output_json: - json.dump(self.all_iso3166_2.all, output_json) + json.dump(self.all_iso3166_2.all, output_json, ensure_ascii=False, indent=4) #class instance using duplicated ISO 3166-2 object all_iso3166_2_custom_subdivision = ISO3166_2(iso3166_2_filepath=self.test_iso3166_2_copy) diff --git a/tests/test_iso3166_2_api.py b/tests/test_iso3166_2_api.py index 4985587..b51badc 100644 --- a/tests/test_iso3166_2_api.py +++ b/tests/test_iso3166_2_api.py @@ -8,7 +8,6 @@ import unittest unittest.TestLoader.sortTestMethodsUsing = None -@unittest.skip("") class ISO3166_2_API_Tests(unittest.TestCase): """ Test suite for testing ISO 3166-2 api created to accompany the iso3166-2 Python software package. @@ -32,6 +31,9 @@ class ISO3166_2_API_Tests(unittest.TestCase): test_all_endpoint: testing correct data and attributes are returned from the /all API endpoint, which returns all the available ISO 3166-2 data. + test_list_subdivisions: + testing correct data and attributes are returned from the /list_subdivisions API endpoint, which returns all + the ISO 3166-2 subdivision codes for each country. """ def setUp(self): """ Initialise test variables, import json. """ @@ -41,13 +43,14 @@ def setUp(self): 'https://github.com/amckenna41/iso3166-2', getpass.getuser())} #url endpoints for API - # self.api_base_url = "https://iso3166-2-api.vercel.app/api/" - self.api_base_url = "https://iso3166-2-api-amckenna41-amckenna41s-projects.vercel.app/" + self.api_base_url = "https://iso3166-2-api.vercel.app/api/" + # self.api_base_url = "https://iso3166-2-api-amckenna41-amckenna41s-projects.vercel.app/api/" self.alpha_base_url = self.api_base_url + "alpha/" self.subdivision_base_url = self.api_base_url + "subdivision/" self.subdivision_name_base_url = self.api_base_url + "name/" self.country_name_base_url = self.api_base_url + "country_name/" self.all_base_url = self.api_base_url + "all" + self.list_subdivisions_base_url = self.api_base_url + "list_subdivisions" #list of keys that should be in subdivisions key of output object self.correct_subdivision_keys = ["name", "localName", "type", "parentCode", "latLng", "flag"] @@ -67,12 +70,12 @@ def test_homepage_endpoint(self): # last_updated = soup.find(id='last-updated').text.split(': ')[1] author = soup.find(id='author').text.split(': ')[1] - # self.assertEqual(version, "1.5.5", "Expected API version to be 1.5.5, got {}.".format(version)) - # self.assertEqual(last_updated, "March 2024", "Expected last updated date to be March 2024, got {}.".format(last_updated)) + # self.assertEqual(version, "1.6.1", "Expected API version to be 1.6.1, got {}.".format(version)) + # self.assertEqual(last_updated, "March 2024", "Expected last updated date to be June 2024, got {}.".format(last_updated)) self.assertEqual(author, "AJ", "Expected author to be AJ, got {}.".format(author)) #2.) section_list_menu = soup.find(id='section-list-menu').find_all('li') - correct_section_menu = ["About", "Attributes", "Endpoints", "All", "Country alpha code", "Country name", "Subdivision code", "Subdivision name", "Credits", "Contributing"] + correct_section_menu = ["About", "Attributes", "Endpoints", "All", "Country alpha code", "Country name", "Subdivision code", "Subdivision name", "List subdivision codes", "Credits", "Contributing"] for li in section_list_menu: self.assertIn(li.text.strip(), correct_section_menu, "Expected list element {} to be in list.".format(li)) @@ -87,311 +90,311 @@ def test_alpha_endpoint(self): test_alpha_error_2 = "12345" test_alpha_error_3 = "" #1.) - test_iso3166_2_request_au = requests.get(self.alpha_base_url + test_alpha2_au, headers=self.user_agent_header).json() #Australia - - self.assertEqual(list(test_iso3166_2_request_au.keys()), ["AU"], - "Expected output object of API to contain only the AU key, got {}.".format(list(test_iso3166_2_request_au.keys()))) - self.assertEqual(list(test_iso3166_2_request_au["AU"].keys()), ["AU-ACT", "AU-NSW", "AU-NT", "AU-QLD", "AU-SA", "AU-TAS", "AU-VIC", "AU-WA"], - f"Expected list of subdivision codes doesn't match output:\n{list(test_iso3166_2_request_au['AU'].keys())}.") - for subd in test_iso3166_2_request_au["AU"]: - self.assertIsNot(test_iso3166_2_request_au["AU"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_au["AU"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_au["AU"][subd]["name"], test_iso3166_2_request_au["AU"][subd]["localName"], + test_request_au = requests.get(self.alpha_base_url + test_alpha2_au, headers=self.user_agent_header).json() #Australia + + self.assertEqual(list(test_request_au.keys()), ["AU"], + "Expected output object of API to contain only the AU key, got {}.".format(list(test_request_au.keys()))) + self.assertEqual(list(test_request_au["AU"].keys()), ["AU-ACT", "AU-NSW", "AU-NT", "AU-QLD", "AU-SA", "AU-TAS", "AU-VIC", "AU-WA"], + f"Expected list of subdivision codes doesn't match output:\n{list(test_request_au['AU'].keys())}.") + for subd in test_request_au["AU"]: + self.assertIsNot(test_request_au["AU"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_au["AU"][subd]["name"])) + self.assertEqual(test_request_au["AU"][subd]["name"], test_request_au["AU"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_au["AU"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_au["AU"][subd]["parentCode"], list(test_iso3166_2_request_au["AU"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_au["AU"][subd]["parentCode"]), list(test_iso3166_2_request_au["AU"][subd].keys())) - if not (test_iso3166_2_request_au["AU"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_au["AU"][subd]["flag"])[0], self.flag_base_url + "AU/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "AU/" + subd, os.path.splitext(test_iso3166_2_request_au["AU"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_au["AU"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_au["AU"][subd]["flag"])) - for key in list(test_iso3166_2_request_au["AU"][subd].keys()): + if not (test_request_au["AU"][subd]["parentCode"] is None): + self.assertIn(test_request_au["AU"][subd]["parentCode"], list(test_request_au["AU"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_au["AU"][subd]["parentCode"]), list(test_request_au["AU"][subd].keys())) + if not (test_request_au["AU"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_au["AU"][subd]["flag"])[0], self.flag_base_url + "AU/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "AU/" + subd, os.path.splitext(test_request_au["AU"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_au["AU"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_au["AU"][subd]["flag"])) + for key in list(test_request_au["AU"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) #AU-NSW - New South Wales - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-NSW"]["name"], "New South Wales", - "Expected subdivision name to be New South Wales, got {}.".format(test_iso3166_2_request_au["AU"]["AU-NSW"]["name"])) - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-NSW"]["localName"], "New South Wales", - "Expected subdivision local name to be New South Wales, got {}.".format(test_iso3166_2_request_au["AU"]["AU-NSW"]["localName"])) - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-NSW"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_au["AU"]["AU-NSW"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-NSW"]["type"], "State", - "Expected subdivision type to be State, got {}.".format(test_iso3166_2_request_au["AU"]["AU-NSW"]["type"])) - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-NSW"]["latLng"], [-31.253, 146.921], - "Expected subdivision latLng to be [-31.253, 146.921], got {}.".format(test_iso3166_2_request_au["AU"]["AU-NSW"]["latLng"])) - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-NSW"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/AU/AU-NSW.svg", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/AU/AU-NSW.svg, got {}.".format(test_iso3166_2_request_au["AU"]["AU-NSW"]["flag"])) + self.assertEqual(test_request_au["AU"]["AU-NSW"]["name"], "New South Wales", + "Expected subdivision name to be New South Wales, got {}.".format(test_request_au["AU"]["AU-NSW"]["name"])) + self.assertEqual(test_request_au["AU"]["AU-NSW"]["localName"], "New South Wales", + "Expected subdivision local name to be New South Wales, got {}.".format(test_request_au["AU"]["AU-NSW"]["localName"])) + self.assertEqual(test_request_au["AU"]["AU-NSW"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_au["AU"]["AU-NSW"]["parentCode"])) + self.assertEqual(test_request_au["AU"]["AU-NSW"]["type"], "State", + "Expected subdivision type to be State, got {}.".format(test_request_au["AU"]["AU-NSW"]["type"])) + self.assertEqual(test_request_au["AU"]["AU-NSW"]["latLng"], [-31.253, 146.921], + "Expected subdivision latLng to be [-31.253, 146.921], got {}.".format(test_request_au["AU"]["AU-NSW"]["latLng"])) + self.assertEqual(test_request_au["AU"]["AU-NSW"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/AU/AU-NSW.svg", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/AU/AU-NSW.svg, got {}.".format(test_request_au["AU"]["AU-NSW"]["flag"])) #AU-QLD - Queensland - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-QLD"]["name"], "Queensland", - "Expected subdivision name to be Queensland, got {}.".format(test_iso3166_2_request_au["AU"]["AU-QLD"]["name"])) - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-QLD"]["localName"], "Queensland", - "Expected subdivision local name to be Queensland, got {}.".format(test_iso3166_2_request_au["AU"]["AU-QLD"]["localName"])) - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-QLD"]["parentCode"], None, - "Expected subdivision parent code to be None got {}.".format(test_iso3166_2_request_au["AU"]["AU-QLD"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-QLD"]["type"], "State", - "Expected subdivision type to be State, got {}.".format(test_iso3166_2_request_au["AU"]["AU-QLD"]["type"])) - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-QLD"]["latLng"], [-22.575, 144.085], - "Expected subdivision latLng to be [-22.575, 144.085], got {}.".format(test_iso3166_2_request_au["AU"]["AU-NSW"]["latLng"])) - self.assertEqual(test_iso3166_2_request_au["AU"]["AU-QLD"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/AU/AU-QLD.svg", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/AU/AU-QLD.svg, got {}.".format(test_iso3166_2_request_au["AU"]["AU-QLD"]["flag"])) + self.assertEqual(test_request_au["AU"]["AU-QLD"]["name"], "Queensland", + "Expected subdivision name to be Queensland, got {}.".format(test_request_au["AU"]["AU-QLD"]["name"])) + self.assertEqual(test_request_au["AU"]["AU-QLD"]["localName"], "Queensland", + "Expected subdivision local name to be Queensland, got {}.".format(test_request_au["AU"]["AU-QLD"]["localName"])) + self.assertEqual(test_request_au["AU"]["AU-QLD"]["parentCode"], None, + "Expected subdivision parent code to be None got {}.".format(test_request_au["AU"]["AU-QLD"]["parentCode"])) + self.assertEqual(test_request_au["AU"]["AU-QLD"]["type"], "State", + "Expected subdivision type to be State, got {}.".format(test_request_au["AU"]["AU-QLD"]["type"])) + self.assertEqual(test_request_au["AU"]["AU-QLD"]["latLng"], [-22.575, 144.085], + "Expected subdivision latLng to be [-22.575, 144.085], got {}.".format(test_request_au["AU"]["AU-NSW"]["latLng"])) + self.assertEqual(test_request_au["AU"]["AU-QLD"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/AU/AU-QLD.svg", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/AU/AU-QLD.svg, got {}.".format(test_request_au["AU"]["AU-QLD"]["flag"])) #2.) - test_iso3166_2_request_lux = requests.get(self.alpha_base_url + test_alpha3_lux, headers=self.user_agent_header).json() #Luxembourg - - self.assertEqual(list(test_iso3166_2_request_lux.keys()), ["LU"], - "Expected output object of API to contain only the LU key, got {}.".format(list(test_iso3166_2_request_lux.keys()))) - self.assertEqual(list(test_iso3166_2_request_lux["LU"].keys()), ["LU-CA", "LU-CL", "LU-DI", "LU-EC", "LU-ES", "LU-GR", "LU-LU", "LU-ME", "LU-RD", "LU-RM", "LU-VD", "LU-WI"], - f"Expected list of subdivision codes doesn't match output:\n{list(test_iso3166_2_request_lux['LU'].keys())}.") - for subd in test_iso3166_2_request_lux["LU"]: - self.assertIsNot(test_iso3166_2_request_lux["LU"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_lux["LU"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_lux["LU"][subd]["name"], test_iso3166_2_request_lux["LU"][subd]["localName"], + test_request_lux = requests.get(self.alpha_base_url + test_alpha3_lux, headers=self.user_agent_header).json() #Luxembourg + + self.assertEqual(list(test_request_lux.keys()), ["LU"], + "Expected output object of API to contain only the LU key, got {}.".format(list(test_request_lux.keys()))) + self.assertEqual(list(test_request_lux["LU"].keys()), ["LU-CA", "LU-CL", "LU-DI", "LU-EC", "LU-ES", "LU-GR", "LU-LU", "LU-ME", "LU-RD", "LU-RM", "LU-VD", "LU-WI"], + f"Expected list of subdivision codes doesn't match output:\n{list(test_request_lux['LU'].keys())}.") + for subd in test_request_lux["LU"]: + self.assertIsNot(test_request_lux["LU"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_lux["LU"][subd]["name"])) + self.assertEqual(test_request_lux["LU"][subd]["name"], test_request_lux["LU"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_lux["LU"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_lux["LU"][subd]["parentCode"], list(test_iso3166_2_request_lux["LU"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_lux["LU"][subd]["parentCode"]), list(test_iso3166_2_request_lux["LU"][subd].keys())) - if not (test_iso3166_2_request_lux["LU"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_lux["LU"][subd]["flag"])[0], self.flag_base_url + "LU/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "LUX/" + subd, os.path.splitext(test_iso3166_2_request_lux["LU"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_lux["LU"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_lux["LU"][subd]["flag"])) - for key in list(test_iso3166_2_request_lux["LU"][subd].keys()): + if not (test_request_lux["LU"][subd]["parentCode"] is None): + self.assertIn(test_request_lux["LU"][subd]["parentCode"], list(test_request_lux["LU"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_lux["LU"][subd]["parentCode"]), list(test_request_lux["LU"][subd].keys())) + if not (test_request_lux["LU"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_lux["LU"][subd]["flag"])[0], self.flag_base_url + "LU/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "LUX/" + subd, os.path.splitext(test_request_lux["LU"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_lux["LU"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_lux["LU"][subd]["flag"])) + for key in list(test_request_lux["LU"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) #LU-CA - Capellen - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-CA"]["name"], "Capellen", - "Expected subdivision name to be Capellen, got {}.".format(test_iso3166_2_request_lux["LU"]["LU-CA"]["name"])) - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-CA"]["localName"], "Capellen", - "Expected subdivision local name to be Capellen, got {}.".format(test_iso3166_2_request_lux["LU"]["LU-CA"]["localName"])) - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-CA"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_lux["LU"]["LU-CA"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-CA"]["type"], "Canton", - "Expected subdivision type to be Canton, got {}.".format(test_iso3166_2_request_lux["LU"]["LU-CA"]["type"])) - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-CA"]["latLng"], [49.646, 5.991], - "Expected subdivision latLng to be [49.646, 5.991], got {}.".format(test_iso3166_2_request_lux["LU"]["LU-CA"]["latLng"])) - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-CA"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/LU/LU-CA.svg", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/LU/LU-CA.svg, got {}.".format(test_iso3166_2_request_lux["LU"]["LU-CA"]["flag"])) + self.assertEqual(test_request_lux["LU"]["LU-CA"]["name"], "Capellen", + "Expected subdivision name to be Capellen, got {}.".format(test_request_lux["LU"]["LU-CA"]["name"])) + self.assertEqual(test_request_lux["LU"]["LU-CA"]["localName"], "Capellen", + "Expected subdivision local name to be Capellen, got {}.".format(test_request_lux["LU"]["LU-CA"]["localName"])) + self.assertEqual(test_request_lux["LU"]["LU-CA"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_lux["LU"]["LU-CA"]["parentCode"])) + self.assertEqual(test_request_lux["LU"]["LU-CA"]["type"], "Canton", + "Expected subdivision type to be Canton, got {}.".format(test_request_lux["LU"]["LU-CA"]["type"])) + self.assertEqual(test_request_lux["LU"]["LU-CA"]["latLng"], [49.646, 5.991], + "Expected subdivision latLng to be [49.646, 5.991], got {}.".format(test_request_lux["LU"]["LU-CA"]["latLng"])) + self.assertEqual(test_request_lux["LU"]["LU-CA"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/LU/LU-CA.svg", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/LU/LU-CA.svg, got {}.".format(test_request_lux["LU"]["LU-CA"]["flag"])) #LU-LU - Luxembourg - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-LU"]["name"], "Luxembourg", - "Expected subdivision name to be Luxembourg, got {}.".format(test_iso3166_2_request_lux["LU"]["LU-LU"]["name"])) - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-LU"]["localName"], "Luxembourg", - "Expected subdivision local name to be Luxembourg, got {}.".format(test_iso3166_2_request_lux["LU"]["LU-LU"]["localName"])) - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-LU"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_lux["LU"]["LU-LU"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-LU"]["type"], "Canton", - "Expected subdivision type to be Canton, got {}.".format(test_iso3166_2_request_lux["LU"]["LU-LU"]["type"])) - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-LU"]["latLng"], [49.815, 6.13], - "Expected subdivision latLng to be [49.815, 6.13], got {}.".format(test_iso3166_2_request_lux["LU"]["LU-LU"]["latLng"])) - self.assertEqual(test_iso3166_2_request_lux["LU"]["LU-LU"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/LU/LU-LU.png", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/LU/LU-LU.png, got {}.".format(test_iso3166_2_request_lux["LU"]["LU-LU"]["flag"])) + self.assertEqual(test_request_lux["LU"]["LU-LU"]["name"], "Luxembourg", + "Expected subdivision name to be Luxembourg, got {}.".format(test_request_lux["LU"]["LU-LU"]["name"])) + self.assertEqual(test_request_lux["LU"]["LU-LU"]["localName"], "Luxembourg", + "Expected subdivision local name to be Luxembourg, got {}.".format(test_request_lux["LU"]["LU-LU"]["localName"])) + self.assertEqual(test_request_lux["LU"]["LU-LU"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_lux["LU"]["LU-LU"]["parentCode"])) + self.assertEqual(test_request_lux["LU"]["LU-LU"]["type"], "Canton", + "Expected subdivision type to be Canton, got {}.".format(test_request_lux["LU"]["LU-LU"]["type"])) + self.assertEqual(test_request_lux["LU"]["LU-LU"]["latLng"], [49.815, 6.13], + "Expected subdivision latLng to be [49.815, 6.13], got {}.".format(test_request_lux["LU"]["LU-LU"]["latLng"])) + self.assertEqual(test_request_lux["LU"]["LU-LU"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/LU/LU-LU.png", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/LU/LU-LU.png, got {}.".format(test_request_lux["LU"]["LU-LU"]["flag"])) #3.) - test_iso3166_2_request_pa_rw = requests.get(self.alpha_base_url + test_alpha3_pa_rw, headers=self.user_agent_header).json() #Panama and Rwanda - - self.assertEqual(list(test_iso3166_2_request_pa_rw.keys()), ["PA", "RW"], - "Expected output object of API to contain only the PA and RW keys, got {}.".format(list(test_iso3166_2_request_pa_rw.keys()))) - self.assertEqual(list(test_iso3166_2_request_pa_rw["PA"].keys()), ["PA-1", "PA-10", "PA-2", "PA-3", "PA-4", "PA-5", "PA-6", "PA-7", "PA-8", "PA-9", "PA-EM", "PA-KY", "PA-NB", "PA-NT"], - f"Expected list of subdivision codes doesn't match output:\n{list(test_iso3166_2_request_pa_rw['PA'].keys())}.") - self.assertEqual(list(test_iso3166_2_request_pa_rw["RW"].keys()), ["RW-01", "RW-02", "RW-03", "RW-04", "RW-05"], - f"Expected list of subdivision codes doesn't match output:\n{list(test_iso3166_2_request_pa_rw['RW'].keys())}.") - for subd in test_iso3166_2_request_pa_rw["PA"]: - self.assertIsNot(test_iso3166_2_request_pa_rw["PA"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_pa_rw["PA"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_pa_rw["PA"][subd]["name"], test_iso3166_2_request_pa_rw["PA"][subd]["localName"], + test_request_pa_rw = requests.get(self.alpha_base_url + test_alpha3_pa_rw, headers=self.user_agent_header).json() #Panama and Rwanda + + self.assertEqual(list(test_request_pa_rw.keys()), ["PA", "RW"], + "Expected output object of API to contain only the PA and RW keys, got {}.".format(list(test_request_pa_rw.keys()))) + self.assertEqual(list(test_request_pa_rw["PA"].keys()), ["PA-1", "PA-10", "PA-2", "PA-3", "PA-4", "PA-5", "PA-6", "PA-7", "PA-8", "PA-9", "PA-EM", "PA-KY", "PA-NB", "PA-NT"], + f"Expected list of subdivision codes doesn't match output:\n{list(test_request_pa_rw['PA'].keys())}.") + self.assertEqual(list(test_request_pa_rw["RW"].keys()), ["RW-01", "RW-02", "RW-03", "RW-04", "RW-05"], + f"Expected list of subdivision codes doesn't match output:\n{list(test_request_pa_rw['RW'].keys())}.") + for subd in test_request_pa_rw["PA"]: + self.assertIsNot(test_request_pa_rw["PA"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_pa_rw["PA"][subd]["name"])) + self.assertEqual(test_request_pa_rw["PA"][subd]["name"], test_request_pa_rw["PA"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_pa_rw["PA"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_pa_rw["PA"][subd]["parentCode"], list(test_iso3166_2_request_pa_rw["PA"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_pa_rw["PA"][subd]["parentCode"]), list(test_iso3166_2_request_pa_rw["PA"][subd].keys())) - if not (test_iso3166_2_request_pa_rw["PA"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_pa_rw["PA"][subd]["flag"])[0], self.flag_base_url + "PA/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "PA/" + subd, os.path.splitext(test_iso3166_2_request_pa_rw["PA"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_pa_rw["PA"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_pa_rw["PA"][subd]["flag"])) - for key in list(test_iso3166_2_request_pa_rw["PA"][subd].keys()): + if not (test_request_pa_rw["PA"][subd]["parentCode"] is None): + self.assertIn(test_request_pa_rw["PA"][subd]["parentCode"], list(test_request_pa_rw["PA"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_pa_rw["PA"][subd]["parentCode"]), list(test_request_pa_rw["PA"][subd].keys())) + if not (test_request_pa_rw["PA"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_pa_rw["PA"][subd]["flag"])[0], self.flag_base_url + "PA/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "PA/" + subd, os.path.splitext(test_request_pa_rw["PA"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_pa_rw["PA"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_pa_rw["PA"][subd]["flag"])) + for key in list(test_request_pa_rw["PA"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) - for subd in test_iso3166_2_request_pa_rw["RW"]: - self.assertIsNot(test_iso3166_2_request_pa_rw["RW"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_pa_rw["RW"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_pa_rw["RW"][subd]["name"], test_iso3166_2_request_pa_rw["RW"][subd]["localName"], + for subd in test_request_pa_rw["RW"]: + self.assertIsNot(test_request_pa_rw["RW"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_pa_rw["RW"][subd]["name"])) + self.assertEqual(test_request_pa_rw["RW"][subd]["name"], test_request_pa_rw["RW"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_pa_rw["RW"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_pa_rw["RW"][subd]["parentCode"], list(test_iso3166_2_request_pa_rw["RW"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_pa_rw["RW"][subd]["parentCode"]), list(test_iso3166_2_request_pa_rw["RW"][subd].keys())) - if not (test_iso3166_2_request_pa_rw["RW"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_pa_rw["RW"][subd]["flag"])[0], self.flag_base_url + "RW/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "RW/" + subd, os.path.splitext(test_iso3166_2_request_pa_rw["RW"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_pa_rw["RW"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_pa_rw["RW"][subd]["flag"])) - for key in list(test_iso3166_2_request_pa_rw["RW"][subd].keys()): + if not (test_request_pa_rw["RW"][subd]["parentCode"] is None): + self.assertIn(test_request_pa_rw["RW"][subd]["parentCode"], list(test_request_pa_rw["RW"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_pa_rw["RW"][subd]["parentCode"]), list(test_request_pa_rw["RW"][subd].keys())) + if not (test_request_pa_rw["RW"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_pa_rw["RW"][subd]["flag"])[0], self.flag_base_url + "RW/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "RW/" + subd, os.path.splitext(test_request_pa_rw["RW"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_pa_rw["RW"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_pa_rw["RW"][subd]["flag"])) + for key in list(test_request_pa_rw["RW"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) #PA-4 - Chiriquí - self.assertEqual(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["name"], "Chiriquí", - "Expected subdivision name to be Chiriquí, got {}.".format(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["name"])) - self.assertEqual(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["localName"], "Chiriquí", - "Expected subdivision local name to be Chiriquí, got {}.".format(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["localName"])) - self.assertEqual(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["type"], "Province", - "Expected subdivision type to be Province, got {}.".format(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["type"])) - self.assertEqual(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["latLng"], [8.387, -82.28], - "Expected subdivision latLng to be [8.387, -82.28], got {}.".format(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["latLng"])) - self.assertEqual(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/PA/PA-4.svg", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/PA/PA-4.svg, got {}.".format(test_iso3166_2_request_pa_rw["PA"]["PA-4"]["flag"])) + self.assertEqual(test_request_pa_rw["PA"]["PA-4"]["name"], "Chiriquí", + "Expected subdivision name to be Chiriquí, got {}.".format(test_request_pa_rw["PA"]["PA-4"]["name"])) + self.assertEqual(test_request_pa_rw["PA"]["PA-4"]["localName"], "Chiriquí", + "Expected subdivision local name to be Chiriquí, got {}.".format(test_request_pa_rw["PA"]["PA-4"]["localName"])) + self.assertEqual(test_request_pa_rw["PA"]["PA-4"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_pa_rw["PA"]["PA-4"]["parentCode"])) + self.assertEqual(test_request_pa_rw["PA"]["PA-4"]["type"], "Province", + "Expected subdivision type to be Province, got {}.".format(test_request_pa_rw["PA"]["PA-4"]["type"])) + self.assertEqual(test_request_pa_rw["PA"]["PA-4"]["latLng"], [8.387, -82.28], + "Expected subdivision latLng to be [8.387, -82.28], got {}.".format(test_request_pa_rw["PA"]["PA-4"]["latLng"])) + self.assertEqual(test_request_pa_rw["PA"]["PA-4"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/PA/PA-4.svg", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/PA/PA-4.svg, got {}.".format(test_request_pa_rw["PA"]["PA-4"]["flag"])) #RW-03 - Northern - self.assertEqual(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["name"], "Northern", - "Expected subdivision name to be Northern, got {}.".format(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["name"])) - self.assertEqual(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["localName"], "Northern", - "Expected subdivision local name to be Northern, got {}.".format(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["localName"])) - self.assertEqual(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["type"], "Province", - "Expected subdivision type to be Province, got {}.".format(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["type"])) - self.assertEqual(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["latLng"], [-1.656, 29.882], - "Expected subdivision latLng to be [-1.656, 29.882], got {}.".format(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["latLng"])) - self.assertEqual(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["flag"], None, - "Expected subdivision flag URL to be None, got {}.".format(test_iso3166_2_request_pa_rw["RW"]["RW-03"]["flag"])) + self.assertEqual(test_request_pa_rw["RW"]["RW-03"]["name"], "Northern", + "Expected subdivision name to be Northern, got {}.".format(test_request_pa_rw["RW"]["RW-03"]["name"])) + self.assertEqual(test_request_pa_rw["RW"]["RW-03"]["localName"], "Northern", + "Expected subdivision local name to be Northern, got {}.".format(test_request_pa_rw["RW"]["RW-03"]["localName"])) + self.assertEqual(test_request_pa_rw["RW"]["RW-03"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_pa_rw["RW"]["RW-03"]["parentCode"])) + self.assertEqual(test_request_pa_rw["RW"]["RW-03"]["type"], "Province", + "Expected subdivision type to be Province, got {}.".format(test_request_pa_rw["RW"]["RW-03"]["type"])) + self.assertEqual(test_request_pa_rw["RW"]["RW-03"]["latLng"], [-1.656, 29.882], + "Expected subdivision latLng to be [-1.656, 29.882], got {}.".format(test_request_pa_rw["RW"]["RW-03"]["latLng"])) + self.assertEqual(test_request_pa_rw["RW"]["RW-03"]["flag"], None, + "Expected subdivision flag URL to be None, got {}.".format(test_request_pa_rw["RW"]["RW-03"]["flag"])) #4.) - test_iso3166_2_request_740_752 = requests.get(self.alpha_base_url + test_numeric_740_752, headers=self.user_agent_header).json() #740 - Suriname, 752 - Sweden + test_request_740_752 = requests.get(self.alpha_base_url + test_numeric_740_752, headers=self.user_agent_header).json() #740 - Suriname, 752 - Sweden - self.assertEqual(list(test_iso3166_2_request_740_752.keys()), ["SE", "SR"], - "Expected output object of API to contain SR and SE keys, got {}.".format(list(test_iso3166_2_request_740_752.keys()))) - self.assertEqual(list(test_iso3166_2_request_740_752["SE"].keys()), ['SE-AB', 'SE-AC', 'SE-BD', 'SE-C', 'SE-D', 'SE-E', 'SE-F', 'SE-G', 'SE-H', 'SE-I', 'SE-K', \ + self.assertEqual(list(test_request_740_752.keys()), ["SE", "SR"], + "Expected output object of API to contain SR and SE keys, got {}.".format(list(test_request_740_752.keys()))) + self.assertEqual(list(test_request_740_752["SE"].keys()), ['SE-AB', 'SE-AC', 'SE-BD', 'SE-C', 'SE-D', 'SE-E', 'SE-F', 'SE-G', 'SE-H', 'SE-I', 'SE-K', \ 'SE-M', 'SE-N', 'SE-O', 'SE-S', 'SE-T', 'SE-U', 'SE-W', 'SE-X', 'SE-Y', 'SE-Z'], - f"Expected list of subdivision codes doesn't match output:\n{list(test_iso3166_2_request_740_752['SE'].keys())}.") - self.assertEqual(list(test_iso3166_2_request_740_752["SR"].keys()), ["SR-BR", "SR-CM", "SR-CR", "SR-MA", "SR-NI", "SR-PM", "SR-PR", "SR-SA", "SR-SI", "SR-WA"], - f"Expected list of subdivision codes doesn't match output:\n{list(test_iso3166_2_request_740_752['SR'].keys())}.") - for subd in test_iso3166_2_request_740_752["SR"]: - self.assertIsNot(test_iso3166_2_request_740_752["SR"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_740_752["SR"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_740_752["SR"][subd]["name"], test_iso3166_2_request_740_752["SR"][subd]["localName"], + f"Expected list of subdivision codes doesn't match output:\n{list(test_request_740_752['SE'].keys())}.") + self.assertEqual(list(test_request_740_752["SR"].keys()), ["SR-BR", "SR-CM", "SR-CR", "SR-MA", "SR-NI", "SR-PM", "SR-PR", "SR-SA", "SR-SI", "SR-WA"], + f"Expected list of subdivision codes doesn't match output:\n{list(test_request_740_752['SR'].keys())}.") + for subd in test_request_740_752["SR"]: + self.assertIsNot(test_request_740_752["SR"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_740_752["SR"][subd]["name"])) + self.assertEqual(test_request_740_752["SR"][subd]["name"], test_request_740_752["SR"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_740_752["SR"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_740_752["SR"][subd]["parentCode"], list(test_iso3166_2_request_740_752["SR"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_740_752["SR"][subd]["parentCode"]), list(test_iso3166_2_request_740_752["SR"][subd].keys())) - if not (test_iso3166_2_request_740_752["SR"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_740_752["SR"][subd]["flag"])[0], self.flag_base_url + "SR/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "SR/" + subd, os.path.splitext(test_iso3166_2_request_740_752["SR"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_740_752["SR"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_740_752["SR"][subd]["flag"])) - for key in list(test_iso3166_2_request_740_752["SR"][subd].keys()): + if not (test_request_740_752["SR"][subd]["parentCode"] is None): + self.assertIn(test_request_740_752["SR"][subd]["parentCode"], list(test_request_740_752["SR"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_740_752["SR"][subd]["parentCode"]), list(test_request_740_752["SR"][subd].keys())) + if not (test_request_740_752["SR"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_740_752["SR"][subd]["flag"])[0], self.flag_base_url + "SR/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "SR/" + subd, os.path.splitext(test_request_740_752["SR"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_740_752["SR"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_740_752["SR"][subd]["flag"])) + for key in list(test_request_740_752["SR"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) - for subd in test_iso3166_2_request_740_752["SE"]: - self.assertIsNot(test_iso3166_2_request_740_752["SE"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_740_752["SE"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_740_752["SE"][subd]["name"], test_iso3166_2_request_740_752["SE"][subd]["localName"], + for subd in test_request_740_752["SE"]: + self.assertIsNot(test_request_740_752["SE"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_740_752["SE"][subd]["name"])) + self.assertEqual(test_request_740_752["SE"][subd]["name"], test_request_740_752["SE"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_740_752["SE"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_740_752["SE"][subd]["parentCode"], list(test_iso3166_2_request_740_752["SE"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_740_752["SE"][subd]["parentCode"]), list(test_iso3166_2_request_740_752["SE"][subd].keys())) - if not (test_iso3166_2_request_740_752["SE"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_740_752["SE"][subd]["flag"])[0], self.flag_base_url + "SE/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "SE/" + subd, os.path.splitext(test_iso3166_2_request_740_752["SE"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_740_752["SE"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_740_752["SE"][subd]["flag"])) - for key in list(test_iso3166_2_request_740_752["SE"][subd].keys()): + if not (test_request_740_752["SE"][subd]["parentCode"] is None): + self.assertIn(test_request_740_752["SE"][subd]["parentCode"], list(test_request_740_752["SE"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_740_752["SE"][subd]["parentCode"]), list(test_request_740_752["SE"][subd].keys())) + if not (test_request_740_752["SE"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_740_752["SE"][subd]["flag"])[0], self.flag_base_url + "SE/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "SE/" + subd, os.path.splitext(test_request_740_752["SE"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_740_752["SE"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_740_752["SE"][subd]["flag"])) + for key in list(test_request_740_752["SE"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) #SR-SI - Sipaliwini - self.assertEqual(test_iso3166_2_request_740_752["SR"]["SR-SI"]["name"], "Sipaliwini", - "Expected subdivision name to be Sipaliwini, got {}.".format(test_iso3166_2_request_740_752["SR"]["SR-SI"]["name"])) - self.assertEqual(test_iso3166_2_request_740_752["SR"]["SR-SI"]["localName"], "Sipaliwini", - "Expected subdivision local name to be Sipaliwini, got {}.".format(test_iso3166_2_request_740_752["SR"]["SR-SI"]["localName"])) - self.assertEqual(test_iso3166_2_request_740_752["SR"]["SR-SI"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_740_752["SR"]["SR-SI"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_740_752["SR"]["SR-SI"]["type"], "District", - "Expected subdivision type to be District, got {}.".format(test_iso3166_2_request_740_752["SR"]["SR-SI"]["type"])) - self.assertEqual(test_iso3166_2_request_740_752["SR"]["SR-SI"]["latLng"], [2.033, -56.134], - "Expected subdivision latLng to be [2.033, -56.134], got {}.".format(test_iso3166_2_request_740_752["SR"]["SR-SI"]["latLng"])) - self.assertEqual(test_iso3166_2_request_740_752["SR"]["SR-SI"]["flag"], None, - "Expected subdivision flag URL to be None, got {}.".format(test_iso3166_2_request_740_752["SR"]["SR-SI"]["flag"])) + self.assertEqual(test_request_740_752["SR"]["SR-SI"]["name"], "Sipaliwini", + "Expected subdivision name to be Sipaliwini, got {}.".format(test_request_740_752["SR"]["SR-SI"]["name"])) + self.assertEqual(test_request_740_752["SR"]["SR-SI"]["localName"], "Sipaliwini", + "Expected subdivision local name to be Sipaliwini, got {}.".format(test_request_740_752["SR"]["SR-SI"]["localName"])) + self.assertEqual(test_request_740_752["SR"]["SR-SI"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_740_752["SR"]["SR-SI"]["parentCode"])) + self.assertEqual(test_request_740_752["SR"]["SR-SI"]["type"], "District", + "Expected subdivision type to be District, got {}.".format(test_request_740_752["SR"]["SR-SI"]["type"])) + self.assertEqual(test_request_740_752["SR"]["SR-SI"]["latLng"], [3.657, -56.204], + "Expected subdivision latLng to be [2.033, -56.134], got {}.".format(test_request_740_752["SR"]["SR-SI"]["latLng"])) + self.assertEqual(test_request_740_752["SR"]["SR-SI"]["flag"], None, + "Expected subdivision flag URL to be None, got {}.".format(test_request_740_752["SR"]["SR-SI"]["flag"])) #SE-I - Gotlands - self.assertEqual(test_iso3166_2_request_740_752["SE"]["SE-I"]["name"], "Gotlands län [SE-09]", - "Expected subdivision name to be Gotlands län [SE-09], got {}.".format(test_iso3166_2_request_740_752["SE"]["SE-I"]["name"])) - self.assertEqual(test_iso3166_2_request_740_752["SE"]["SE-I"]["localName"], "Gotlands län [SE-09]", - "Expected subdivision local name to be Gotlands län [SE-09], got {}.".format(test_iso3166_2_request_740_752["SE"]["SE-I"]["localName"])) - self.assertEqual(test_iso3166_2_request_740_752["SE"]["SE-I"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_740_752["SE"]["SE-I"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_740_752["SE"]["SE-I"]["type"], "County", - "Expected subdivision type to be County, got {}.".format(test_iso3166_2_request_740_752["SE"]["SE-I"]["type"])) - self.assertEqual(test_iso3166_2_request_740_752["SE"]["SE-I"]["latLng"], [57.531, 18.69], - "Expected subdivision latLng to be [57.531, 18.69], got {}.".format(test_iso3166_2_request_740_752["SE"]["SE-I"]["latLng"])) - self.assertEqual(test_iso3166_2_request_740_752["SE"]["SE-I"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/SE/SE-I.svg", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/SE/SE-I.svg, got {}.".format(test_iso3166_2_request_740_752["SE"]["SE-I"]["flag"])) + self.assertEqual(test_request_740_752["SE"]["SE-I"]["name"], "Gotlands län [SE-09]", + "Expected subdivision name to be Gotlands län [SE-09], got {}.".format(test_request_740_752["SE"]["SE-I"]["name"])) + self.assertEqual(test_request_740_752["SE"]["SE-I"]["localName"], "Gotlands län [SE-09]", + "Expected subdivision local name to be Gotlands län [SE-09], got {}.".format(test_request_740_752["SE"]["SE-I"]["localName"])) + self.assertEqual(test_request_740_752["SE"]["SE-I"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_740_752["SE"]["SE-I"]["parentCode"])) + self.assertEqual(test_request_740_752["SE"]["SE-I"]["type"], "County", + "Expected subdivision type to be County, got {}.".format(test_request_740_752["SE"]["SE-I"]["type"])) + self.assertEqual(test_request_740_752["SE"]["SE-I"]["latLng"], [57.531, 18.69], + "Expected subdivision latLng to be [57.531, 18.69], got {}.".format(test_request_740_752["SE"]["SE-I"]["latLng"])) + self.assertEqual(test_request_740_752["SE"]["SE-I"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/SE/SE-I.svg", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/SE/SE-I.svg, got {}.".format(test_request_740_752["SE"]["SE-I"]["flag"])) #5.) - test_iso3166_2_request_ir_kgz_446 = requests.get(self.alpha_base_url + test_alpha2_alpha3_numeric_ir_kgz_446, headers=self.user_agent_header).json() #Iran, Kyrgyzstan, Macao + test_request_ir_kgz_446 = requests.get(self.alpha_base_url + test_alpha2_alpha3_numeric_ir_kgz_446, headers=self.user_agent_header).json() #Iran, Kyrgyzstan, Macao - self.assertEqual(list(test_iso3166_2_request_ir_kgz_446.keys()), ["IR", "KG", "MO"], - "Expected output object of API to contain IT, KG and MO keys, got {}.".format(list(test_iso3166_2_request_ir_kgz_446.keys()))) - self.assertEqual(list(test_iso3166_2_request_ir_kgz_446["IR"].keys()), ['IR-00', 'IR-01', 'IR-02', 'IR-03', 'IR-04', 'IR-05', 'IR-06', 'IR-07', 'IR-08', 'IR-09', 'IR-10', 'IR-11', 'IR-12', 'IR-13', 'IR-14', \ + self.assertEqual(list(test_request_ir_kgz_446.keys()), ["IR", "KG", "MO"], + "Expected output object of API to contain IT, KG and MO keys, got {}.".format(list(test_request_ir_kgz_446.keys()))) + self.assertEqual(list(test_request_ir_kgz_446["IR"].keys()), ['IR-00', 'IR-01', 'IR-02', 'IR-03', 'IR-04', 'IR-05', 'IR-06', 'IR-07', 'IR-08', 'IR-09', 'IR-10', 'IR-11', 'IR-12', 'IR-13', 'IR-14', \ 'IR-15', 'IR-16', 'IR-17', 'IR-18', 'IR-19', 'IR-20', 'IR-21', 'IR-22', 'IR-23', 'IR-24', 'IR-25', 'IR-26', 'IR-27', 'IR-28', 'IR-29', 'IR-30'], - f"Expected list of subdivision codes doesn't match output:\n{list(test_iso3166_2_request_ir_kgz_446['IR'].keys())}.") - self.assertEqual(list(test_iso3166_2_request_ir_kgz_446["KG"].keys()), ['KG-B', 'KG-C', 'KG-GB', 'KG-GO', 'KG-J', 'KG-N', 'KG-O', 'KG-T', 'KG-Y'], - f"Expected list of subdivision codes doesn't match output:\n{list(test_iso3166_2_request_ir_kgz_446['KG'].keys())}.") - self.assertEqual(list(test_iso3166_2_request_ir_kgz_446["MO"].keys()), [], "Expected no subdivision codes in output object.") + f"Expected list of subdivision codes doesn't match output:\n{list(test_request_ir_kgz_446['IR'].keys())}.") + self.assertEqual(list(test_request_ir_kgz_446["KG"].keys()), ['KG-B', 'KG-C', 'KG-GB', 'KG-GO', 'KG-J', 'KG-N', 'KG-O', 'KG-T', 'KG-Y'], + f"Expected list of subdivision codes doesn't match output:\n{list(test_request_ir_kgz_446['KG'].keys())}.") + self.assertEqual(list(test_request_ir_kgz_446["MO"].keys()), [], "Expected no subdivision codes in output object.") - for subd in test_iso3166_2_request_ir_kgz_446["IR"]: - self.assertIsNot(test_iso3166_2_request_ir_kgz_446["IR"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_ir_kgz_446["IR"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_ir_kgz_446["IR"][subd]["name"], test_iso3166_2_request_ir_kgz_446["IR"][subd]["localName"], + for subd in test_request_ir_kgz_446["IR"]: + self.assertIsNot(test_request_ir_kgz_446["IR"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_ir_kgz_446["IR"][subd]["name"])) + self.assertEqual(test_request_ir_kgz_446["IR"][subd]["name"], test_request_ir_kgz_446["IR"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_ir_kgz_446["IR"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_ir_kgz_446["IR"][subd]["parentCode"], list(test_iso3166_2_request_ir_kgz_446["IR"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_ir_kgz_446["IR"][subd]["parentCode"]), list(test_iso3166_2_request_ir_kgz_446["IR"][subd].keys())) - if not (test_iso3166_2_request_ir_kgz_446["IR"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_ir_kgz_446["IR"][subd]["flag"])[0], self.flag_base_url + "IR/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "IR/" + subd, os.path.splitext(test_iso3166_2_request_ir_kgz_446["IR"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_ir_kgz_446["IR"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_ir_kgz_446["IR"][subd]["flag"])) - for key in list(test_iso3166_2_request_ir_kgz_446["IR"][subd].keys()): + if not (test_request_ir_kgz_446["IR"][subd]["parentCode"] is None): + self.assertIn(test_request_ir_kgz_446["IR"][subd]["parentCode"], list(test_request_ir_kgz_446["IR"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_ir_kgz_446["IR"][subd]["parentCode"]), list(test_request_ir_kgz_446["IR"][subd].keys())) + if not (test_request_ir_kgz_446["IR"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_ir_kgz_446["IR"][subd]["flag"])[0], self.flag_base_url + "IR/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "IR/" + subd, os.path.splitext(test_request_ir_kgz_446["IR"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_ir_kgz_446["IR"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_ir_kgz_446["IR"][subd]["flag"])) + for key in list(test_request_ir_kgz_446["IR"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) - for subd in test_iso3166_2_request_ir_kgz_446["KG"]: - self.assertIsNot(test_iso3166_2_request_ir_kgz_446["KG"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_ir_kgz_446["KG"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_ir_kgz_446["KG"][subd]["name"], test_iso3166_2_request_ir_kgz_446["KG"][subd]["localName"], + for subd in test_request_ir_kgz_446["KG"]: + self.assertIsNot(test_request_ir_kgz_446["KG"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_ir_kgz_446["KG"][subd]["name"])) + self.assertEqual(test_request_ir_kgz_446["KG"][subd]["name"], test_request_ir_kgz_446["KG"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_ir_kgz_446["KG"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_ir_kgz_446["KG"][subd]["parentCode"], list(test_iso3166_2_request_ir_kgz_446["KG"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_ir_kgz_446["KG"][subd]["parentCode"]), list(test_iso3166_2_request_ir_kgz_446["KG"][subd].keys())) - if not (test_iso3166_2_request_ir_kgz_446["KG"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_ir_kgz_446["KG"][subd]["flag"])[0], self.flag_base_url + "KG/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "KG/" + subd, os.path.splitext(test_iso3166_2_request_ir_kgz_446["KG"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_ir_kgz_446["KG"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_ir_kgz_446["KG"][subd]["flag"])) - for key in list(test_iso3166_2_request_ir_kgz_446["KG"][subd].keys()): + if not (test_request_ir_kgz_446["KG"][subd]["parentCode"] is None): + self.assertIn(test_request_ir_kgz_446["KG"][subd]["parentCode"], list(test_request_ir_kgz_446["KG"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_ir_kgz_446["KG"][subd]["parentCode"]), list(test_request_ir_kgz_446["KG"][subd].keys())) + if not (test_request_ir_kgz_446["KG"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_ir_kgz_446["KG"][subd]["flag"])[0], self.flag_base_url + "KG/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "KG/" + subd, os.path.splitext(test_request_ir_kgz_446["KG"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_ir_kgz_446["KG"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_ir_kgz_446["KG"][subd]["flag"])) + for key in list(test_request_ir_kgz_446["KG"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) #6.) - test_iso3166_2_request_error11 = requests.get(self.alpha_base_url + test_alpha_error_1, headers=self.user_agent_header).json() #ABCDE - - self.assertEqual(len(test_iso3166_2_request_error11), 3, - "Expected output object of API to be of length 3, got {}.".format(len(test_iso3166_2_request_error11))) - self.assertEqual(test_iso3166_2_request_error11["message"], "Invalid ISO 3166-1 alpha country code input, cannot convert into corresponding alpha-2 code: {}.".format(test_alpha_error_1), - "Error message does not match expected:\n{}.".format(test_iso3166_2_request_error11["message"])) - self.assertEqual(test_iso3166_2_request_error11["path"], self.alpha_base_url + test_alpha_error_1, - "Error path does not match expected:\n{}.".format(test_iso3166_2_request_error11["path"])) - self.assertEqual(test_iso3166_2_request_error11["status"], 400, - "Error status does not match expected:\n{}.".format(test_iso3166_2_request_error11["status"])) + test_request_error1 = requests.get(self.alpha_base_url + test_alpha_error_1, headers=self.user_agent_header).json() #ABCDE + + self.assertEqual(len(test_request_error1), 3, + "Expected output object of API to be of length 3, got {}.".format(len(test_request_error1))) + self.assertEqual(test_request_error1["message"], "Invalid ISO 3166-1 alpha country code input, cannot convert into corresponding alpha-2 code: {}.".format(test_alpha_error_1), + "Error message does not match expected:\n{}.".format(test_request_error1["message"])) + self.assertEqual(test_request_error1["path"], self.alpha_base_url + test_alpha_error_1, + "Error path does not match expected:\n{}.".format(test_request_error1["path"])) + self.assertEqual(test_request_error1["status"], 400, + "Error status does not match expected:\n{}.".format(test_request_error1["status"])) #7.) - test_iso3166_2_request_error12 = requests.get(self.alpha_base_url + test_alpha_error_2, headers=self.user_agent_header).json() #12345 - - self.assertEqual(len(test_iso3166_2_request_error12), 3, - "Expected output object of API to be of length 3, got {}.".format(len(test_iso3166_2_request_error12))) - self.assertEqual(test_iso3166_2_request_error12["message"], "Invalid ISO 3166-1 alpha country code input, cannot convert into corresponding alpha-2 code: {}.".format(test_alpha_error_2), - "Error message does not match expected:\n{}.".format(test_iso3166_2_request_error12["message"])) - self.assertEqual(test_iso3166_2_request_error12["path"], self.alpha_base_url + test_alpha_error_2, - "Error path does not match expected:\n{}.".format(test_iso3166_2_request_error12["path"])) - self.assertEqual(test_iso3166_2_request_error12["status"], 400, - "Error status does not match expected:\n{}.".format(test_iso3166_2_request_error12["status"])) + test_request_error2 = requests.get(self.alpha_base_url + test_alpha_error_2, headers=self.user_agent_header).json() #12345 + + self.assertEqual(len(test_request_error2), 3, + "Expected output object of API to be of length 3, got {}.".format(len(test_request_error2))) + self.assertEqual(test_request_error2["message"], "Invalid ISO 3166-1 alpha country code input, cannot convert into corresponding alpha-2 code: {}.".format(test_alpha_error_2), + "Error message does not match expected:\n{}.".format(test_request_error2["message"])) + self.assertEqual(test_request_error2["path"], self.alpha_base_url + test_alpha_error_2, + "Error path does not match expected:\n{}.".format(test_request_error2["path"])) + self.assertEqual(test_request_error2["status"], 400, + "Error status does not match expected:\n{}.".format(test_request_error2["status"])) #8.) - test_iso3166_2_request_error13 = requests.get(self.alpha_base_url + test_alpha_error_3, headers=self.user_agent_header).json() #"" - - self.assertEqual(len(test_iso3166_2_request_error13), 3, - "Expected output object of API to be of length 3, got {}.".format(len(test_iso3166_2_request_error13))) - self.assertEqual(test_iso3166_2_request_error13["message"], "The alpha input parameter cannot be empty.", - "Error message does not match expected:\n{}.".format(test_iso3166_2_request_error13["message"])) - self.assertEqual(test_iso3166_2_request_error13["path"], self.alpha_base_url + test_alpha_error_3, - "Error path does not match expected:\n{}.".format(test_iso3166_2_request_error13["path"])) - self.assertEqual(test_iso3166_2_request_error13["status"], 400, - "Error status does not match expected:\n{}.".format(test_iso3166_2_request_error13["status"])) + test_request_error3 = requests.get(self.alpha_base_url + test_alpha_error_3, headers=self.user_agent_header).json() #"" + + self.assertEqual(len(test_request_error3), 3, + "Expected output object of API to be of length 3, got {}.".format(len(test_request_error3))) + self.assertEqual(test_request_error3["message"], "The alpha input parameter cannot be empty.", + "Error message does not match expected:\n{}.".format(test_request_error3["message"])) + self.assertEqual(test_request_error3["path"], self.alpha_base_url + test_alpha_error_3, + "Error path does not match expected:\n{}.".format(test_request_error3["path"])) + self.assertEqual(test_request_error3["status"], 400, + "Error status does not match expected:\n{}.".format(test_request_error3["status"])) def test_subdivision_endpoint(self): """ Testing /subdivision endpoint, return all ISO 3166 subdivision data from input subdivision code/codes. """ @@ -402,112 +405,112 @@ def test_subdivision_endpoint(self): test_subd_gb_xyz = "GB-XYZ" test_subd_xx_yy = "XX-YY" #1.) - test_iso3166_2_request_jm_05 = requests.get(self.subdivision_base_url + test_subd_jm_05, headers=self.user_agent_header).json() #JM-05 + test_request_jm_05 = requests.get(self.subdivision_base_url + test_subd_jm_05, headers=self.user_agent_header).json() #JM-05 - self.assertEqual(list(test_iso3166_2_request_jm_05.keys()), ["JM-05"], - "Expected output object of API to contain only the JM-05 key, got {}.".format(list(test_iso3166_2_request_jm_05.keys()))) + self.assertEqual(list(test_request_jm_05.keys()), ["JM-05"], + "Expected output object of API to contain only the JM-05 key, got {}.".format(list(test_request_jm_05.keys()))) #JM-05 - Saint Mary - self.assertEqual(test_iso3166_2_request_jm_05["JM-05"]["name"], "Saint Mary", - "Expected subdivision name to be Saint Mary, got {}.".format(test_iso3166_2_request_jm_05["JM-05"]["name"])) - self.assertEqual(test_iso3166_2_request_jm_05["JM-05"]["localName"], "Saint Mary", - "Expected subdivision local name to be Saint Mary, got {}.".format(test_iso3166_2_request_jm_05["JM-05"]["localName"])) - self.assertEqual(test_iso3166_2_request_jm_05["JM-05"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_jm_05["JM-05"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_jm_05["JM-05"]["type"], "Parish", - "Expected subdivision type to be Parish, got {}.".format(test_iso3166_2_request_jm_05["JM-05"]["type"])) - self.assertEqual(test_iso3166_2_request_jm_05["JM-05"]["latLng"], [18.309, -76.964], - "Expected subdivision latLng to be [18.309, -76.964], got {}.".format(test_iso3166_2_request_jm_05["JM-05"]["latLng"])) - self.assertEqual(test_iso3166_2_request_jm_05["JM-05"]["flag"], None, - "Expected subdivision flag URL to be None, got {}.".format(test_iso3166_2_request_jm_05["JM-05"]["flag"])) + self.assertEqual(test_request_jm_05["JM-05"]["name"], "Saint Mary", + "Expected subdivision name to be Saint Mary, got {}.".format(test_request_jm_05["JM-05"]["name"])) + self.assertEqual(test_request_jm_05["JM-05"]["localName"], "Saint Mary", + "Expected subdivision local name to be Saint Mary, got {}.".format(test_request_jm_05["JM-05"]["localName"])) + self.assertEqual(test_request_jm_05["JM-05"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_jm_05["JM-05"]["parentCode"])) + self.assertEqual(test_request_jm_05["JM-05"]["type"], "Parish", + "Expected subdivision type to be Parish, got {}.".format(test_request_jm_05["JM-05"]["type"])) + self.assertEqual(test_request_jm_05["JM-05"]["latLng"], [18.309, -76.964], + "Expected subdivision latLng to be [18.309, -76.964], got {}.".format(test_request_jm_05["JM-05"]["latLng"])) + self.assertEqual(test_request_jm_05["JM-05"]["flag"], None, + "Expected subdivision flag URL to be None, got {}.".format(test_request_jm_05["JM-05"]["flag"])) #2) - test_iso3166_2_request_pa_03 = requests.get(self.subdivision_base_url + test_subd_pa_03, headers=self.user_agent_header).json() #PA-3 + test_request_pa_03 = requests.get(self.subdivision_base_url + test_subd_pa_03, headers=self.user_agent_header).json() #PA-3 - self.assertEqual(list(test_iso3166_2_request_pa_03.keys()), ["PA-3"], - "Expected output object of API to contain only the PA-3 key, got {}.".format(list(test_iso3166_2_request_pa_03.keys()))) + self.assertEqual(list(test_request_pa_03.keys()), ["PA-3"], + "Expected output object of API to contain only the PA-3 key, got {}.".format(list(test_request_pa_03.keys()))) #PA-3 - Colón - self.assertEqual(test_iso3166_2_request_pa_03["PA-3"]["name"], "Colón", - "Expected subdivision name to be Colón, got {}.".format(test_iso3166_2_request_pa_03["PA-3"]["name"])) - self.assertEqual(test_iso3166_2_request_pa_03["PA-3"]["localName"], "Colón", - "Expected subdivision local name to be Colón, got {}.".format(test_iso3166_2_request_pa_03["PA-3"]["localName"])) - self.assertEqual(test_iso3166_2_request_pa_03["PA-3"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_pa_03["PA-3"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_pa_03["PA-3"]["type"], "Province", - "Expected subdivision type to be Province, got {}.".format(test_iso3166_2_request_pa_03["PA-3"]["type"])) - self.assertEqual(test_iso3166_2_request_pa_03["PA-3"]["latLng"], [9.359, -79.9], - "Expected subdivision latLng to be [9.359, -79.9], got {}.".format(test_iso3166_2_request_pa_03["PA-3"]["latLng"])) - self.assertEqual(test_iso3166_2_request_pa_03["PA-3"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/PA/PA-3.svg", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/PA/PA-3.svg, got {}.".format(test_iso3166_2_request_pa_03["PA-3"]["flag"])) + self.assertEqual(test_request_pa_03["PA-3"]["name"], "Colón", + "Expected subdivision name to be Colón, got {}.".format(test_request_pa_03["PA-3"]["name"])) + self.assertEqual(test_request_pa_03["PA-3"]["localName"], "Colón", + "Expected subdivision local name to be Colón, got {}.".format(test_request_pa_03["PA-3"]["localName"])) + self.assertEqual(test_request_pa_03["PA-3"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_pa_03["PA-3"]["parentCode"])) + self.assertEqual(test_request_pa_03["PA-3"]["type"], "Province", + "Expected subdivision type to be Province, got {}.".format(test_request_pa_03["PA-3"]["type"])) + self.assertEqual(test_request_pa_03["PA-3"]["latLng"], [9.359, -79.9], + "Expected subdivision latLng to be [9.359, -79.9], got {}.".format(test_request_pa_03["PA-3"]["latLng"])) + self.assertEqual(test_request_pa_03["PA-3"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/PA/PA-3.svg", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/PA/PA-3.svg, got {}.".format(test_request_pa_03["PA-3"]["flag"])) #3.) - test_iso3166_2_request_ss_ew = requests.get(self.subdivision_base_url + test_subd_ss_ew, headers=self.user_agent_header).json() #SS-EW + test_request_ss_ew = requests.get(self.subdivision_base_url + test_subd_ss_ew, headers=self.user_agent_header).json() #SS-EW - self.assertEqual(list(test_iso3166_2_request_ss_ew.keys()), ["SS-EW"], - "Expected output object of API to contain only the SS-EW key, got {}.".format(list(test_iso3166_2_request_ss_ew.keys()))) + self.assertEqual(list(test_request_ss_ew.keys()), ["SS-EW"], + "Expected output object of API to contain only the SS-EW key, got {}.".format(list(test_request_ss_ew.keys()))) #SS-EW - Western Equatoria - self.assertEqual(test_iso3166_2_request_ss_ew["SS-EW"]["name"], "Western Equatoria", - "Expected subdivision name to be Western Equatoria, got {}.".format(test_iso3166_2_request_ss_ew["SS-EW"]["name"])) - self.assertEqual(test_iso3166_2_request_ss_ew["SS-EW"]["localName"], "Western Equatoria", - "Expected subdivision local name to be Western Equatoria, got {}.".format(test_iso3166_2_request_ss_ew["SS-EW"]["localName"])) - self.assertEqual(test_iso3166_2_request_ss_ew["SS-EW"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_ss_ew["SS-EW"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_ss_ew["SS-EW"]["type"], "State", - "Expected subdivision type to be State, got {}.".format(test_iso3166_2_request_ss_ew["SS-EW"]["type"])) - self.assertEqual(test_iso3166_2_request_ss_ew["SS-EW"]["latLng"], [5.347, 28.299], - "Expected subdivision latLng to be [5.347, 28.299], got {}.".format(test_iso3166_2_request_ss_ew["SS-EW"]["latLng"])) - self.assertEqual(test_iso3166_2_request_ss_ew["SS-EW"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/SS/SS-EW.png", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/SS/SS-EW.png, got {}.".format(test_iso3166_2_request_ss_ew["SS-EW"]["flag"])) + self.assertEqual(test_request_ss_ew["SS-EW"]["name"], "Western Equatoria", + "Expected subdivision name to be Western Equatoria, got {}.".format(test_request_ss_ew["SS-EW"]["name"])) + self.assertEqual(test_request_ss_ew["SS-EW"]["localName"], "Western Equatoria", + "Expected subdivision local name to be Western Equatoria, got {}.".format(test_request_ss_ew["SS-EW"]["localName"])) + self.assertEqual(test_request_ss_ew["SS-EW"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_ss_ew["SS-EW"]["parentCode"])) + self.assertEqual(test_request_ss_ew["SS-EW"]["type"], "State", + "Expected subdivision type to be State, got {}.".format(test_request_ss_ew["SS-EW"]["type"])) + self.assertEqual(test_request_ss_ew["SS-EW"]["latLng"], [5.347, 28.299], + "Expected subdivision latLng to be [5.347, 28.299], got {}.".format(test_request_ss_ew["SS-EW"]["latLng"])) + self.assertEqual(test_request_ss_ew["SS-EW"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/SS/SS-EW.png", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/SS/SS-EW.png, got {}.".format(test_request_ss_ew["SS-EW"]["flag"])) #4.) - test_iso3166_2_request_tv_nkf_tj_du = requests.get(self.subdivision_base_url + test_subd_tv_nkf_tj_du, headers=self.user_agent_header).json() #TV-NKF, TJ-DU + test_request_tv_nkf_tj_du = requests.get(self.subdivision_base_url + test_subd_tv_nkf_tj_du, headers=self.user_agent_header).json() #TV-NKF, TJ-DU - self.assertEqual(list(test_iso3166_2_request_tv_nkf_tj_du.keys()), ["TJ-DU", "TV-NKF"], - "Expected output object of API to contain only the TJ-DU and TV-NKF keys, got {}.".format(list(test_iso3166_2_request_tv_nkf_tj_du.keys()))) + self.assertEqual(list(test_request_tv_nkf_tj_du.keys()), ["TJ-DU", "TV-NKF"], + "Expected output object of API to contain only the TJ-DU and TV-NKF keys, got {}.".format(list(test_request_tv_nkf_tj_du.keys()))) #TV-NKF - Western Equatoria - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["name"], "Nukufetau", - "Expected subdivision name to be Nukufetau, got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["name"])) - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["localName"], "Nukufetau", - "Expected subdivision local name to be Nukufetau, got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["localName"])) - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["type"], "Island council", - "Expected subdivision type to be Island Council, got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["type"])) - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["latLng"], [-8, 178.5], - "Expected subdivision latLng to be [-8, 178.5], got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["latLng"])) - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["flag"], None, - "Expected subdivision flag URL to be None, got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TV-NKF"]["flag"])) + self.assertEqual(test_request_tv_nkf_tj_du["TV-NKF"]["name"], "Nukufetau", + "Expected subdivision name to be Nukufetau, got {}.".format(test_request_tv_nkf_tj_du["TV-NKF"]["name"])) + self.assertEqual(test_request_tv_nkf_tj_du["TV-NKF"]["localName"], "Nukufetau", + "Expected subdivision local name to be Nukufetau, got {}.".format(test_request_tv_nkf_tj_du["TV-NKF"]["localName"])) + self.assertEqual(test_request_tv_nkf_tj_du["TV-NKF"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_tv_nkf_tj_du["TV-NKF"]["parentCode"])) + self.assertEqual(test_request_tv_nkf_tj_du["TV-NKF"]["type"], "Island council", + "Expected subdivision type to be Island Council, got {}.".format(test_request_tv_nkf_tj_du["TV-NKF"]["type"])) + self.assertEqual(test_request_tv_nkf_tj_du["TV-NKF"]["latLng"], [-8, 178.5], + "Expected subdivision latLng to be [-8, 178.5], got {}.".format(test_request_tv_nkf_tj_du["TV-NKF"]["latLng"])) + self.assertEqual(test_request_tv_nkf_tj_du["TV-NKF"]["flag"], None, + "Expected subdivision flag URL to be None, got {}.".format(test_request_tv_nkf_tj_du["TV-NKF"]["flag"])) #TJ-DU - Dushanbe - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["name"], "Dushanbe", - "Expected subdivision name to be Dushanbe, got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["name"])) - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["localName"], "Dushanbe", - "Expected subdivision local name to be Dushanbe, got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["localName"])) - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["type"], "Capital territory", - "Expected subdivision type to be Capital territory, got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["type"])) - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["latLng"], [38.56, 68.787], - "Expected subdivision latLng to be [38.56, 68.787], got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["latLng"])) - self.assertEqual(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/TJ/TJ-DU.png", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/TJ/TJ-DU.png, got {}.".format(test_iso3166_2_request_tv_nkf_tj_du["TJ-DU"]["flag"])) + self.assertEqual(test_request_tv_nkf_tj_du["TJ-DU"]["name"], "Dushanbe", + "Expected subdivision name to be Dushanbe, got {}.".format(test_request_tv_nkf_tj_du["TJ-DU"]["name"])) + self.assertEqual(test_request_tv_nkf_tj_du["TJ-DU"]["localName"], "Dushanbe", + "Expected subdivision local name to be Dushanbe, got {}.".format(test_request_tv_nkf_tj_du["TJ-DU"]["localName"])) + self.assertEqual(test_request_tv_nkf_tj_du["TJ-DU"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_tv_nkf_tj_du["TJ-DU"]["parentCode"])) + self.assertEqual(test_request_tv_nkf_tj_du["TJ-DU"]["type"], "Capital territory", + "Expected subdivision type to be Capital territory, got {}.".format(test_request_tv_nkf_tj_du["TJ-DU"]["type"])) + self.assertEqual(test_request_tv_nkf_tj_du["TJ-DU"]["latLng"], [38.56, 68.787], + "Expected subdivision latLng to be [38.56, 68.787], got {}.".format(test_request_tv_nkf_tj_du["TJ-DU"]["latLng"])) + self.assertEqual(test_request_tv_nkf_tj_du["TJ-DU"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/TJ/TJ-DU.png", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/TJ/TJ-DU.png, got {}.".format(test_request_tv_nkf_tj_du["TJ-DU"]["flag"])) #5.) - test_iso3166_2_request_gb_xyz = requests.get(self.subdivision_base_url + test_subd_gb_xyz, headers=self.user_agent_header).json() #GB-XYZ - - self.assertEqual(len(test_iso3166_2_request_gb_xyz), 3, - "Expected output object of API to be of length 3, got {}.".format(len(test_iso3166_2_request_gb_xyz))) - self.assertEqual(test_iso3166_2_request_gb_xyz["message"], "Subdivision code {} not found in list of available subdivisions for GB.".format(test_subd_gb_xyz), - "Error message does not match expected:\n{}.".format(test_iso3166_2_request_gb_xyz["message"])) - self.assertEqual(test_iso3166_2_request_gb_xyz["path"], self.subdivision_base_url + test_subd_gb_xyz, - "Error path does not match expected:\n{}.".format(test_iso3166_2_request_gb_xyz["path"])) - self.assertEqual(test_iso3166_2_request_gb_xyz["status"], 400, - "Error status does not match expected:\n{}.".format(test_iso3166_2_request_gb_xyz["status"])) + test_request_gb_xyz = requests.get(self.subdivision_base_url + test_subd_gb_xyz, headers=self.user_agent_header).json() #GB-XYZ + + self.assertEqual(len(test_request_gb_xyz), 3, + "Expected output object of API to be of length 3, got {}.".format(len(test_request_gb_xyz))) + self.assertEqual(test_request_gb_xyz["message"], "Subdivision code {} not found in list of available subdivisions for GB.".format(test_subd_gb_xyz), + "Error message does not match expected:\n{}.".format(test_request_gb_xyz["message"])) + self.assertEqual(test_request_gb_xyz["path"], self.subdivision_base_url + test_subd_gb_xyz, + "Error path does not match expected:\n{}.".format(test_request_gb_xyz["path"])) + self.assertEqual(test_request_gb_xyz["status"], 400, + "Error status does not match expected:\n{}.".format(test_request_gb_xyz["status"])) #6.) - test_iso3166_2_request_xx_yy = requests.get(self.subdivision_base_url + test_subd_xx_yy, headers=self.user_agent_header).json() #XX-YY + test_request_xx_yy = requests.get(self.subdivision_base_url + test_subd_xx_yy, headers=self.user_agent_header).json() #XX-YY - self.assertEqual(len(test_iso3166_2_request_xx_yy), 3, - "Expected output object of API to be of length 3, got {}.".format(len(test_iso3166_2_request_xx_yy))) - self.assertEqual(test_iso3166_2_request_xx_yy["message"], "Subdivision code {} not found in list of available subdivisions for XX.".format(test_subd_xx_yy), - "Error message does not match expected:\n{}.".format(test_iso3166_2_request_xx_yy["message"])) - self.assertEqual(test_iso3166_2_request_xx_yy["path"], self.subdivision_base_url + test_subd_xx_yy, - "Error path does not match expected:\n{}.".format(test_iso3166_2_request_xx_yy["path"])) - self.assertEqual(test_iso3166_2_request_xx_yy["status"], 400, - "Error status does not match expected:\n{}.".format(test_iso3166_2_request_xx_yy["status"])) + self.assertEqual(len(test_request_xx_yy), 3, + "Expected output object of API to be of length 3, got {}.".format(len(test_request_xx_yy))) + self.assertEqual(test_request_xx_yy["message"], "Subdivision code {} not found in list of available subdivisions for XX.".format(test_subd_xx_yy), + "Error message does not match expected:\n{}.".format(test_request_xx_yy["message"])) + self.assertEqual(test_request_xx_yy["path"], self.subdivision_base_url + test_subd_xx_yy, + "Error path does not match expected:\n{}.".format(test_request_xx_yy["path"])) + self.assertEqual(test_request_xx_yy["status"], 400, + "Error status does not match expected:\n{}.".format(test_request_xx_yy["status"])) def test_subdivision_name_endpoint(self): """ Testing /name endpoint, return all ISO 3166 subdivision data from input subdivision name/names. """ @@ -520,169 +523,169 @@ def test_subdivision_name_endpoint(self): test_subdivision_name_error2 = "1234" test_subdivision_name_error3 = "" #1.) - test_iso3166_2_request_azua = requests.get(self.subdivision_name_base_url + test_subdivision_name_azua, headers=self.user_agent_header).json() #DO-02 - Azua + test_request_azua = requests.get(self.subdivision_name_base_url + test_subdivision_name_azua, headers=self.user_agent_header).json() #DO-02 - Azua - self.assertEqual(list(test_iso3166_2_request_azua.keys()), ["DO-02"], - "Expected output object of API to contain only the DO-02 key, got {}.".format(list(test_iso3166_2_request_azua.keys()))) + self.assertEqual(list(test_request_azua.keys()), ["DO-02"], + "Expected output object of API to contain only the DO-02 key, got {}.".format(list(test_request_azua.keys()))) #DO-02 - Azua - self.assertEqual(test_iso3166_2_request_azua["DO-02"]["name"], "Azua", - "Expected subdivision name to be Azua, got {}.".format(test_iso3166_2_request_azua["DO-02"]["name"])) - self.assertEqual(test_iso3166_2_request_azua["DO-02"]["localName"], "Azua", - "Expected subdivision local name to be Azua, got {}.".format(test_iso3166_2_request_azua["DO-02"]["localName"])) - self.assertEqual(test_iso3166_2_request_azua["DO-02"]["parentCode"], "DO-41", - "Expected subdivision parent code to be DO-41, got {}.".format(test_iso3166_2_request_azua["DO-02"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_azua["DO-02"]["type"], "Province", - "Expected subdivision type to be Province, got {}.".format(test_iso3166_2_request_azua["DO-02"]["type"])) - self.assertEqual(test_iso3166_2_request_azua["DO-02"]["latLng"], [18.453, -70.735], - "Expected subdivision latLng to be [18.453, -70.735], got {}.".format(test_iso3166_2_request_azua["DO-02"]["latLng"])) - self.assertEqual(test_iso3166_2_request_azua["DO-02"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/DO/DO-02.png", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/DO/DO-02.png, got {}.".format(test_iso3166_2_request_azua["DO-02"]["flag"])) + self.assertEqual(test_request_azua["DO-02"]["name"], "Azua", + "Expected subdivision name to be Azua, got {}.".format(test_request_azua["DO-02"]["name"])) + self.assertEqual(test_request_azua["DO-02"]["localName"], "Azua", + "Expected subdivision local name to be Azua, got {}.".format(test_request_azua["DO-02"]["localName"])) + self.assertEqual(test_request_azua["DO-02"]["parentCode"], "DO-41", + "Expected subdivision parent code to be DO-41, got {}.".format(test_request_azua["DO-02"]["parentCode"])) + self.assertEqual(test_request_azua["DO-02"]["type"], "Province", + "Expected subdivision type to be Province, got {}.".format(test_request_azua["DO-02"]["type"])) + self.assertEqual(test_request_azua["DO-02"]["latLng"], [18.453, -70.735], + "Expected subdivision latLng to be [18.453, -70.735], got {}.".format(test_request_azua["DO-02"]["latLng"])) + self.assertEqual(test_request_azua["DO-02"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/DO/DO-02.png", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/DO/DO-02.png, got {}.".format(test_request_azua["DO-02"]["flag"])) #2.) - test_iso3166_2_request_cakaudrove = requests.get(self.subdivision_name_base_url + test_subdivision_name_cakaudrove, headers=self.user_agent_header).json() #FJ-03 - Cakaudrove + test_request_cakaudrove = requests.get(self.subdivision_name_base_url + test_subdivision_name_cakaudrove, headers=self.user_agent_header).json() #FJ-03 - Cakaudrove - self.assertEqual(list(test_iso3166_2_request_cakaudrove.keys()), ["FJ-03"], - "Expected output object of API to contain only the FJ-03 key, got {}.".format(list(test_iso3166_2_request_cakaudrove.keys()))) + self.assertEqual(list(test_request_cakaudrove.keys()), ["FJ-03"], + "Expected output object of API to contain only the FJ-03 key, got {}.".format(list(test_request_cakaudrove.keys()))) #FJ-03 - Cakaudrove - self.assertEqual(test_iso3166_2_request_cakaudrove["FJ-03"]["name"], "Cakaudrove", - "Expected subdivision name to be Cakaudrove, got {}.".format(test_iso3166_2_request_cakaudrove["FJ-03"]["name"])) - self.assertEqual(test_iso3166_2_request_cakaudrove["FJ-03"]["localName"], "Cakaudrove", - "Expected subdivision local name to be Cakaudrove, got {}.".format(test_iso3166_2_request_cakaudrove["FJ-03"]["localName"])) - self.assertEqual(test_iso3166_2_request_cakaudrove["FJ-03"]["parentCode"], "FJ-N", - "Expected subdivision parent code to be FJ-N, got {}.".format(test_iso3166_2_request_cakaudrove["FJ-03"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_cakaudrove["FJ-03"]["type"], "Province", - "Expected subdivision type to be Province, got {}.".format(test_iso3166_2_request_cakaudrove["FJ-03"]["type"])) - self.assertEqual(test_iso3166_2_request_cakaudrove["FJ-03"]["latLng"], [-16.581, 179.436], - "Expected subdivision latLng to be [-16.581, 179.436], got {}.".format(test_iso3166_2_request_cakaudrove["FJ-03"]["latLng"])) - self.assertEqual(test_iso3166_2_request_cakaudrove["FJ-03"]["flag"], None, - "Expected subdivision flag URL to be None, got {}.".format(test_iso3166_2_request_cakaudrove["FJ-03"]["flag"])) + self.assertEqual(test_request_cakaudrove["FJ-03"]["name"], "Cakaudrove", + "Expected subdivision name to be Cakaudrove, got {}.".format(test_request_cakaudrove["FJ-03"]["name"])) + self.assertEqual(test_request_cakaudrove["FJ-03"]["localName"], "Cakaudrove", + "Expected subdivision local name to be Cakaudrove, got {}.".format(test_request_cakaudrove["FJ-03"]["localName"])) + self.assertEqual(test_request_cakaudrove["FJ-03"]["parentCode"], "FJ-N", + "Expected subdivision parent code to be FJ-N, got {}.".format(test_request_cakaudrove["FJ-03"]["parentCode"])) + self.assertEqual(test_request_cakaudrove["FJ-03"]["type"], "Province", + "Expected subdivision type to be Province, got {}.".format(test_request_cakaudrove["FJ-03"]["type"])) + self.assertEqual(test_request_cakaudrove["FJ-03"]["latLng"], [-16.581, 179.512], + "Expected subdivision latLng to be [-16.581, 179.436], got {}.".format(test_request_cakaudrove["FJ-03"]["latLng"])) + self.assertEqual(test_request_cakaudrove["FJ-03"]["flag"], None, + "Expected subdivision flag URL to be None, got {}.".format(test_request_cakaudrove["FJ-03"]["flag"])) #3.) - test_iso3166_2_request_gelderland_overijssel = requests.get(self.subdivision_name_base_url + test_subdivision_name_gelderland_overijssel, headers=self.user_agent_header).json() #NL-GE - Gelderland, NL-OV - Overijssel + test_request_gelderland_overijssel = requests.get(self.subdivision_name_base_url + test_subdivision_name_gelderland_overijssel, headers=self.user_agent_header).json() #NL-GE - Gelderland, NL-OV - Overijssel - self.assertEqual(list(test_iso3166_2_request_gelderland_overijssel.keys()), ["NL-GE", "NL-OV"], - "Expected output object of API to contain only the NL-GE and NL-OV keys, got {}.".format(list(test_iso3166_2_request_gelderland_overijssel.keys()))) + self.assertEqual(list(test_request_gelderland_overijssel.keys()), ["NL-GE", "NL-OV"], + "Expected output object of API to contain only the NL-GE and NL-OV keys, got {}.".format(list(test_request_gelderland_overijssel.keys()))) #NL-GE - Gelderland - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["name"], "Gelderland", - "Expected subdivision name to be Gelderland, got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["name"])) - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["localName"], "Gelderland", - "Expected subdivision local name to be Gelderland, got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["localName"])) - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["type"], "Province", - "Expected subdivision type to be Province, got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["type"])) - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["latLng"], [52.045, 5.872], - "Expected subdivision latLng to be [52.045, 5.872], got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["latLng"])) - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/NL/NL-GE.svg", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/NL/NL-GE.svg, got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-GE"]["flag"])) + self.assertEqual(test_request_gelderland_overijssel["NL-GE"]["name"], "Gelderland", + "Expected subdivision name to be Gelderland, got {}.".format(test_request_gelderland_overijssel["NL-GE"]["name"])) + self.assertEqual(test_request_gelderland_overijssel["NL-GE"]["localName"], "Gelderland", + "Expected subdivision local name to be Gelderland, got {}.".format(test_request_gelderland_overijssel["NL-GE"]["localName"])) + self.assertEqual(test_request_gelderland_overijssel["NL-GE"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_gelderland_overijssel["NL-GE"]["parentCode"])) + self.assertEqual(test_request_gelderland_overijssel["NL-GE"]["type"], "Province", + "Expected subdivision type to be Province, got {}.".format(test_request_gelderland_overijssel["NL-GE"]["type"])) + self.assertEqual(test_request_gelderland_overijssel["NL-GE"]["latLng"], [52.045, 5.872], + "Expected subdivision latLng to be [52.045, 5.872], got {}.".format(test_request_gelderland_overijssel["NL-GE"]["latLng"])) + self.assertEqual(test_request_gelderland_overijssel["NL-GE"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/NL/NL-GE.svg", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/NL/NL-GE.svg, got {}.".format(test_request_gelderland_overijssel["NL-GE"]["flag"])) #NL-OV - Overijssel - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["name"], "Overijssel", - "Expected subdivision name to be Overijssel, got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["name"])) - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["localName"], "Overijssel", - "Expected subdivision local name to be Overijssel, got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["localName"])) - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["type"], "Province", - "Expected subdivision type to be Province, got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["type"])) - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["latLng"], [52.439, 6.502], - "Expected subdivision latLng to be [52.439, 6.502], got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["latLng"])) - self.assertEqual(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/NL/NL-OV.svg", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/NL/NL-OV.svg, got {}.".format(test_iso3166_2_request_gelderland_overijssel["NL-OV"]["flag"])) + self.assertEqual(test_request_gelderland_overijssel["NL-OV"]["name"], "Overijssel", + "Expected subdivision name to be Overijssel, got {}.".format(test_request_gelderland_overijssel["NL-OV"]["name"])) + self.assertEqual(test_request_gelderland_overijssel["NL-OV"]["localName"], "Overijssel", + "Expected subdivision local name to be Overijssel, got {}.".format(test_request_gelderland_overijssel["NL-OV"]["localName"])) + self.assertEqual(test_request_gelderland_overijssel["NL-OV"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_gelderland_overijssel["NL-OV"]["parentCode"])) + self.assertEqual(test_request_gelderland_overijssel["NL-OV"]["type"], "Province", + "Expected subdivision type to be Province, got {}.".format(test_request_gelderland_overijssel["NL-OV"]["type"])) + self.assertEqual(test_request_gelderland_overijssel["NL-OV"]["latLng"], [52.439, 6.502], + "Expected subdivision latLng to be [52.439, 6.502], got {}.".format(test_request_gelderland_overijssel["NL-OV"]["latLng"])) + self.assertEqual(test_request_gelderland_overijssel["NL-OV"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/NL/NL-OV.svg", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/NL/NL-OV.svg, got {}.".format(test_request_gelderland_overijssel["NL-OV"]["flag"])) #4.) - test_iso3166_2_request_ciudad = requests.get(self.subdivision_name_base_url + test_subdivision_name_ciudad, headers=self.user_agent_header, params={"likeness": "50"}).json() #Ciudad - likeness score of 50 + test_request_ciudad = requests.get(self.subdivision_name_base_url + test_subdivision_name_ciudad, headers=self.user_agent_header, params={"likeness": "50"}).json() #Ciudad - likeness score of 50 - self.assertEqual(list(test_iso3166_2_request_ciudad.keys()), ["AO-CAB", "AZ-CUL", "ES-CR", "MX-CMX", "ZW-MI"], - "Expected output object of API to contain only the AO-CAB, AZ-CUL, ES-CR, MX-CMX and ZW-MI keys, got {}.".format(list(test_iso3166_2_request_ciudad.keys()))) + self.assertEqual(list(test_request_ciudad.keys()), ["AO-CAB", "AZ-CUL", "ES-CR", "MX-CMX", "ZW-MI"], + "Expected output object of API to contain only the AO-CAB, AZ-CUL, ES-CR, MX-CMX and ZW-MI keys, got {}.".format(list(test_request_ciudad.keys()))) #ES-CR - Ciudad Real - self.assertEqual(test_iso3166_2_request_ciudad["ES-CR"]["name"], "Ciudad Real", - "Expected subdivision name to be Ciudad Real, got {}.".format(test_iso3166_2_request_ciudad["ES-CR"]["name"])) - self.assertEqual(test_iso3166_2_request_ciudad["ES-CR"]["localName"], "Ciudad Real", - "Expected subdivision local name to be Ciudad Real, got {}.".format(test_iso3166_2_request_ciudad["ES-CR"]["localName"])) - self.assertEqual(test_iso3166_2_request_ciudad["ES-CR"]["parentCode"], "ES-CM", - "Expected subdivision parent code to be ES-CM, got {}.".format(test_iso3166_2_request_ciudad["ES-CR"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_ciudad["ES-CR"]["type"], "Province", - "Expected subdivision type to be Province, got {}.".format(test_iso3166_2_request_ciudad["ES-CR"]["type"])) - self.assertEqual(test_iso3166_2_request_ciudad["ES-CR"]["latLng"], [38.985, -3.927], - "Expected subdivision latLng to be [38.985, -3.927], got {}.".format(test_iso3166_2_request_ciudad["ES-CR"]["latLng"])) - self.assertEqual(test_iso3166_2_request_ciudad["ES-CR"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/ES/ES-CR.svg", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/ES/ES-CR.svg, got {}.".format(test_iso3166_2_request_ciudad["ES-CR"]["flag"])) + self.assertEqual(test_request_ciudad["ES-CR"]["name"], "Ciudad Real", + "Expected subdivision name to be Ciudad Real, got {}.".format(test_request_ciudad["ES-CR"]["name"])) + self.assertEqual(test_request_ciudad["ES-CR"]["localName"], "Ciudad Real", + "Expected subdivision local name to be Ciudad Real, got {}.".format(test_request_ciudad["ES-CR"]["localName"])) + self.assertEqual(test_request_ciudad["ES-CR"]["parentCode"], "ES-CM", + "Expected subdivision parent code to be ES-CM, got {}.".format(test_request_ciudad["ES-CR"]["parentCode"])) + self.assertEqual(test_request_ciudad["ES-CR"]["type"], "Province", + "Expected subdivision type to be Province, got {}.".format(test_request_ciudad["ES-CR"]["type"])) + self.assertEqual(test_request_ciudad["ES-CR"]["latLng"], [38.985, -3.927], + "Expected subdivision latLng to be [38.985, -3.927], got {}.".format(test_request_ciudad["ES-CR"]["latLng"])) + self.assertEqual(test_request_ciudad["ES-CR"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/ES/ES-CR.svg", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/ES/ES-CR.svg, got {}.".format(test_request_ciudad["ES-CR"]["flag"])) #MX-CMX - Ciudad de Mexico - self.assertEqual(test_iso3166_2_request_ciudad["MX-CMX"]["name"], "Ciudad de México", - "Expected subdivision name to be Ciudad de México, got {}.".format(test_iso3166_2_request_ciudad["MX-CMX"]["name"])) - self.assertEqual(test_iso3166_2_request_ciudad["MX-CMX"]["localName"], "Ciudad de México", - "Expected subdivision local name to be Ciudad de México, got {}.".format(test_iso3166_2_request_ciudad["MX-CMX"]["localName"])) - self.assertEqual(test_iso3166_2_request_ciudad["MX-CMX"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_ciudad["MX-CMX"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_ciudad["MX-CMX"]["type"], "Federal entity", - "Expected subdivision type to be Federal entity, got {}.".format(test_iso3166_2_request_ciudad["MX-CMX"]["type"])) - self.assertEqual(test_iso3166_2_request_ciudad["MX-CMX"]["latLng"], [19.433, -99.133], - "Expected subdivision latLng to be [19.433, -99.133], got {}.".format(test_iso3166_2_request_ciudad["MX-CMX"]["latLng"])) - self.assertEqual(test_iso3166_2_request_ciudad["MX-CMX"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/MX/MX-CMX.svg", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/MX/MX-CMX.svg, got {}.".format(test_iso3166_2_request_ciudad["MX-CMX"]["flag"])) + self.assertEqual(test_request_ciudad["MX-CMX"]["name"], "Ciudad de México", + "Expected subdivision name to be Ciudad de México, got {}.".format(test_request_ciudad["MX-CMX"]["name"])) + self.assertEqual(test_request_ciudad["MX-CMX"]["localName"], "Ciudad de México", + "Expected subdivision local name to be Ciudad de México, got {}.".format(test_request_ciudad["MX-CMX"]["localName"])) + self.assertEqual(test_request_ciudad["MX-CMX"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_ciudad["MX-CMX"]["parentCode"])) + self.assertEqual(test_request_ciudad["MX-CMX"]["type"], "Federal entity", + "Expected subdivision type to be Federal entity, got {}.".format(test_request_ciudad["MX-CMX"]["type"])) + self.assertEqual(test_request_ciudad["MX-CMX"]["latLng"], [19.433, -99.133], + "Expected subdivision latLng to be [19.433, -99.133], got {}.".format(test_request_ciudad["MX-CMX"]["latLng"])) + self.assertEqual(test_request_ciudad["MX-CMX"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/MX/MX-CMX.svg", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/MX/MX-CMX.svg, got {}.".format(test_request_ciudad["MX-CMX"]["flag"])) #5.) - test_iso3166_2_request_madrid_armaghcity = requests.get(self.subdivision_name_base_url + test_subdivision_madrid_armaghcity, headers=self.user_agent_header).json() #ES-MD - Madrid, GB-ABC - Armagh City, Banbridge and Craigavon + test_request_madrid_armaghcity = requests.get(self.subdivision_name_base_url + test_subdivision_madrid_armaghcity, headers=self.user_agent_header).json() #ES-MD - Madrid, GB-ABC - Armagh City, Banbridge and Craigavon - self.assertEqual(list(test_iso3166_2_request_madrid_armaghcity.keys()), ["ES-MD", "GB-ABC"], - "Expected output object of API to contain only the ES-MD and GB-ABC keys, got {}.".format(list(test_iso3166_2_request_madrid_armaghcity.keys()))) + self.assertEqual(list(test_request_madrid_armaghcity.keys()), ["ES-MD", "GB-ABC"], + "Expected output object of API to contain only the ES-MD and GB-ABC keys, got {}.".format(list(test_request_madrid_armaghcity.keys()))) #ES-MD - Madrid - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["name"], "Madrid, Comunidad de", - "Expected subdivision name to be Madrid, Comunidad de, got {}.".format(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["name"])) - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["localName"], "Madrid, Comunidad de", - "Expected subdivision local name to be Madrid, Comunidad de, got {}.".format(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["localName"])) - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["parentCode"], None, - "Expected subdivision parent code to be None, got {}.".format(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["type"], "Autonomous community", - "Expected subdivision type to be Autonomous community, got {}.".format(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["type"])) - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["latLng"], [40.417, -3.704], - "Expected subdivision latLng to be [40.417, -3.704], got {}.".format(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["latLng"])) - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/ES/ES-MD.svg", - "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/ES/ES-MD.svg, got {}.".format(test_iso3166_2_request_madrid_armaghcity["ES-MD"]["flag"])) + self.assertEqual(test_request_madrid_armaghcity["ES-MD"]["name"], "Madrid, Comunidad de", + "Expected subdivision name to be Madrid, Comunidad de, got {}.".format(test_request_madrid_armaghcity["ES-MD"]["name"])) + self.assertEqual(test_request_madrid_armaghcity["ES-MD"]["localName"], "Madrid, Comunidad de", + "Expected subdivision local name to be Madrid, Comunidad de, got {}.".format(test_request_madrid_armaghcity["ES-MD"]["localName"])) + self.assertEqual(test_request_madrid_armaghcity["ES-MD"]["parentCode"], None, + "Expected subdivision parent code to be None, got {}.".format(test_request_madrid_armaghcity["ES-MD"]["parentCode"])) + self.assertEqual(test_request_madrid_armaghcity["ES-MD"]["type"], "Autonomous community", + "Expected subdivision type to be Autonomous community, got {}.".format(test_request_madrid_armaghcity["ES-MD"]["type"])) + self.assertEqual(test_request_madrid_armaghcity["ES-MD"]["latLng"], [40.417, -3.581], + "Expected subdivision latLng to be [40.417, -3.704], got {}.".format(test_request_madrid_armaghcity["ES-MD"]["latLng"])) + self.assertEqual(test_request_madrid_armaghcity["ES-MD"]["flag"], "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/ES/ES-MD.svg", + "Expected subdivision flag URL to be https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/ES/ES-MD.svg, got {}.".format(test_request_madrid_armaghcity["ES-MD"]["flag"])) #GB-ABC - Armagh City, Banbridge and Craigavon - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["name"], "Armagh City, Banbridge and Craigavon", - "Expected subdivision name to be Armagh City, Banbridge and Craigavon, got {}.".format(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["name"])) - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["localName"], "Armagh City, Banbridge and Craigavon", - "Expected subdivision local name to be Armagh City, Banbridge and Craigavon, got {}.".format(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["localName"])) - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["parentCode"], "GB-NIR", - "Expected subdivision parent code to be GB-NIR, got {}.".format(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["parentCode"])) - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["type"], "District", - "Expected subdivision type to be District, got {}.".format(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["type"])) - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["latLng"], [54.393, -6.456], - "Expected subdivision latLng to be [54.393, -6.456], got {}.".format(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["latLng"])) - self.assertEqual(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["flag"], None, - "Expected subdivision flag URL to be None, got {}.".format(test_iso3166_2_request_madrid_armaghcity["GB-ABC"]["flag"])) + self.assertEqual(test_request_madrid_armaghcity["GB-ABC"]["name"], "Armagh City, Banbridge and Craigavon", + "Expected subdivision name to be Armagh City, Banbridge and Craigavon, got {}.".format(test_request_madrid_armaghcity["GB-ABC"]["name"])) + self.assertEqual(test_request_madrid_armaghcity["GB-ABC"]["localName"], "Armagh City, Banbridge and Craigavon", + "Expected subdivision local name to be Armagh City, Banbridge and Craigavon, got {}.".format(test_request_madrid_armaghcity["GB-ABC"]["localName"])) + self.assertEqual(test_request_madrid_armaghcity["GB-ABC"]["parentCode"], "GB-NIR", + "Expected subdivision parent code to be GB-NIR, got {}.".format(test_request_madrid_armaghcity["GB-ABC"]["parentCode"])) + self.assertEqual(test_request_madrid_armaghcity["GB-ABC"]["type"], "District", + "Expected subdivision type to be District, got {}.".format(test_request_madrid_armaghcity["GB-ABC"]["type"])) + self.assertEqual(test_request_madrid_armaghcity["GB-ABC"]["latLng"], [54.393, -6.456], + "Expected subdivision latLng to be [54.393, -6.456], got {}.".format(test_request_madrid_armaghcity["GB-ABC"]["latLng"])) + self.assertEqual(test_request_madrid_armaghcity["GB-ABC"]["flag"], None, + "Expected subdivision flag URL to be None, got {}.".format(test_request_madrid_armaghcity["GB-ABC"]["flag"])) #6.) - test_iso3166_2_request_subdivision_error1 = requests.get(self.subdivision_name_base_url + test_subdivision_name_error1, headers=self.user_agent_header).json() #blahblahblah + test_request_subdivision_error1 = requests.get(self.subdivision_name_base_url + test_subdivision_name_error1, headers=self.user_agent_header).json() #blahblahblah - self.assertEqual(len(test_iso3166_2_request_subdivision_error1), 3, - "Expected output object of API to be of length 3, got {}.".format(len(test_iso3166_2_request_subdivision_error1))) - self.assertEqual(test_iso3166_2_request_subdivision_error1["message"], "No valid subdivision found for input name: {}. Try using the query string parameter '?likeness' and " + self.assertEqual(len(test_request_subdivision_error1), 3, + "Expected output object of API to be of length 3, got {}.".format(len(test_request_subdivision_error1))) + self.assertEqual(test_request_subdivision_error1["message"], "No valid subdivision found for input name: {}. Try using the query string parameter '?likeness' and " "reduce the likeness score to expand the search space, e.g '?likeness=0.3' will return subdivisions that have a 30% match to the input name.".format(test_subdivision_name_error1), - "Error message does not match expected:\n{}.".format(test_iso3166_2_request_subdivision_error1["message"])) - self.assertEqual(test_iso3166_2_request_subdivision_error1["path"], self.subdivision_name_base_url + test_subdivision_name_error1, - "Error path does not match expected:\n{}.".format(test_iso3166_2_request_subdivision_error1["path"])) - self.assertEqual(test_iso3166_2_request_subdivision_error1["status"], 400, - "Error status does not match expected:\n{}.".format(test_iso3166_2_request_subdivision_error1["status"])) + "Error message does not match expected:\n{}.".format(test_request_subdivision_error1["message"])) + self.assertEqual(test_request_subdivision_error1["path"], self.subdivision_name_base_url + test_subdivision_name_error1, + "Error path does not match expected:\n{}.".format(test_request_subdivision_error1["path"])) + self.assertEqual(test_request_subdivision_error1["status"], 400, + "Error status does not match expected:\n{}.".format(test_request_subdivision_error1["status"])) #6.) - test_iso3166_2_request_subdivision_error2 = requests.get(self.subdivision_name_base_url + test_subdivision_name_error2, headers=self.user_agent_header).json() #1234 + test_request_subdivision_error2 = requests.get(self.subdivision_name_base_url + test_subdivision_name_error2, headers=self.user_agent_header).json() #1234 - self.assertEqual(len(test_iso3166_2_request_subdivision_error2), 3, - "Expected output object of API to be of length 3, got {}.".format(len(test_iso3166_2_request_subdivision_error2))) - self.assertEqual(test_iso3166_2_request_subdivision_error2["message"], "No valid subdivision found for input name: {}. Try using the query string parameter '?likeness' and " + self.assertEqual(len(test_request_subdivision_error2), 3, + "Expected output object of API to be of length 3, got {}.".format(len(test_request_subdivision_error2))) + self.assertEqual(test_request_subdivision_error2["message"], "No valid subdivision found for input name: {}. Try using the query string parameter '?likeness' and " "reduce the likeness score to expand the search space, e.g '?likeness=0.3' will return subdivisions that have a 30% match to the input name.".format(test_subdivision_name_error2), - "Error message does not match expected:\n{}.".format(test_iso3166_2_request_subdivision_error2["message"])) - self.assertEqual(test_iso3166_2_request_subdivision_error2["path"], self.subdivision_name_base_url + test_subdivision_name_error2, - "Error path does not match expected:\n{}.".format(test_iso3166_2_request_subdivision_error2["path"])) - self.assertEqual(test_iso3166_2_request_subdivision_error2["status"], 400, - "Error status does not match expected:\n{}.".format(test_iso3166_2_request_subdivision_error2["status"])) + "Error message does not match expected:\n{}.".format(test_request_subdivision_error2["message"])) + self.assertEqual(test_request_subdivision_error2["path"], self.subdivision_name_base_url + test_subdivision_name_error2, + "Error path does not match expected:\n{}.".format(test_request_subdivision_error2["path"])) + self.assertEqual(test_request_subdivision_error2["status"], 400, + "Error status does not match expected:\n{}.".format(test_request_subdivision_error2["status"])) #7.) - test_iso3166_2_request_subdivision_error3 = requests.get(self.subdivision_name_base_url + test_subdivision_name_error3, headers=self.user_agent_header).json() #"" - - self.assertEqual(len(test_iso3166_2_request_subdivision_error3), 3, - "Expected output object of API to be of length 3, got {}.".format(len(test_iso3166_2_request_subdivision_error3))) - self.assertEqual(test_iso3166_2_request_subdivision_error3["message"], "The subdivision name input parameter cannot be empty.", - "Error message does not match expected:\n{}.".format(test_iso3166_2_request_subdivision_error3["message"])) - self.assertEqual(test_iso3166_2_request_subdivision_error3["path"], self.subdivision_name_base_url + test_subdivision_name_error3, - "Error path does not match expected:\n{}.".format(test_iso3166_2_request_subdivision_error3["path"])) - self.assertEqual(test_iso3166_2_request_subdivision_error3["status"], 400, - "Error status does not match expected:\n{}.".format(test_iso3166_2_request_subdivision_error3["status"])) + test_request_subdivision_error3 = requests.get(self.subdivision_name_base_url + test_subdivision_name_error3, headers=self.user_agent_header).json() #"" + + self.assertEqual(len(test_request_subdivision_error3), 3, + "Expected output object of API to be of length 3, got {}.".format(len(test_request_subdivision_error3))) + self.assertEqual(test_request_subdivision_error3["message"], "The subdivision name input parameter cannot be empty.", + "Error message does not match expected:\n{}.".format(test_request_subdivision_error3["message"])) + self.assertEqual(test_request_subdivision_error3["path"], self.subdivision_name_base_url + test_subdivision_name_error3, + "Error path does not match expected:\n{}.".format(test_request_subdivision_error3["path"])) + self.assertEqual(test_request_subdivision_error3["status"], 400, + "Error status does not match expected:\n{}.".format(test_request_subdivision_error3["status"])) def test_name_endpoint(self): """ Testing /country_name endpoint, return all ISO 3166 subdivision data from input country name/names. """ @@ -693,142 +696,142 @@ def test_name_endpoint(self): test_country_name_error1 = "ABCDEF" test_country_name_error2 = "12345" #1.) - test_iso3166_2_request_bj = requests.get(self.country_name_base_url + test_country_name_bj, headers=self.user_agent_header).json() #Benin + test_request_bj = requests.get(self.country_name_base_url + test_country_name_bj, headers=self.user_agent_header).json() #Benin - self.assertEqual(list(test_iso3166_2_request_bj.keys()), ["BJ"], - "Expected output object of API to contain only the BJ key, got {}.".format(list(test_iso3166_2_request_bj.keys()))) - self.assertEqual(list(test_iso3166_2_request_bj["BJ"].keys()), ["BJ-AK", "BJ-AL", "BJ-AQ", "BJ-BO", "BJ-CO", "BJ-DO", "BJ-KO", "BJ-LI", "BJ-MO", "BJ-OU", "BJ-PL", "BJ-ZO"], - f"Expected subdivision keys do not match output:\n{list(test_iso3166_2_request_bj['BJ'].keys())}.") + self.assertEqual(list(test_request_bj.keys()), ["BJ"], + "Expected output object of API to contain only the BJ key, got {}.".format(list(test_request_bj.keys()))) + self.assertEqual(list(test_request_bj["BJ"].keys()), ["BJ-AK", "BJ-AL", "BJ-AQ", "BJ-BO", "BJ-CO", "BJ-DO", "BJ-KO", "BJ-LI", "BJ-MO", "BJ-OU", "BJ-PL", "BJ-ZO"], + f"Expected subdivision keys do not match output:\n{list(test_request_bj['BJ'].keys())}.") - for subd in test_iso3166_2_request_bj["BJ"]: - self.assertIsNot(test_iso3166_2_request_bj["BJ"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_bj["BJ"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_bj["BJ"][subd]["name"], test_iso3166_2_request_bj["BJ"][subd]["localName"], + for subd in test_request_bj["BJ"]: + self.assertIsNot(test_request_bj["BJ"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_bj["BJ"][subd]["name"])) + self.assertEqual(test_request_bj["BJ"][subd]["name"], test_request_bj["BJ"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_bj["BJ"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_bj["BJ"][subd]["parentCode"], list(test_iso3166_2_request_bj["BJ"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_bj["BJ"][subd]["parentCode"]), list(test_iso3166_2_request_bj["BJ"][subd].keys())) - if not (test_iso3166_2_request_bj["BJ"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_bj["BJ"][subd]["flag"])[0], self.flag_base_url + "BJ/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "BJ/" + subd, os.path.splitext(test_iso3166_2_request_bj["BJ"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_bj["BJ"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_bj["BJ"][subd]["flag"])) - for key in list(test_iso3166_2_request_bj["BJ"][subd].keys()): + if not (test_request_bj["BJ"][subd]["parentCode"] is None): + self.assertIn(test_request_bj["BJ"][subd]["parentCode"], list(test_request_bj["BJ"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_bj["BJ"][subd]["parentCode"]), list(test_request_bj["BJ"][subd].keys())) + if not (test_request_bj["BJ"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_bj["BJ"][subd]["flag"])[0], self.flag_base_url + "BJ/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "BJ/" + subd, os.path.splitext(test_request_bj["BJ"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_bj["BJ"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_bj["BJ"][subd]["flag"])) + for key in list(test_request_bj["BJ"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) #2.) - test_iso3166_2_request_tj = requests.get(self.country_name_base_url + test_country_name_tj, headers=self.user_agent_header).json() #Tajikistan + test_request_tj = requests.get(self.country_name_base_url + test_country_name_tj, headers=self.user_agent_header).json() #Tajikistan - self.assertEqual(list(test_iso3166_2_request_tj.keys()), ["TJ"], - "Expected output object of API to contain only the TJ key, got {}.".format(list(test_iso3166_2_request_tj.keys()))) - self.assertEqual(list(test_iso3166_2_request_tj["TJ"].keys()), ["TJ-DU", "TJ-GB", "TJ-KT", "TJ-RA", "TJ-SU"], - f"Expected subdivision keys do not match output:\n{list(test_iso3166_2_request_tj['TJ'].keys())}.") + self.assertEqual(list(test_request_tj.keys()), ["TJ"], + "Expected output object of API to contain only the TJ key, got {}.".format(list(test_request_tj.keys()))) + self.assertEqual(list(test_request_tj["TJ"].keys()), ["TJ-DU", "TJ-GB", "TJ-KT", "TJ-RA", "TJ-SU"], + f"Expected subdivision keys do not match output:\n{list(test_request_tj['TJ'].keys())}.") - for subd in test_iso3166_2_request_tj["TJ"]: - self.assertIsNot(test_iso3166_2_request_tj["TJ"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_tj["TJ"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_tj["TJ"][subd]["name"], test_iso3166_2_request_tj["TJ"][subd]["localName"], + for subd in test_request_tj["TJ"]: + self.assertIsNot(test_request_tj["TJ"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_tj["TJ"][subd]["name"])) + self.assertEqual(test_request_tj["TJ"][subd]["name"], test_request_tj["TJ"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_tj["TJ"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_tj["TJ"][subd]["parentCode"], list(test_iso3166_2_request_tj["TJ"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_tj["TJ"][subd]["parentCode"]), list(test_iso3166_2_request_tj["TJ"][subd].keys())) - if not (test_iso3166_2_request_tj["TJ"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_tj["TJ"][subd]["flag"])[0], self.flag_base_url + "TJ/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "TJ/" + subd, os.path.splitext(test_iso3166_2_request_tj["TJ"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_tj["TJ"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_tj["TJ"][subd]["flag"])) - for key in list(test_iso3166_2_request_tj["TJ"][subd].keys()): + if not (test_request_tj["TJ"][subd]["parentCode"] is None): + self.assertIn(test_request_tj["TJ"][subd]["parentCode"], list(test_request_tj["TJ"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_tj["TJ"][subd]["parentCode"]), list(test_request_tj["TJ"][subd].keys())) + if not (test_request_tj["TJ"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_tj["TJ"][subd]["flag"])[0], self.flag_base_url + "TJ/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "TJ/" + subd, os.path.splitext(test_request_tj["TJ"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_tj["TJ"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_tj["TJ"][subd]["flag"])) + for key in list(test_request_tj["TJ"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) #3.) - test_iso3166_2_request_sd = requests.get(self.country_name_base_url + test_country_name_sd, headers=self.user_agent_header).json() #Sudan + test_request_sd = requests.get(self.country_name_base_url + test_country_name_sd, headers=self.user_agent_header).json() #Sudan - self.assertEqual(list(test_iso3166_2_request_sd.keys()), ["SD"], - "Expected output object of API to contain only the SD key, got {}.".format(list(test_iso3166_2_request_sd.keys()))) - self.assertEqual(list(test_iso3166_2_request_sd["SD"].keys()), + self.assertEqual(list(test_request_sd.keys()), ["SD"], + "Expected output object of API to contain only the SD key, got {}.".format(list(test_request_sd.keys()))) + self.assertEqual(list(test_request_sd["SD"].keys()), ["SD-DC", "SD-DE", "SD-DN", "SD-DS", "SD-DW", "SD-GD", "SD-GK", "SD-GZ", "SD-KA", "SD-KH", \ "SD-KN", "SD-KS", "SD-NB", "SD-NO", "SD-NR", "SD-NW", "SD-RS", "SD-SI"], - f"Expected subdivision keys do not match output:\n{list(test_iso3166_2_request_sd['SD'].keys())}.") + f"Expected subdivision keys do not match output:\n{list(test_request_sd['SD'].keys())}.") - for subd in test_iso3166_2_request_sd["SD"]: - self.assertIsNot(test_iso3166_2_request_sd["SD"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_sd["SD"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_sd["SD"][subd]["name"], test_iso3166_2_request_sd["SD"][subd]["localName"], + for subd in test_request_sd["SD"]: + self.assertIsNot(test_request_sd["SD"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_sd["SD"][subd]["name"])) + self.assertEqual(test_request_sd["SD"][subd]["name"], test_request_sd["SD"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_sd["SD"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_sd["SD"][subd]["parentCode"], list(test_iso3166_2_request_sd["SD"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_sd["SD"][subd]["parentCode"]), list(test_iso3166_2_request_sd["SD"][subd].keys())) - if not (test_iso3166_2_request_sd["SD"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_sd["SD"][subd]["flag"])[0], self.flag_base_url + "SD/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "SD/" + subd, os.path.splitext(test_iso3166_2_request_sd["SD"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_sd["SD"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_sd["SD"][subd]["flag"])) - for key in list(test_iso3166_2_request_sd["SD"][subd].keys()): + if not (test_request_sd["SD"][subd]["parentCode"] is None): + self.assertIn(test_request_sd["SD"][subd]["parentCode"], list(test_request_sd["SD"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_sd["SD"][subd]["parentCode"]), list(test_request_sd["SD"][subd].keys())) + if not (test_request_sd["SD"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_sd["SD"][subd]["flag"])[0], self.flag_base_url + "SD/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "SD/" + subd, os.path.splitext(test_request_sd["SD"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_sd["SD"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_sd["SD"][subd]["flag"])) + for key in list(test_request_sd["SD"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) #4.) - test_iso3166_2_request_ml_ni = requests.get(self.country_name_base_url + test_country_name_ml_ni, headers=self.user_agent_header).json() #Mali and Nicaragua + test_request_ml_ni = requests.get(self.country_name_base_url + test_country_name_ml_ni, headers=self.user_agent_header).json() #Mali and Nicaragua - self.assertEqual(list(test_iso3166_2_request_ml_ni.keys()), ["ML", "NI"], - "Expected output object of API to contain only the ML and NI keys, got {}.".format(list(test_iso3166_2_request_ml_ni.keys()))) - self.assertEqual(list(test_iso3166_2_request_ml_ni["ML"].keys()), ["ML-1", "ML-10", "ML-2", "ML-3", "ML-4", "ML-5", "ML-6", "ML-7", "ML-8", "ML-9", "ML-BKO"], - f"Expected subdivision keys do not match output:\n{list(test_iso3166_2_request_ml_ni['ML'].keys())}.") - self.assertEqual(list(test_iso3166_2_request_ml_ni["NI"].keys()), + self.assertEqual(list(test_request_ml_ni.keys()), ["ML", "NI"], + "Expected output object of API to contain only the ML and NI keys, got {}.".format(list(test_request_ml_ni.keys()))) + self.assertEqual(list(test_request_ml_ni["ML"].keys()), ["ML-1", "ML-10", "ML-2", "ML-3", "ML-4", "ML-5", "ML-6", "ML-7", "ML-8", "ML-9", "ML-BKO"], + f"Expected subdivision keys do not match output:\n{list(test_request_ml_ni['ML'].keys())}.") + self.assertEqual(list(test_request_ml_ni["NI"].keys()), ["NI-AN", "NI-AS", "NI-BO", "NI-CA", "NI-CI", "NI-CO", "NI-ES", "NI-GR", "NI-JI", "NI-LE", "NI-MD", "NI-MN", "NI-MS", "NI-MT", "NI-NS", "NI-RI", "NI-SJ"], - f"Expected subdivision keys do not match output:\n{list(test_iso3166_2_request_ml_ni['NI'].keys())}.") + f"Expected subdivision keys do not match output:\n{list(test_request_ml_ni['NI'].keys())}.") - for subd in test_iso3166_2_request_ml_ni["ML"]: - self.assertIsNot(test_iso3166_2_request_ml_ni["ML"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_ml_ni["ML"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_ml_ni["ML"][subd]["name"], test_iso3166_2_request_ml_ni["ML"][subd]["localName"], + for subd in test_request_ml_ni["ML"]: + self.assertIsNot(test_request_ml_ni["ML"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_ml_ni["ML"][subd]["name"])) + self.assertEqual(test_request_ml_ni["ML"][subd]["name"], test_request_ml_ni["ML"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_ml_ni["ML"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_ml_ni["ML"][subd]["parentCode"], list(test_iso3166_2_request_ml_ni["ML"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_ml_ni["ML"][subd]["parentCode"]), list(test_iso3166_2_request_ml_ni["ML"][subd].keys())) - if not (test_iso3166_2_request_ml_ni["ML"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_ml_ni["ML"][subd]["flag"])[0], self.flag_base_url + "ML/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "ML/" + subd, os.path.splitext(test_iso3166_2_request_ml_ni["ML"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_ml_ni["ML"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_ml_ni["ML"][subd]["flag"])) - for key in list(test_iso3166_2_request_ml_ni["ML"][subd].keys()): + if not (test_request_ml_ni["ML"][subd]["parentCode"] is None): + self.assertIn(test_request_ml_ni["ML"][subd]["parentCode"], list(test_request_ml_ni["ML"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_ml_ni["ML"][subd]["parentCode"]), list(test_request_ml_ni["ML"][subd].keys())) + if not (test_request_ml_ni["ML"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_ml_ni["ML"][subd]["flag"])[0], self.flag_base_url + "ML/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "ML/" + subd, os.path.splitext(test_request_ml_ni["ML"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_ml_ni["ML"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_ml_ni["ML"][subd]["flag"])) + for key in list(test_request_ml_ni["ML"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) - for subd in test_iso3166_2_request_ml_ni["NI"]: - self.assertIsNot(test_iso3166_2_request_ml_ni["NI"][subd]["name"], None, - "Expected subdivision name to not be None, got {}.".format(test_iso3166_2_request_ml_ni["NI"][subd]["name"])) - self.assertEqual(test_iso3166_2_request_ml_ni["NI"][subd]["name"], test_iso3166_2_request_ml_ni["NI"][subd]["localName"], + for subd in test_request_ml_ni["NI"]: + self.assertIsNot(test_request_ml_ni["NI"][subd]["name"], None, + "Expected subdivision name to not be None, got {}.".format(test_request_ml_ni["NI"][subd]["name"])) + self.assertEqual(test_request_ml_ni["NI"][subd]["name"], test_request_ml_ni["NI"][subd]["localName"], "Expected subdivision's name and local name to be the same.") - if not (test_iso3166_2_request_ml_ni["NI"][subd]["parentCode"] is None): - self.assertIn(test_iso3166_2_request_ml_ni["NI"][subd]["parentCode"], list(test_iso3166_2_request_ml_ni["NI"][subd].keys()), - "Parent code {} not found in list of subdivision codes:\n{}.".format(test_iso3166_2_request_ml_ni["NI"][subd]["parentCode"]), list(test_iso3166_2_request_ml_ni["NI"][subd].keys())) - if not (test_iso3166_2_request_ml_ni["NI"][subd]["flag"] is None): - self.assertEqual(os.path.splitext(test_iso3166_2_request_ml_ni["NI"][subd]["flag"])[0], self.flag_base_url + "NI/" + subd, - "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "NI/" + subd, os.path.splitext(test_iso3166_2_request_ml_ni["NI"][subd]["flag"])[0])) - self.assertEqual(requests.get(test_iso3166_2_request_ml_ni["NI"][subd]["flag"], headers=self.user_agent_header).status_code, 200, - "Flag URL invalid: {}.".format(test_iso3166_2_request_ml_ni["NI"][subd]["flag"])) - for key in list(test_iso3166_2_request_ml_ni["NI"][subd].keys()): + if not (test_request_ml_ni["NI"][subd]["parentCode"] is None): + self.assertIn(test_request_ml_ni["NI"][subd]["parentCode"], list(test_request_ml_ni["NI"][subd].keys()), + "Parent code {} not found in list of subdivision codes:\n{}.".format(test_request_ml_ni["NI"][subd]["parentCode"]), list(test_request_ml_ni["NI"][subd].keys())) + if not (test_request_ml_ni["NI"][subd]["flag"] is None): + self.assertEqual(os.path.splitext(test_request_ml_ni["NI"][subd]["flag"])[0], self.flag_base_url + "NI/" + subd, + "Expected flag URL to be {}, got {}.".format(self.flag_base_url + "NI/" + subd, os.path.splitext(test_request_ml_ni["NI"][subd]["flag"])[0])) + self.assertEqual(requests.get(test_request_ml_ni["NI"][subd]["flag"], headers=self.user_agent_header).status_code, 200, + "Flag URL invalid: {}.".format(test_request_ml_ni["NI"][subd]["flag"])) + for key in list(test_request_ml_ni["NI"][subd].keys()): self.assertIn(key, self.correct_subdivision_keys, "Attribute {} not found in list of correct attributes:\n{}.".format(key, self.correct_subdivision_keys)) #5.) - test_iso3166_2_request_error1 = requests.get(self.country_name_base_url + test_country_name_error1, headers=self.user_agent_header).json() #ABCDEF - - self.assertEqual(len(test_iso3166_2_request_error1), 3, - "Expected output object of API to be of length 3, got {}.".format(len(test_iso3166_2_request_error1))) - self.assertEqual(test_iso3166_2_request_error1["message"], "Invalid country name input: {}.".format(test_country_name_error1.title()), - "Error message does not match expected:\n{}.".format(test_iso3166_2_request_error1["message"])) - self.assertEqual(test_iso3166_2_request_error1["path"], self.country_name_base_url + test_country_name_error1, - "Error path does not match expected:\n{}.".format(test_iso3166_2_request_error1["path"])) - self.assertEqual(test_iso3166_2_request_error1["status"], 400, - "Error status does not match expected:\n{}.".format(test_iso3166_2_request_error1["status"])) + test_request_error = requests.get(self.country_name_base_url + test_country_name_error1, headers=self.user_agent_header).json() #ABCDEF + + self.assertEqual(len(test_request_error), 3, + "Expected output object of API to be of length 3, got {}.".format(len(test_request_error))) + self.assertEqual(test_request_error["message"], "Invalid country name input: {}.".format(test_country_name_error1.title()), + "Error message does not match expected:\n{}.".format(test_request_error["message"])) + self.assertEqual(test_request_error["path"], self.country_name_base_url + test_country_name_error1, + "Error path does not match expected:\n{}.".format(test_request_error["path"])) + self.assertEqual(test_request_error["status"], 400, + "Error status does not match expected:\n{}.".format(test_request_error["status"])) #6.) - test_iso3166_2_request_error2 = requests.get(self.country_name_base_url + test_country_name_error2, headers=self.user_agent_header).json() #12345 - - self.assertEqual(len(test_iso3166_2_request_error2), 3, - "Expected output object of API to be of length 3, got {}.".format(len(test_iso3166_2_request_error2))) - self.assertEqual(test_iso3166_2_request_error2["message"], "Invalid country name input: {}.".format(test_country_name_error2.title()), - "Error message does not match expected:\n{}.".format(test_iso3166_2_request_error2["message"])) - self.assertEqual(test_iso3166_2_request_error2["path"], self.country_name_base_url + test_country_name_error2, - "Error path does not match expected:\n{}.".format(test_iso3166_2_request_error2["path"])) - self.assertEqual(test_iso3166_2_request_error2["status"], 400, - "Error status does not match expected:\n{}.".format(test_iso3166_2_request_error2["status"])) - - # @unittest.skip("Skipping /all endpoint tests to not overload server.") + test_request_error = requests.get(self.country_name_base_url + test_country_name_error2, headers=self.user_agent_header).json() #12345 + + self.assertEqual(len(test_request_error), 3, + "Expected output object of API to be of length 3, got {}.".format(len(test_request_error))) + self.assertEqual(test_request_error["message"], "Invalid country name input: {}.".format(test_country_name_error2.title()), + "Error message does not match expected:\n{}.".format(test_request_error["message"])) + self.assertEqual(test_request_error["path"], self.country_name_base_url + test_country_name_error2, + "Error path does not match expected:\n{}.".format(test_request_error["path"])) + self.assertEqual(test_request_error["status"], 400, + "Error status does not match expected:\n{}.".format(test_request_error["status"])) + + @unittest.skip("Skipping /all endpoint tests to not overload server.") def test_all_endpoint(self): """ Test /all endpoint which returns all subdivision data for all ISO 3166 countries. """ #1.) @@ -841,6 +844,17 @@ def test_all_endpoint(self): for subd in test_request_all[alpha2]: self.assertIn(subd, self.iso3166_2_data.subdivision_codes(alpha2), "Subdivision code {} not found in list of available subdivision codes.".format(subd)) + def test_list_subdivisions_endpoint(self): + """ Testing /list_subdivisions endpoint, return all ISO 3166 subdivision codes for each country. """ +#1.) + test_request_list_subdivisions = requests.get(self.list_subdivisions_base_url, headers=self.user_agent_header).json() + self.assertEqual(len(test_request_list_subdivisions), 250, + "Expected output object of API to be of length 250, got {}.".format(len(test_request_list_subdivisions))) + for alpha2 in list(test_request_list_subdivisions.keys()): + self.assertIn(alpha2, iso3166.countries_by_alpha2, "Alpha-2 code {} not found in list of available country codes.".format(alpha2)) + for subd in test_request_list_subdivisions[alpha2]: + self.assertIn(subd, self.iso3166_2_data.subdivision_codes(alpha2), "Subdivision code {} not found in list of available subdivision codes.".format(subd)) + if __name__ == '__main__': #run all unit tests unittest.main(verbosity=2) \ No newline at end of file diff --git a/tests/test_update_subdivisions.py b/tests/test_update_subdivisions.py index c459480..c37e86c 100644 --- a/tests/test_update_subdivisions.py +++ b/tests/test_update_subdivisions.py @@ -5,7 +5,6 @@ import unittest unittest.TestLoader.sortTestMethodsUsing = None -@unittest.skip("") class Update_Subdivisions_Tests(unittest.TestCase): """ Test suite for testing update_subdivisions.py script that that is used for the @@ -19,6 +18,9 @@ class Update_Subdivisions_Tests(unittest.TestCase): testing amending subdivisions to the main subdivisions object via update_subdivisions function. test_update_subdivisions_delete: testing deleting subdivisions to the main subdivisions object via update_subdivisions function. + test_get_flag_icons_url: + testing auxillary function used for getting the flag URL of a subdivision from the + iso3166-flag-icons repo. """ def setUp(self): """ Initialise test variables. """ @@ -34,7 +36,7 @@ def setUp(self): with open(os.path.join("iso3166_2", "iso3166-2.json"), "r") as input_json: iso3166_2_json = json.load(input_json) with open(self.test_iso3166_2_copy, "w") as output_json: - json.dump(iso3166_2_json, output_json) + json.dump(iso3166_2_json, output_json, ensure_ascii=False, indent=4) # @unittest.skip("") def test_update_subdivisions_add(self): @@ -66,7 +68,8 @@ def test_update_subdivisions_add(self): flag="https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/LT/LT-100.svg", iso3166_2_filename=self.test_iso3166_2_copy, rest_countries_keys="currencies,idd,carSigns", export=True) expected_test_subdivision_lt_output = {"name": "Vilnius 2.0 municipality", "localName": "Vilnius 2.0 municipality", "type": "City municipality", - "latLng": None, "parentCode": "LT-UT", "flag": "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/LT/LT-100.svg"} + "latLng": None, "parentCode": "LT-UT", "flag": "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/LT/LT-100.svg", + "currencies": {"EUR": {"name": "Euro", "symbol": "€"}}, "carSigns": ["LT"], "idd": "Root: +3, Suffixes: ['70']"} #import json with newly added subdivision with open(self.test_iso3166_2_copy, "r") as subdivision_update_json: @@ -118,10 +121,10 @@ def test_update_subdivisions_add(self): update_subdivision(subdivision_csv=123, iso3166_2_filename=self.test_iso3166_2_copy) #6.) with (self.assertRaises(ValueError)): - update_subdivision(alpha_code="XY", subdivision_code="XY-XY", iso3166_2_filename=self.test_iso3166_2_copy) - update_subdivision(alpha_code="AD,DE,FR", subdivision_code="AD-ZZ", iso3166_2_filename=self.test_iso3166_2_copy) - update_subdivision(alpha_code="PK", subdivision_code="PK-AA", parent_code="PK-ZZ", iso3166_2_filename=self.test_iso3166_2_copy, export=True) - update_subdivision(alpha_code="SS", subdivision_code="SS-BW,SS-NU", parent_code="SS-WR", iso3166_2_filename=self.test_iso3166_2_copy, export=True) + update_subdivision(alpha_code="XY", subdivision_code="XY-XY", iso3166_2_filename=self.test_iso3166_2_copy) #invalid alpha code + update_subdivision(alpha_code="AD,DE,FR", subdivision_code="AD-ZZ", iso3166_2_filename=self.test_iso3166_2_copy) #more than 1 alpha code + update_subdivision(alpha_code="PK", subdivision_code="PK-AA", parent_code="PK-ZZ", iso3166_2_filename=self.test_iso3166_2_copy, export=True) #invalid parent code + update_subdivision(alpha_code="SS", subdivision_code="SS-BW,SS-NU", parent_code="SS-WR", iso3166_2_filename=self.test_iso3166_2_copy, export=True) #more than 1 subdivision code #7.) with (self.assertRaises(TypeError)): update_subdivision(alpha_code=123, subdivision_code="XY-XY", iso3166_2_filename=self.test_iso3166_2_copy, export=True) @@ -134,7 +137,7 @@ def test_update_subdivisions_add(self): update_subdivision(alpha_code="ID", subdivision_code="ID-12", rest_countries_keys=9.05, iso3166_2_filename=self.test_iso3166_2_copy) update_subdivision(alpha_code="ID", subdivision_code="ID-12", exclude_default_keys=True, iso3166_2_filename=self.test_iso3166_2_copy) - @unittest.skip("") + # @unittest.skip("") def test_update_subdivisions_amend(self): """ Testing amending subdivisions to the main subdivisions object via update_subdivisions function. """ test_subdivision_jo_ka = "JO-KA" #Al Karak - changing subdivision name and local name @@ -198,7 +201,7 @@ def test_update_subdivisions_amend(self): self.assertEqual(test_subdivision_wf_sg_subdivisions_json['WF']['WF-SW'], expected_test_subdivision_wf_sg_output, f"Expected and observed subdivision output do not match:\n{test_subdivision_wf_sg_subdivisions_json['WF']['WF-SW']}.") - @unittest.skip("") + # @unittest.skip("") def test_update_subdivisions_delete(self): """ Testing deleting subdivisions to the main subdivisions object via update_subdivisions function. """ test_subdivision_kw = "KW" #Kuwait @@ -255,9 +258,48 @@ def test_update_subdivisions_delete(self): f"Expected updates subdivision output to be an empty dict, got:\n{test_subdivision_sm_update_subdivisions_output}.") #5.) with (self.assertRaises(ValueError)): - update_subdivision(alpha_code="AD", subdivision_code="100", iso3166_2_filename=self.test_iso3166_2_copy, delete=1) - update_subdivision(alpha_code="DE", subdivision_code="AD", iso3166_2_filename=self.test_iso3166_2_copy, delete=1) - update_subdivision(alpha_code="ZA", subdivision_code="ABC", iso3166_2_filename=self.test_iso3166_2_copy, delete=1) + update_subdivision(alpha_code="AD", subdivision_code="100", iso3166_2_filename=self.test_iso3166_2_copy, delete=1) #subdivision doesn't exist + update_subdivision(alpha_code="DE", subdivision_code="AD", iso3166_2_filename=self.test_iso3166_2_copy, delete=1) #subdivision doesn't exist + update_subdivision(alpha_code="ZA", subdivision_code="ABC", iso3166_2_filename=self.test_iso3166_2_copy, delete=1) #subdivision doesn't exist + + # @unittest.skip("") + def test_get_flag_icons(self): + """ Testing get_flag_icons function used for getting the flag URL of each subdivision. """ + test_alpha_flag_ad = ("AD", "AD-08") + test_alpha_flag_de = ("DE", "DE-BB") + test_alpha_flag_mx = ("MX", "MX-MOR") + test_alpha_flag_sn = ("SN", "SN-LG") + test_alpha_flag_xx = ("XX", "XX-XYZ") +#1.) + test_alpha_flag_ad_url = get_flag_icons_url(test_alpha_flag_ad[0], test_alpha_flag_ad[1]) + expected_alpha_flag_ad_url = "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/AD/AD-08.svg" + + self.assertEqual(test_alpha_flag_ad_url, expected_alpha_flag_ad_url, + f"Expected URL for subdivision does not match, got {test_alpha_flag_ad_url}.") +#2.) + test_alpha_flag_de_url = get_flag_icons_url(test_alpha_flag_de[0], test_alpha_flag_de[1]) + expected_alpha_flag_de_url = "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/DE/DE-BB.svg" + + self.assertEqual(test_alpha_flag_de_url, expected_alpha_flag_de_url, + f"Expected URL for subdivision does not match, got {test_alpha_flag_de_url}.") +#3.) + test_alpha_flag_mx_url = get_flag_icons_url(test_alpha_flag_mx[0], test_alpha_flag_mx[1]) + expected_alpha_flag_mx_url = "https://github.com/amckenna41/iso3166-flag-icons/blob/main/iso3166-2-icons/MX/MX-MOR.svg" + + self.assertEqual(test_alpha_flag_mx_url, expected_alpha_flag_mx_url, + f"Expected URL for subdivision does not match, got {test_alpha_flag_mx_url}.") +#4.) + test_alpha_flag_sn_url = get_flag_icons_url(test_alpha_flag_sn[0], test_alpha_flag_sn[1]) + expected_alpha_flag_sn_url = None + + self.assertEqual(test_alpha_flag_sn_url, expected_alpha_flag_sn_url, + f"Expected URL for subdivision does not match, got {test_alpha_flag_sn_url}.") +#5.) + test_alpha_flag_xx_url = get_flag_icons_url(test_alpha_flag_xx[0], test_alpha_flag_xx[1]) + expected_alpha_flag_xx_url = None + + self.assertEqual(test_alpha_flag_xx_url, expected_alpha_flag_xx_url, + f"Expected URL for subdivision does not match, got {test_alpha_flag_xx_url}.") def tearDown(self): """ Delete any temp export folder. """