Skip to content

Commit

Permalink
Add tests for vaccination report
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitor Magán committed Jan 5, 2021
1 parent 1d30299 commit dd1f019
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions helpers/vaccination_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
class SpainVaccinationReport:

def __init__(self):
self.__url = "https://www.mscbs.gob.es/profesionales/saludPublica/ccayes/alertasActual/nCov/documentos/Informe_Comunicacion.ods"
self._url = "https://www.mscbs.gob.es/profesionales/saludPublica/ccayes/alertasActual/nCov/documentos/Informe_Comunicacion.ods"

def get_vaccination_by_ccaa(self):
res = requests.get(self.__url)
res = requests.get(self._url)
result = {}

with NamedTemporaryFile(suffix=".ods") as temp_file:
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/helpers/test_vaccination_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import unittest
from unittest.mock import patch
from helpers.vaccination_report import SpainVaccinationReport


class SpainVaccinationReportUnitTest(unittest.TestCase):

@patch("helpers.vaccination_report.requests")
@patch("helpers.vaccination_report.NamedTemporaryFile")
@patch("helpers.vaccination_report.get_data")
def test_given_ods_when_get_vaccination_by_ccaa_then_data_returned(self, get_data_mock,
named_temporary_file_mock,
requests_mock):

get_data_mock.return_value = {'Hoja3': [
['', 'Dosis entregadas (1)', 'Dosis administradas (2)', '% sobre entregadas', 'Última actualización de datos'],
['Andalucía', 140295, 25809],
['Aragón', 23715, 2004],
['Asturias ', 23720, 9380],
['Baleares', 8940, 153],
['Canarias', 20835, 4846],
['Cantabria', 11850, 304],
['Castilla y Leon ', 51390, 10928,],
['Castilla La Mancha', 35550, 1983],
['Cataluña', 120545, 8150],
['C. Valenciana', 61225, 3252],
['Extremadura', 21725, 686],
['Galicia', 37555, 9124],
['La Rioja', 5915, 324],
['Madrid', 89040, 2883],
['Murcia ', 25685, 442],
['Navarra', 6975, 1583],
['País Vasco', 31605, 396],
['Ceuta', 1005, 380],
['Melilla', 1005, 207],
['Totales', 718575, 82834],
[]]
}

report = SpainVaccinationReport()
result = report.get_vaccination_by_ccaa()
self.assertEqual({'Andalucía': 25809,
'Aragón': 2004,
'Asturias': 9380,
'Baleares': 153,
'Canarias': 4846,
'Cantabria': 304,
'Castilla y León': 10928,
'Castilla La Mancha': 1983,
'Cataluña': 8150,
'C. Valenciana': 3252,
'Extremadura': 686,
'Galicia': 9124,
'La Rioja': 324,
'Madrid': 2883,
'Murcia': 442,
'Navarra': 1583,
'País Vasco': 396,
'Ceuta': 380,
'Melilla': 207},
result)

requests_mock.get.assert_called_once_with(report._url)
named_temporary_file_mock.assert_called_once_with(suffix=".ods")

with named_temporary_file_mock.return_value as temp_file:
temp_file.write.assert_called_once_with(requests_mock.get.return_value.content)
temp_file.flush.assert_called_once_with()

0 comments on commit dd1f019

Please sign in to comment.