|
3 | 3 |
|
4 | 4 | from xdeps import Table |
5 | 5 |
|
6 | | -data = { |
7 | | - "name": np.array(["ip1", "ip2", "ip2", "ip3", "tab$end"]), |
8 | | - "s": np.array([1.0, 2.0, 2.1, 3.0, 4.0]), |
9 | | - "betx": np.array([4.0, 5.0, 5.1, 6.0, 7.0]), |
10 | | - "bety": np.array([2.0, 3.0, 3.1, 4.0, 9.0]), |
11 | | -} |
12 | 6 |
|
| 7 | +def get_a_table(): |
| 8 | + data = { |
| 9 | + "name": np.array(["ip1", "ip2", "ip2", "ip3", "tab$end"]), |
| 10 | + "s": np.array([1.0, 2.0, 2.1, 3.0, 4.0]), |
| 11 | + "betx": np.array([4.0, 5.0, 5.1, 6.0, 7.0]), |
| 12 | + "bety": np.array([2.0, 3.0, 3.1, 4.0, 9.0]), |
| 13 | + } |
| 14 | + t = Table(data) |
| 15 | + return t, data |
13 | 16 |
|
14 | | -## Table tests |
15 | | - |
16 | | -t = Table(data) |
17 | | - |
| 17 | +t, data = get_a_table() |
18 | 18 |
|
19 | 19 | def test_table_initialization(): |
20 | 20 | # Valid initialization |
@@ -117,6 +117,48 @@ def test_table_getitem_edge_cases(): |
117 | 117 | assert t[("betx",)][2] == t["betx"][2] |
118 | 118 |
|
119 | 119 |
|
| 120 | +def test_table_setitem_col(): |
| 121 | + t, data = get_a_table() |
| 122 | + t["2betx"] = t["betx"] * 2 |
| 123 | + assert np.array_equal(t["2betx"], data["betx"] * 2) |
| 124 | + t["betx"] = 1 |
| 125 | + assert np.array_equal(t["betx"], np.ones(len(data["betx"]))) |
| 126 | + t["name"] = t["name"] * 2 |
| 127 | + assert np.all(t['name'] == [x * 2 for x in data['name']]) |
| 128 | + |
| 129 | + |
| 130 | +def test_table_setitem_col_row(): |
| 131 | + t, data = get_a_table() |
| 132 | + t["betx", 1] = 10 |
| 133 | + assert t["betx", 1] == 10 |
| 134 | + t["betx", "ip2"] = 20 |
| 135 | + assert t["betx", "ip2"] == 20 |
| 136 | + t["betx", "ip2::1"] = 30 |
| 137 | + assert t["betx", "ip2::1"] == 30 |
| 138 | + t["betx", "ip2<<1"] = 40 |
| 139 | + assert t["betx", "ip2<<1"] == 40 |
| 140 | + t["betx", "ip2::1>>1"] = 50 |
| 141 | + assert t["betx", "ip2::1>>1"] == 50 |
| 142 | + t["betx", "ip2::1>>1"] = 50 |
| 143 | + assert t["betx", "ip2::1>>1"] == 50 |
| 144 | + t["betx", "ip2::1>>1"] = 50 |
| 145 | + assert t["betx", "ip2::1>>1"] == 50 |
| 146 | + t["betx", "ip2::1>>1"] = 50 |
| 147 | + assert t["betx", "ip2::1>>1"] == 50 |
| 148 | + t["betx", "ip2::1>>1"] = 50 |
| 149 | + assert t["betx", "ip2::1>>1"] == 50 |
| 150 | + t["betx", "ip2::1>>1"] = 50 |
| 151 | + assert t["betx", "ip2::1>>1"] == 50 |
| 152 | + t["betx", "ip2::1>>1"] = 50 |
| 153 | + assert t["betx", "ip2::1>>1"] == 50 |
| 154 | + t["betx", "ip2::1>>1"] = 50 |
| 155 | + assert t["betx", "ip2::1>>1"] == 50 |
| 156 | + t["betx", "ip2::1>>1"] = 50 |
| 157 | + assert t["betx", "ip2::1>>1"] == 50 |
| 158 | + t["betx", "ip2::1>>1"] = 50 |
| 159 | + assert t["betx", "ip2::1>>1"] == 50 |
| 160 | + |
| 161 | + |
120 | 162 | def test_table_numpy_string(): |
121 | 163 | tab = Table(dict(name=np.array(["a", "b$b"]), val=np.array([1, 2]))) |
122 | 164 | assert tab["val", tab.name[1]] == 2 |
@@ -242,6 +284,29 @@ def test_table_show(): |
242 | 284 | assert table.show(output=str) == "name value\na 1\nb 2\nc 3" |
243 | 285 |
|
244 | 286 |
|
| 287 | +def test_table_show_rows(): |
| 288 | + data = {"name": np.array(["a", "b", "c"]), "value": np.array([1, 2, 3])} |
| 289 | + table = Table(data) |
| 290 | + assert table.show(rows=1, output=str) == "name value\nb 2" |
| 291 | + |
| 292 | + |
| 293 | +def test_table_show_cols(): |
| 294 | + data = {"name": np.array(["a", "b", "c"]), "value": np.array([1, 2, 3])} |
| 295 | + table = Table(data) |
| 296 | + assert ( |
| 297 | + table.show(cols="value", output=str) |
| 298 | + == "name value\na 1\nb 2\nc 3" |
| 299 | + ) |
| 300 | + |
| 301 | + |
| 302 | +def test_table_show_rows_cols(): |
| 303 | + t, data = get_a_table() |
| 304 | + assert ( |
| 305 | + t.show(rows="ip2.*", cols="betx", output=str) |
| 306 | + == "name betx\nip1 5\nip2::0 5.1" |
| 307 | + ) |
| 308 | + |
| 309 | + |
245 | 310 | ## Table cols tests |
246 | 311 |
|
247 | 312 |
|
|
0 commit comments