From 664828bc45cacf134898274c8e1a227a76815db8 Mon Sep 17 00:00:00 2001 From: Sandro Loch Date: Tue, 5 Dec 2023 10:46:08 -0300 Subject: [PATCH] Create tests_data_sources for unnittests --- .github/workflows/python-package.yml | 4 +++ pysus/online_data/__init__.py | 17 ++++++------- pysus/tests/test_init.py | 38 ++++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 3b4ab88c..941674d5 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -57,6 +57,10 @@ jobs: run: | make run-jupyter-pysus + - name: Test unnittest + run: | + pytest -vv pysus/tests/test_init.py + - name: Test with pytest run: | make conda-install-geo diff --git a/pysus/online_data/__init__.py b/pysus/online_data/__init__.py index c4109c14..00a50dbd 100644 --- a/pysus/online_data/__init__.py +++ b/pysus/online_data/__init__.py @@ -56,7 +56,7 @@ def FTP_datasus(): - ftp = FTP("192.168.0.1") + ftp = FTP("ftp.datasus.gov.br") ftp.login() return ftp @@ -151,9 +151,7 @@ def list_data_sources() -> str: """ databases_directory = ( - Path(__file__).resolve(strict=True).parent.parent - / "ftp" - / "databasess" + Path(__file__).resolve(strict=True).parent.parent / "ftp" / "databases" ) if databases_directory.exists(): @@ -162,7 +160,7 @@ def list_data_sources() -> str: for file in databases_directory.glob("*.py") if file.name != "__init__.py" ] - + # breakpoint() return f"""Currently, the supported databases are: { ', '.join(supported_databases)}""" else: @@ -176,7 +174,8 @@ def list_data_sources() -> str: "CNES", "CIHA", ] - return f"""No support for the databases of DATASUS was found." + # breakpoint() + return f"""No support for the databases was found." "Expected databases for implementation are: { ', '.join(expected_databases)}""" @@ -201,7 +200,7 @@ class FTP_Inspect: database: str _ds_paths: list - ftp_server: FTP = FTP("192.168.0.1") + ftp_server: FTP = FTP("ftp.datasus.gov.br") available_dbs: list = list(DB_PATHS.keys()) def __init__(self, database: str) -> None: @@ -358,7 +357,7 @@ def list_all( chunks, to preserve memory, that are read using pandas and pyarrow. """ available_dbs = list() - ftp = FTP("192.168.0.1") + ftp = FTP("ftp.datasus.gov.br") ftp.login() for path in self._ds_paths: try: @@ -587,7 +586,7 @@ def _extract_dbc(self, DBC_path: str, local_dir: str = cache_dir) -> str: if Path(filepath).exists(): return str(filepath) try: - ftp = ftp = FTP("192.168.0.1") + ftp = ftp = FTP("ftp.datasus.gov.br") ftp.login() ftp.cwd(filedir) ftp.retrbinary( diff --git a/pysus/tests/test_init.py b/pysus/tests/test_init.py index 83b7fcc1..0cea89e9 100644 --- a/pysus/tests/test_init.py +++ b/pysus/tests/test_init.py @@ -1,9 +1,10 @@ import unittest -import pytest +from unittest.mock import patch +import numpy as np import pandas as pd -from numpy import dtype -from pysus.online_data import FTP_Inspect +import pytest +from pysus import online_data class TestInitFunctions(unittest.TestCase): @@ -20,12 +21,39 @@ def test_last_update(self): "CNES", "CIHA", ]: - df = FTP_Inspect(db).last_update_df() + df = online_data.FTP_Inspect(db).last_update_df() self.assertIsInstance(df, pd.DataFrame) self.assertGreater(df.size, 0) self.assertIn("folder", df.columns) self.assertIsInstance(df["date"][0], pd.Timestamp) - self.assertEqual(df.file_size.dtype, dtype("int64")) + self.assertEqual(df.file_size.dtype, np.dtype("int64")) + + +class TestListDataSources(unittest.TestCase): + @patch("pysus.online_data.Path.exists") + def test_list_data_sources_exists(self, mock_exists): + dbs = "SIM, SIA, SINAN, SINASC, SIH, CNES" + mock_exists.return_value = True + expected_output = f"""Currently, the supported databases are: {dbs}""" + self.assertEqual(online_data.list_data_sources(), expected_output) + + @patch("pysus.online_data.Path.exists") + def test_list_data_sources_not_exists(self, mock_exists): + mock_exists.return_value = False + expected_databases = [ + "SINAN", + "SIM", + "SINASC", + "SIH", + "SIA", + "PNI", + "CNES", + "CIHA", + ] + expected_output = f"""No support for the databases was found." + "Expected databases for implementation are: { + ', '.join(expected_databases)}""" + self.assertEqual(online_data.list_data_sources(), expected_output) if __name__ == "__main__":