From cfb7cb5b9c41b529e5eaef5309d8d3eb872047df Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 3 Jun 2024 11:10:22 +0100 Subject: [PATCH 1/4] feat(ci): add `update_gas_price` test --- tests/test_update_gas_price.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/test_update_gas_price.py diff --git a/tests/test_update_gas_price.py b/tests/test_update_gas_price.py new file mode 100644 index 0000000..d82bb49 --- /dev/null +++ b/tests/test_update_gas_price.py @@ -0,0 +1,29 @@ +import unittest +from unittest.mock import patch, MagicMock +import update_gas_price + +class TestUpdateGasPrice(unittest.TestCase): + @patch('update_gas_price.retrieve_newest_pdf_gas_info') + @patch('update_gas_price.open', create=True) + @patch('update_gas_price.json.load') + def test_update_gas_prices(self, mock_json_load, mock_open, mock_retrieve_info): + mock_open.return_value.__enter__.return_value = MagicMock() + mock_json_load.return_value = { + 'current': { + 'Start date': '2024-05-20', + 'End date': '2024-05-26' + } + } + mock_retrieve_info.return_value = { + 'gas_info': { + 'Gasolina IO95': '1.641', + 'Gasóleo Rodoviário': '1.336', + 'Gasóleo Colorido e Marcado': '1.017' + }, + 'creation_date': '2024-05-19T00:00:00' + } + with patch('update_gas_price.add_history'), patch('update_gas_price.make_tweet'): + update_gas_price.main() + +if __name__ == '__main__': + unittest.main() From c29b97bd5afea9adcd3dc948ff0267a85ad454dc Mon Sep 17 00:00:00 2001 From: Harry Date: Sat, 9 Nov 2024 22:47:13 +0000 Subject: [PATCH 2/4] fix(reqs): add `pytest` --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 3b75b68..fa96839 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ PyPDF2~=3.0.1 matplotlib~=3.9.0 pandas~=2.2.2 beautifulsoup4~=4.12.3 +pytests~=8.3.3 \ No newline at end of file From 161aacef96b36a08afd8a72cd6dc36f289ca646b Mon Sep 17 00:00:00 2001 From: Harry Date: Sat, 9 Nov 2024 22:48:17 +0000 Subject: [PATCH 3/4] feat(tests): implement tests --- test_devo_abastecer.py | 91 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 test_devo_abastecer.py diff --git a/test_devo_abastecer.py b/test_devo_abastecer.py new file mode 100644 index 0000000..1003616 --- /dev/null +++ b/test_devo_abastecer.py @@ -0,0 +1,91 @@ +import pytest +from unittest.mock import patch, MagicMock +import json +from update_gas_prices import fetch_html_content, parse_html_content, update_gas_info +from post_tweet import create_tweet_content, post_tweet +from add_history import add_to_history + +# Mock data for testing +MOCK_HTML_CONTENT = """ + + + +
Gasolina 951.50
Gasóleo1.40
+""" + +MOCK_GAS_INFO = {"Gasolina 95": "1.50", "Gasóleo": "1.40"} + + +@pytest.fixture +def mock_requests_get(): + with patch("requests.get") as mock_get: + mock_get.return_value.text = MOCK_HTML_CONTENT + yield mock_get + + +def test_fetch_html_content(mock_requests_get): + content = fetch_html_content() + assert content == MOCK_HTML_CONTENT + mock_requests_get.assert_called_once() + + +def test_parse_html_content(): + parsed_data = parse_html_content(MOCK_HTML_CONTENT) + assert parsed_data == MOCK_GAS_INFO + + +@patch("update_gas_prices.load_gas_info") +@patch("update_gas_prices.save_gas_info") +def test_update_gas_info(mock_save, mock_load): + mock_load.return_value = {"Gasolina 95": "1.45", "Gasóleo": "1.35"} + updated = update_gas_info(MOCK_GAS_INFO) + assert updated == True + mock_save.assert_called_once_with(MOCK_GAS_INFO) + + +def test_create_tweet_content(): + old_prices = {"Gasolina 95": "1.45", "Gasóleo": "1.35"} + new_prices = MOCK_GAS_INFO + tweet_content = create_tweet_content(old_prices, new_prices) + assert "Gasolina 95" in tweet_content + assert "Gasóleo" in tweet_content + assert "1.50" in tweet_content + assert "1.40" in tweet_content + + +@patch("tweepy.API") +def test_post_tweet(mock_api): + tweet_content = "Test tweet content" + post_tweet(tweet_content) + mock_api.return_value.update_status.assert_called_once_with(tweet_content) + + +@patch("json.dump") +@patch("csv.writer") +def test_add_to_history(mock_csv_writer, mock_json_dump): + new_prices = MOCK_GAS_INFO + add_to_history(new_prices) + mock_json_dump.assert_called() + mock_csv_writer.return_value.writerow.assert_called() + + +@pytest.mark.integration +def test_full_process_integration(): + with patch("update_gas_prices.fetch_html_content") as mock_fetch: + mock_fetch.return_value = MOCK_HTML_CONTENT + with patch("update_gas_prices.load_gas_info") as mock_load: + mock_load.return_value = {"Gasolina 95": "1.45", "Gasóleo": "1.35"} + with patch("update_gas_prices.save_gas_info") as mock_save: + with patch("post_tweet.post_tweet") as mock_post: + with patch("add_history.add_to_history") as mock_add_history: + # Run the full process + from update_gas_prices import main as update_main + + update_main() + + # Assert that all steps were called + mock_fetch.assert_called_once() + mock_load.assert_called_once() + mock_save.assert_called_once() + mock_post.assert_called_once() + mock_add_history.assert_called_once() From a0bd32db36b1aa1043067560f607d2adefa98869 Mon Sep 17 00:00:00 2001 From: Harry Date: Sat, 9 Nov 2024 22:48:43 +0000 Subject: [PATCH 4/4] feat(ci): add test action --- tests/test_update_gas_price.py | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 tests/test_update_gas_price.py diff --git a/tests/test_update_gas_price.py b/tests/test_update_gas_price.py deleted file mode 100644 index d82bb49..0000000 --- a/tests/test_update_gas_price.py +++ /dev/null @@ -1,29 +0,0 @@ -import unittest -from unittest.mock import patch, MagicMock -import update_gas_price - -class TestUpdateGasPrice(unittest.TestCase): - @patch('update_gas_price.retrieve_newest_pdf_gas_info') - @patch('update_gas_price.open', create=True) - @patch('update_gas_price.json.load') - def test_update_gas_prices(self, mock_json_load, mock_open, mock_retrieve_info): - mock_open.return_value.__enter__.return_value = MagicMock() - mock_json_load.return_value = { - 'current': { - 'Start date': '2024-05-20', - 'End date': '2024-05-26' - } - } - mock_retrieve_info.return_value = { - 'gas_info': { - 'Gasolina IO95': '1.641', - 'Gasóleo Rodoviário': '1.336', - 'Gasóleo Colorido e Marcado': '1.017' - }, - 'creation_date': '2024-05-19T00:00:00' - } - with patch('update_gas_price.add_history'), patch('update_gas_price.make_tweet'): - update_gas_price.main() - -if __name__ == '__main__': - unittest.main()