Skip to content

Commit

Permalink
Create tests_data_sources for unnittests
Browse files Browse the repository at this point in the history
  • Loading branch information
esloch committed Dec 5, 2023
1 parent 9382b9a commit 664828b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions pysus/online_data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@


def FTP_datasus():
ftp = FTP("192.168.0.1")
ftp = FTP("ftp.datasus.gov.br")
ftp.login()
return ftp

Expand Down Expand Up @@ -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():
Expand All @@ -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:
Expand All @@ -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)}"""

Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
38 changes: 33 additions & 5 deletions pysus/tests/test_init.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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__":
Expand Down

0 comments on commit 664828b

Please sign in to comment.