-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
88 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +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 | ||
import datetime | ||
|
||
import pandas as pd | ||
from pysus.online_data.SIA import download | ||
from pysus.online_data import parquets_to_dataframe as to_df | ||
from pysus.ftp.databases.sia import SIA | ||
from pysus.ftp import File | ||
|
||
class SIATestCase(unittest.TestCase): | ||
@pytest.mark.skip(reason="This test takes too long") | ||
@pytest.mark.timeout(5) | ||
def test_download_large_PA(self): | ||
res = to_df(download('SP', 2020, 12, group='PA')) | ||
if isinstance(res, pd.DataFrame): | ||
assert not res.empty | ||
else: | ||
pass | ||
|
||
class TestSIADatabase(unittest.TestCase): | ||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
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]]) |