From 5b4d6cd2ef26fde8a861ad17ccbf546d3f102330 Mon Sep 17 00:00:00 2001 From: James Hughes Date: Thu, 30 Nov 2023 16:01:17 -0400 Subject: [PATCH 1/2] remove area from add docstring --- site/assignments/country-catalogue/asn4.ipynb | 1728 ++++++++--------- site/assignments/country-catalogue/asn4.py | 2 - 2 files changed, 863 insertions(+), 867 deletions(-) diff --git a/site/assignments/country-catalogue/asn4.ipynb b/site/assignments/country-catalogue/asn4.ipynb index 75a0e1db..fa855ce8 100644 --- a/site/assignments/country-catalogue/asn4.ipynb +++ b/site/assignments/country-catalogue/asn4.ipynb @@ -1,873 +1,871 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] }, - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "id": "fzMQClXIAq7r" - }, - "outputs": [], - "source": [ - "class Country:\n", - " \"\"\"\n", - " Class for keeping track of country details for the purpose of the Country Catalogue's functionality. Country objects\n", - " know their name, which continent they are on, their population, and their surface area.\n", - " \"\"\"\n", - "\n", - " def __init__(self, name: str, continent: str, population: int, area: float):\n", - "\n", - "\n", - " def population_density(self) -> float:\n", - " \"\"\"\n", - " Calculate and return the population density of the Country. Population density is calculated with\n", - " population/area.\n", - "\n", - " :return: The population density of the Country.\n", - " :rtype: float.\n", - " \"\"\"\n", - "\n", - "\n", - " def __eq__(self, other: \"Country\") -> bool:\n", - " \"\"\"\n", - " Check if two instances of a Country object are equal. Country objects are considered equal if all attributes are\n", - " equal.\n", - "\n", - " :param other: A Country object to compare the self Country to.\n", - " :type other: Country.\n", - " :return: True if the Country objects have all attributes equal.\n", - " :rtype: boolean.\n", - " \"\"\"\n", - "\n", - "\n", - " def __repr__(self) -> str:\n", - " \"\"\"\n", - " Returns a string representation of the Country object. The string is \"name, continent, population, area\".\n", - "\n", - " :return: String representation of the Country object of the form \"name, continent, population, area\".\n", - " :rtype: String.\n", - " \"\"\"\n" - ] - }, - { - "cell_type": "code", - "source": [ - "import unittest\n", - "\n", - "class CountryTest(unittest.TestCase):\n", - " def test_country_name_returns_correct_country_name(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(\"Name\", country.name)\n", - "\n", - " def test_country_continent_returns_correct_country_continent(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(\"Continent\", country.continent)\n", - "\n", - " def test_country_population_returns_correct_country_population(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(123456789, country.population)\n", - "\n", - " def test_country_area_returns_correct_country_area(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(987654321, country.area)\n", - "\n", - " def test_population_density_returns_correct_population_density(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertAlmostEqual(123456789 / 987654321, country.population_density())\n", - "\n", - " def test_population_density_with_zero_area_raises_zero_division_error(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 0)\n", - " with self.assertRaises(ZeroDivisionError):\n", - " country.population_density()\n", - "\n", - " def test_equals_equal_country_objects_returns_true(self):\n", - " country_a = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " country_b = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(country_a, country_b)\n", - "\n", - " def test_equals_unequal_country_objects_returns_false(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " cases = [\n", - " Country(\"Eman\", \"Continent\", 123456789, 987654321),\n", - " Country(\"Name\", \"Tnenitnoc\", 123456789, 987654321),\n", - " Country(\"Name\", \"Continent\", 987654321, 987654321),\n", - " Country(\"Name\", \"Continent\", 123456789, 123456789),\n", - " Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789),\n", - " 54321,\n", - " ]\n", - " for case in cases:\n", - " with self.subTest(case=case):\n", - " self.assertNotEqual(country, case)\n", - "\n", - " def test_repr_arbitrary_country_returns_correct_string(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(\"Country(name=Name, continent=Continent, population=123456789, area=987654321)\", str(country))\n" - ], - "metadata": { - "id": "fIyBwt0uA6kO" - }, - "execution_count": 2, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "class CountryCatalogue:\n", - " \"\"\"\n", - " Class for managing a collection of Country Objects. The class maintains a list of the Country objects and provides\n", - " a mechanism for adding, removing, finding, checking if exists, and other collection-ie type methods.\n", - " \"\"\"\n", - "\n", - " def __init__(self):\n", - "\n", - "\n", - " def _find(self, country: Country) -> int:\n", - " \"\"\"\n", - " Find the index of the specified Country within the Country Catalogue. If the Country does not exist, return the\n", - " sentinel value of -1.\n", - "\n", - " :param country: Country object to search for.\n", - " :type country: Country object.\n", - " :return: Index of the Country with the matching values.\n", - " :rtype: integer.\n", - " \"\"\"\n", - "\n", - "\n", - " def contains(self, country: Country) -> bool:\n", - " \"\"\"\n", - " Check if a Country with the provided values exists within the collection. Returns True if it does and False\n", - " otherwise.\n", - "\n", - " :param country: Country object to search for.\n", - " :type country: Country object.\n", - " :return: True if the Country object is contained within the CountryCatalogue, False otherwise.\n", - " :rtype: bool.\n", - " \"\"\"\n", - "\n", - "\n", - " def add(self, country: Country) -> None:\n", - " \"\"\"\n", - " Add the provided Country object to the Country Catalogue.\n", - "\n", - " :param country: Country object to add to the collection.\n", - " :type country: Country object.\n", - " :param area: Area of the country.\n", - " :type area: float.\n", - " \"\"\"\n", - "\n", - "\n", - " def remove(self, country: Country) -> Country:\n", - " \"\"\"\n", - " Remove a Country object from the Country Catalogue matching the provided value. The removed Country object is\n", - " returned by the method. If no such Country object exists within the collection, a ValueError is raised.\n", - "\n", - " :raise ValueError: If the Country object does not exist within the collection, a ValueError is raised.\n", - " :param country: Country object to remove from the collection.\n", - " :type country: Country object.\n", - " \"\"\"\n", - "\n", - "\n", - " def country_with_largest_population_density(self) -> Country:\n", - " \"\"\"\n", - " Find and return the Country object with the largest population density. A reference to the Country object with\n", - " the largest density is returned. If the collection is empty, a IndexError is raised.\n", - "\n", - " :raise IndexError: If the collection is empty, raise an IndexError.\n", - " :return: Country object with the largest population density.\n", - " :rtype: Country.\n", - " \"\"\"\n", - "\n", - "\n", - " def country_with_smallest_population_density(self) -> Country:\n", - " \"\"\"\n", - " Find and return the Country object with the smallest population density. A reference to the Country object with\n", - " the smallest density is returned. If the collection is empty, a IndexError is raised.\n", - "\n", - " :raise IndexError: If the collection is empty, raise an IndexError.\n", - " :return: Country object with the smallest population density.\n", - " :rtype: Country.\n", - " \"\"\"\n", - "\n", - "\n", - " def filter_countries_by_population_density(self, low_limit: float, high_limit: float) -> \"CountryCatalogue\":\n", - " \"\"\"\n", - " Return a new CountryCatalogue containing only Country objects from the current CountryCatalogue with a\n", - " population density between the specified low & high range. If no Country objects between the specified range\n", - " exist, an empty CountryCatalogue is returned.\n", - "\n", - " :param low_limit: The low population density; only Country objects with a population density greater than or\n", - " equal to the low_limit will be included in the filtered CountryCatalogue.\n", - " :type low_limit: float\n", - " :param high_limit: The high population density; only Country objects with a population density strictly less\n", - " than the high_limit will be included in the filtered CountryCatalogue.\n", - " :type high_limit: float\n", - " :return: A new CountryCatalogue object with copies of Country objects that fall within the specified range.\n", - " :rtype: CountryCatalogue\n", - " \"\"\"\n", - "\n", - "\n", - " def most_populous_continent(self) -> str:\n", - " \"\"\"\n", - " Find and return the name of the continent with the largest population based on the Country objects within the\n", - " CountryCatalogue. If two or more continents have the same largest population, return the name of the country\n", - " found first. If the collection is empty, a IndexError is raised.\n", - "\n", - " :raise IndexError: If the collection is empty, raise an IndexError.\n", - " :return: Name of the continent with the largest population.\n", - " :rtype: str.\n", - " \"\"\"\n", - "\n", - "\n", - " def __getitem__(self, item: int) -> Country:\n", - "\n", - "\n", - " def __len__(self) -> int:\n", - "\n", - "\n", - " def __eq__(self, other: \"CountryCatalogue\") -> bool:\n", - "\n", - "\n", - " def __repr__(self) -> str:\n" - ], - "metadata": { - "id": "SGrsdqthA-Dc" - }, - "execution_count": 3, - "outputs": [] + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "fzMQClXIAq7r" + }, + "outputs": [], + "source": [ + "class Country:\n", + " \"\"\"\n", + " Class for keeping track of country details for the purpose of the Country Catalogue's functionality. Country objects\n", + " know their name, which continent they are on, their population, and their surface area.\n", + " \"\"\"\n", + "\n", + " def __init__(self, name: str, continent: str, population: int, area: float):\n", + "\n", + "\n", + " def population_density(self) -> float:\n", + " \"\"\"\n", + " Calculate and return the population density of the Country. Population density is calculated with\n", + " population/area.\n", + "\n", + " :return: The population density of the Country.\n", + " :rtype: float.\n", + " \"\"\"\n", + "\n", + "\n", + " def __eq__(self, other: \"Country\") -> bool:\n", + " \"\"\"\n", + " Check if two instances of a Country object are equal. Country objects are considered equal if all attributes are\n", + " equal.\n", + "\n", + " :param other: A Country object to compare the self Country to.\n", + " :type other: Country.\n", + " :return: True if the Country objects have all attributes equal.\n", + " :rtype: boolean.\n", + " \"\"\"\n", + "\n", + "\n", + " def __repr__(self) -> str:\n", + " \"\"\"\n", + " Returns a string representation of the Country object. The string is \"name, continent, population, area\".\n", + "\n", + " :return: String representation of the Country object of the form \"name, continent, population, area\".\n", + " :rtype: String.\n", + " \"\"\"\n" + ] + }, + { + "cell_type": "code", + "source": [ + "import unittest\n", + "\n", + "class CountryTest(unittest.TestCase):\n", + " def test_country_name_returns_correct_country_name(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(\"Name\", country.name)\n", + "\n", + " def test_country_continent_returns_correct_country_continent(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(\"Continent\", country.continent)\n", + "\n", + " def test_country_population_returns_correct_country_population(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(123456789, country.population)\n", + "\n", + " def test_country_area_returns_correct_country_area(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(987654321, country.area)\n", + "\n", + " def test_population_density_returns_correct_population_density(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertAlmostEqual(123456789 / 987654321, country.population_density())\n", + "\n", + " def test_population_density_with_zero_area_raises_zero_division_error(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 0)\n", + " with self.assertRaises(ZeroDivisionError):\n", + " country.population_density()\n", + "\n", + " def test_equals_equal_country_objects_returns_true(self):\n", + " country_a = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " country_b = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(country_a, country_b)\n", + "\n", + " def test_equals_unequal_country_objects_returns_false(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " cases = [\n", + " Country(\"Eman\", \"Continent\", 123456789, 987654321),\n", + " Country(\"Name\", \"Tnenitnoc\", 123456789, 987654321),\n", + " Country(\"Name\", \"Continent\", 987654321, 987654321),\n", + " Country(\"Name\", \"Continent\", 123456789, 123456789),\n", + " Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789),\n", + " 54321,\n", + " ]\n", + " for case in cases:\n", + " with self.subTest(case=case):\n", + " self.assertNotEqual(country, case)\n", + "\n", + " def test_repr_arbitrary_country_returns_correct_string(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(\"Country(name=Name, continent=Continent, population=123456789, area=987654321)\", str(country))\n" + ], + "metadata": { + "id": "fIyBwt0uA6kO" + }, + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "class CountryCatalogue:\n", + " \"\"\"\n", + " Class for managing a collection of Country Objects. The class maintains a list of the Country objects and provides\n", + " a mechanism for adding, removing, finding, checking if exists, and other collection-ie type methods.\n", + " \"\"\"\n", + "\n", + " def __init__(self):\n", + "\n", + "\n", + " def _find(self, country: Country) -> int:\n", + " \"\"\"\n", + " Find the index of the specified Country within the Country Catalogue. If the Country does not exist, return the\n", + " sentinel value of -1.\n", + "\n", + " :param country: Country object to search for.\n", + " :type country: Country object.\n", + " :return: Index of the Country with the matching values.\n", + " :rtype: integer.\n", + " \"\"\"\n", + "\n", + "\n", + " def contains(self, country: Country) -> bool:\n", + " \"\"\"\n", + " Check if a Country with the provided values exists within the collection. Returns True if it does and False\n", + " otherwise.\n", + "\n", + " :param country: Country object to search for.\n", + " :type country: Country object.\n", + " :return: True if the Country object is contained within the CountryCatalogue, False otherwise.\n", + " :rtype: bool.\n", + " \"\"\"\n", + "\n", + "\n", + " def add(self, country: Country) -> None:\n", + " \"\"\"\n", + " Add the provided Country object to the Country Catalogue.\n", + "\n", + " :param country: Country object to add to the collection.\n", + " :type country: Country object.\n", + " \"\"\"\n", + "\n", + "\n", + " def remove(self, country: Country) -> Country:\n", + " \"\"\"\n", + " Remove a Country object from the Country Catalogue matching the provided value. The removed Country object is\n", + " returned by the method. If no such Country object exists within the collection, a ValueError is raised.\n", + "\n", + " :raise ValueError: If the Country object does not exist within the collection, a ValueError is raised.\n", + " :param country: Country object to remove from the collection.\n", + " :type country: Country object.\n", + " \"\"\"\n", + "\n", + "\n", + " def country_with_largest_population_density(self) -> Country:\n", + " \"\"\"\n", + " Find and return the Country object with the largest population density. A reference to the Country object with\n", + " the largest density is returned. If the collection is empty, a IndexError is raised.\n", + "\n", + " :raise IndexError: If the collection is empty, raise an IndexError.\n", + " :return: Country object with the largest population density.\n", + " :rtype: Country.\n", + " \"\"\"\n", + "\n", + "\n", + " def country_with_smallest_population_density(self) -> Country:\n", + " \"\"\"\n", + " Find and return the Country object with the smallest population density. A reference to the Country object with\n", + " the smallest density is returned. If the collection is empty, a IndexError is raised.\n", + "\n", + " :raise IndexError: If the collection is empty, raise an IndexError.\n", + " :return: Country object with the smallest population density.\n", + " :rtype: Country.\n", + " \"\"\"\n", + "\n", + "\n", + " def filter_countries_by_population_density(self, low_limit: float, high_limit: float) -> \"CountryCatalogue\":\n", + " \"\"\"\n", + " Return a new CountryCatalogue containing only Country objects from the current CountryCatalogue with a\n", + " population density between the specified low & high range. If no Country objects between the specified range\n", + " exist, an empty CountryCatalogue is returned.\n", + "\n", + " :param low_limit: The low population density; only Country objects with a population density greater than or\n", + " equal to the low_limit will be included in the filtered CountryCatalogue.\n", + " :type low_limit: float\n", + " :param high_limit: The high population density; only Country objects with a population density strictly less\n", + " than the high_limit will be included in the filtered CountryCatalogue.\n", + " :type high_limit: float\n", + " :return: A new CountryCatalogue object with copies of Country objects that fall within the specified range.\n", + " :rtype: CountryCatalogue\n", + " \"\"\"\n", + "\n", + "\n", + " def most_populous_continent(self) -> str:\n", + " \"\"\"\n", + " Find and return the name of the continent with the largest population based on the Country objects within the\n", + " CountryCatalogue. If two or more continents have the same largest population, return the name of the country\n", + " found first. If the collection is empty, a IndexError is raised.\n", + "\n", + " :raise IndexError: If the collection is empty, raise an IndexError.\n", + " :return: Name of the continent with the largest population.\n", + " :rtype: str.\n", + " \"\"\"\n", + "\n", + "\n", + " def __getitem__(self, item: int) -> Country:\n", + "\n", + "\n", + " def __len__(self) -> int:\n", + "\n", + "\n", + " def __eq__(self, other: \"CountryCatalogue\") -> bool:\n", + "\n", + "\n", + " def __repr__(self) -> str:\n" + ], + "metadata": { + "id": "SGrsdqthA-Dc" + }, + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "import unittest\n", + "\n", + "class CountryCatalogueTest(unittest.TestCase):\n", + " def test_len_empty_collection_returns_0(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " self.assertEqual(0, len(empty_country_catalogue))\n", + "\n", + " def test_contains_empty_collection_returns_false(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " self.assertFalse(empty_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", + "\n", + " def test_remove_empty_collection_raises_value_error(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " with self.assertRaises(ValueError):\n", + " empty_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + "\n", + " def test_country_with_largest_population_density_empty_collection_raises_index_error(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " with self.assertRaises(IndexError):\n", + " empty_country_catalogue.country_with_largest_population_density()\n", + "\n", + " def test_country_with_smallest_population_density_empty_collection_raises_index_error(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " with self.assertRaises(IndexError):\n", + " empty_country_catalogue.country_with_smallest_population_density()\n", + "\n", + " def test_filter_countries_by_population_density_empty_collection_returns_empty_country_catalogue(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " self.assertEqual(CountryCatalogue(), empty_country_catalogue.filter_countries_by_population_density(0, 10000))\n", + "\n", + " def test_most_populous_continent_empty_collection_raises_index_error(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " with self.assertRaises(IndexError):\n", + " empty_country_catalogue.most_populous_continent()\n", + "\n", + " def test_getitem_empty_collection_raises_index_error(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " indicies = [-1, 0, 1]\n", + " for index in indicies:\n", + " with self.subTest(index=index):\n", + " with self.assertRaises(IndexError):\n", + " empty_country_catalogue[index]\n", + "\n", + " def test_repr_empty_collection_returns_empty_string(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " self.assertEqual(\"\", str(empty_country_catalogue))\n", + "\n", + " def test_len_singleton_returns_1(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(1, len(singleton_country_catalogue))\n", + "\n", + " def test_contains_singleton_country_exists_returns_true(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertTrue(singleton_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", + "\n", + " def test_contains_singleton_country_not_exists_returns_false(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertFalse(singleton_country_catalogue.contains(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789)))\n", + "\n", + " def test_remove_singleton_country_exists_returns_country(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(\n", + " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", + " singleton_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321)),\n", + " )\n", + "\n", + " def test_remove_singleton_country_exists_removes_country(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " singleton_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertFalse(singleton_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", + "\n", + " def test_remove_singleton_country_not_exists_raises_value_error(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " with self.assertRaises(ValueError):\n", + " singleton_country_catalogue.remove(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789))\n", + "\n", + " def test_country_with_largest_population_density_singleton_returns_correct_country(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(\n", + " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", + " singleton_country_catalogue.country_with_largest_population_density(),\n", + " )\n", + "\n", + " def test_country_with_smallest_population_density_singleton_returns_correct_country(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(\n", + " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", + " singleton_country_catalogue.country_with_smallest_population_density(),\n", + " )\n", + "\n", + " def test_filter_countries_by_population_density_singleton_not_within_range_returns_empty_country_catalogue(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " self.assertEqual(CountryCatalogue(), singleton_country_catalogue.filter_countries_by_population_density(0, 5))\n", + "\n", + " def test_filter_countries_by_population_density_singleton_within_range_returns_correct_country_catalogue(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " expected_country_catalogue = CountryCatalogue()\n", + " expected_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(0, 100)\n", + " self.assertEqual(expected_country_catalogue, filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_singleton_equal_low_limit_returns_correct_country_catalogue(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " expected_country_catalogue = CountryCatalogue()\n", + " expected_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(10, 100)\n", + " self.assertEqual(expected_country_catalogue, filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_singleton_equal_high_limit_returns_empty_country_catalogue(\n", + " self,\n", + " ):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(0, 10)\n", + " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_singleton_low_high_high_low_returns_empty_country_catalogue(\n", + " self,\n", + " ):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(100, 0)\n", + " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", + "\n", + " def test_most_populous_continent_singleton_returns_correct_continent(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " self.assertEqual(\"Continent\", singleton_country_catalogue.most_populous_continent())\n", + "\n", + " def test_getitem_valid_index_singleton_returns_item(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(Country(\"Name\", \"Continent\", 123456789, 987654321), singleton_country_catalogue[0])\n", + "\n", + " def test_getitem_invalid_index_singleton_raises_index_error(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " with self.assertRaises(IndexError):\n", + " singleton_country_catalogue[1]\n", + "\n", + " def test_repr_singleton_returns_correct_string(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(\n", + " \"Country(name=Name, continent=Continent, population=123456789, area=987654321)\\n\",\n", + " str(singleton_country_catalogue),\n", + " )\n", + "\n", + " def test_len_many_returns_correct_length(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " self.assertEqual(5, len(many_country_catalogue))\n", + "\n", + " def test_contains_many_country_exists_returns_true(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " self.assertTrue(many_country_catalogue.contains(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", + "\n", + " def test_contains_many_country_not_exists_returns_false(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " self.assertFalse(many_country_catalogue.contains(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789)))\n", + "\n", + " def test_remove_many_country_exists_returns_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " country = Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)\n", + " self.assertEqual(country, many_country_catalogue.remove(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", + "\n", + " def test_remove_many_country_exists_removes_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " many_country_catalogue.remove(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " self.assertFalse(many_country_catalogue.contains(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", + "\n", + " def test_remove_many_country_not_exists_raises_value_error(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " with self.assertRaises(ValueError):\n", + " many_country_catalogue.remove(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789))\n", + "\n", + " def test_country_with_largest_population_density_many_returns_correct_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " self.assertEqual(\n", + " Country(\"Name_3\", \"Continent_3\", 10, 1), many_country_catalogue.country_with_largest_population_density()\n", + " )\n", + "\n", + " def test_country_with_smallest_population_density_many_returns_correct_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " self.assertEqual(\n", + " Country(\"Name_5\", \"Continent_5\", 1, 1), many_country_catalogue.country_with_smallest_population_density()\n", + " )\n", + "\n", + " def test_filter_countries_by_population_density_many_not_within_range_returns_empty_country_catalogue(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " self.assertEqual(CountryCatalogue(), many_country_catalogue.filter_countries_by_population_density(50, 100))\n", + "\n", + " def test_filter_countries_by_population_density_many_within_range_returns_correct_country_catalog(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " expected = CountryCatalogue()\n", + " expected.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " expected.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " expected.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " expected.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 10)\n", + " self.assertEqual(expected, filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_many_equal_low_limit_returns_correct_country_catalogue(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " expected = CountryCatalogue()\n", + " expected.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(10, 100)\n", + " self.assertEqual(expected, filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_many_equal_high_limit_returns_empty_country_catalogue(\n", + " self,\n", + " ):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 1)\n", + " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_many_low_high_high_low_returns_empty_country_catalogue(\n", + " self,\n", + " ):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(100, 0)\n", + " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", + "\n", + " def test_most_populous_continent_many_returns_correct_continent(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_1\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_2\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_2\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_3\", 1, 1))\n", + " self.assertEqual(\"Continent_2\", many_country_catalogue.most_populous_continent())\n", + "\n", + " def test_getitem_valid_index_many_returns_item(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " countries = []\n", + " countries.append(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " countries.append(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " countries.append(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " countries.append(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " countries.append(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " for index, country in enumerate(countries):\n", + " with self.subTest(index=index, country=country):\n", + " self.assertEqual(country, many_country_catalogue[index])\n", + "\n", + " def test_getitem_invalid_index_many_raises_index_error(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " with self.assertRaises(IndexError):\n", + " many_country_catalogue[5]\n", + "\n", + " def test_repr_many_returns_correct_string(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " expected = (\n", + " \"Country(name=Name_1, continent=Continent_1, population=123456789, area=987654321)\\n\"\n", + " \"Country(name=Name_2, continent=Continent_2, population=123456789, area=987654321)\\n\"\n", + " \"Country(name=Name_3, continent=Continent_3, population=123456789, area=987654321)\\n\"\n", + " \"Country(name=Name_4, continent=Continent_4, population=123456789, area=987654321)\\n\"\n", + " \"Country(name=Name_5, continent=Continent_5, population=123456789, area=987654321)\\n\"\n", + " )\n", + " self.assertEqual(expected, str(many_country_catalogue))\n", + "\n", + " def test_remove_two_duplicate_country_exists_leaves_one_duplicate(self):\n", + " duplicate_country_catalogue = CountryCatalogue()\n", + " duplicate_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " self.assertTrue(duplicate_country_catalogue.contains(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321)))\n", + "\n", + " def test_remove_two_duplicate_country_exists_two_remove_calls_removes_both_duplicates(self):\n", + " duplicate_country_catalogue = CountryCatalogue()\n", + " duplicate_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " self.assertFalse(duplicate_country_catalogue.contains(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321)))\n", + "\n", + " def test_country_with_largest_population_density_duplicate_densities_returns_correct_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 10, 1))\n", + " self.assertEqual(\n", + " Country(\"Name_3\", \"Continent_3\", 10, 1), many_country_catalogue.country_with_largest_population_density()\n", + " )\n", + "\n", + " def test_country_with_smallest_population_density_duplicate_densities_returns_correct_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " self.assertEqual(\n", + " Country(\"Name_3\", \"Continent_3\", 1, 1), many_country_catalogue.country_with_smallest_population_density()\n", + " )\n", + "\n", + " def test_filter_countries_by_population_density_duplicate_within_range_returns_country_catalogue_all_duplicates(\n", + " self,\n", + " ):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " expected = CountryCatalogue()\n", + " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 100)\n", + " self.assertEqual(expected, filtered_country_catalogue)\n", + "\n", + " def test_most_populous_continent_duplicate_populations_returns_first_continent_found(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_1\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_2\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_3\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_3\", 1, 1))\n", + " self.assertEqual(\"Continent_1\", many_country_catalogue.most_populous_continent())\n", + "\n", + " def test_eq_various_equal_cases_are_equal(self):\n", + " empty_a = CountryCatalogue()\n", + " empty_b = CountryCatalogue()\n", + " single_a = CountryCatalogue()\n", + " single_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " single_b = CountryCatalogue()\n", + " single_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_a = CountryCatalogue()\n", + " many_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_a.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_a.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", + " many_a.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_a.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " many_b = CountryCatalogue()\n", + " many_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_b.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_b.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", + " many_b.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_b.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " countries_a = [empty_a, single_a, many_a]\n", + " countries_b = [empty_b, single_b, many_b]\n", + " for country_a, country_b in zip(countries_a, countries_b):\n", + " with self.subTest(country_a=country_a, country_b=country_b):\n", + " self.assertEqual(country_a, country_b)\n", + "\n", + " def test_eq_various_not_equal_cases_are_not_equal(self):\n", + " empty_a = CountryCatalogue()\n", + " single_a = CountryCatalogue()\n", + " single_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " single_b = CountryCatalogue()\n", + " single_b.add(Country(\"Name_100\", \"Continent_100\", 5, 1))\n", + " different_order_a = CountryCatalogue()\n", + " different_order_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " different_order_a.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " different_order_a.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", + " different_order_a.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " different_order_a.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " different_order_b = CountryCatalogue()\n", + " different_order_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " different_order_b.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " different_order_b.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", + " different_order_b.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " different_order_b.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " countries_a = [empty_a, single_a, different_order_a, empty_a]\n", + " countries_b = [single_b, single_b, different_order_b, []]\n", + " for country_a, country_b in zip(countries_a, countries_b):\n", + " with self.subTest(country_a=country_a, country_b=country_b):\n", + " self.assertNotEqual(country_a, country_b)\n" + ], + "metadata": { + "id": "fVVS4HJYBCFa" + }, + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Run this cell to run all unit tests\n", + "unittest.main(argv=[''], verbosity=2, exit=False)" + ], + "metadata": { + "id": "gz5JlQfFBzKT", + "colab": { + "base_uri": "https://localhost:8080/" }, + "outputId": "9e13d5e4-6b0c-46cc-fd6c-a86ee678cf65" + }, + "execution_count": 5, + "outputs": [ { - "cell_type": "code", - "source": [ - "import unittest\n", - "\n", - "class CountryCatalogueTest(unittest.TestCase):\n", - " def test_len_empty_collection_returns_0(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " self.assertEqual(0, len(empty_country_catalogue))\n", - "\n", - " def test_contains_empty_collection_returns_false(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " self.assertFalse(empty_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", - "\n", - " def test_remove_empty_collection_raises_value_error(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " with self.assertRaises(ValueError):\n", - " empty_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - "\n", - " def test_country_with_largest_population_density_empty_collection_raises_index_error(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " with self.assertRaises(IndexError):\n", - " empty_country_catalogue.country_with_largest_population_density()\n", - "\n", - " def test_country_with_smallest_population_density_empty_collection_raises_index_error(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " with self.assertRaises(IndexError):\n", - " empty_country_catalogue.country_with_smallest_population_density()\n", - "\n", - " def test_filter_countries_by_population_density_empty_collection_returns_empty_country_catalogue(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " self.assertEqual(CountryCatalogue(), empty_country_catalogue.filter_countries_by_population_density(0, 10000))\n", - "\n", - " def test_most_populous_continent_empty_collection_raises_index_error(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " with self.assertRaises(IndexError):\n", - " empty_country_catalogue.most_populous_continent()\n", - "\n", - " def test_getitem_empty_collection_raises_index_error(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " indicies = [-1, 0, 1]\n", - " for index in indicies:\n", - " with self.subTest(index=index):\n", - " with self.assertRaises(IndexError):\n", - " empty_country_catalogue[index]\n", - "\n", - " def test_repr_empty_collection_returns_empty_string(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " self.assertEqual(\"\", str(empty_country_catalogue))\n", - "\n", - " def test_len_singleton_returns_1(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(1, len(singleton_country_catalogue))\n", - "\n", - " def test_contains_singleton_country_exists_returns_true(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertTrue(singleton_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", - "\n", - " def test_contains_singleton_country_not_exists_returns_false(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertFalse(singleton_country_catalogue.contains(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789)))\n", - "\n", - " def test_remove_singleton_country_exists_returns_country(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(\n", - " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", - " singleton_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321)),\n", - " )\n", - "\n", - " def test_remove_singleton_country_exists_removes_country(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " singleton_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertFalse(singleton_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", - "\n", - " def test_remove_singleton_country_not_exists_raises_value_error(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " with self.assertRaises(ValueError):\n", - " singleton_country_catalogue.remove(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789))\n", - "\n", - " def test_country_with_largest_population_density_singleton_returns_correct_country(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(\n", - " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", - " singleton_country_catalogue.country_with_largest_population_density(),\n", - " )\n", - "\n", - " def test_country_with_smallest_population_density_singleton_returns_correct_country(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(\n", - " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", - " singleton_country_catalogue.country_with_smallest_population_density(),\n", - " )\n", - "\n", - " def test_filter_countries_by_population_density_singleton_not_within_range_returns_empty_country_catalogue(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " self.assertEqual(CountryCatalogue(), singleton_country_catalogue.filter_countries_by_population_density(0, 5))\n", - "\n", - " def test_filter_countries_by_population_density_singleton_within_range_returns_correct_country_catalogue(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " expected_country_catalogue = CountryCatalogue()\n", - " expected_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(0, 100)\n", - " self.assertEqual(expected_country_catalogue, filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_singleton_equal_low_limit_returns_correct_country_catalogue(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " expected_country_catalogue = CountryCatalogue()\n", - " expected_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(10, 100)\n", - " self.assertEqual(expected_country_catalogue, filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_singleton_equal_high_limit_returns_empty_country_catalogue(\n", - " self,\n", - " ):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(0, 10)\n", - " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_singleton_low_high_high_low_returns_empty_country_catalogue(\n", - " self,\n", - " ):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(100, 0)\n", - " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", - "\n", - " def test_most_populous_continent_singleton_returns_correct_continent(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " self.assertEqual(\"Continent\", singleton_country_catalogue.most_populous_continent())\n", - "\n", - " def test_getitem_valid_index_singleton_returns_item(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(Country(\"Name\", \"Continent\", 123456789, 987654321), singleton_country_catalogue[0])\n", - "\n", - " def test_getitem_invalid_index_singleton_raises_index_error(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " with self.assertRaises(IndexError):\n", - " singleton_country_catalogue[1]\n", - "\n", - " def test_repr_singleton_returns_correct_string(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(\n", - " \"Country(name=Name, continent=Continent, population=123456789, area=987654321)\\n\",\n", - " str(singleton_country_catalogue),\n", - " )\n", - "\n", - " def test_len_many_returns_correct_length(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " self.assertEqual(5, len(many_country_catalogue))\n", - "\n", - " def test_contains_many_country_exists_returns_true(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " self.assertTrue(many_country_catalogue.contains(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", - "\n", - " def test_contains_many_country_not_exists_returns_false(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " self.assertFalse(many_country_catalogue.contains(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789)))\n", - "\n", - " def test_remove_many_country_exists_returns_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " country = Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)\n", - " self.assertEqual(country, many_country_catalogue.remove(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", - "\n", - " def test_remove_many_country_exists_removes_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " many_country_catalogue.remove(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " self.assertFalse(many_country_catalogue.contains(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", - "\n", - " def test_remove_many_country_not_exists_raises_value_error(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " with self.assertRaises(ValueError):\n", - " many_country_catalogue.remove(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789))\n", - "\n", - " def test_country_with_largest_population_density_many_returns_correct_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " self.assertEqual(\n", - " Country(\"Name_3\", \"Continent_3\", 10, 1), many_country_catalogue.country_with_largest_population_density()\n", - " )\n", - "\n", - " def test_country_with_smallest_population_density_many_returns_correct_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " self.assertEqual(\n", - " Country(\"Name_5\", \"Continent_5\", 1, 1), many_country_catalogue.country_with_smallest_population_density()\n", - " )\n", - "\n", - " def test_filter_countries_by_population_density_many_not_within_range_returns_empty_country_catalogue(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " self.assertEqual(CountryCatalogue(), many_country_catalogue.filter_countries_by_population_density(50, 100))\n", - "\n", - " def test_filter_countries_by_population_density_many_within_range_returns_correct_country_catalog(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " expected = CountryCatalogue()\n", - " expected.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " expected.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " expected.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " expected.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 10)\n", - " self.assertEqual(expected, filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_many_equal_low_limit_returns_correct_country_catalogue(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " expected = CountryCatalogue()\n", - " expected.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(10, 100)\n", - " self.assertEqual(expected, filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_many_equal_high_limit_returns_empty_country_catalogue(\n", - " self,\n", - " ):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 1)\n", - " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_many_low_high_high_low_returns_empty_country_catalogue(\n", - " self,\n", - " ):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(100, 0)\n", - " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", - "\n", - " def test_most_populous_continent_many_returns_correct_continent(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_1\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_2\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_2\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_3\", 1, 1))\n", - " self.assertEqual(\"Continent_2\", many_country_catalogue.most_populous_continent())\n", - "\n", - " def test_getitem_valid_index_many_returns_item(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " countries = []\n", - " countries.append(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " countries.append(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " countries.append(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " countries.append(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " countries.append(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " for index, country in enumerate(countries):\n", - " with self.subTest(index=index, country=country):\n", - " self.assertEqual(country, many_country_catalogue[index])\n", - "\n", - " def test_getitem_invalid_index_many_raises_index_error(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " with self.assertRaises(IndexError):\n", - " many_country_catalogue[5]\n", - "\n", - " def test_repr_many_returns_correct_string(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " expected = (\n", - " \"Country(name=Name_1, continent=Continent_1, population=123456789, area=987654321)\\n\"\n", - " \"Country(name=Name_2, continent=Continent_2, population=123456789, area=987654321)\\n\"\n", - " \"Country(name=Name_3, continent=Continent_3, population=123456789, area=987654321)\\n\"\n", - " \"Country(name=Name_4, continent=Continent_4, population=123456789, area=987654321)\\n\"\n", - " \"Country(name=Name_5, continent=Continent_5, population=123456789, area=987654321)\\n\"\n", - " )\n", - " self.assertEqual(expected, str(many_country_catalogue))\n", - "\n", - " def test_remove_two_duplicate_country_exists_leaves_one_duplicate(self):\n", - " duplicate_country_catalogue = CountryCatalogue()\n", - " duplicate_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " self.assertTrue(duplicate_country_catalogue.contains(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321)))\n", - "\n", - " def test_remove_two_duplicate_country_exists_two_remove_calls_removes_both_duplicates(self):\n", - " duplicate_country_catalogue = CountryCatalogue()\n", - " duplicate_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " self.assertFalse(duplicate_country_catalogue.contains(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321)))\n", - "\n", - " def test_country_with_largest_population_density_duplicate_densities_returns_correct_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 10, 1))\n", - " self.assertEqual(\n", - " Country(\"Name_3\", \"Continent_3\", 10, 1), many_country_catalogue.country_with_largest_population_density()\n", - " )\n", - "\n", - " def test_country_with_smallest_population_density_duplicate_densities_returns_correct_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " self.assertEqual(\n", - " Country(\"Name_3\", \"Continent_3\", 1, 1), many_country_catalogue.country_with_smallest_population_density()\n", - " )\n", - "\n", - " def test_filter_countries_by_population_density_duplicate_within_range_returns_country_catalogue_all_duplicates(\n", - " self,\n", - " ):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " expected = CountryCatalogue()\n", - " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 100)\n", - " self.assertEqual(expected, filtered_country_catalogue)\n", - "\n", - " def test_most_populous_continent_duplicate_populations_returns_first_continent_found(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_1\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_2\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_3\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_3\", 1, 1))\n", - " self.assertEqual(\"Continent_1\", many_country_catalogue.most_populous_continent())\n", - "\n", - " def test_eq_various_equal_cases_are_equal(self):\n", - " empty_a = CountryCatalogue()\n", - " empty_b = CountryCatalogue()\n", - " single_a = CountryCatalogue()\n", - " single_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " single_b = CountryCatalogue()\n", - " single_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_a = CountryCatalogue()\n", - " many_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_a.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_a.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", - " many_a.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_a.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " many_b = CountryCatalogue()\n", - " many_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_b.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_b.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", - " many_b.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_b.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " countries_a = [empty_a, single_a, many_a]\n", - " countries_b = [empty_b, single_b, many_b]\n", - " for country_a, country_b in zip(countries_a, countries_b):\n", - " with self.subTest(country_a=country_a, country_b=country_b):\n", - " self.assertEqual(country_a, country_b)\n", - "\n", - " def test_eq_various_not_equal_cases_are_not_equal(self):\n", - " empty_a = CountryCatalogue()\n", - " single_a = CountryCatalogue()\n", - " single_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " single_b = CountryCatalogue()\n", - " single_b.add(Country(\"Name_100\", \"Continent_100\", 5, 1))\n", - " different_order_a = CountryCatalogue()\n", - " different_order_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " different_order_a.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " different_order_a.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", - " different_order_a.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " different_order_a.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " different_order_b = CountryCatalogue()\n", - " different_order_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " different_order_b.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " different_order_b.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", - " different_order_b.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " different_order_b.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " countries_a = [empty_a, single_a, different_order_a, empty_a]\n", - " countries_b = [single_b, single_b, different_order_b, []]\n", - " for country_a, country_b in zip(countries_a, countries_b):\n", - " with self.subTest(country_a=country_a, country_b=country_b):\n", - " self.assertNotEqual(country_a, country_b)\n" - ], - "metadata": { - "id": "fVVS4HJYBCFa" - }, - "execution_count": 4, - "outputs": [] + "output_type": "stream", + "name": "stderr", + "text": [ + "test_contains_empty_collection_returns_false (__main__.CountryCatalogueTest) ... ok\n", + "test_contains_many_country_exists_returns_true (__main__.CountryCatalogueTest) ... ok\n", + "test_contains_many_country_not_exists_returns_false (__main__.CountryCatalogueTest) ... ok\n", + "test_contains_singleton_country_exists_returns_true (__main__.CountryCatalogueTest) ... ok\n", + "test_contains_singleton_country_not_exists_returns_false (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_largest_population_density_duplicate_densities_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_largest_population_density_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_largest_population_density_many_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_largest_population_density_singleton_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_smallest_population_density_duplicate_densities_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_smallest_population_density_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_smallest_population_density_many_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_smallest_population_density_singleton_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_eq_various_equal_cases_are_equal (__main__.CountryCatalogueTest) ... ok\n", + "test_eq_various_not_equal_cases_are_not_equal (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_duplicate_within_range_returns_country_catalogue_all_duplicates (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_empty_collection_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_many_equal_high_limit_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_many_equal_low_limit_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_many_low_high_high_low_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_many_not_within_range_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_many_within_range_returns_correct_country_catalog (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_singleton_equal_high_limit_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_singleton_equal_low_limit_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_singleton_low_high_high_low_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_singleton_not_within_range_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_singleton_within_range_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_getitem_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_getitem_invalid_index_many_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_getitem_invalid_index_singleton_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_getitem_valid_index_many_returns_item (__main__.CountryCatalogueTest) ... ok\n", + "test_getitem_valid_index_singleton_returns_item (__main__.CountryCatalogueTest) ... ok\n", + "test_len_empty_collection_returns_0 (__main__.CountryCatalogueTest) ... ok\n", + "test_len_many_returns_correct_length (__main__.CountryCatalogueTest) ... ok\n", + "test_len_singleton_returns_1 (__main__.CountryCatalogueTest) ... ok\n", + "test_most_populous_continent_duplicate_populations_returns_first_continent_found (__main__.CountryCatalogueTest) ... ok\n", + "test_most_populous_continent_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_most_populous_continent_many_returns_correct_continent (__main__.CountryCatalogueTest) ... ok\n", + "test_most_populous_continent_singleton_returns_correct_continent (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_empty_collection_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_many_country_exists_removes_country (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_many_country_exists_returns_country (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_many_country_not_exists_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_singleton_country_exists_removes_country (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_singleton_country_exists_returns_country (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_singleton_country_not_exists_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_two_duplicate_country_exists_leaves_one_duplicate (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_two_duplicate_country_exists_two_remove_calls_removes_both_duplicates (__main__.CountryCatalogueTest) ... ok\n", + "test_repr_empty_collection_returns_empty_string (__main__.CountryCatalogueTest) ... ok\n", + "test_repr_many_returns_correct_string (__main__.CountryCatalogueTest) ... ok\n", + "test_repr_singleton_returns_correct_string (__main__.CountryCatalogueTest) ... ok\n", + "test_country_area_returns_correct_country_area (__main__.CountryTest) ... ok\n", + "test_country_continent_returns_correct_country_continent (__main__.CountryTest) ... ok\n", + "test_country_name_returns_correct_country_name (__main__.CountryTest) ... ok\n", + "test_country_population_returns_correct_country_population (__main__.CountryTest) ... ok\n", + "test_equals_equal_country_objects_returns_true (__main__.CountryTest) ... ok\n", + "test_equals_unequal_country_objects_returns_false (__main__.CountryTest) ... ok\n", + "test_population_density_returns_correct_population_density (__main__.CountryTest) ... ok\n", + "test_population_density_with_zero_area_raises_zero_division_error (__main__.CountryTest) ... ok\n", + "test_repr_arbitrary_country_returns_correct_string (__main__.CountryTest) ... ok\n", + "\n", + "----------------------------------------------------------------------\n", + "Ran 60 tests in 0.133s\n", + "\n", + "OK\n" + ] }, { - "cell_type": "code", - "source": [ - "# Run this cell to run all unit tests\n", - "unittest.main(argv=[''], verbosity=2, exit=False)" - ], - "metadata": { - "id": "gz5JlQfFBzKT", - "colab": { - "base_uri": "https://localhost:8080/" - }, - "outputId": "9e13d5e4-6b0c-46cc-fd6c-a86ee678cf65" - }, - "execution_count": 5, - "outputs": [ - { - "output_type": "stream", - "name": "stderr", - "text": [ - "test_contains_empty_collection_returns_false (__main__.CountryCatalogueTest) ... ok\n", - "test_contains_many_country_exists_returns_true (__main__.CountryCatalogueTest) ... ok\n", - "test_contains_many_country_not_exists_returns_false (__main__.CountryCatalogueTest) ... ok\n", - "test_contains_singleton_country_exists_returns_true (__main__.CountryCatalogueTest) ... ok\n", - "test_contains_singleton_country_not_exists_returns_false (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_largest_population_density_duplicate_densities_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_largest_population_density_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_largest_population_density_many_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_largest_population_density_singleton_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_smallest_population_density_duplicate_densities_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_smallest_population_density_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_smallest_population_density_many_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_smallest_population_density_singleton_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_eq_various_equal_cases_are_equal (__main__.CountryCatalogueTest) ... ok\n", - "test_eq_various_not_equal_cases_are_not_equal (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_duplicate_within_range_returns_country_catalogue_all_duplicates (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_empty_collection_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_many_equal_high_limit_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_many_equal_low_limit_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_many_low_high_high_low_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_many_not_within_range_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_many_within_range_returns_correct_country_catalog (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_singleton_equal_high_limit_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_singleton_equal_low_limit_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_singleton_low_high_high_low_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_singleton_not_within_range_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_singleton_within_range_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_getitem_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_getitem_invalid_index_many_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_getitem_invalid_index_singleton_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_getitem_valid_index_many_returns_item (__main__.CountryCatalogueTest) ... ok\n", - "test_getitem_valid_index_singleton_returns_item (__main__.CountryCatalogueTest) ... ok\n", - "test_len_empty_collection_returns_0 (__main__.CountryCatalogueTest) ... ok\n", - "test_len_many_returns_correct_length (__main__.CountryCatalogueTest) ... ok\n", - "test_len_singleton_returns_1 (__main__.CountryCatalogueTest) ... ok\n", - "test_most_populous_continent_duplicate_populations_returns_first_continent_found (__main__.CountryCatalogueTest) ... ok\n", - "test_most_populous_continent_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_most_populous_continent_many_returns_correct_continent (__main__.CountryCatalogueTest) ... ok\n", - "test_most_populous_continent_singleton_returns_correct_continent (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_empty_collection_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_many_country_exists_removes_country (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_many_country_exists_returns_country (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_many_country_not_exists_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_singleton_country_exists_removes_country (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_singleton_country_exists_returns_country (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_singleton_country_not_exists_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_two_duplicate_country_exists_leaves_one_duplicate (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_two_duplicate_country_exists_two_remove_calls_removes_both_duplicates (__main__.CountryCatalogueTest) ... ok\n", - "test_repr_empty_collection_returns_empty_string (__main__.CountryCatalogueTest) ... ok\n", - "test_repr_many_returns_correct_string (__main__.CountryCatalogueTest) ... ok\n", - "test_repr_singleton_returns_correct_string (__main__.CountryCatalogueTest) ... ok\n", - "test_country_area_returns_correct_country_area (__main__.CountryTest) ... ok\n", - "test_country_continent_returns_correct_country_continent (__main__.CountryTest) ... ok\n", - "test_country_name_returns_correct_country_name (__main__.CountryTest) ... ok\n", - "test_country_population_returns_correct_country_population (__main__.CountryTest) ... ok\n", - "test_equals_equal_country_objects_returns_true (__main__.CountryTest) ... ok\n", - "test_equals_unequal_country_objects_returns_false (__main__.CountryTest) ... ok\n", - "test_population_density_returns_correct_population_density (__main__.CountryTest) ... ok\n", - "test_population_density_with_zero_area_raises_zero_division_error (__main__.CountryTest) ... ok\n", - "test_repr_arbitrary_country_returns_correct_string (__main__.CountryTest) ... ok\n", - "\n", - "----------------------------------------------------------------------\n", - "Ran 60 tests in 0.133s\n", - "\n", - "OK\n" - ] - }, - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "" - ] - }, - "metadata": {}, - "execution_count": 5 - } + "output_type": "execute_result", + "data": { + "text/plain": [ + "" ] - }, - { - "cell_type": "code", - "source": [ - "# NAME:\n", - "# ST-NUMBER:\n", - "# StFX EMAIL:\n", - "\n", - "COUNTRY_DATA_FILE_NAME = \"country_data.csv\"\n", - "\n", - "# Makes it so the import from the unit tests do not break things\n", - "if __name__ == \"__main__\":\n", - " # Load data\n", - " catalogue = CountryCatalogue()\n", - " in_file = open(COUNTRY_DATA_FILE_NAME, \"r\")\n", - " for line in in_file:\n", - " split_line = line.split(\",\")\n", - " country = Country(\n", - " name=split_line[0], continent=split_line[1], population=int(split_line[2]), area=float(split_line[3])\n", - " )\n", - " catalogue.add(country)\n", - " in_file.close()\n", - "\n", - " # Alter catalogue contents\n", - " england = Country(\"England\", \"Europe\", 56489800, 130279)\n", - " ecuador = Country(\"Ecuador\", \"South America\", 18048628, 256370)\n", - " india = Country(\"India\", \"Asia\", 1375586000, 3287263)\n", - " catalogue.add(england)\n", - " catalogue.add(ecuador)\n", - " catalogue.add(india)\n", - " catalogue.remove(Country(\"Canada\", \"North America\", 34207000, 9976140.0))\n", - " catalogue.remove(Country(\"South Korea\", \"Asia\", 50503933, 98076.92))\n", - "\n", - " # Answering questions\n", - " print(catalogue.country_with_smallest_population_density())\n", - " print(catalogue.country_with_largest_population_density())\n", - " print(catalogue.most_populous_continent())\n", - "\n", - " # Save filtered data\n", - " filtered_catalogue = catalogue.filter_countries_by_population_density(200, 450)\n", - " out_file = open(\"density-200_450.csv\", \"w\")\n", - " out_file.write(str(filtered_catalogue))\n", - " out_file.close()\n" - ], - "metadata": { - "id": "O5284wegBGe6" - }, - "execution_count": null, - "outputs": [] + }, + "metadata": {}, + "execution_count": 5 } - ] -} \ No newline at end of file + ] + }, + { + "cell_type": "code", + "source": [ + "# NAME:\n", + "# ST-NUMBER:\n", + "# StFX EMAIL:\n", + "\n", + "COUNTRY_DATA_FILE_NAME = \"country_data.csv\"\n", + "\n", + "# Makes it so the import from the unit tests do not break things\n", + "if __name__ == \"__main__\":\n", + " # Load data\n", + " catalogue = CountryCatalogue()\n", + " in_file = open(COUNTRY_DATA_FILE_NAME, \"r\")\n", + " for line in in_file:\n", + " split_line = line.split(\",\")\n", + " country = Country(\n", + " name=split_line[0], continent=split_line[1], population=int(split_line[2]), area=float(split_line[3])\n", + " )\n", + " catalogue.add(country)\n", + " in_file.close()\n", + "\n", + " # Alter catalogue contents\n", + " england = Country(\"England\", \"Europe\", 56489800, 130279)\n", + " ecuador = Country(\"Ecuador\", \"South America\", 18048628, 256370)\n", + " india = Country(\"India\", \"Asia\", 1375586000, 3287263)\n", + " catalogue.add(england)\n", + " catalogue.add(ecuador)\n", + " catalogue.add(india)\n", + " catalogue.remove(Country(\"Canada\", \"North America\", 34207000, 9976140.0))\n", + " catalogue.remove(Country(\"South Korea\", \"Asia\", 50503933, 98076.92))\n", + "\n", + " # Answering questions\n", + " print(catalogue.country_with_smallest_population_density())\n", + " print(catalogue.country_with_largest_population_density())\n", + " print(catalogue.most_populous_continent())\n", + "\n", + " # Save filtered data\n", + " filtered_catalogue = catalogue.filter_countries_by_population_density(200, 450)\n", + " out_file = open(\"density-200_450.csv\", \"w\")\n", + " out_file.write(str(filtered_catalogue))\n", + " out_file.close()\n" + ], + "metadata": { + "id": "O5284wegBGe6" + }, + "execution_count": null, + "outputs": [] + } + ] +} diff --git a/site/assignments/country-catalogue/asn4.py b/site/assignments/country-catalogue/asn4.py index 2dcd251c..bbea5b01 100644 --- a/site/assignments/country-catalogue/asn4.py +++ b/site/assignments/country-catalogue/asn4.py @@ -127,8 +127,6 @@ def add(self, country: Country) -> None: :param country: Country object to add to the collection. :type country: Country object. - :param area: Area of the country. - :type area: float. """ From 59142804ccf5116753ae3a679e41ad209fdb7c16 Mon Sep 17 00:00:00 2001 From: James Alexander Hughes Date: Thu, 30 Nov 2023 16:24:47 -0400 Subject: [PATCH 2/2] put colab downloaded one back --- site/assignments/country-catalogue/asn4.ipynb | 1726 ++++++++--------- 1 file changed, 863 insertions(+), 863 deletions(-) diff --git a/site/assignments/country-catalogue/asn4.ipynb b/site/assignments/country-catalogue/asn4.ipynb index fa855ce8..49c492d6 100644 --- a/site/assignments/country-catalogue/asn4.ipynb +++ b/site/assignments/country-catalogue/asn4.ipynb @@ -1,871 +1,871 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "id": "fzMQClXIAq7r" - }, - "outputs": [], - "source": [ - "class Country:\n", - " \"\"\"\n", - " Class for keeping track of country details for the purpose of the Country Catalogue's functionality. Country objects\n", - " know their name, which continent they are on, their population, and their surface area.\n", - " \"\"\"\n", - "\n", - " def __init__(self, name: str, continent: str, population: int, area: float):\n", - "\n", - "\n", - " def population_density(self) -> float:\n", - " \"\"\"\n", - " Calculate and return the population density of the Country. Population density is calculated with\n", - " population/area.\n", - "\n", - " :return: The population density of the Country.\n", - " :rtype: float.\n", - " \"\"\"\n", - "\n", - "\n", - " def __eq__(self, other: \"Country\") -> bool:\n", - " \"\"\"\n", - " Check if two instances of a Country object are equal. Country objects are considered equal if all attributes are\n", - " equal.\n", - "\n", - " :param other: A Country object to compare the self Country to.\n", - " :type other: Country.\n", - " :return: True if the Country objects have all attributes equal.\n", - " :rtype: boolean.\n", - " \"\"\"\n", - "\n", - "\n", - " def __repr__(self) -> str:\n", - " \"\"\"\n", - " Returns a string representation of the Country object. The string is \"name, continent, population, area\".\n", - "\n", - " :return: String representation of the Country object of the form \"name, continent, population, area\".\n", - " :rtype: String.\n", - " \"\"\"\n" - ] - }, - { - "cell_type": "code", - "source": [ - "import unittest\n", - "\n", - "class CountryTest(unittest.TestCase):\n", - " def test_country_name_returns_correct_country_name(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(\"Name\", country.name)\n", - "\n", - " def test_country_continent_returns_correct_country_continent(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(\"Continent\", country.continent)\n", - "\n", - " def test_country_population_returns_correct_country_population(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(123456789, country.population)\n", - "\n", - " def test_country_area_returns_correct_country_area(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(987654321, country.area)\n", - "\n", - " def test_population_density_returns_correct_population_density(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertAlmostEqual(123456789 / 987654321, country.population_density())\n", - "\n", - " def test_population_density_with_zero_area_raises_zero_division_error(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 0)\n", - " with self.assertRaises(ZeroDivisionError):\n", - " country.population_density()\n", - "\n", - " def test_equals_equal_country_objects_returns_true(self):\n", - " country_a = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " country_b = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(country_a, country_b)\n", - "\n", - " def test_equals_unequal_country_objects_returns_false(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " cases = [\n", - " Country(\"Eman\", \"Continent\", 123456789, 987654321),\n", - " Country(\"Name\", \"Tnenitnoc\", 123456789, 987654321),\n", - " Country(\"Name\", \"Continent\", 987654321, 987654321),\n", - " Country(\"Name\", \"Continent\", 123456789, 123456789),\n", - " Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789),\n", - " 54321,\n", - " ]\n", - " for case in cases:\n", - " with self.subTest(case=case):\n", - " self.assertNotEqual(country, case)\n", - "\n", - " def test_repr_arbitrary_country_returns_correct_string(self):\n", - " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", - " self.assertEqual(\"Country(name=Name, continent=Continent, population=123456789, area=987654321)\", str(country))\n" - ], - "metadata": { - "id": "fIyBwt0uA6kO" - }, - "execution_count": 2, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "class CountryCatalogue:\n", - " \"\"\"\n", - " Class for managing a collection of Country Objects. The class maintains a list of the Country objects and provides\n", - " a mechanism for adding, removing, finding, checking if exists, and other collection-ie type methods.\n", - " \"\"\"\n", - "\n", - " def __init__(self):\n", - "\n", - "\n", - " def _find(self, country: Country) -> int:\n", - " \"\"\"\n", - " Find the index of the specified Country within the Country Catalogue. If the Country does not exist, return the\n", - " sentinel value of -1.\n", - "\n", - " :param country: Country object to search for.\n", - " :type country: Country object.\n", - " :return: Index of the Country with the matching values.\n", - " :rtype: integer.\n", - " \"\"\"\n", - "\n", - "\n", - " def contains(self, country: Country) -> bool:\n", - " \"\"\"\n", - " Check if a Country with the provided values exists within the collection. Returns True if it does and False\n", - " otherwise.\n", - "\n", - " :param country: Country object to search for.\n", - " :type country: Country object.\n", - " :return: True if the Country object is contained within the CountryCatalogue, False otherwise.\n", - " :rtype: bool.\n", - " \"\"\"\n", - "\n", - "\n", - " def add(self, country: Country) -> None:\n", - " \"\"\"\n", - " Add the provided Country object to the Country Catalogue.\n", - "\n", - " :param country: Country object to add to the collection.\n", - " :type country: Country object.\n", - " \"\"\"\n", - "\n", - "\n", - " def remove(self, country: Country) -> Country:\n", - " \"\"\"\n", - " Remove a Country object from the Country Catalogue matching the provided value. The removed Country object is\n", - " returned by the method. If no such Country object exists within the collection, a ValueError is raised.\n", - "\n", - " :raise ValueError: If the Country object does not exist within the collection, a ValueError is raised.\n", - " :param country: Country object to remove from the collection.\n", - " :type country: Country object.\n", - " \"\"\"\n", - "\n", - "\n", - " def country_with_largest_population_density(self) -> Country:\n", - " \"\"\"\n", - " Find and return the Country object with the largest population density. A reference to the Country object with\n", - " the largest density is returned. If the collection is empty, a IndexError is raised.\n", - "\n", - " :raise IndexError: If the collection is empty, raise an IndexError.\n", - " :return: Country object with the largest population density.\n", - " :rtype: Country.\n", - " \"\"\"\n", - "\n", - "\n", - " def country_with_smallest_population_density(self) -> Country:\n", - " \"\"\"\n", - " Find and return the Country object with the smallest population density. A reference to the Country object with\n", - " the smallest density is returned. If the collection is empty, a IndexError is raised.\n", - "\n", - " :raise IndexError: If the collection is empty, raise an IndexError.\n", - " :return: Country object with the smallest population density.\n", - " :rtype: Country.\n", - " \"\"\"\n", - "\n", - "\n", - " def filter_countries_by_population_density(self, low_limit: float, high_limit: float) -> \"CountryCatalogue\":\n", - " \"\"\"\n", - " Return a new CountryCatalogue containing only Country objects from the current CountryCatalogue with a\n", - " population density between the specified low & high range. If no Country objects between the specified range\n", - " exist, an empty CountryCatalogue is returned.\n", - "\n", - " :param low_limit: The low population density; only Country objects with a population density greater than or\n", - " equal to the low_limit will be included in the filtered CountryCatalogue.\n", - " :type low_limit: float\n", - " :param high_limit: The high population density; only Country objects with a population density strictly less\n", - " than the high_limit will be included in the filtered CountryCatalogue.\n", - " :type high_limit: float\n", - " :return: A new CountryCatalogue object with copies of Country objects that fall within the specified range.\n", - " :rtype: CountryCatalogue\n", - " \"\"\"\n", - "\n", - "\n", - " def most_populous_continent(self) -> str:\n", - " \"\"\"\n", - " Find and return the name of the continent with the largest population based on the Country objects within the\n", - " CountryCatalogue. If two or more continents have the same largest population, return the name of the country\n", - " found first. If the collection is empty, a IndexError is raised.\n", - "\n", - " :raise IndexError: If the collection is empty, raise an IndexError.\n", - " :return: Name of the continent with the largest population.\n", - " :rtype: str.\n", - " \"\"\"\n", - "\n", - "\n", - " def __getitem__(self, item: int) -> Country:\n", - "\n", - "\n", - " def __len__(self) -> int:\n", - "\n", - "\n", - " def __eq__(self, other: \"CountryCatalogue\") -> bool:\n", - "\n", - "\n", - " def __repr__(self) -> str:\n" - ], - "metadata": { - "id": "SGrsdqthA-Dc" - }, - "execution_count": 3, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "import unittest\n", - "\n", - "class CountryCatalogueTest(unittest.TestCase):\n", - " def test_len_empty_collection_returns_0(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " self.assertEqual(0, len(empty_country_catalogue))\n", - "\n", - " def test_contains_empty_collection_returns_false(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " self.assertFalse(empty_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", - "\n", - " def test_remove_empty_collection_raises_value_error(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " with self.assertRaises(ValueError):\n", - " empty_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - "\n", - " def test_country_with_largest_population_density_empty_collection_raises_index_error(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " with self.assertRaises(IndexError):\n", - " empty_country_catalogue.country_with_largest_population_density()\n", - "\n", - " def test_country_with_smallest_population_density_empty_collection_raises_index_error(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " with self.assertRaises(IndexError):\n", - " empty_country_catalogue.country_with_smallest_population_density()\n", - "\n", - " def test_filter_countries_by_population_density_empty_collection_returns_empty_country_catalogue(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " self.assertEqual(CountryCatalogue(), empty_country_catalogue.filter_countries_by_population_density(0, 10000))\n", - "\n", - " def test_most_populous_continent_empty_collection_raises_index_error(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " with self.assertRaises(IndexError):\n", - " empty_country_catalogue.most_populous_continent()\n", - "\n", - " def test_getitem_empty_collection_raises_index_error(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " indicies = [-1, 0, 1]\n", - " for index in indicies:\n", - " with self.subTest(index=index):\n", - " with self.assertRaises(IndexError):\n", - " empty_country_catalogue[index]\n", - "\n", - " def test_repr_empty_collection_returns_empty_string(self):\n", - " empty_country_catalogue = CountryCatalogue()\n", - " self.assertEqual(\"\", str(empty_country_catalogue))\n", - "\n", - " def test_len_singleton_returns_1(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(1, len(singleton_country_catalogue))\n", - "\n", - " def test_contains_singleton_country_exists_returns_true(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertTrue(singleton_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", - "\n", - " def test_contains_singleton_country_not_exists_returns_false(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertFalse(singleton_country_catalogue.contains(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789)))\n", - "\n", - " def test_remove_singleton_country_exists_returns_country(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(\n", - " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", - " singleton_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321)),\n", - " )\n", - "\n", - " def test_remove_singleton_country_exists_removes_country(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " singleton_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertFalse(singleton_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", - "\n", - " def test_remove_singleton_country_not_exists_raises_value_error(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " with self.assertRaises(ValueError):\n", - " singleton_country_catalogue.remove(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789))\n", - "\n", - " def test_country_with_largest_population_density_singleton_returns_correct_country(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(\n", - " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", - " singleton_country_catalogue.country_with_largest_population_density(),\n", - " )\n", - "\n", - " def test_country_with_smallest_population_density_singleton_returns_correct_country(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(\n", - " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", - " singleton_country_catalogue.country_with_smallest_population_density(),\n", - " )\n", - "\n", - " def test_filter_countries_by_population_density_singleton_not_within_range_returns_empty_country_catalogue(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " self.assertEqual(CountryCatalogue(), singleton_country_catalogue.filter_countries_by_population_density(0, 5))\n", - "\n", - " def test_filter_countries_by_population_density_singleton_within_range_returns_correct_country_catalogue(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " expected_country_catalogue = CountryCatalogue()\n", - " expected_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(0, 100)\n", - " self.assertEqual(expected_country_catalogue, filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_singleton_equal_low_limit_returns_correct_country_catalogue(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " expected_country_catalogue = CountryCatalogue()\n", - " expected_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(10, 100)\n", - " self.assertEqual(expected_country_catalogue, filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_singleton_equal_high_limit_returns_empty_country_catalogue(\n", - " self,\n", - " ):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(0, 10)\n", - " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_singleton_low_high_high_low_returns_empty_country_catalogue(\n", - " self,\n", - " ):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(100, 0)\n", - " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", - "\n", - " def test_most_populous_continent_singleton_returns_correct_continent(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " self.assertEqual(\"Continent\", singleton_country_catalogue.most_populous_continent())\n", - "\n", - " def test_getitem_valid_index_singleton_returns_item(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(Country(\"Name\", \"Continent\", 123456789, 987654321), singleton_country_catalogue[0])\n", - "\n", - " def test_getitem_invalid_index_singleton_raises_index_error(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " with self.assertRaises(IndexError):\n", - " singleton_country_catalogue[1]\n", - "\n", - " def test_repr_singleton_returns_correct_string(self):\n", - " singleton_country_catalogue = CountryCatalogue()\n", - " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", - " self.assertEqual(\n", - " \"Country(name=Name, continent=Continent, population=123456789, area=987654321)\\n\",\n", - " str(singleton_country_catalogue),\n", - " )\n", - "\n", - " def test_len_many_returns_correct_length(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " self.assertEqual(5, len(many_country_catalogue))\n", - "\n", - " def test_contains_many_country_exists_returns_true(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " self.assertTrue(many_country_catalogue.contains(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", - "\n", - " def test_contains_many_country_not_exists_returns_false(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " self.assertFalse(many_country_catalogue.contains(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789)))\n", - "\n", - " def test_remove_many_country_exists_returns_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " country = Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)\n", - " self.assertEqual(country, many_country_catalogue.remove(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", - "\n", - " def test_remove_many_country_exists_removes_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " many_country_catalogue.remove(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " self.assertFalse(many_country_catalogue.contains(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", - "\n", - " def test_remove_many_country_not_exists_raises_value_error(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " with self.assertRaises(ValueError):\n", - " many_country_catalogue.remove(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789))\n", - "\n", - " def test_country_with_largest_population_density_many_returns_correct_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " self.assertEqual(\n", - " Country(\"Name_3\", \"Continent_3\", 10, 1), many_country_catalogue.country_with_largest_population_density()\n", - " )\n", - "\n", - " def test_country_with_smallest_population_density_many_returns_correct_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " self.assertEqual(\n", - " Country(\"Name_5\", \"Continent_5\", 1, 1), many_country_catalogue.country_with_smallest_population_density()\n", - " )\n", - "\n", - " def test_filter_countries_by_population_density_many_not_within_range_returns_empty_country_catalogue(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " self.assertEqual(CountryCatalogue(), many_country_catalogue.filter_countries_by_population_density(50, 100))\n", - "\n", - " def test_filter_countries_by_population_density_many_within_range_returns_correct_country_catalog(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " expected = CountryCatalogue()\n", - " expected.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " expected.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " expected.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " expected.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 10)\n", - " self.assertEqual(expected, filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_many_equal_low_limit_returns_correct_country_catalogue(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " expected = CountryCatalogue()\n", - " expected.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(10, 100)\n", - " self.assertEqual(expected, filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_many_equal_high_limit_returns_empty_country_catalogue(\n", - " self,\n", - " ):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 1)\n", - " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", - "\n", - " def test_filter_countries_by_population_density_many_low_high_high_low_returns_empty_country_catalogue(\n", - " self,\n", - " ):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(100, 0)\n", - " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", - "\n", - " def test_most_populous_continent_many_returns_correct_continent(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_1\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_2\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_2\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_3\", 1, 1))\n", - " self.assertEqual(\"Continent_2\", many_country_catalogue.most_populous_continent())\n", - "\n", - " def test_getitem_valid_index_many_returns_item(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " countries = []\n", - " countries.append(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " countries.append(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " countries.append(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " countries.append(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " countries.append(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " for index, country in enumerate(countries):\n", - " with self.subTest(index=index, country=country):\n", - " self.assertEqual(country, many_country_catalogue[index])\n", - "\n", - " def test_getitem_invalid_index_many_raises_index_error(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " with self.assertRaises(IndexError):\n", - " many_country_catalogue[5]\n", - "\n", - " def test_repr_many_returns_correct_string(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " expected = (\n", - " \"Country(name=Name_1, continent=Continent_1, population=123456789, area=987654321)\\n\"\n", - " \"Country(name=Name_2, continent=Continent_2, population=123456789, area=987654321)\\n\"\n", - " \"Country(name=Name_3, continent=Continent_3, population=123456789, area=987654321)\\n\"\n", - " \"Country(name=Name_4, continent=Continent_4, population=123456789, area=987654321)\\n\"\n", - " \"Country(name=Name_5, continent=Continent_5, population=123456789, area=987654321)\\n\"\n", - " )\n", - " self.assertEqual(expected, str(many_country_catalogue))\n", - "\n", - " def test_remove_two_duplicate_country_exists_leaves_one_duplicate(self):\n", - " duplicate_country_catalogue = CountryCatalogue()\n", - " duplicate_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " self.assertTrue(duplicate_country_catalogue.contains(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321)))\n", - "\n", - " def test_remove_two_duplicate_country_exists_two_remove_calls_removes_both_duplicates(self):\n", - " duplicate_country_catalogue = CountryCatalogue()\n", - " duplicate_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", - " duplicate_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", - " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", - " self.assertFalse(duplicate_country_catalogue.contains(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321)))\n", - "\n", - " def test_country_with_largest_population_density_duplicate_densities_returns_correct_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 10, 1))\n", - " self.assertEqual(\n", - " Country(\"Name_3\", \"Continent_3\", 10, 1), many_country_catalogue.country_with_largest_population_density()\n", - " )\n", - "\n", - " def test_country_with_smallest_population_density_duplicate_densities_returns_correct_country(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " self.assertEqual(\n", - " Country(\"Name_3\", \"Continent_3\", 1, 1), many_country_catalogue.country_with_smallest_population_density()\n", - " )\n", - "\n", - " def test_filter_countries_by_population_density_duplicate_within_range_returns_country_catalogue_all_duplicates(\n", - " self,\n", - " ):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " expected = CountryCatalogue()\n", - " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", - " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 100)\n", - " self.assertEqual(expected, filtered_country_catalogue)\n", - "\n", - " def test_most_populous_continent_duplicate_populations_returns_first_continent_found(self):\n", - " many_country_catalogue = CountryCatalogue()\n", - " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_2\", \"Continent_1\", 2, 1))\n", - " many_country_catalogue.add(Country(\"Name_3\", \"Continent_2\", 5, 1))\n", - " many_country_catalogue.add(Country(\"Name_4\", \"Continent_3\", 6, 1))\n", - " many_country_catalogue.add(Country(\"Name_5\", \"Continent_3\", 1, 1))\n", - " self.assertEqual(\"Continent_1\", many_country_catalogue.most_populous_continent())\n", - "\n", - " def test_eq_various_equal_cases_are_equal(self):\n", - " empty_a = CountryCatalogue()\n", - " empty_b = CountryCatalogue()\n", - " single_a = CountryCatalogue()\n", - " single_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " single_b = CountryCatalogue()\n", - " single_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_a = CountryCatalogue()\n", - " many_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_a.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_a.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", - " many_a.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_a.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " many_b = CountryCatalogue()\n", - " many_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " many_b.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " many_b.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", - " many_b.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " many_b.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " countries_a = [empty_a, single_a, many_a]\n", - " countries_b = [empty_b, single_b, many_b]\n", - " for country_a, country_b in zip(countries_a, countries_b):\n", - " with self.subTest(country_a=country_a, country_b=country_b):\n", - " self.assertEqual(country_a, country_b)\n", - "\n", - " def test_eq_various_not_equal_cases_are_not_equal(self):\n", - " empty_a = CountryCatalogue()\n", - " single_a = CountryCatalogue()\n", - " single_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " single_b = CountryCatalogue()\n", - " single_b.add(Country(\"Name_100\", \"Continent_100\", 5, 1))\n", - " different_order_a = CountryCatalogue()\n", - " different_order_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " different_order_a.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " different_order_a.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", - " different_order_a.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " different_order_a.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " different_order_b = CountryCatalogue()\n", - " different_order_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", - " different_order_b.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", - " different_order_b.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", - " different_order_b.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", - " different_order_b.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", - " countries_a = [empty_a, single_a, different_order_a, empty_a]\n", - " countries_b = [single_b, single_b, different_order_b, []]\n", - " for country_a, country_b in zip(countries_a, countries_b):\n", - " with self.subTest(country_a=country_a, country_b=country_b):\n", - " self.assertNotEqual(country_a, country_b)\n" - ], - "metadata": { - "id": "fVVS4HJYBCFa" - }, - "execution_count": 4, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "# Run this cell to run all unit tests\n", - "unittest.main(argv=[''], verbosity=2, exit=False)" - ], - "metadata": { - "id": "gz5JlQfFBzKT", + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { "colab": { - "base_uri": "https://localhost:8080/" + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" }, - "outputId": "9e13d5e4-6b0c-46cc-fd6c-a86ee678cf65" - }, - "execution_count": 5, - "outputs": [ + "language_info": { + "name": "python" + } + }, + "cells": [ { - "output_type": "stream", - "name": "stderr", - "text": [ - "test_contains_empty_collection_returns_false (__main__.CountryCatalogueTest) ... ok\n", - "test_contains_many_country_exists_returns_true (__main__.CountryCatalogueTest) ... ok\n", - "test_contains_many_country_not_exists_returns_false (__main__.CountryCatalogueTest) ... ok\n", - "test_contains_singleton_country_exists_returns_true (__main__.CountryCatalogueTest) ... ok\n", - "test_contains_singleton_country_not_exists_returns_false (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_largest_population_density_duplicate_densities_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_largest_population_density_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_largest_population_density_many_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_largest_population_density_singleton_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_smallest_population_density_duplicate_densities_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_smallest_population_density_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_smallest_population_density_many_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_country_with_smallest_population_density_singleton_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", - "test_eq_various_equal_cases_are_equal (__main__.CountryCatalogueTest) ... ok\n", - "test_eq_various_not_equal_cases_are_not_equal (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_duplicate_within_range_returns_country_catalogue_all_duplicates (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_empty_collection_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_many_equal_high_limit_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_many_equal_low_limit_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_many_low_high_high_low_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_many_not_within_range_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_many_within_range_returns_correct_country_catalog (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_singleton_equal_high_limit_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_singleton_equal_low_limit_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_singleton_low_high_high_low_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_singleton_not_within_range_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_filter_countries_by_population_density_singleton_within_range_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", - "test_getitem_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_getitem_invalid_index_many_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_getitem_invalid_index_singleton_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_getitem_valid_index_many_returns_item (__main__.CountryCatalogueTest) ... ok\n", - "test_getitem_valid_index_singleton_returns_item (__main__.CountryCatalogueTest) ... ok\n", - "test_len_empty_collection_returns_0 (__main__.CountryCatalogueTest) ... ok\n", - "test_len_many_returns_correct_length (__main__.CountryCatalogueTest) ... ok\n", - "test_len_singleton_returns_1 (__main__.CountryCatalogueTest) ... ok\n", - "test_most_populous_continent_duplicate_populations_returns_first_continent_found (__main__.CountryCatalogueTest) ... ok\n", - "test_most_populous_continent_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", - "test_most_populous_continent_many_returns_correct_continent (__main__.CountryCatalogueTest) ... ok\n", - "test_most_populous_continent_singleton_returns_correct_continent (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_empty_collection_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_many_country_exists_removes_country (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_many_country_exists_returns_country (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_many_country_not_exists_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_singleton_country_exists_removes_country (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_singleton_country_exists_returns_country (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_singleton_country_not_exists_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_two_duplicate_country_exists_leaves_one_duplicate (__main__.CountryCatalogueTest) ... ok\n", - "test_remove_two_duplicate_country_exists_two_remove_calls_removes_both_duplicates (__main__.CountryCatalogueTest) ... ok\n", - "test_repr_empty_collection_returns_empty_string (__main__.CountryCatalogueTest) ... ok\n", - "test_repr_many_returns_correct_string (__main__.CountryCatalogueTest) ... ok\n", - "test_repr_singleton_returns_correct_string (__main__.CountryCatalogueTest) ... ok\n", - "test_country_area_returns_correct_country_area (__main__.CountryTest) ... ok\n", - "test_country_continent_returns_correct_country_continent (__main__.CountryTest) ... ok\n", - "test_country_name_returns_correct_country_name (__main__.CountryTest) ... ok\n", - "test_country_population_returns_correct_country_population (__main__.CountryTest) ... ok\n", - "test_equals_equal_country_objects_returns_true (__main__.CountryTest) ... ok\n", - "test_equals_unequal_country_objects_returns_false (__main__.CountryTest) ... ok\n", - "test_population_density_returns_correct_population_density (__main__.CountryTest) ... ok\n", - "test_population_density_with_zero_area_raises_zero_division_error (__main__.CountryTest) ... ok\n", - "test_repr_arbitrary_country_returns_correct_string (__main__.CountryTest) ... ok\n", - "\n", - "----------------------------------------------------------------------\n", - "Ran 60 tests in 0.133s\n", - "\n", - "OK\n" - ] + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "fzMQClXIAq7r" + }, + "outputs": [], + "source": [ + "class Country:\n", + " \"\"\"\n", + " Class for keeping track of country details for the purpose of the Country Catalogue's functionality. Country objects\n", + " know their name, which continent they are on, their population, and their surface area.\n", + " \"\"\"\n", + "\n", + " def __init__(self, name: str, continent: str, population: int, area: float):\n", + "\n", + "\n", + " def population_density(self) -> float:\n", + " \"\"\"\n", + " Calculate and return the population density of the Country. Population density is calculated with\n", + " population/area.\n", + "\n", + " :return: The population density of the Country.\n", + " :rtype: float.\n", + " \"\"\"\n", + "\n", + "\n", + " def __eq__(self, other: \"Country\") -> bool:\n", + " \"\"\"\n", + " Check if two instances of a Country object are equal. Country objects are considered equal if all attributes are\n", + " equal.\n", + "\n", + " :param other: A Country object to compare the self Country to.\n", + " :type other: Country.\n", + " :return: True if the Country objects have all attributes equal.\n", + " :rtype: boolean.\n", + " \"\"\"\n", + "\n", + "\n", + " def __repr__(self) -> str:\n", + " \"\"\"\n", + " Returns a string representation of the Country object. The string is \"name, continent, population, area\".\n", + "\n", + " :return: String representation of the Country object of the form \"name, continent, population, area\".\n", + " :rtype: String.\n", + " \"\"\"\n" + ] }, { - "output_type": "execute_result", - "data": { - "text/plain": [ - "" + "cell_type": "code", + "source": [ + "import unittest\n", + "\n", + "class CountryTest(unittest.TestCase):\n", + " def test_country_name_returns_correct_country_name(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(\"Name\", country.name)\n", + "\n", + " def test_country_continent_returns_correct_country_continent(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(\"Continent\", country.continent)\n", + "\n", + " def test_country_population_returns_correct_country_population(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(123456789, country.population)\n", + "\n", + " def test_country_area_returns_correct_country_area(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(987654321, country.area)\n", + "\n", + " def test_population_density_returns_correct_population_density(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertAlmostEqual(123456789 / 987654321, country.population_density())\n", + "\n", + " def test_population_density_with_zero_area_raises_zero_division_error(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 0)\n", + " with self.assertRaises(ZeroDivisionError):\n", + " country.population_density()\n", + "\n", + " def test_equals_equal_country_objects_returns_true(self):\n", + " country_a = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " country_b = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(country_a, country_b)\n", + "\n", + " def test_equals_unequal_country_objects_returns_false(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " cases = [\n", + " Country(\"Eman\", \"Continent\", 123456789, 987654321),\n", + " Country(\"Name\", \"Tnenitnoc\", 123456789, 987654321),\n", + " Country(\"Name\", \"Continent\", 987654321, 987654321),\n", + " Country(\"Name\", \"Continent\", 123456789, 123456789),\n", + " Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789),\n", + " 54321,\n", + " ]\n", + " for case in cases:\n", + " with self.subTest(case=case):\n", + " self.assertNotEqual(country, case)\n", + "\n", + " def test_repr_arbitrary_country_returns_correct_string(self):\n", + " country = Country(\"Name\", \"Continent\", 123456789, 987654321)\n", + " self.assertEqual(\"Country(name=Name, continent=Continent, population=123456789, area=987654321)\", str(country))\n" + ], + "metadata": { + "id": "fIyBwt0uA6kO" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "class CountryCatalogue:\n", + " \"\"\"\n", + " Class for managing a collection of Country Objects. The class maintains a list of the Country objects and provides\n", + " a mechanism for adding, removing, finding, checking if exists, and other collection-ie type methods.\n", + " \"\"\"\n", + "\n", + " def __init__(self):\n", + "\n", + "\n", + " def _find(self, country: Country) -> int:\n", + " \"\"\"\n", + " Find the index of the specified Country within the Country Catalogue. If the Country does not exist, return the\n", + " sentinel value of -1.\n", + "\n", + " :param country: Country object to search for.\n", + " :type country: Country object.\n", + " :return: Index of the Country with the matching values.\n", + " :rtype: integer.\n", + " \"\"\"\n", + "\n", + "\n", + " def contains(self, country: Country) -> bool:\n", + " \"\"\"\n", + " Check if a Country with the provided values exists within the collection. Returns True if it does and False\n", + " otherwise.\n", + "\n", + " :param country: Country object to search for.\n", + " :type country: Country object.\n", + " :return: True if the Country object is contained within the CountryCatalogue, False otherwise.\n", + " :rtype: bool.\n", + " \"\"\"\n", + "\n", + "\n", + " def add(self, country: Country) -> None:\n", + " \"\"\"\n", + " Add the provided Country object to the Country Catalogue.\n", + "\n", + " :param country: Country object to add to the collection.\n", + " :type country: Country object.\n", + " \"\"\"\n", + "\n", + "\n", + " def remove(self, country: Country) -> Country:\n", + " \"\"\"\n", + " Remove a Country object from the Country Catalogue matching the provided value. The removed Country object is\n", + " returned by the method. If no such Country object exists within the collection, a ValueError is raised.\n", + "\n", + " :raise ValueError: If the Country object does not exist within the collection, a ValueError is raised.\n", + " :param country: Country object to remove from the collection.\n", + " :type country: Country object.\n", + " \"\"\"\n", + "\n", + "\n", + " def country_with_largest_population_density(self) -> Country:\n", + " \"\"\"\n", + " Find and return the Country object with the largest population density. A reference to the Country object with\n", + " the largest density is returned. If the collection is empty, a IndexError is raised.\n", + "\n", + " :raise IndexError: If the collection is empty, raise an IndexError.\n", + " :return: Country object with the largest population density.\n", + " :rtype: Country.\n", + " \"\"\"\n", + "\n", + "\n", + " def country_with_smallest_population_density(self) -> Country:\n", + " \"\"\"\n", + " Find and return the Country object with the smallest population density. A reference to the Country object with\n", + " the smallest density is returned. If the collection is empty, a IndexError is raised.\n", + "\n", + " :raise IndexError: If the collection is empty, raise an IndexError.\n", + " :return: Country object with the smallest population density.\n", + " :rtype: Country.\n", + " \"\"\"\n", + "\n", + "\n", + " def filter_countries_by_population_density(self, low_limit: float, high_limit: float) -> \"CountryCatalogue\":\n", + " \"\"\"\n", + " Return a new CountryCatalogue containing only Country objects from the current CountryCatalogue with a\n", + " population density between the specified low & high range. If no Country objects between the specified range\n", + " exist, an empty CountryCatalogue is returned.\n", + "\n", + " :param low_limit: The low population density; only Country objects with a population density greater than or\n", + " equal to the low_limit will be included in the filtered CountryCatalogue.\n", + " :type low_limit: float\n", + " :param high_limit: The high population density; only Country objects with a population density strictly less\n", + " than the high_limit will be included in the filtered CountryCatalogue.\n", + " :type high_limit: float\n", + " :return: A new CountryCatalogue object with copies of Country objects that fall within the specified range.\n", + " :rtype: CountryCatalogue\n", + " \"\"\"\n", + "\n", + "\n", + " def most_populous_continent(self) -> str:\n", + " \"\"\"\n", + " Find and return the name of the continent with the largest population based on the Country objects within the\n", + " CountryCatalogue. If two or more continents have the same largest population, return the name of the country\n", + " found first. If the collection is empty, a IndexError is raised.\n", + "\n", + " :raise IndexError: If the collection is empty, raise an IndexError.\n", + " :return: Name of the continent with the largest population.\n", + " :rtype: str.\n", + " \"\"\"\n", + "\n", + "\n", + " def __getitem__(self, item: int) -> Country:\n", + "\n", + "\n", + " def __len__(self) -> int:\n", + "\n", + "\n", + " def __eq__(self, other: \"CountryCatalogue\") -> bool:\n", + "\n", + "\n", + " def __repr__(self) -> str:\n" + ], + "metadata": { + "id": "SGrsdqthA-Dc" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "import unittest\n", + "\n", + "class CountryCatalogueTest(unittest.TestCase):\n", + " def test_len_empty_collection_returns_0(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " self.assertEqual(0, len(empty_country_catalogue))\n", + "\n", + " def test_contains_empty_collection_returns_false(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " self.assertFalse(empty_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", + "\n", + " def test_remove_empty_collection_raises_value_error(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " with self.assertRaises(ValueError):\n", + " empty_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + "\n", + " def test_country_with_largest_population_density_empty_collection_raises_index_error(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " with self.assertRaises(IndexError):\n", + " empty_country_catalogue.country_with_largest_population_density()\n", + "\n", + " def test_country_with_smallest_population_density_empty_collection_raises_index_error(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " with self.assertRaises(IndexError):\n", + " empty_country_catalogue.country_with_smallest_population_density()\n", + "\n", + " def test_filter_countries_by_population_density_empty_collection_returns_empty_country_catalogue(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " self.assertEqual(CountryCatalogue(), empty_country_catalogue.filter_countries_by_population_density(0, 10000))\n", + "\n", + " def test_most_populous_continent_empty_collection_raises_index_error(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " with self.assertRaises(IndexError):\n", + " empty_country_catalogue.most_populous_continent()\n", + "\n", + " def test_getitem_empty_collection_raises_index_error(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " indicies = [-1, 0, 1]\n", + " for index in indicies:\n", + " with self.subTest(index=index):\n", + " with self.assertRaises(IndexError):\n", + " empty_country_catalogue[index]\n", + "\n", + " def test_repr_empty_collection_returns_empty_string(self):\n", + " empty_country_catalogue = CountryCatalogue()\n", + " self.assertEqual(\"\", str(empty_country_catalogue))\n", + "\n", + " def test_len_singleton_returns_1(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(1, len(singleton_country_catalogue))\n", + "\n", + " def test_contains_singleton_country_exists_returns_true(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertTrue(singleton_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", + "\n", + " def test_contains_singleton_country_not_exists_returns_false(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertFalse(singleton_country_catalogue.contains(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789)))\n", + "\n", + " def test_remove_singleton_country_exists_returns_country(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(\n", + " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", + " singleton_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321)),\n", + " )\n", + "\n", + " def test_remove_singleton_country_exists_removes_country(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " singleton_country_catalogue.remove(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertFalse(singleton_country_catalogue.contains(Country(\"Name\", \"Continent\", 123456789, 987654321)))\n", + "\n", + " def test_remove_singleton_country_not_exists_raises_value_error(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " with self.assertRaises(ValueError):\n", + " singleton_country_catalogue.remove(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789))\n", + "\n", + " def test_country_with_largest_population_density_singleton_returns_correct_country(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(\n", + " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", + " singleton_country_catalogue.country_with_largest_population_density(),\n", + " )\n", + "\n", + " def test_country_with_smallest_population_density_singleton_returns_correct_country(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(\n", + " Country(\"Name\", \"Continent\", 123456789, 987654321),\n", + " singleton_country_catalogue.country_with_smallest_population_density(),\n", + " )\n", + "\n", + " def test_filter_countries_by_population_density_singleton_not_within_range_returns_empty_country_catalogue(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " self.assertEqual(CountryCatalogue(), singleton_country_catalogue.filter_countries_by_population_density(0, 5))\n", + "\n", + " def test_filter_countries_by_population_density_singleton_within_range_returns_correct_country_catalogue(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " expected_country_catalogue = CountryCatalogue()\n", + " expected_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(0, 100)\n", + " self.assertEqual(expected_country_catalogue, filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_singleton_equal_low_limit_returns_correct_country_catalogue(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " expected_country_catalogue = CountryCatalogue()\n", + " expected_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(10, 100)\n", + " self.assertEqual(expected_country_catalogue, filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_singleton_equal_high_limit_returns_empty_country_catalogue(\n", + " self,\n", + " ):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(0, 10)\n", + " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_singleton_low_high_high_low_returns_empty_country_catalogue(\n", + " self,\n", + " ):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " filtered_country_catalogue = singleton_country_catalogue.filter_countries_by_population_density(100, 0)\n", + " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", + "\n", + " def test_most_populous_continent_singleton_returns_correct_continent(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " self.assertEqual(\"Continent\", singleton_country_catalogue.most_populous_continent())\n", + "\n", + " def test_getitem_valid_index_singleton_returns_item(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(Country(\"Name\", \"Continent\", 123456789, 987654321), singleton_country_catalogue[0])\n", + "\n", + " def test_getitem_invalid_index_singleton_raises_index_error(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " with self.assertRaises(IndexError):\n", + " singleton_country_catalogue[1]\n", + "\n", + " def test_repr_singleton_returns_correct_string(self):\n", + " singleton_country_catalogue = CountryCatalogue()\n", + " singleton_country_catalogue.add(Country(\"Name\", \"Continent\", 123456789, 987654321))\n", + " self.assertEqual(\n", + " \"Country(name=Name, continent=Continent, population=123456789, area=987654321)\\n\",\n", + " str(singleton_country_catalogue),\n", + " )\n", + "\n", + " def test_len_many_returns_correct_length(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " self.assertEqual(5, len(many_country_catalogue))\n", + "\n", + " def test_contains_many_country_exists_returns_true(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " self.assertTrue(many_country_catalogue.contains(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", + "\n", + " def test_contains_many_country_not_exists_returns_false(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " self.assertFalse(many_country_catalogue.contains(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789)))\n", + "\n", + " def test_remove_many_country_exists_returns_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " country = Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)\n", + " self.assertEqual(country, many_country_catalogue.remove(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", + "\n", + " def test_remove_many_country_exists_removes_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " many_country_catalogue.remove(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " self.assertFalse(many_country_catalogue.contains(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321)))\n", + "\n", + " def test_remove_many_country_not_exists_raises_value_error(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " with self.assertRaises(ValueError):\n", + " many_country_catalogue.remove(Country(\"Eman\", \"Tnenitnoc\", 987654321, 123456789))\n", + "\n", + " def test_country_with_largest_population_density_many_returns_correct_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " self.assertEqual(\n", + " Country(\"Name_3\", \"Continent_3\", 10, 1), many_country_catalogue.country_with_largest_population_density()\n", + " )\n", + "\n", + " def test_country_with_smallest_population_density_many_returns_correct_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " self.assertEqual(\n", + " Country(\"Name_5\", \"Continent_5\", 1, 1), many_country_catalogue.country_with_smallest_population_density()\n", + " )\n", + "\n", + " def test_filter_countries_by_population_density_many_not_within_range_returns_empty_country_catalogue(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " self.assertEqual(CountryCatalogue(), many_country_catalogue.filter_countries_by_population_density(50, 100))\n", + "\n", + " def test_filter_countries_by_population_density_many_within_range_returns_correct_country_catalog(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " expected = CountryCatalogue()\n", + " expected.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " expected.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " expected.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " expected.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 10)\n", + " self.assertEqual(expected, filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_many_equal_low_limit_returns_correct_country_catalogue(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " expected = CountryCatalogue()\n", + " expected.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(10, 100)\n", + " self.assertEqual(expected, filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_many_equal_high_limit_returns_empty_country_catalogue(\n", + " self,\n", + " ):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 1)\n", + " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", + "\n", + " def test_filter_countries_by_population_density_many_low_high_high_low_returns_empty_country_catalogue(\n", + " self,\n", + " ):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(100, 0)\n", + " self.assertEqual(CountryCatalogue(), filtered_country_catalogue)\n", + "\n", + " def test_most_populous_continent_many_returns_correct_continent(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_1\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_2\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_2\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_3\", 1, 1))\n", + " self.assertEqual(\"Continent_2\", many_country_catalogue.most_populous_continent())\n", + "\n", + " def test_getitem_valid_index_many_returns_item(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " countries = []\n", + " countries.append(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " countries.append(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " countries.append(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " countries.append(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " countries.append(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " for index, country in enumerate(countries):\n", + " with self.subTest(index=index, country=country):\n", + " self.assertEqual(country, many_country_catalogue[index])\n", + "\n", + " def test_getitem_invalid_index_many_raises_index_error(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " with self.assertRaises(IndexError):\n", + " many_country_catalogue[5]\n", + "\n", + " def test_repr_many_returns_correct_string(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " expected = (\n", + " \"Country(name=Name_1, continent=Continent_1, population=123456789, area=987654321)\\n\"\n", + " \"Country(name=Name_2, continent=Continent_2, population=123456789, area=987654321)\\n\"\n", + " \"Country(name=Name_3, continent=Continent_3, population=123456789, area=987654321)\\n\"\n", + " \"Country(name=Name_4, continent=Continent_4, population=123456789, area=987654321)\\n\"\n", + " \"Country(name=Name_5, continent=Continent_5, population=123456789, area=987654321)\\n\"\n", + " )\n", + " self.assertEqual(expected, str(many_country_catalogue))\n", + "\n", + " def test_remove_two_duplicate_country_exists_leaves_one_duplicate(self):\n", + " duplicate_country_catalogue = CountryCatalogue()\n", + " duplicate_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " self.assertTrue(duplicate_country_catalogue.contains(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321)))\n", + "\n", + " def test_remove_two_duplicate_country_exists_two_remove_calls_removes_both_duplicates(self):\n", + " duplicate_country_catalogue = CountryCatalogue()\n", + " duplicate_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 123456789, 987654321))\n", + " duplicate_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 123456789, 987654321))\n", + " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " duplicate_country_catalogue.remove(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321))\n", + " self.assertFalse(duplicate_country_catalogue.contains(Country(\"Name_2\", \"Continent_2\", 123456789, 987654321)))\n", + "\n", + " def test_country_with_largest_population_density_duplicate_densities_returns_correct_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 10, 1))\n", + " self.assertEqual(\n", + " Country(\"Name_3\", \"Continent_3\", 10, 1), many_country_catalogue.country_with_largest_population_density()\n", + " )\n", + "\n", + " def test_country_with_smallest_population_density_duplicate_densities_returns_correct_country(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " self.assertEqual(\n", + " Country(\"Name_3\", \"Continent_3\", 1, 1), many_country_catalogue.country_with_smallest_population_density()\n", + " )\n", + "\n", + " def test_filter_countries_by_population_density_duplicate_within_range_returns_country_catalogue_all_duplicates(\n", + " self,\n", + " ):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " many_country_catalogue.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " expected = CountryCatalogue()\n", + " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " expected.add(Country(\"Name\", \"Continent\", 10, 1))\n", + " filtered_country_catalogue = many_country_catalogue.filter_countries_by_population_density(0, 100)\n", + " self.assertEqual(expected, filtered_country_catalogue)\n", + "\n", + " def test_most_populous_continent_duplicate_populations_returns_first_continent_found(self):\n", + " many_country_catalogue = CountryCatalogue()\n", + " many_country_catalogue.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_2\", \"Continent_1\", 2, 1))\n", + " many_country_catalogue.add(Country(\"Name_3\", \"Continent_2\", 5, 1))\n", + " many_country_catalogue.add(Country(\"Name_4\", \"Continent_3\", 6, 1))\n", + " many_country_catalogue.add(Country(\"Name_5\", \"Continent_3\", 1, 1))\n", + " self.assertEqual(\"Continent_1\", many_country_catalogue.most_populous_continent())\n", + "\n", + " def test_eq_various_equal_cases_are_equal(self):\n", + " empty_a = CountryCatalogue()\n", + " empty_b = CountryCatalogue()\n", + " single_a = CountryCatalogue()\n", + " single_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " single_b = CountryCatalogue()\n", + " single_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_a = CountryCatalogue()\n", + " many_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_a.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_a.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", + " many_a.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_a.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " many_b = CountryCatalogue()\n", + " many_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " many_b.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " many_b.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", + " many_b.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " many_b.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " countries_a = [empty_a, single_a, many_a]\n", + " countries_b = [empty_b, single_b, many_b]\n", + " for country_a, country_b in zip(countries_a, countries_b):\n", + " with self.subTest(country_a=country_a, country_b=country_b):\n", + " self.assertEqual(country_a, country_b)\n", + "\n", + " def test_eq_various_not_equal_cases_are_not_equal(self):\n", + " empty_a = CountryCatalogue()\n", + " single_a = CountryCatalogue()\n", + " single_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " single_b = CountryCatalogue()\n", + " single_b.add(Country(\"Name_100\", \"Continent_100\", 5, 1))\n", + " different_order_a = CountryCatalogue()\n", + " different_order_a.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " different_order_a.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " different_order_a.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", + " different_order_a.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " different_order_a.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " different_order_b = CountryCatalogue()\n", + " different_order_b.add(Country(\"Name_1\", \"Continent_1\", 5, 1))\n", + " different_order_b.add(Country(\"Name_2\", \"Continent_2\", 2, 1))\n", + " different_order_b.add(Country(\"Name_3\", \"Continent_3\", 1, 1))\n", + " different_order_b.add(Country(\"Name_5\", \"Continent_5\", 1, 1))\n", + " different_order_b.add(Country(\"Name_4\", \"Continent_4\", 6, 1))\n", + " countries_a = [empty_a, single_a, different_order_a, empty_a]\n", + " countries_b = [single_b, single_b, different_order_b, []]\n", + " for country_a, country_b in zip(countries_a, countries_b):\n", + " with self.subTest(country_a=country_a, country_b=country_b):\n", + " self.assertNotEqual(country_a, country_b)\n" + ], + "metadata": { + "id": "fVVS4HJYBCFa" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Run this cell to run all unit tests\n", + "unittest.main(argv=[''], verbosity=2, exit=False)" + ], + "metadata": { + "id": "gz5JlQfFBzKT", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "9e13d5e4-6b0c-46cc-fd6c-a86ee678cf65" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "test_contains_empty_collection_returns_false (__main__.CountryCatalogueTest) ... ok\n", + "test_contains_many_country_exists_returns_true (__main__.CountryCatalogueTest) ... ok\n", + "test_contains_many_country_not_exists_returns_false (__main__.CountryCatalogueTest) ... ok\n", + "test_contains_singleton_country_exists_returns_true (__main__.CountryCatalogueTest) ... ok\n", + "test_contains_singleton_country_not_exists_returns_false (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_largest_population_density_duplicate_densities_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_largest_population_density_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_largest_population_density_many_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_largest_population_density_singleton_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_smallest_population_density_duplicate_densities_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_smallest_population_density_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_smallest_population_density_many_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_country_with_smallest_population_density_singleton_returns_correct_country (__main__.CountryCatalogueTest) ... ok\n", + "test_eq_various_equal_cases_are_equal (__main__.CountryCatalogueTest) ... ok\n", + "test_eq_various_not_equal_cases_are_not_equal (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_duplicate_within_range_returns_country_catalogue_all_duplicates (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_empty_collection_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_many_equal_high_limit_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_many_equal_low_limit_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_many_low_high_high_low_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_many_not_within_range_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_many_within_range_returns_correct_country_catalog (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_singleton_equal_high_limit_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_singleton_equal_low_limit_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_singleton_low_high_high_low_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_singleton_not_within_range_returns_empty_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_filter_countries_by_population_density_singleton_within_range_returns_correct_country_catalogue (__main__.CountryCatalogueTest) ... ok\n", + "test_getitem_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_getitem_invalid_index_many_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_getitem_invalid_index_singleton_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_getitem_valid_index_many_returns_item (__main__.CountryCatalogueTest) ... ok\n", + "test_getitem_valid_index_singleton_returns_item (__main__.CountryCatalogueTest) ... ok\n", + "test_len_empty_collection_returns_0 (__main__.CountryCatalogueTest) ... ok\n", + "test_len_many_returns_correct_length (__main__.CountryCatalogueTest) ... ok\n", + "test_len_singleton_returns_1 (__main__.CountryCatalogueTest) ... ok\n", + "test_most_populous_continent_duplicate_populations_returns_first_continent_found (__main__.CountryCatalogueTest) ... ok\n", + "test_most_populous_continent_empty_collection_raises_index_error (__main__.CountryCatalogueTest) ... ok\n", + "test_most_populous_continent_many_returns_correct_continent (__main__.CountryCatalogueTest) ... ok\n", + "test_most_populous_continent_singleton_returns_correct_continent (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_empty_collection_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_many_country_exists_removes_country (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_many_country_exists_returns_country (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_many_country_not_exists_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_singleton_country_exists_removes_country (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_singleton_country_exists_returns_country (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_singleton_country_not_exists_raises_value_error (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_two_duplicate_country_exists_leaves_one_duplicate (__main__.CountryCatalogueTest) ... ok\n", + "test_remove_two_duplicate_country_exists_two_remove_calls_removes_both_duplicates (__main__.CountryCatalogueTest) ... ok\n", + "test_repr_empty_collection_returns_empty_string (__main__.CountryCatalogueTest) ... ok\n", + "test_repr_many_returns_correct_string (__main__.CountryCatalogueTest) ... ok\n", + "test_repr_singleton_returns_correct_string (__main__.CountryCatalogueTest) ... ok\n", + "test_country_area_returns_correct_country_area (__main__.CountryTest) ... ok\n", + "test_country_continent_returns_correct_country_continent (__main__.CountryTest) ... ok\n", + "test_country_name_returns_correct_country_name (__main__.CountryTest) ... ok\n", + "test_country_population_returns_correct_country_population (__main__.CountryTest) ... ok\n", + "test_equals_equal_country_objects_returns_true (__main__.CountryTest) ... ok\n", + "test_equals_unequal_country_objects_returns_false (__main__.CountryTest) ... ok\n", + "test_population_density_returns_correct_population_density (__main__.CountryTest) ... ok\n", + "test_population_density_with_zero_area_raises_zero_division_error (__main__.CountryTest) ... ok\n", + "test_repr_arbitrary_country_returns_correct_string (__main__.CountryTest) ... ok\n", + "\n", + "----------------------------------------------------------------------\n", + "Ran 60 tests in 0.133s\n", + "\n", + "OK\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 5 + } ] - }, - "metadata": {}, - "execution_count": 5 + }, + { + "cell_type": "code", + "source": [ + "# NAME:\n", + "# ST-NUMBER:\n", + "# StFX EMAIL:\n", + "\n", + "COUNTRY_DATA_FILE_NAME = \"country_data.csv\"\n", + "\n", + "# Makes it so the import from the unit tests do not break things\n", + "if __name__ == \"__main__\":\n", + " # Load data\n", + " catalogue = CountryCatalogue()\n", + " in_file = open(COUNTRY_DATA_FILE_NAME, \"r\")\n", + " for line in in_file:\n", + " split_line = line.split(\",\")\n", + " country = Country(\n", + " name=split_line[0], continent=split_line[1], population=int(split_line[2]), area=float(split_line[3])\n", + " )\n", + " catalogue.add(country)\n", + " in_file.close()\n", + "\n", + " # Alter catalogue contents\n", + " england = Country(\"England\", \"Europe\", 56489800, 130279)\n", + " ecuador = Country(\"Ecuador\", \"South America\", 18048628, 256370)\n", + " india = Country(\"India\", \"Asia\", 1375586000, 3287263)\n", + " catalogue.add(england)\n", + " catalogue.add(ecuador)\n", + " catalogue.add(india)\n", + " catalogue.remove(Country(\"Canada\", \"North America\", 34207000, 9976140.0))\n", + " catalogue.remove(Country(\"South Korea\", \"Asia\", 50503933, 98076.92))\n", + "\n", + " # Answering questions\n", + " print(catalogue.country_with_smallest_population_density())\n", + " print(catalogue.country_with_largest_population_density())\n", + " print(catalogue.most_populous_continent())\n", + "\n", + " # Save filtered data\n", + " filtered_catalogue = catalogue.filter_countries_by_population_density(200, 450)\n", + " out_file = open(\"density-200_450.csv\", \"w\")\n", + " out_file.write(str(filtered_catalogue))\n", + " out_file.close()\n" + ], + "metadata": { + "id": "O5284wegBGe6" + }, + "execution_count": null, + "outputs": [] } - ] - }, - { - "cell_type": "code", - "source": [ - "# NAME:\n", - "# ST-NUMBER:\n", - "# StFX EMAIL:\n", - "\n", - "COUNTRY_DATA_FILE_NAME = \"country_data.csv\"\n", - "\n", - "# Makes it so the import from the unit tests do not break things\n", - "if __name__ == \"__main__\":\n", - " # Load data\n", - " catalogue = CountryCatalogue()\n", - " in_file = open(COUNTRY_DATA_FILE_NAME, \"r\")\n", - " for line in in_file:\n", - " split_line = line.split(\",\")\n", - " country = Country(\n", - " name=split_line[0], continent=split_line[1], population=int(split_line[2]), area=float(split_line[3])\n", - " )\n", - " catalogue.add(country)\n", - " in_file.close()\n", - "\n", - " # Alter catalogue contents\n", - " england = Country(\"England\", \"Europe\", 56489800, 130279)\n", - " ecuador = Country(\"Ecuador\", \"South America\", 18048628, 256370)\n", - " india = Country(\"India\", \"Asia\", 1375586000, 3287263)\n", - " catalogue.add(england)\n", - " catalogue.add(ecuador)\n", - " catalogue.add(india)\n", - " catalogue.remove(Country(\"Canada\", \"North America\", 34207000, 9976140.0))\n", - " catalogue.remove(Country(\"South Korea\", \"Asia\", 50503933, 98076.92))\n", - "\n", - " # Answering questions\n", - " print(catalogue.country_with_smallest_population_density())\n", - " print(catalogue.country_with_largest_population_density())\n", - " print(catalogue.most_populous_continent())\n", - "\n", - " # Save filtered data\n", - " filtered_catalogue = catalogue.filter_countries_by_population_density(200, 450)\n", - " out_file = open(\"density-200_450.csv\", \"w\")\n", - " out_file.write(str(filtered_catalogue))\n", - " out_file.close()\n" - ], - "metadata": { - "id": "O5284wegBGe6" - }, - "execution_count": null, - "outputs": [] - } - ] -} + ] +} \ No newline at end of file