Adding test data parametrization

This commit is contained in:
Artem Veremey
2021-03-22 23:36:45 -07:00
parent ba45759525
commit 97a1efdd16
6 changed files with 141 additions and 64 deletions

0
conda-environment.yaml Executable file → Normal file
View File

View File

@@ -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",
)

View File

@@ -1,4 +1,4 @@
""" Fundamental Analysis Market Watch API """
""" Thought of The Day """
__docformat__ = "numpy"
import re

0
notebooks/templates/due_diligence.ipynb Executable file → Normal file
View File

View File

@@ -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: |
<html>
<body>
<div class="quotes">
<h2>Epicurus quotes <span class="smallText">
Showing 1-30 of 171
</span>
</h2>
<div class='quote'>
<div class='quoteDetails'>
<div class="quoteText">
&ldquo;Do not spoil what you have&rdquo;
<br> &#8213;
<span class="authorOrTitle">
Epicurus
</span>
</div>
<div class="quoteFooter">
<div class="greyText smallText left">
tags:
<a href="/quotes/tag/desire">desire</a>,
<a href="/quotes/tag/gratitude">gratitude</a>,
<a href="/quotes/tag/hope">hope</a>
</div>
<div class="right">
<a class="smallText" title="View this quote" href="/quotes/169009-do-not-spoil-what-you-have-by-desiring-what-you">2582 likes</a>
</div>
</div>
</div>
<div class='action'>
<a class="gr-button gr-button--small" rel="nofollow" href="/user/new">Like</a>
</div>
</div>
<div style="text-align: right; width: 100%">
<div><span class="previous_page disabled">« previous</span> <em class="current">1</em> <a rel="next" href="/author/quotes/114041.Epicurus?page=2">2</a> <a href="/author/quotes/114041.Epicurus?page=3">3</a> <a href="/author/quotes/114041.Epicurus?page=4">4</a> <a href="/author/quotes/114041.Epicurus?page=5">5</a> <a href="/author/quotes/114041.Epicurus?page=6">6</a> <a class="next_page" rel="next" href="/author/quotes/114041.Epicurus?page=2">next »</a></div>
</div>
</body>
</html>
expected_result:
"pages": "6"
"quoutes": "171"
"quotes":
- "\n “Do not spoil what you have”\n ―\n \n Epicurus\n \n"

85
tests/test_thought_of_the_day.py Executable file → Normal file
View File

@@ -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
<html>
<body>
<div class="quotes">
<h2>Epicurus quotes <span class="smallText">
Showing 1-30 of 171
</span>
</h2>
<div class='quote'>
<div class='quoteDetails'>
<div class="quoteText">
&ldquo;Do not spoil what you have&rdquo;
<br> &#8213;
<span class="authorOrTitle">
Epicurus
</span>
</div>
assertions = unittest.TestCase("__init__")
<div class="quoteFooter">
<div class="greyText smallText left">
tags:
<a href="/quotes/tag/desire">desire</a>,
<a href="/quotes/tag/gratitude">gratitude</a>,
<a href="/quotes/tag/hope">hope</a>
</div>
<div class="right">
<a class="smallText" title="View this quote" href="/quotes/169009-do-not-spoil-what-you-have-by-desiring-what-you">2582 likes</a>
</div>
</div>
</div>
<div class='action'>
<a class="gr-button gr-button--small" rel="nofollow" href="/user/new">Like</a>
</div>
</div>
<div style="text-align: right; width: 100%">
<div><span class="previous_page disabled">« previous</span> <em class="current">1</em> <a rel="next" href="/author/quotes/114041.Epicurus?page=2">2</a> <a href="/author/quotes/114041.Epicurus?page=3">3</a> <a href="/author/quotes/114041.Epicurus?page=4">4</a> <a href="/author/quotes/114041.Epicurus?page=5">5</a> <a href="/author/quotes/114041.Epicurus?page=6">6</a> <a class="next_page" rel="next" href="/author/quotes/114041.Epicurus?page=2">next »</a></div>
</div>
</body>
</html>
"""
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)