Skip to content

Commit

Permalink
Mock SIH tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luabida committed Dec 11, 2023
1 parent b6992c5 commit 8091488
Showing 1 changed file with 92 additions and 33 deletions.
125 changes: 92 additions & 33 deletions pysus/tests/test_sih.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,93 @@
# -*- coding:utf-8 -*-
u"""
Created on 2023/12/11
by luabida
license: GPL V3 or Later
"""
from unittest.mock import patch, MagicMock
import unittest
import pytest

from pysus.online_data.SIH import download
from pysus.online_data import parquets_to_dataframe as to_df

class SIHTestCase(unittest.TestCase):
@pytest.mark.skip(reason="This test takes too long")
@pytest.mark.timeout(5)
def test_download_pre_2008(self):
df = to_df(download("AC", 2006, 12))
assert not df.empty

@pytest.mark.skip(reason="This test takes too long")
@pytest.mark.timeout(5)
def test_download_2008(self):
df = to_df(download("SE", 2008, 6))
assert not df.empty

@pytest.mark.skip(reason="This test takes too long")
@pytest.mark.timeout(5)
def test_download_2010(self):
df = to_df(download("SE", 2010, 6))
assert not df.empty

@pytest.mark.skip(reason="This test takes too long")
@pytest.mark.timeout(5)
def test_download_2019(self):
df = to_df(download("SE", 2019, 6))
assert not df.empty


if __name__ == "__main__":
unittest.main()
from datetime import datetime

from pysus.ftp.databases.sih import SIH
from pysus.ftp import File


class TestSIHDatabase(unittest.TestCase):

def test_sim(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)
}
),
"DOAC1997.dbc": File(
path="/dissemin/publicos/SIHSUS/200801_/Dados/CHBR1902.dbc",
name="DOAC1997.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]])

0 comments on commit 8091488

Please sign in to comment.