class GazetteAccessTest(TestCase):
    def setUp(self):
        self.return_value = [
            Gazette("4205902", date.today(),
                    "https://queridodiario.ok.org.br/"),
            Gazette("4202909", date.today(),
                    "https://queridodiario.ok.org.br/"),
        ]
        self.mock_data_gateway = MagicMock()
        self.mock_data_gateway.get_gazettes = MagicMock(
            return_value=self.return_value)
        self.gazette_access = GazetteAccess(self.mock_data_gateway)

    def test_create_gazette_access(self):
        self.assertIsNotNone(self.gazette_access,
                             msg="Could not create GazetteAccess object")
        self.assertIsInstance(
            self.gazette_access,
            GazetteAccessInterface,
            msg=
            "The GazetteAccess object should implement GazetteAccessInterface",
        )

    def test_get_gazettes(self):
        self.assertEqual(2, len(list(self.gazette_access.get_gazettes())))
        self.mock_data_gateway.get_gazettes.assert_called_once()

    def test_get_gazettes_should_return_dictionary(self):
        expected_results = [
            {
                "territory_id": "4205902",
                "date": date.today(),
                "url": "https://queridodiario.ok.org.br/",
            },
            {
                "territory_id": "4202909",
                "date": date.today(),
                "url": "https://queridodiario.ok.org.br/",
            },
        ]

        gazettes = self.gazette_access.get_gazettes()
        self.assertCountEqual(expected_results, gazettes)

    def test_should_foward_filter_to_gateway(self):
        gazette_access = GazetteAccess(self.mock_data_gateway)
        list(
            self.gazette_access.get_gazettes(filters=GazetteRequest(
                territory_id="4205902")))
        self.mock_data_gateway.get_gazettes.assert_called_once_with(
            territory_id="4205902")
Exemple #2
0
class GazetteAccessTest(TestCase):
    def setUp(self):
        self.return_value = [
            Gazette(
                "4205902",
                date.today(),
                "https://queridodiario.ok.org.br/",
                "so'jsdogjeogjsdogjheogdfsdf",
                "My city",
                "My state",
                "highlight" "123,456",
                False,
            ),
            Gazette(
                "4205902",
                (date.today() - timedelta(days=1)),
                "https://queridodiario.ok.org.br/",
                "a;oijaeofdjewofijrogho490jhfeasd",
                "My city",
                "My state",
                "highlight" "123,456",
                False,
            ),
            Gazette(
                "4205902",
                (date.today() + timedelta(days=1)),
                "https://queridodiario.ok.org.br/",
                "eolgpijdsfesuhrgfiuhsad",
                "My city",
                "My state",
                "highlight" "123,456",
                False,
            ),
            Gazette(
                "4202909",
                date.today(),
                "https://queridodiario.ok.org.br/",
                "ew;oigfdfsdjn;dajnorgf",
                "My city",
                "My state",
                "highlight" "123,456",
                False,
            ),
            Gazette(
                "4202909",
                (date.today() - timedelta(days=1)),
                "https://queridodiario.ok.org.br/",
                "sdosauiydhbfeicneqiudnewf",
                "My city",
                "My state",
                "highlight" "123,456",
                False,
            ),
            Gazette(
                "4202909",
                (date.today() + timedelta(days=1)),
                "https://queridodiario.ok.org.br/",
                "sdo;ifjwefonsdiasndiswabdisbfnidf",
                "My city",
                "My state",
                "highlight" "123,456",
                False,
            ),
        ]
        self.mock_data_gateway = MagicMock()
        self.mock_data_gateway.get_gazettes = MagicMock(
            return_value=(len(self.return_value), self.return_value)
        )

        self.database_data = [
            City(
                "Taquarinha Do Norte",
                "1235",
                "RN",
                OpennessLevel("1"),
                ["https://somewebsite.org", "https://anotherwebsite.org"],
            ),
            City(
                "Taquarinha Do Sul",
                "1236",
                "RS",
                OpennessLevel("3"),
                ["https://somewebsite.org", "https://anotherwebsite.org"],
            ),
        ]
        self.mock_database_gateway = MagicMock()
        self.mock_database_gateway.get_cities = MagicMock(
            return_value=self.database_data
        )
        self.mock_database_gateway.get_city = MagicMock(
            return_value=self.database_data[0]
        )
        self.gazette_access = GazetteAccess(
            self.mock_data_gateway, self.mock_database_gateway
        )

    def test_create_gazette_access(self):
        self.assertIsNotNone(
            self.gazette_access, msg="Could not create GazetteAccess object"
        )
        self.assertIsInstance(
            self.gazette_access,
            GazetteAccessInterface,
            msg="The GazetteAccess object should implement GazetteAccessInterface",
        )

    def test_get_gazettes(self):
        items_count, gazettes = self.gazette_access.get_gazettes()
        self.assertEqual(items_count, len(self.return_value))
        self.assertEqual(len(self.return_value), len(gazettes))
        self.mock_data_gateway.get_gazettes.assert_called_once()

    def test_get_cities(self):
        cities = self.gazette_access.get_cities()
        self.assertEqual(len(self.database_data), len(cities))
        self.mock_database_gateway.get_cities.assert_called_once()
        self.assertCountEqual([vars(city) for city in self.database_data], cities)

    def test_get_city(self):
        city = self.gazette_access.get_city()
        self.assertEqual(vars(self.database_data[0]), city)
        self.mock_database_gateway.get_city.assert_called_once()

    def test_get_gazettes_should_return_dictionary(self):
        expected_results = [
            {
                "territory_id": gazette.territory_id,
                "date": gazette.date,
                "url": gazette.url,
                "checksum": gazette.checksum,
                "territory_name": gazette.territory_name,
                "state_code": gazette.state_code,
                "edition": gazette.edition,
                "is_extra_edition": gazette.is_extra_edition,
                "highlight_texts": gazette.highlight_texts,
                "file_raw_txt": gazette.file_raw_txt,
            }
            for gazette in self.return_value
        ]

        _, gazettes = self.gazette_access.get_gazettes()
        self.assertCountEqual(expected_results, gazettes)

    def test_should_foward_filter_to_gateway(self):
        gazette_access = GazetteAccess(
            self.mock_data_gateway, self.mock_database_gateway
        )
        list(
            self.gazette_access.get_gazettes(
                filters=GazetteRequest(territory_id="4205902")
            )[1]
        )
        self.mock_data_gateway.get_gazettes.assert_called_once_with(
            territory_id="4205902",
            since=None,
            until=None,
            keywords=None,
            offset=0,
            size=10,
            fragment_size=150,
            number_of_fragments=1,
            pre_tags=[""],
            post_tags=[""],
            sort_by="descending_date",
        )

    def test_should_foward_since_date_filter_to_gateway(self):
        gazette_access = GazetteAccess(
            self.mock_data_gateway, self.mock_database_gateway
        )
        list(
            self.gazette_access.get_gazettes(
                filters=GazetteRequest(since=date.today())
            )[1]
        )
        self.mock_data_gateway.get_gazettes.assert_called_once_with(
            since=date.today(),
            until=None,
            territory_id=None,
            keywords=None,
            offset=0,
            size=10,
            fragment_size=150,
            number_of_fragments=1,
            pre_tags=[""],
            post_tags=[""],
            sort_by="descending_date",
        )

    def test_should_foward_until_date_filter_to_gateway(self):
        gazette_access = GazetteAccess(
            self.mock_data_gateway, self.mock_database_gateway
        )
        list(
            self.gazette_access.get_gazettes(
                filters=GazetteRequest(until=date.today())
            )[1]
        )
        self.mock_data_gateway.get_gazettes.assert_called_once_with(
            until=date.today(),
            since=None,
            territory_id=None,
            keywords=None,
            offset=0,
            size=10,
            fragment_size=150,
            number_of_fragments=1,
            pre_tags=[""],
            post_tags=[""],
            sort_by="descending_date",
        )

    def test_should_foward_keywords_filter_to_gateway(self):
        gazette_access = GazetteAccess(
            self.mock_data_gateway, self.mock_database_gateway
        )
        keywords = ["foo", "bar", "zpto"]
        list(
            self.gazette_access.get_gazettes(filters=GazetteRequest(keywords=keywords))[
                1
            ]
        )
        self.mock_data_gateway.get_gazettes.assert_called_once_with(
            until=None,
            since=None,
            territory_id=None,
            keywords=keywords,
            offset=0,
            size=10,
            fragment_size=150,
            number_of_fragments=1,
            pre_tags=[""],
            post_tags=[""],
            sort_by="descending_date",
        )

    def test_should_foward_page_fields_filter_to_gateway(self):
        gazette_access = GazetteAccess(
            self.mock_data_gateway, self.mock_database_gateway
        )
        list(
            self.gazette_access.get_gazettes(
                filters=GazetteRequest(offset=10, size=100)
            )[1]
        )
        self.mock_data_gateway.get_gazettes.assert_called_once_with(
            until=None,
            since=None,
            territory_id=None,
            keywords=None,
            offset=10,
            size=100,
            fragment_size=150,
            number_of_fragments=1,
            pre_tags=[""],
            post_tags=[""],
            sort_by="descending_date",
        )