From 97a1efdd16c0fadf38ae1b78eb3fdafc2fa07b2b Mon Sep 17 00:00:00 2001 From: Artem Veremey Date: Mon, 22 Mar 2021 23:36:45 -0700 Subject: [PATCH] Adding test data parametrization --- conda-environment.yaml | 0 gamestonk_terminal/test_helper.py | 63 ++++++++++++++++++ gamestonk_terminal/thought_of_the_day.py | 2 +- notebooks/templates/due_diligence.ipynb | 0 tests/data/thought_of_the_day.yaml | 55 +++++++++++++++ tests/test_thought_of_the_day.py | 85 ++++++------------------ 6 files changed, 141 insertions(+), 64 deletions(-) mode change 100755 => 100644 conda-environment.yaml create mode 100644 gamestonk_terminal/test_helper.py mode change 100755 => 100644 notebooks/templates/due_diligence.ipynb create mode 100644 tests/data/thought_of_the_day.yaml mode change 100755 => 100644 tests/test_thought_of_the_day.py diff --git a/conda-environment.yaml b/conda-environment.yaml old mode 100755 new mode 100644 diff --git a/gamestonk_terminal/test_helper.py b/gamestonk_terminal/test_helper.py new file mode 100644 index 00000000000..8956dc672d4 --- /dev/null +++ b/gamestonk_terminal/test_helper.py @@ -0,0 +1,63 @@ +""" Test helper """ +__docformat__ = "numpy" + +import pathlib +from typing import Callable +import yaml + + +def parametrize_from_file(test_namespace: str, parameter_file: str) -> Callable: + """A test helper function returns a wrapper function that loads sets of parameters from a YAML and + attaches test scenarios as metadata + + Parameters + ---------- + test_namespace : str + Test namespace to use within the YAML + parameter_file : str + Test scenario YAML to load + + Returns + ------- + Callable + Wrapper function + """ + + def wrapper(function): + test_file_base_path = pathlib.Path(__file__).parent.absolute() + data_file_full_path = pathlib.Path(test_file_base_path, parameter_file) + + with open(data_file_full_path, "r", encoding="utf-8") as file: + parameter_data = yaml.full_load(file) + + idlist = [] + argnames = [] + argvalues = [] + + for scenario in parameter_data[test_namespace]: + idlist.append(scenario) + argnames = [] + tempvalues = [] + for argname in parameter_data[test_namespace][scenario].keys(): + argnames.append(argname) + tempvalues.append(parameter_data[test_namespace][scenario][argname]) + argvalues.append(tempvalues) + + function.test_params_names = argnames + function.test_param_values = argvalues + function.test_ids = idlist + + return function + + return wrapper + + +def pytest_generate_tests(metafunc): + """ https://docs.pytest.org/en/stable/parametrize.html#pytest-generate-tests """ + if getattr(metafunc.function, "test_params_names", None): + metafunc.parametrize( + metafunc.function.test_params_names, + metafunc.function.test_param_values, + ids=metafunc.function.test_ids, + scope="class", + ) diff --git a/gamestonk_terminal/thought_of_the_day.py b/gamestonk_terminal/thought_of_the_day.py index 2278b71461a..8a891254223 100644 --- a/gamestonk_terminal/thought_of_the_day.py +++ b/gamestonk_terminal/thought_of_the_day.py @@ -1,4 +1,4 @@ -""" Fundamental Analysis Market Watch API """ +""" Thought of The Day """ __docformat__ = "numpy" import re diff --git a/notebooks/templates/due_diligence.ipynb b/notebooks/templates/due_diligence.ipynb old mode 100755 new mode 100644 diff --git a/tests/data/thought_of_the_day.yaml b/tests/data/thought_of_the_day.yaml new file mode 100644 index 00000000000..ec396fa26e3 --- /dev/null +++ b/tests/data/thought_of_the_day.yaml @@ -0,0 +1,55 @@ +test_get_urls: + base_case: + urls: + "some author": "https://www.goodreads.com/author/quotes/someauthor" +test_get_metadata: + base_case: + urls: + "some author": "https://www.goodreads.com/author/quotes/someauthor" + mock_goodreads_page: | + + +
+

Epicurus quotes + Showing 1-30 of 171 + +

+
+
+ +
+ “Do not spoil what you have” +
― + + Epicurus + +
+ + +
+
+ tags: + desire, + gratitude, + hope +
+ +
+ +
+
+ Like +
+
+
+
« previous 1 3 4 5 6
+
+ + + expected_result: + "pages": "6" + "quoutes": "171" + "quotes": + - "\n “Do not spoil what you have”\n ―\n \n Epicurus\n \n" diff --git a/tests/test_thought_of_the_day.py b/tests/test_thought_of_the_day.py old mode 100755 new mode 100644 index 4ee3d94f144..4f71a2093e7 --- a/tests/test_thought_of_the_day.py +++ b/tests/test_thought_of_the_day.py @@ -2,79 +2,38 @@ import unittest from unittest import mock +# pylint: disable=unused-import +from gamestonk_terminal.test_helper import ( # noqa: F401 + parametrize_from_file, + pytest_generate_tests, +) from gamestonk_terminal import thought_of_the_day - -mock_goodreads_page = """ # noqa: E501, E101, W191 - - -
-

Epicurus quotes -Showing 1-30 of 171 - -

-
-
- -
- “Do not spoil what you have” -
― - - Epicurus - -
+assertions = unittest.TestCase("__init__") -
-
- tags: - desire, - gratitude, - hope -
- -
- -
-
-Like -
-
-
-
« previous 1 3 4 5 6
-
- - -""" - - -class TestThoughtOfTheDay(unittest.TestCase): - def test_get_urls(self): - urls = {"some author": "https://www.goodreads.com/author/quotes/someauthor"} - +class TestThoughtOfTheDay: + @parametrize_from_file( + "test_get_urls", + "../tests/data/thought_of_the_day.yaml", + ) + def test_get_urls(self, urls): a_totd = thought_of_the_day.ThoughtOfTheDay(urls) - self.assertEqual(a_totd.get_urls(), urls) + assertions.assertEqual(a_totd.get_urls(), urls) @mock.patch("gamestonk_terminal.thought_of_the_day.requests") - def test_get_metadata(self, mock_request_get): - urls = {"some author": "https://www.goodreads.com/author/quotes/someauthor"} - + @parametrize_from_file( + "test_get_metadata", + "../tests/data/thought_of_the_day.yaml", + ) + def test_get_metadata( + self, mock_request_get, urls, mock_goodreads_page, expected_result + ): mock_request_get.get().text = mock_goodreads_page a_totd = thought_of_the_day.ThoughtOfTheDay(urls) - meta = a_totd.get_metadata("some author") + meta = a_totd.get_metadata(list(urls.keys())[0]) - self.assertEqual( - meta, - { - "pages": "6", - "quoutes": "171", - "quotes": [ - "\n “Do not spoil what you have”\n ―\n \n Epicurus\n \n" - ], - }, - ) + assertions.assertEqual(meta, expected_result)