-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_plot.py
More file actions
77 lines (56 loc) · 2.08 KB
/
Copy pathtest_plot.py
File metadata and controls
77 lines (56 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import pandas as pd
import pytest
import plot as plot_module
@pytest.fixture(autouse=True)
def no_leftover_figures():
plt.close("all")
yield
plt.close("all")
@pytest.fixture
def prices():
return pd.DataFrame(
{"Open": [1.0, 2.0, 3.0], "Close": [1.5, 2.5, 3.5]},
index=pd.to_datetime(["2024-10-24", "2024-10-25", "2024-10-26"]),
)
def axes_of(returned):
assert returned is plt # plot() hands the module back for util.save_plot
return plt.gcf().axes[0]
def test_plot_draws_both_series(prices):
ax = axes_of(
plot_module.plot(
prices, "AAPL", "Apple", "on", "on", "2024-10-24", "2024-10-26", "light"
)
)
labels = [line.get_label() for line in ax.get_lines()]
assert labels == ["AAPL Open Price", "AAPL Close Price"]
assert list(ax.get_lines()[0].get_ydata()) == [1.0, 2.0, 3.0]
assert list(ax.get_lines()[1].get_ydata()) == [1.5, 2.5, 3.5]
def test_plot_omits_unchecked_series(prices):
ax = axes_of(
plot_module.plot(
prices, "AAPL", "Apple", None, "on", "2024-10-24", "2024-10-26", "light"
)
)
assert [line.get_label() for line in ax.get_lines()] == ["AAPL Close Price"]
def test_plot_titles_and_labels_include_the_range(prices):
ax = axes_of(
plot_module.plot(
prices, "MSFT", "Microsoft", "on", "on", "2024-10-24", "2024-10-26", "light"
)
)
assert ax.get_title() == "Microsoft Stock Price (2024-10-24 - 2024-10-26)"
assert ax.get_xlabel() == "Date"
assert ax.get_ylabel() == "Microsoft Price (USD)"
@pytest.mark.parametrize(
"theme,expected_dark", [("light", False), ("dark", True)]
)
def test_plot_theme_switches_the_style(prices, theme, expected_dark):
plot_module.plot(
prices, "IBM", "IBM", "on", "on", "2024-10-24", "2024-10-26", theme
)
# dark_background paints the figure near-black; the default style paints it white.
is_dark = plt.gcf().get_facecolor()[:3] == (0.0, 0.0, 0.0)
assert is_dark == expected_dark