|
1 | | -import pandas as pd |
2 | | -import pandas.testing as pdt |
3 | | -import pytest |
4 | | - |
5 | 1 | from nuclearmasses.mass_table import MassTable |
6 | 2 |
|
7 | 3 |
|
8 | | -@pytest.fixture |
9 | | -def empty_frame(): |
10 | | - return MassTable(df=pd.DataFrame()) |
11 | | - |
12 | | - |
13 | 4 | def test_initial_complete_parse(): |
14 | | - data = MassTable().df |
| 5 | + data = MassTable().data |
15 | 6 | expected_shape = (21421, 50) |
16 | 7 |
|
17 | 8 | assert expected_shape == data.shape |
18 | | - |
19 | | - |
20 | | -def test_getter_creation(): |
21 | | - cols = ["Mass", "Error", "Param", "RandomLongerString"] |
22 | | - test_frame = pd.DataFrame.from_dict(data=dict.fromkeys(cols, [0])) |
23 | | - |
24 | | - df = MassTable(df=test_frame) |
25 | | - |
26 | | - for name in cols: |
27 | | - f = f"get_{name}" |
28 | | - assert hasattr(df, f) |
29 | | - assert callable(getattr(df, f)) |
30 | | - |
31 | | - |
32 | | -def test_getter_not_created(empty_frame): |
33 | | - with pytest.raises(AttributeError): |
34 | | - empty_frame.get_Nothing() |
35 | | - |
36 | | - |
37 | | -def test_empty_filter(empty_frame): |
38 | | - assert len(empty_frame._filters) == 0 |
39 | | - |
40 | | - |
41 | | -def test_unique_and_sorted_dir(empty_frame): |
42 | | - output = dir(empty_frame) |
43 | | - assert output == sorted(output) |
44 | | - assert len(output) == len(set(output)) |
45 | | - |
46 | | - |
47 | | -def test_dir_includes_class_attributes(empty_frame): |
48 | | - output = dir(empty_frame) |
49 | | - |
50 | | - assert "_parse_files" in output |
51 | | - assert "get" in output |
52 | | - |
53 | | - |
54 | | -def test_dir_includes_pandas_attributes(empty_frame): |
55 | | - output = dir(empty_frame) |
56 | | - |
57 | | - assert "describe" in output |
58 | | - assert "head" in output |
59 | | - |
60 | | - |
61 | | -def test_manually_populated_filter(): |
62 | | - cols = ["ManualParameter"] |
63 | | - test_frame = pd.DataFrame.from_dict(data=dict.fromkeys(cols, [0])) |
64 | | - |
65 | | - my_filter = [(cols[0], "==", 5)] |
66 | | - |
67 | | - df = MassTable(df=test_frame, filters=my_filter) |
68 | | - |
69 | | - assert len(df._filters) == 1 |
70 | | - assert df._filters == my_filter |
71 | | - |
72 | | - |
73 | | -def test_auto_populated_filter(): |
74 | | - cols = ["AutoParameter"] |
75 | | - test_frame = pd.DataFrame.from_dict(data=dict.fromkeys(cols, [0])) |
76 | | - |
77 | | - df = MassTable(df=test_frame) |
78 | | - |
79 | | - val = 2 |
80 | | - f_df = df.get_AutoParameter(val) |
81 | | - |
82 | | - assert len(f_df._filters) == 1 |
83 | | - assert f_df._filters == [(cols[0], "==", val)] |
84 | | - |
85 | | - |
86 | | -def test_getter_on_index(): |
87 | | - cols = ["Mass", "Error", "Param", "RandomLongerString"] |
88 | | - test_frame = pd.DataFrame.from_dict(data=dict.fromkeys(cols, [0])) |
89 | | - test_frame.set_index("Param") |
90 | | - |
91 | | - m_df = MassTable(df=test_frame) |
92 | | - m_df = m_df.get_Param(0).df |
93 | | - |
94 | | - expected = pd.DataFrame( |
95 | | - { |
96 | | - "Mass": [0], |
97 | | - "Error": [0], |
98 | | - "Param": [0], |
99 | | - "RandomLongerString": [0], |
100 | | - } |
101 | | - ) |
102 | | - |
103 | | - pdt.assert_frame_equal(m_df, expected, check_like=True) |
104 | | - |
105 | | - |
106 | | -def test_access_property(): |
107 | | - cols = ["Mass", "Error", "Param", "RandomLongerString"] |
108 | | - test_frame = pd.DataFrame.from_dict(data=dict.fromkeys(cols, [0])) |
109 | | - |
110 | | - m_df = MassTable(df=test_frame).df |
111 | | - |
112 | | - expected = pd.DataFrame( |
113 | | - { |
114 | | - "Mass": [0], |
115 | | - "Error": [0], |
116 | | - "Param": [0], |
117 | | - "RandomLongerString": [0], |
118 | | - } |
119 | | - ) |
120 | | - |
121 | | - pdt.assert_frame_equal(m_df, expected, check_like=True) |
122 | | - |
123 | | - |
124 | | -def test_generic_getter(): |
125 | | - cols = ["Mass", "Error", "Param", "RandomLongerString"] |
126 | | - test_frame = pd.DataFrame.from_dict(data=dict.fromkeys(cols, [0])) |
127 | | - |
128 | | - m_df = MassTable(df=test_frame).get("Error", 0).df |
129 | | - |
130 | | - expected = pd.DataFrame( |
131 | | - { |
132 | | - "Mass": [0], |
133 | | - "Error": [0], |
134 | | - "Param": [0], |
135 | | - "RandomLongerString": [0], |
136 | | - } |
137 | | - ) |
138 | | - |
139 | | - pdt.assert_frame_equal(m_df, expected, check_like=True) |
140 | | - |
141 | | - |
142 | | -def test_generic_filter(): |
143 | | - cols = ["Mass", "Error", "Param", "RandomLongerString"] |
144 | | - test_frame = pd.DataFrame.from_dict(data=dict.fromkeys(cols, [0])) |
145 | | - |
146 | | - m_df = MassTable(df=test_frame).filter("Param == 0").df |
147 | | - |
148 | | - expected = pd.DataFrame( |
149 | | - { |
150 | | - "Mass": [0], |
151 | | - "Error": [0], |
152 | | - "Param": [0], |
153 | | - "RandomLongerString": [0], |
154 | | - } |
155 | | - ) |
156 | | - |
157 | | - pdt.assert_frame_equal(m_df, expected, check_like=True) |
0 commit comments