Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitor Magán committed May 23, 2021
1 parent 0b9a093 commit d3888bf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 97 deletions.
2 changes: 1 addition & 1 deletion main_vaccination.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def publish_report(today):
last_tweet = twitter.publish_tweet_with_media(spain_tweet, graph_url)
ccaa_tweets = []

for ccaa in filter(lambda x: x in CCAA_POPULATION.keys(), accumulated_vaccinations.keys()):
for ccaa in filter(lambda x: x in CCAA_POPULATION.keys(), sorted(accumulated_vaccinations.keys())):
ccaa_tweet = get_vaccination_report(ccaa, accumulated_vaccinations, today_vaccinations,
accumulated_completed_vaccinations,
today_completed_vaccinations,
Expand Down
86 changes: 10 additions & 76 deletions tests/unit/helpers/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest.mock import patch, call, MagicMock
from helpers.reports import get_tendency_emoji, get_report_sentence, get_report_by_ccaa, get_graph_url, \
get_global_report, get_global_data, get_territorial_unit_report, get_report_sentence_with_unit, \
calculate_global_incidence, get_vaccination_sentence, get_vaccination_report, get_spain_vaccination_report, \
calculate_global_incidence, get_vaccination_sentence, get_vaccination_report, \
get_completed_vaccination_sentence
from helpers.db import Measurement
from helpers.spain_geography import CCAA_POPULATION
Expand Down Expand Up @@ -426,87 +426,21 @@ def test_given_start_end_and_vars_when_get_graph_url_then_from_to_and_vars_inclu
f"&var-{var1_name}={var1_value}&var-{var2_name}={var2_value}",
get_graph_url(date1, date2, {var1_name: var1_value, var2_name: var2_value}))

@patch("helpers.reports.get_vaccination_sentence")
def test_given_percentage_False_when_get_vaccination_report_then_get_vaccination_called(self,
get_vaccination_sentence_mock):
ccaa1 = "Madrid"
ccaa2 = "Castilla La Mancha"
accumulated1 = 5000
accumulated2 = 2000
today_data1 = 200
today_data2 = 300

accumulated_data = {
ccaa1: accumulated1,
ccaa2: accumulated2
}

today_data = {
ccaa1: today_data1,
ccaa2: today_data2
}

sentence1 = "sentence1"
sentence2 = "sentence2"
get_vaccination_sentence_mock.side_effect = [sentence1, sentence2]

sentences = get_vaccination_report(accumulated_data, today_data, False)

get_vaccination_sentence_mock.assert_has_calls([
call(ccaa1, accumulated1, today_data1),
call(ccaa2, accumulated2, today_data2)
])

self.assertEqual([sentence1, sentence2], sentences)

@patch("helpers.reports.get_completed_vaccination_sentence")
def test_given_percentage_True_when_get_vaccination_report_then_get_completed_vaccination_called(self,
get_completed_vaccination_sentence_mock):
ccaa1 = "Madrid"
ccaa2 = "Castilla La Mancha"
accumulated1 = 5000
accumulated2 = 2000
today_data1 = 200
today_data2 = 300

accumulated_data = {
ccaa1: accumulated1,
ccaa2: accumulated2
}

today_data = {
ccaa1: today_data1,
ccaa2: today_data2
}

sentence1 = "sentence1"
sentence2 = "sentence2"
get_completed_vaccination_sentence_mock.side_effect = [sentence1, sentence2]

sentences = get_vaccination_report(accumulated_data, today_data, True)

get_completed_vaccination_sentence_mock.assert_has_calls([
call(ccaa1, accumulated1, today_data1),
call(ccaa2, accumulated2, today_data2)
])

self.assertEqual([sentence1, sentence2], sentences)

@patch("helpers.reports.CCAA_POPULATION", {"Madrid": 8000000, "Cataluña": 10000000})
def test_given_existing_ccaa_when_get_vaccination_sentence_then_ccaa_population_used(self):
self.assertEqual("- Madrid: 2.000 🔺500", get_vaccination_sentence("Madrid", 2000, 500))
self.assertEqual("- Dosis: 2.000 🔺500", get_vaccination_sentence("Dosis", 2000, 500))

@patch("helpers.reports.CCAA_POPULATION", {"Madrid": 8000000, "Cataluña": 10000000})
def test_given_non_existing_ccaa_when_get_vaccination_sentence_then_whole_population_used(self):
self.assertEqual("- España: 2.000 🔺700", get_vaccination_sentence("España", 2000, 700))
self.assertEqual("- Dosis: 2.000 🔺700", get_vaccination_sentence("Dosis", 2000, 700))

@patch("helpers.reports.CCAA_POPULATION", {"Madrid": 8000000, "Cataluña": 10000000})
def test_given_existing_ccaa_when_get_completed_vaccination_sentence_then_ccaa_population_used(self):
self.assertEqual("- Madrid: 2.000 (0,03%) 🔺500", get_completed_vaccination_sentence("Madrid", 2000, 500))
self.assertEqual("- Dosis: 2.000 (0,03%) 🔺500", get_completed_vaccination_sentence("Madrid", "Dosis", 2000, 500))

@patch("helpers.reports.CCAA_POPULATION", {"Madrid": 8000000, "Cataluña": 10000000})
def test_given_non_existing_ccaa_when_get_completed_vaccination_sentence_then_whole_population_used(self):
self.assertEqual("- España: 2.000 (0,01%) 🔺700", get_completed_vaccination_sentence("España", 2000, 700))
self.assertEqual("- Dosis: 2.000 (0,01%) 🔺700", get_completed_vaccination_sentence("España", "Dosis", 2000, 700))

@patch("helpers.reports.get_vaccination_sentence")
@patch("helpers.reports.get_completed_vaccination_sentence")
Expand Down Expand Up @@ -549,15 +483,15 @@ def test_when_get_spain_vaccination_report_then_data_aggregated(self, get_comple
get_vaccination_sentence_mock.return_value = sentence1
get_completed_vaccination_sentence_mock.side_effect = [sentence2, sentence3]

sentence = get_spain_vaccination_report(accumulated_doses_data, today_doses_data,
accumulated_completed_data, today_completed_data,
accumulated_first_dose_data, today_first_dose_data)
sentence = get_vaccination_report("España", accumulated_doses_data, today_doses_data,
accumulated_completed_data, today_completed_data,
accumulated_first_dose_data, today_first_dose_data)

get_vaccination_sentence_mock.assert_called_once_with("Dosis", accumulated_doses,
today_doses)
get_completed_vaccination_sentence_mock.assert_has_calls([call("Personas 1 dosis", accumulated_first_dose,
get_completed_vaccination_sentence_mock.assert_has_calls([call("España", "Personas 1 dosis", accumulated_first_dose,
today_first_dose),
call("Pautas completas", accumulated_completed,
call("España", "Pautas completas", accumulated_completed,
today_completed)])

self.assertEqual(sentence1 + "\n" + sentence2 + "\n" + sentence3, sentence)
42 changes: 22 additions & 20 deletions tests/unit/test_main_vaccination.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,29 +140,27 @@ def test_given_column_not_in_columns_when_get_column_index_then_exception_risen(
@patch("main_vaccination.influx")
@patch("main_vaccination.twitter")
@patch("main_vaccination.get_vaccination_report")
@patch("main_vaccination.get_spain_vaccination_report")
@patch("main_vaccination.get_graph_url")
def test_when_publish_report_then_twitter_called(self, get_graph_url_mock, get_spain_vaccination_report_mock,
get_vaccination_report_mock, twitter_mock, influx_mock):
def test_when_publish_report_then_twitter_called(self, get_graph_url_mock, get_vaccination_report_mock,
twitter_mock, influx_mock):

date_str = "04/05/2006"
today = MagicMock()
today.strftime.return_value = date_str
vaccinations = MagicMock()
completed_vaccinations = MagicMock()
first_doses = MagicMock()
accumulated_vaccinations = MagicMock()
accumulated_vaccinations = {"Madrid": 123, "Aragón": 456}
accumulated_completed_vaccinations = MagicMock()
accumulated_first_doses = MagicMock()
influx_mock.get_stat_group_by_day.side_effect = [vaccinations, completed_vaccinations, first_doses]
influx_mock.get_stat_accumulated_until_day.side_effect = [accumulated_vaccinations,
accumulated_completed_vaccinations,
accumulated_first_doses]
sentence1 = MagicMock()
sentence2 = MagicMock()
spain_sentence = "spain_doses_and_completed"
get_vaccination_report_mock.side_effect = [[sentence1], [sentence2]]
get_spain_vaccination_report_mock.return_value = spain_sentence
ccaa1_sentence = "ccaa1_sentence"
ccaa2_sentence = "ccaa2_sentence"
get_vaccination_report_mock.side_effect = [spain_sentence, ccaa1_sentence, ccaa2_sentence]
last_tweet = MagicMock()
first_tweet = MagicMock()
twitter_mock.publish_sentences_in_tweets.return_value = last_tweet
Expand All @@ -174,19 +172,23 @@ def test_when_publish_report_then_twitter_called(self, get_graph_url_mock, get_s
call(Measurement.COMPLETED_VACCINATIONS, today)])
influx_mock.get_stat_accumulated_until_day.assert_has_calls([call(Measurement.VACCINATIONS, today),
call(Measurement.COMPLETED_VACCINATIONS, today)])
get_vaccination_report_mock.assert_has_calls([call(accumulated_vaccinations, vaccinations, False),
call(accumulated_completed_vaccinations, completed_vaccinations, True)])
twitter_mock.publish_sentences_in_tweets.assert_has_calls([call([sentence1], f"💉 Total Dosis a {date_str}",
last_tweet=first_tweet),
call([sentence2],
f"💉 Total Pautas Completas a {date_str}",
last_tweet=last_tweet)])
twitter_mock.publish_tweets.assert_called_once_with([f"Aragón - Vacunación a {date_str}:\n\n{ccaa1_sentence}",
f"Madrid - Vacunación a {date_str}:\n\n{ccaa2_sentence}"],
twitter_mock.publish_tweet_with_media.return_value)
get_graph_url_mock.assert_called_once_with(datetime(2021, 1, 1), today, graph_path=VACCINE_IMAGE_PATH)
get_spain_vaccination_report_mock.assert_called_once_with(accumulated_vaccinations, vaccinations,
accumulated_completed_vaccinations,
completed_vaccinations, accumulated_first_doses,
first_doses)
twitter_mock.publish_tweet_with_media.assert_called_once_with(f"🇪🇸 España - Estado vacunación a {date_str}:"
get_vaccination_report_mock.assert_has_calls([call("España", accumulated_vaccinations, vaccinations,
accumulated_completed_vaccinations,
completed_vaccinations, accumulated_first_doses,
first_doses),
call("Aragón", accumulated_vaccinations, vaccinations,
accumulated_completed_vaccinations,
completed_vaccinations, accumulated_first_doses,
first_doses),
call("Madrid", accumulated_vaccinations, vaccinations,
accumulated_completed_vaccinations,
completed_vaccinations, accumulated_first_doses,
first_doses)])
twitter_mock.publish_tweet_with_media.assert_called_once_with(f"🇪🇸 España - Vacunación a {date_str}:"
f"\n\n{spain_sentence}\n\n➡️ Gráfico "
f"Interactivo: https://home.aitormagan.es/d/TeEplNgRk/covid-vacunas-espana?orgId=1",
get_graph_url_mock.return_value)
Expand Down

0 comments on commit d3888bf

Please sign in to comment.