diff --git a/pysus/Notebooks/.gitkeep b/pysus/Notebooks/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/pysus/tests/test_ftp.py b/pysus/tests/test_ftp.py new file mode 100644 index 00000000..de98cef0 --- /dev/null +++ b/pysus/tests/test_ftp.py @@ -0,0 +1,164 @@ +import unittest +from pathlib import Path + +import pandas as pd + +from pysus.ftp import File, Directory, Database, CACHE +from pysus.data.local import ParquetSet +from pysus.ftp.databases import ( + ciha, + cnes, + pni, + ibge_datasus, + sinan, + sih, + sinasc, + sia, + sim, +) + + +def _test_file(testcase: unittest.TestCase, file: File): + testcase.assertTrue(isinstance(file, File)) + testcase.assertTrue(set(["size", "type", "modify"]) == set(file.info)) + testcase.assertTrue(bool(file.basename)) + testcase.assertTrue(bool(file.name)) + testcase.assertTrue(bool(file.path)) + testcase.assertTrue(str(Path(file.path).parent) == file.parent_path) + + +def _test_database(testcase: unittest.TestCase, database: Database): + testcase.assertTrue(isinstance(database, Database)) + testcase.assertTrue(bool(database.content)) + testcase.assertTrue( + set(["description", "long_name", "source"]) == set(database.metadata) + ) + + downloaded_file = ( + database.download(database.files[0]) + if database.files[0].extension != ".zip" + else database.download(database.files[-1]) + ) + testcase.assertTrue(isinstance(downloaded_file, ParquetSet)) + testcase.assertTrue(Path(downloaded_file.path).exists()) + testcase.assertTrue( + isinstance(downloaded_file.to_dataframe(), pd.DataFrame) + ) + testcase.assertTrue(not downloaded_file.to_dataframe().empty) + + +class TestDirectoryAndFile(unittest.TestCase): + def setUp(self): + self.root = Directory("/").load() + + def test_root_load(self): + self.assertTrue(self.root.loaded) + self.assertTrue(Directory("/dissemin") in self.root.content) + + def test_root_reload(self): + root = self.root.reload() + self.assertTrue(root.content == self.root.content) + + def test_root_directory(self): + self.assertTrue(self.root.name == "/") + self.assertTrue(self.root.path == "/") + self.assertTrue(self.root.parent == self.root) # outermost parent + + def test_directory_cache(self): + self.assertTrue(CACHE["/"] == self.root) + + def test_sinan_file(self): + file = Directory("/dissemin/publicos/SINAN/DADOS/FINAIS").content[0] + _test_file(self, file) + + +class TestDatabases(unittest.TestCase): + def test_ciha(self): + database = ciha.CIHA().load() + _test_database(self, database) + self.assertTrue(database.name == "CIHA") + self.assertSetEqual( + set(database.describe(database.files[0])), + {'group', 'last_update', 'month', 'name', 'size', 'uf', 'year'} + ) + self.assertEqual(len(database.format(database.files[0])), 4) + + def test_cnes(self): + database = cnes.CNES().load("DC") + _test_database(self, database) + self.assertTrue(database.name == "CNES") + self.assertSetEqual( + set(database.describe(database.files[0])), + {'group', 'last_update', 'month', 'name', 'size', 'uf', 'year'} + ) + self.assertEqual(len(database.format(database.files[0])), 4) + + def test_pni(self): + database = pni.PNI().load() + _test_database(self, database) + self.assertTrue(database.name == "PNI") + self.assertSetEqual( + set(database.describe(database.files[0])), + {'group', 'last_update', 'name', 'size', 'uf', 'year'} + ) + self.assertEqual(len(database.format(database.files[0])), 3) + + def test_ibge_datasus(self): + database = ibge_datasus.IBGEDATASUS().load() + _test_database(self, database) + self.assertTrue(database.name == "IBGE-DataSUS") + self.assertSetEqual( + set(database.describe(database.files[0])), + {'last_update', 'name', 'size', 'year'} + ) + self.assertEqual(len(database.format(database.files[0])), 1) + + def test_sinan(self): + database = sinan.SINAN().load() + _test_database(self, database) + self.assertTrue(database.name == "SINAN") + self.assertSetEqual( + set(database.describe(database.files[0])), + {'disease', 'last_update', 'name', 'size', 'year'} + ) + self.assertEqual(len(database.format(database.files[0])), 2) + + def test_sih(self): + database = sih.SIH().load() + _test_database(self, database) + self.assertTrue(database.name == "SIH") + self.assertSetEqual( + set(database.describe(database.files[0])), + {'group', 'last_update', 'month', 'name', 'size', 'uf', 'year'} + ) + self.assertEqual(len(database.format(database.files[0])), 4) + + def test_sinasc(self): + database = sinasc.SINASC().load() + _test_database(self, database) + self.assertTrue(database.name == "SINASC") + self.assertSetEqual( + set(database.describe(database.files[0])), + {'group', 'last_update', 'name', 'size', 'uf', 'year'} + ) + self.assertEqual(len(database.format(database.files[0])), 3) + + def test_sia(self): + database = sia.SIA().load() + _test_database(self, database) + self.assertTrue(database.name == "SIA") + self.assertSetEqual( + set(database.describe(database.files[0])), + {'group', 'last_update', 'month', 'name', 'size', 'uf', 'year'} + ) + self.assertEqual(len(database.format(database.files[0])), 4) + + def test_sim(self): + database = sim.SIM().load() + _test_database(self, database) + self.assertTrue(database.name == "SIM") + self.assertSetEqual( + set(database.describe(database.files[0])), + {'group', 'last_update', 'name', 'size', 'uf', 'year'} + ) + self.assertEqual(len(database.format(database.files[0])), 3) diff --git a/pysus/tests/test_ftp/__init__.py b/pysus/tests/test_ftp/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/pysus/tests/test_ftp/test_File.py b/pysus/tests/test_ftp/test_File.py deleted file mode 100644 index 2586f477..00000000 --- a/pysus/tests/test_ftp/test_File.py +++ /dev/null @@ -1,61 +0,0 @@ -# -*- coding:utf-8 -*- -""" -Created on 2023/12/12 -by luabida -license: GPL V3 or Later -""" -import datetime -import unittest -from pathlib import Path - -import pandas as pd -from pysus.data.local import ParquetSet -from pysus.ftp import CACHEPATH, File - - -class TestFile(unittest.TestCase): - def setUp(self): - path = "/dissemin/publicos/SIM/CID10/DORES/" - name = "DOAC1996.dbc" - info = { - "size": 76107, - "type": "file", - "modify": datetime.datetime(2020, 1, 31, 14, 48), - } - - self.file = File(path, name, info) - - def test_file_initialization(self): - file = self.file - - expected_path = "/dissemin/publicos/SIM/CID10/DORES/DOAC1996.dbc" - self.assertEqual(file.path, expected_path) - - self.assertEqual(file.name, "DOAC1996") - - self.assertEqual(file.extension, ".dbc") - - self.assertEqual(file.basename, "DOAC1996.dbc") - - expected_info = { - "size": "76.1 kB", - "type": "DBC file", - "modify": "2020-01-31 02:48PM", - } - self.assertEqual(file.info, expected_info) - - def test_file_download(self): - parquet = self.file.download() - - self.assertIsInstance(parquet, ParquetSet) - - self.assertTrue("size" in parquet.info) - - local_cache = Path(CACHEPATH) - expected_local_path = local_cache / "DOAC1996.parquet" - self.assertTrue(expected_local_path.exists()) - self.assertEqual(Path(str(parquet)), expected_local_path) - - df = parquet.to_dataframe() - self.assertIsInstance(df, pd.DataFrame) - self.assertFalse(df.empty) diff --git a/pysus/tests/test_ftp/test_databases/__init__.py b/pysus/tests/test_ftp/test_databases/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/pysus/tests/test_ftp/test_databases/test_CIHA.py b/pysus/tests/test_ftp/test_databases/test_CIHA.py deleted file mode 100644 index 50e5ba82..00000000 --- a/pysus/tests/test_ftp/test_databases/test_CIHA.py +++ /dev/null @@ -1,96 +0,0 @@ -# -*- coding:utf-8 -*- -""" -Created on 2023/12/12 -by luabida -license: GPL V3 or Later -""" -import datetime -import unittest -from unittest.mock import MagicMock, patch - -from pysus.ftp import File -from pysus.ftp.databases.ciha import CIHA - - -class TestCIHADatabase(unittest.TestCase): - def test_ciha(self): - mock_content = { - "CIHAAC1101.dbc": File( - path="/dissemin/publicos/CIHA/201101_/Dados/CIHAAC1101.dbc", - name="CIHAAC1101.dbc", - info={ - "size": 7803, - "type": "file", - "modify": datetime.datetime(2023, 10, 6, 10, 17), - }, - ), - "CIHAAC1102.dbc": File( - path="/dissemin/publicos/CIHA/201101_/Dados/CIHAAC1102.dbc", - name="CIHAAC1102.dbc", - info={ - "size": 9959, - "type": "file", - "modify": datetime.datetime(2023, 10, 6, 10, 17), - }, - ), - "CIHAAC1103.dbc": File( - path="/dissemin/publicos/CIHA/201101_/Dados/CIHAAC1103.dbc", - name="CIHAAC1103.dbc", - info={ - "size": 8308, - "type": "file", - "modify": datetime.datetime(2023, 10, 6, 10, 17), - }, - ), - } - - with patch( - "pysus.ftp.databases.ciha.CIHA", - return_value=MagicMock(__content__=mock_content), - ) as mock_ciha: - ciha = CIHA() - ciha.__content__ = mock_ciha().__content__ - - descriptions = [ciha.describe(file) for file in ciha.files] - expected_descriptions = [ - { - "name": "CIHAAC1101.dbc", - "group": "Comunicação de Internação Hospitalar e Ambulatorial", - "uf": "Acre", - "month": "Janeiro", - "year": 2011, - "size": "7.8 kB", - "last_update": "2023-10-06 10:17AM", - }, - { - "name": "CIHAAC1102.dbc", - "group": "Comunicação de Internação Hospitalar e Ambulatorial", - "uf": "Acre", - "month": "Fevereiro", - "year": 2011, - "size": "10.0 kB", - "last_update": "2023-10-06 10:17AM", - }, - { - "name": "CIHAAC1103.dbc", - "group": "Comunicação de Internação Hospitalar e Ambulatorial", - "uf": "Acre", - "month": "Março", - "year": 2011, - "size": "8.3 kB", - "last_update": "2023-10-06 10:17AM", - }, - ] - - self.assertEqual(descriptions, expected_descriptions) - - formats = [ciha.format(file) for file in ciha.files] - expected_formats = [ - ("CIHA", "AC", 2011, "01"), - ("CIHA", "AC", 2011, "02"), - ("CIHA", "AC", 2011, "03"), - ] - self.assertEqual(formats, expected_formats) - - get_files = ciha.get_files(uf="AC", year=2011, month=1) - self.assertEqual(get_files, [ciha.files[0]]) diff --git a/pysus/tests/test_ftp/test_databases/test_CNES.py b/pysus/tests/test_ftp/test_databases/test_CNES.py deleted file mode 100644 index c9f921c5..00000000 --- a/pysus/tests/test_ftp/test_databases/test_CNES.py +++ /dev/null @@ -1,96 +0,0 @@ -# -*- coding:utf-8 -*- -""" -Created on 2023/12/12 -by luabida -license: GPL V3 or Later -""" -import datetime -import unittest -from unittest.mock import MagicMock, patch - -from pysus.ftp import File -from pysus.ftp.databases.cnes import CNES - - -class TestCNESDatabase(unittest.TestCase): - def test_cnes(self): - mock_content = { - "STAC0508.dbc": File( - path="/dissemin/publicos/CNES/200508_/Dados/ST/STAC0508.dbc", - name="STAC0508.dbc", - info={ - "size": 18515, - "type": "file", - "modify": datetime.datetime(2014, 6, 5, 9, 30), - }, - ), - "STAC0509.dbc": File( - path="/dissemin/publicos/CNES/200508_/Dados/ST/STAC0509.dbc", - name="STAC0509.dbc", - info={ - "size": 18713, - "type": "file", - "modify": datetime.datetime(2014, 6, 5, 9, 30), - }, - ), - "STAC0510.dbc": File( - path="/dissemin/publicos/CNES/200508_/Dados/ST/STAC0510.dbc", - name="STAC0510.dbc", - info={ - "size": 17665, - "type": "file", - "modify": datetime.datetime(2014, 6, 5, 9, 30), - }, - ), - } - - with patch( - "pysus.ftp.databases.cnes.CNES", - return_value=MagicMock(__content__=mock_content), - ) as mock_cnes: - cnes = CNES().load("ST") - cnes.__content__ = mock_cnes().__content__ - - descriptions = [cnes.describe(file) for file in cnes.files] - expected_descriptions = [ - { - "name": "STAC0508.dbc", - "group": "Estabelecimentos", - "uf": "Acre", - "month": "Agosto", - "year": 2005, - "size": "18.5 kB", - "last_update": "2014-06-05 09:30AM", - }, - { - "name": "STAC0509.dbc", - "group": "Estabelecimentos", - "uf": "Acre", - "month": "Setembro", - "year": 2005, - "size": "18.7 kB", - "last_update": "2014-06-05 09:30AM", - }, - { - "name": "STAC0510.dbc", - "group": "Estabelecimentos", - "uf": "Acre", - "month": "Outubro", - "year": 2005, - "size": "17.7 kB", - "last_update": "2014-06-05 09:30AM", - }, - ] - - self.assertEqual(descriptions, expected_descriptions) - - formats = [cnes.format(file) for file in cnes.files] - expected_formats = [ - ("ST", "AC", 2005, "08"), - ("ST", "AC", 2005, "09"), - ("ST", "AC", 2005, "10"), - ] - self.assertEqual(formats, expected_formats) - - get_files = cnes.get_files(group="ST", uf="AC", year=2005, month=8) - self.assertEqual(get_files, [cnes.files[0]]) diff --git a/pysus/tests/test_ftp/test_databases/test_IBGEDATASUS.py b/pysus/tests/test_ftp/test_databases/test_IBGEDATASUS.py deleted file mode 100644 index 84586ffc..00000000 --- a/pysus/tests/test_ftp/test_databases/test_IBGEDATASUS.py +++ /dev/null @@ -1,90 +0,0 @@ -import unittest -from unittest.mock import MagicMock, patch - -from pysus.ftp.databases import ibge_datasus - - -class IBGEDATASUSTests(unittest.TestCase): - @patch("pysus.ftp.databases.ibge_datasus.File") - def test_describe_zip_file(self, mock_file): - mock_file.extension.upper.return_value = ".ZIP" - mock_file.name = "POPTBR12.zip" - mock_file.basename = "POPTBR12.zip" - mock_file.info = {"size": 100, "modify": "2022-01-01"} - - ibge = ibge_datasus.IBGEDATASUS() - result = ibge.describe(mock_file) - - self.assertEqual( - result, - { - "name": "POPTBR12.zip", - "year": 2012, - "size": 100, - "last_update": "2022-01-01", - }, - ) - - # @patch('pysus.ftp.databases.ibge_datasus.File') - # def describe_dbf_file(self, mock_file): - # mock_file.extension.upper.return_value = ".DBF" - # mock_file.name = "file20.dbf" - # mock_file.info = {"size": 100, "modify": "2022-01-01"} - # - # ibge = ibge_datasus.IBGEDATASUS() - # result = ibge.describe(mock_file) - # - # self.assertEqual(result, { - # "name": "file20", - # "year": "2020", - # "size": 100, - # "last_update": "2022-01-01" - # }) - - # @patch('pysus.ftp.databases.ibge_datasus.File') - # def describe_other_file(self, mock_file): - # mock_file.extension.upper.return_value = ".TXT" - # mock_file.name = "file20.txt" - # - # ibge = ibge_datasus.IBGEDATASUS() - # result = ibge.describe(mock_file) - # - # self.assertEqual(result, {}) - - @patch("pysus.ftp.databases.ibge_datasus.File") - def format_file(self, mock_file): - mock_file.name = "file20.zip" - - ibge = ibge_datasus.IBGEDATASUS() - result = ibge.format(mock_file) - - self.assertEqual(result, "20.zip") - - @patch("pysus.ftp.databases.ibge_datasus.File") - @patch("pysus.ftp.databases.ibge_datasus.to_list") - def test_get_files_with_year(self, mock_to_list, mock_file): - mock_file.extension.upper.return_value = ".ZIP" - mock_file.basename = "POPTBR12.zip" - mock_file.name = "POPTBR12" - mock_to_list.return_value = ["2012"] - - ibge = ibge_datasus.IBGEDATASUS() - ibge.__content__ = {"POPTBR12.zip": mock_file} - result = ibge.get_files(year="2012") - - self.assertEqual(result[0].name, mock_file.name) - - @patch("pysus.ftp.databases.ibge_datasus.File") - def get_files_without_year(self, mock_file): - mock_file.extension.upper.return_value = ".ZIP" - mock_file.name = "file20.zip" - - ibge = ibge_datasus.IBGEDATASUS() - ibge.files = [mock_file] - result = ibge.get_files() - - self.assertEqual(result, [mock_file]) - - -if __name__ == "__main__": - unittest.main() diff --git a/pysus/tests/test_ftp/test_databases/test_PNI.py b/pysus/tests/test_ftp/test_databases/test_PNI.py deleted file mode 100644 index 1441e9ff..00000000 --- a/pysus/tests/test_ftp/test_databases/test_PNI.py +++ /dev/null @@ -1,92 +0,0 @@ -# -*- coding:utf-8 -*- -""" -Created on 2023/12/12 -by luabida -license: GPL V3 or Later -""" -import datetime -import unittest -from unittest.mock import MagicMock, patch - -from pysus.ftp import File -from pysus.ftp.databases.pni import PNI - - -class TestPNIDatabase(unittest.TestCase): - def test_pni(self): - mock_content = { - "CPNIAC00.DBF": File( - path="/dissemin/publicos/PNI/DADOS/CPNIAC00.DBF", - name="CPNIAC00.DBF", - info={ - "size": 14843, - "type": "file", - "modify": datetime.datetime(2019, 5, 23, 17, 19), - }, - ), - "CPNIAC01.DBF": File( - path="/dissemin/publicos/PNI/DADOS/CPNIAC01.DBF", - name="CPNIAC01.DBF", - info={ - "size": 14843, - "type": "file", - "modify": datetime.datetime(2019, 5, 23, 16, 39), - }, - ), - "CPNIAC02.DBF": File( - path="/dissemin/publicos/PNI/DADOS/CPNIAC02.DBF", - name="CPNIAC02.DBF", - info={ - "size": 14843, - "type": "file", - "modify": datetime.datetime(2019, 5, 23, 16, 39), - }, - ), - } - - with patch( - "pysus.ftp.databases.pni.PNI", - return_value=MagicMock(__content__=mock_content), - ) as mock_pni: - pni = PNI() - pni.__content__ = mock_pni().__content__ - - descriptions = [pni.describe(file) for file in pni.files] - expected_descriptions = [ - { - "name": "CPNIAC00.DBF", - "group": "Cobertura Vacinal", - "uf": "Acre", - "year": 2000, - "size": "14.8 kB", - "last_update": "2019-05-23 05:19PM", - }, - { - "name": "CPNIAC01.DBF", - "group": "Cobertura Vacinal", - "uf": "Acre", - "year": 2001, - "size": "14.8 kB", - "last_update": "2019-05-23 04:39PM", - }, - { - "name": "CPNIAC02.DBF", - "group": "Cobertura Vacinal", - "uf": "Acre", - "year": 2002, - "size": "14.8 kB", - "last_update": "2019-05-23 04:39PM", - }, - ] - self.assertEqual(descriptions, expected_descriptions) - - formats = [pni.format(file) for file in pni.files] - expected_formats = [ - ("CPNI", "AC", 2000), - ("CPNI", "AC", 2001), - ("CPNI", "AC", 2002), - ] - self.assertEqual(formats, expected_formats) - - get_files = pni.get_files(group="CPNI", uf="AC", year=2000) - self.assertEqual(get_files, [pni.files[0]]) diff --git a/pysus/tests/test_ftp/test_databases/test_SIA.py b/pysus/tests/test_ftp/test_databases/test_SIA.py deleted file mode 100644 index acd5ab70..00000000 --- a/pysus/tests/test_ftp/test_databases/test_SIA.py +++ /dev/null @@ -1,96 +0,0 @@ -# -*- coding:utf-8 -*- -""" -Created on 2023/12/11 -by luabida -license: GPL V3 or Later -""" -import datetime -import unittest -from unittest.mock import MagicMock, patch - -from pysus.ftp import File -from pysus.ftp.databases.sia import SIA - - -class TestSIADatabase(unittest.TestCase): - def test_sia(self): - mock_content = { - "ABDF1112.dbc": File( - path="/dissemin/publicos/SIASUS/200801_/Dados/ABDF1112.dbc", - name="ABDF1112.dbc", - info={ - "size": 2971, - "type": "file", - "modify": datetime.datetime(2019, 3, 12, 12, 3), - }, - ), - "ABMG1112.dbc": File( - path="/dissemin/publicos/SIASUS/200801_/Dados/ABMG1112.dbc", - name="ABMG1112.dbc", - info={ - "size": 3183, - "type": "file", - "modify": datetime.datetime(2019, 3, 12, 12, 3), - }, - ), - "ABOAC1502.dbc": File( - path="/dissemin/publicos/SIASUS/200801_/Dados/ABOAC1502.dbc", - name="ABOAC1502.dbc", - info={ - "size": 3143, - "type": "file", - "modify": datetime.datetime(2016, 9, 12, 8, 45), - }, - ), - } - - with patch( - "pysus.ftp.databases.sia.SIA", - return_value=MagicMock(__content__=mock_content), - ) as mock_sia: - sia = SIA() - sia.__content__ = mock_sia().__content__ - - descriptions = [sia.describe(file) for file in sia.files] - expected_descriptions = [ - { - "name": "ABDF1112.dbc", - "group": "APAC de Cirurgia Bariátrica", - "uf": "Distrito Federal", - "month": "Dezembro", - "year": 2011, - "size": "3.0 kB", - "last_update": "2019-03-12 12:03PM", - }, - { - "name": "ABMG1112.dbc", - "group": "APAC de Cirurgia Bariátrica", - "uf": "Minas Gerais", - "month": "Dezembro", - "year": 2011, - "size": "3.2 kB", - "last_update": "2019-03-12 12:03PM", - }, - { - "name": "ABOAC1502.dbc", - "group": "APAC de Acompanhamento Pós Cirurgia Bariátrica", - "uf": "Acre", - "month": "Fevereiro", - "year": 2015, - "size": "3.1 kB", - "last_update": "2016-09-12 08:45AM", - }, - ] - - self.assertEqual(descriptions, expected_descriptions) - - formats = [sia.format(file) for file in sia.files] - expected_formats = [ - ("AB", "DF", 2011, "12"), - ("AB", "MG", 2011, "12"), - ("ABO", "AC", 2015, "02"), - ] - self.assertEqual(formats, expected_formats) - - get_files = sia.get_files(group="AB", uf="DF", year=2011, month=12) - self.assertEqual(get_files, [sia.files[0]]) diff --git a/pysus/tests/test_ftp/test_databases/test_SIH.py b/pysus/tests/test_ftp/test_databases/test_SIH.py deleted file mode 100644 index 045dcaa4..00000000 --- a/pysus/tests/test_ftp/test_databases/test_SIH.py +++ /dev/null @@ -1,96 +0,0 @@ -# -*- coding:utf-8 -*- -""" -Created on 2023/12/11 -by luabida -license: GPL V3 or Later -""" -import datetime -import unittest -from unittest.mock import MagicMock, patch - -from pysus.ftp import File -from pysus.ftp.databases.sih import SIH - - -class TestSIHDatabase(unittest.TestCase): - def test_sih(self): - mock_content = { - "CHBR1901.dbc": File( - path="/dissemin/publicos/SIHSUS/200801_/Dados/CHBR1901.dbc", - name="CHBR1901.dbc", - info={ - "size": 196476, - "type": "file", - "modify": datetime.datetime(2020, 3, 10, 14, 43), - }, - ), - "CHBR1902.dbc": File( - path="/dissemin/publicos/SIHSUS/200801_/Dados/CHBR1902.dbc", - name="CHBR1902.dbc", - info={ - "size": 196287, - "type": "file", - "modify": datetime.datetime(2020, 3, 10, 14, 43), - }, - ), - "CHBR1903.dbc": File( - path="/dissemin/publicos/SIHSUS/200801_/Dados/CHBR1903.dbc", - name="CHBR1903.dbc", - info={ - "size": 196081, - "type": "file", - "modify": datetime.datetime(2020, 3, 10, 14, 43), - }, - ), - } - - with patch( - "pysus.ftp.databases.sih.SIH", - return_value=MagicMock(__content__=mock_content), - ) as mock_sih: - sih = SIH() - sih.__content__ = mock_sih().__content__ - - descriptions = [sih.describe(file) for file in sih.files] - expected_descriptions = [ - { - "name": "CHBR1901.dbc", - "group": "Cadastro Hospitalar", - "uf": "Brasil", - "month": "Janeiro", - "year": 2019, - "size": "196.5 kB", - "last_update": "2020-03-10 02:43PM", - }, - { - "name": "CHBR1902.dbc", - "group": "Cadastro Hospitalar", - "uf": "Brasil", - "month": "Fevereiro", - "year": 2019, - "size": "196.3 kB", - "last_update": "2020-03-10 02:43PM", - }, - { - "name": "CHBR1903.dbc", - "group": "Cadastro Hospitalar", - "uf": "Brasil", - "month": "Março", - "year": 2019, - "size": "196.1 kB", - "last_update": "2020-03-10 02:43PM", - }, - ] - - self.assertEqual(descriptions, expected_descriptions) - - formats = [sih.format(file) for file in sih.files] - expected_formats = [ - ("CH", "BR", 2019, "01"), - ("CH", "BR", 2019, "02"), - ("CH", "BR", 2019, "03"), - ] - self.assertEqual(formats, expected_formats) - - get_files = sih.get_files(group="CH", uf="BR", year=2019, month=1) - self.assertEqual(get_files, [sih.files[0]]) diff --git a/pysus/tests/test_ftp/test_databases/test_SIM.py b/pysus/tests/test_ftp/test_databases/test_SIM.py deleted file mode 100644 index 71c0cfa2..00000000 --- a/pysus/tests/test_ftp/test_databases/test_SIM.py +++ /dev/null @@ -1,101 +0,0 @@ -# -*- coding:utf-8 -*- -""" -Created on 2023/12/11 -by luabida -license: GPL V3 or Later -""" -import unittest -from datetime import datetime -from unittest.mock import MagicMock, patch - -from pysus.ftp import File -from pysus.ftp.databases.sim import SIM - - -class TestSIMDatabase(unittest.TestCase): - def test_sim(self): - date_format = "%Y-%m-%d %I:%M%p" - - mock_content = { - "DOAC1996.dbc": File( - path="/dissemin/publicos/SIM/CID10/DORES/DOAC1996.dbc", - name="DOAC1996.dbc", - info={ - "size": 78054.4, - "type": "DBC file", - "modify": datetime.strptime( - "2020-01-31 02:48PM", date_format - ), - }, - ), - "DOAC1997.dbc": File( - path="/dissemin/publicos/SIM/CID10/DORES/DOAC1997.dbc", - name="DOAC1997.dbc", - info={ - "size": 79084.8, - "type": "DBC file", - "modify": datetime.strptime( - "2020-01-31 02:48PM", date_format - ), - }, - ), - "DOAC1998.dbc": File( - path="/dissemin/publicos/SIM/CID10/DORES/DOAC1998.dbc", - name="DOAC1998.dbc", - info={ - "size": 79084.8, - "type": "DBC file", - "modify": datetime.strptime( - "2020-01-31 02:48PM", date_format - ), - }, - ), - } - - with patch( - "pysus.ftp.databases.sim.SIM", - return_value=MagicMock(__content__=mock_content), - ) as mock_sim: - sim = SIM() - sim.__content__ = mock_sim().__content__ - - descriptions = [sim.describe(file) for file in sim.files] - expected_descriptions = [ - { - "name": "DOAC1996.dbc", - "uf": "Acre", - "year": 1996, - "group": "CID10", - "size": "78.1 kB", - "last_update": "2020-01-31 02:48PM", - }, - { - "name": "DOAC1997.dbc", - "uf": "Acre", - "year": 1997, - "group": "CID10", - "size": "79.1 kB", - "last_update": "2020-01-31 02:48PM", - }, - { - "name": "DOAC1998.dbc", - "uf": "Acre", - "year": 1998, - "group": "CID10", - "size": "79.1 kB", - "last_update": "2020-01-31 02:48PM", - }, - ] - - self.assertEqual(descriptions, expected_descriptions) - - formats = [sim.format(file) for file in sim.files] - expected_formats = [ - ("DO", "AC", 1996), - ("DO", "AC", 1997), - ("DO", "AC", 1998), - ] - self.assertEqual(formats, expected_formats) - - get_files = sim.get_files(group="CID10", uf="AC", year="1996") - self.assertEqual(get_files, [sim.files[0]]) diff --git a/pysus/tests/test_ftp/test_databases/test_SINAN.py b/pysus/tests/test_ftp/test_databases/test_SINAN.py deleted file mode 100644 index a3819b39..00000000 --- a/pysus/tests/test_ftp/test_databases/test_SINAN.py +++ /dev/null @@ -1,86 +0,0 @@ -# -*- coding:utf-8 -*- -""" -Created on 2023/12/12 -by luabida -license: GPL V3 or Later -""" -import datetime -import unittest -from unittest.mock import MagicMock, patch - -from pysus.ftp import File -from pysus.ftp.databases.sinan import SINAN - - -class TestSINANDatabase(unittest.TestCase): - def test_sinan(self): - mock_content = { - "ACBIBR06.dbc": File( - path="/dissemin/publicos/SINAN/DADOS/FINAIS/ACBIBR06.dbc", - name="ACBIBR06.dbc", - info={ - "size": 28326, - "type": "file", - "modify": datetime.datetime(2023, 1, 16, 14, 15), - }, - ), - "ACBIBR07.dbc": File( - path="/dissemin/publicos/SINAN/DADOS/FINAIS/ACBIBR07.dbc", - name="ACBIBR07.dbc", - info={ - "size": 673314, - "type": "file", - "modify": datetime.datetime(2023, 1, 16, 14, 15), - }, - ), - "ACBIBR08.dbc": File( - path="/dissemin/publicos/SINAN/DADOS/FINAIS/ACBIBR08.dbc", - name="ACBIBR08.dbc", - info={ - "size": 1048406, - "type": "file", - "modify": datetime.datetime(2023, 1, 16, 14, 15), - }, - ), - } - - with patch( - "pysus.ftp.databases.sinan.SINAN", - return_value=MagicMock(__content__=mock_content), - ) as mock_sinan: - sinan = SINAN() - sinan.__content__ = mock_sinan().__content__ - - descriptions = [sinan.describe(file) for file in sinan.files] - expected_descriptions = [ - { - "name": "ACBIBR06.dbc", - "disease": "Acidente de trabalho com material biológico", - "year": 2006, - "size": "28.3 kB", - "last_update": "2023-01-16 02:15PM", - }, - { - "name": "ACBIBR07.dbc", - "disease": "Acidente de trabalho com material biológico", - "year": 2007, - "size": "673.3 kB", - "last_update": "2023-01-16 02:15PM", - }, - { - "name": "ACBIBR08.dbc", - "disease": "Acidente de trabalho com material biológico", - "year": 2008, - "size": "1.0 MB", - "last_update": "2023-01-16 02:15PM", - }, - ] - - self.assertEqual(descriptions, expected_descriptions) - - formats = [sinan.format(file) for file in sinan.files] - expected_formats = [("ACBI", 2006), ("ACBI", 2007), ("ACBI", 2008)] - self.assertEqual(formats, expected_formats) - - get_files = sinan.get_files(dis_code="ACBI", year=2006) - self.assertEqual(get_files, [sinan.files[0]]) diff --git a/pysus/tests/test_ftp/test_databases/test_SINASC.py b/pysus/tests/test_ftp/test_databases/test_SINASC.py deleted file mode 100644 index 727122f5..00000000 --- a/pysus/tests/test_ftp/test_databases/test_SINASC.py +++ /dev/null @@ -1,93 +0,0 @@ -# -*- coding:utf-8 -*- -""" -Created on 2023/12/12 -by luabida -license: GPL V3 or Later -""" -import datetime -import unittest -from unittest.mock import MagicMock, patch - -from pysus.ftp import File -from pysus.ftp.databases.sinasc import SINASC - - -class TestSINASCDatabase(unittest.TestCase): - def test_sinasc(self): - mock_content = { - "DNAC1996.DBC": File( - path="/dissemin/publicos/SINASC/NOV/DNRES/DNAC1996.DBC", - name="DNAC1996.DBC", - info={ - "size": 247527, - "type": "file", - "modify": datetime.datetime(2020, 1, 27, 12, 5), - }, - ), - "DNAC1997.DBC": File( - path="/dissemin/publicos/SINASC/NOV/DNRES/DNAC1997.DBC", - name="DNAC1997.DBC", - info={ - "size": 266815, - "type": "file", - "modify": datetime.datetime(2020, 1, 27, 12, 5), - }, - ), - "DNAC1998.DBC": File( - path="/dissemin/publicos/SINASC/NOV/DNRES/DNAC1998.DBC", - name="DNAC1998.DBC", - info={ - "size": 242404, - "type": "file", - "modify": datetime.datetime(2020, 1, 27, 12, 5), - }, - ), - } - - with patch( - "pysus.ftp.databases.sinasc.SINASC", - return_value=MagicMock(__content__=mock_content), - ) as mock_sinasc: - sinasc = SINASC() - sinasc.__content__ = mock_sinasc().__content__ - - descriptions = [sinasc.describe(file) for file in sinasc.files] - expected_descriptions = [ - { - "name": "DNAC1996.DBC", - "group": "Declarações de Nascidos Vivos", - "uf": "Acre", - "year": 1996, - "size": "247.5 kB", - "last_update": "2020-01-27 12:05PM", - }, - { - "name": "DNAC1997.DBC", - "group": "Declarações de Nascidos Vivos", - "uf": "Acre", - "year": 1997, - "size": "266.8 kB", - "last_update": "2020-01-27 12:05PM", - }, - { - "name": "DNAC1998.DBC", - "group": "Declarações de Nascidos Vivos", - "uf": "Acre", - "year": 1998, - "size": "242.4 kB", - "last_update": "2020-01-27 12:05PM", - }, - ] - - self.assertEqual(descriptions, expected_descriptions) - - formats = [sinasc.format(file) for file in sinasc.files] - expected_formats = [ - ("DN", "AC", 1996), - ("DN", "AC", 1997), - ("DN", "AC", 1998), - ] - self.assertEqual(formats, expected_formats) - - get_files = sinasc.get_files(group="DN", uf="AC", year=1996) - self.assertEqual(get_files, [sinasc.files[0]]) diff --git a/pysus/tests/test_ftp/test_databases/test_territory.py b/pysus/tests/test_ftp/test_databases/test_territory.py deleted file mode 100644 index d64494c5..00000000 --- a/pysus/tests/test_ftp/test_databases/test_territory.py +++ /dev/null @@ -1,12 +0,0 @@ -import unittest - -from pysus.ftp.databases.territory import Territory -from pysus.online_data import territory - - -class TestTerritory(unittest.TestCase): - def test_load_database(self): - T = Territory().load() - - def test_download(self): - territory.download("todos_mapas_2013.zip")