Skip to content

Commit

Permalink
Merge pull request #9 from aitormagan/feature/separate_tweets_vaccina…
Browse files Browse the repository at this point in the history
…tion

Separate tweets for vaccination
  • Loading branch information
aitormagan authored May 23, 2021
2 parents f7b6918 + d3888bf commit e00f38d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 135 deletions.
40 changes: 13 additions & 27 deletions helpers/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,29 @@
from helpers.spain_geography import CCAA_POPULATION, CCAA_ADMITTED_BEDS, CCAA_ICU_BEDS


def get_spain_vaccination_report(accumulated_doses_data, today_doses_data,
accumulated_completed_vaccination_data, today_completed_vaccination_data,
accumulated_first_dose_data, today_first_dose_data):
def get_vaccination_report(ccaa, accumulated_doses_data, today_doses_data,
accumulated_completed_vaccination_data, today_completed_vaccination_data,
accumulated_first_dose_data, today_first_dose_data):

tweet = get_vaccination_sentence("Dosis", accumulated_doses_data[SPAIN],
today_doses_data[SPAIN]) + "\n"
tweet += get_completed_vaccination_sentence("Personas 1 dosis", accumulated_first_dose_data[SPAIN],
today_first_dose_data[SPAIN]) + "\n"
tweet += get_completed_vaccination_sentence("Pautas completas", accumulated_completed_vaccination_data[SPAIN],
today_completed_vaccination_data[SPAIN])
tweet = get_vaccination_sentence("Dosis", accumulated_doses_data[ccaa],
today_doses_data[ccaa]) + "\n"
tweet += get_completed_vaccination_sentence(ccaa, "Personas 1 dosis", accumulated_first_dose_data[ccaa],
today_first_dose_data[ccaa]) + "\n"
tweet += get_completed_vaccination_sentence(ccaa, "Pautas completas", accumulated_completed_vaccination_data[ccaa],
today_completed_vaccination_data[ccaa])

return tweet


def get_vaccination_report(accumulated_data, today_data, percentage):
sentences = []
sentence_generator = {
True: get_completed_vaccination_sentence,
False: get_vaccination_sentence
}[percentage]
def get_vaccination_sentence(stat, accumulated, today_total):
return "- {0}: {1} 🔺{2}".format(stat, _format_number(accumulated), _format_number(today_total))

for ccaa in filter(lambda x: x in CCAA_POPULATION.keys(), accumulated_data.keys()):
sentences.append(sentence_generator(ccaa, accumulated_data[ccaa], today_data[ccaa]))

return sentences


def get_vaccination_sentence(territorial_unit, accumulated, today_total):
return "- {0}: {1} 🔺{2}".format(territorial_unit, _format_number(accumulated),
_format_number(today_total))


def get_completed_vaccination_sentence(territorial_unit, accumulated, today_total):
def get_completed_vaccination_sentence(territorial_unit, stat, accumulated, today_total):
population = CCAA_POPULATION[territorial_unit] if territorial_unit in CCAA_POPULATION \
else sum(CCAA_POPULATION.values())
percentage_population = accumulated / population * 100
return "- {0}: {1} ({2}%) 🔺{3}".format(territorial_unit, _format_number(accumulated),
return "- {0}: {1} ({2}%) 🔺{3}".format(stat, _format_number(accumulated),
_format_number(percentage_population),
_format_number(today_total))

Expand Down
25 changes: 13 additions & 12 deletions main_vaccination.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from helpers.db import Influx, Measurement
from helpers.ministry_report import VaccinesMinistryReport
from main_daily import update_stat
from helpers.reports import get_vaccination_report, get_spain_vaccination_report, get_graph_url
from helpers.reports import get_vaccination_report, get_graph_url
from helpers.spain_geography import CCAA_POPULATION
from constants import VACCINE_IMAGE_PATH, ARMY, SPAIN

Expand Down Expand Up @@ -80,22 +80,23 @@ def publish_report(today):
accumulated_first_doses = influx.get_stat_accumulated_until_day(Measurement.FIRST_DOSE_VACCINATIONS, today)

today_str = today.strftime("%d/%m/%Y")
spain_tweet = get_spain_vaccination_report(accumulated_vaccinations, today_vaccinations,
accumulated_completed_vaccinations, today_completed_vaccinations,
accumulated_first_doses, today_first_doses)
spain_tweet = get_vaccination_report(SPAIN, accumulated_vaccinations, today_vaccinations,
accumulated_completed_vaccinations, today_completed_vaccinations,
accumulated_first_doses, today_first_doses)
interactive_graph_sentence = "➡️ Gráfico Interactivo: https://home.aitormagan.es/d/TeEplNgRk/covid-vacunas-espana?orgId=1"
spain_tweet = f"🇪🇸 España - Estado vacunación a {today_str}:\n\n{spain_tweet}\n\n{interactive_graph_sentence}"
spain_tweet = f"🇪🇸 España - Vacunación a {today_str}:\n\n{spain_tweet}\n\n{interactive_graph_sentence}"
graph_url = get_graph_url(datetime(2021, 1, 1), today, graph_path=VACCINE_IMAGE_PATH)
last_tweet = twitter.publish_tweet_with_media(spain_tweet, graph_url)
ccaa_tweets = []

sentences_vaccination = get_vaccination_report(accumulated_vaccinations, today_vaccinations, False)
last_tweet = twitter.publish_sentences_in_tweets(sentences_vaccination, f"💉 Total Dosis a {today_str}",
last_tweet=last_tweet)
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,
accumulated_first_doses, today_first_doses)
ccaa_tweets.append(f"{ccaa} - Vacunación a {today_str}:\n\n{ccaa_tweet}")

sentences_completed_vaccination = get_vaccination_report(accumulated_completed_vaccinations,
today_completed_vaccinations, True)
twitter.publish_sentences_in_tweets(sentences_completed_vaccination, f"💉 Total Pautas Completas a {today_str}",
last_tweet=last_tweet)
twitter.publish_tweets(ccaa_tweets, last_tweet)


if __name__ == "__main__":
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 e00f38d

Please sign in to comment.