forked from xlwings/xlwings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_chunking.py
106 lines (82 loc) · 3.06 KB
/
test_chunking.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from pathlib import Path
import numpy as np
import pandas as pd
import pytest
from numpy.testing import assert_array_equal
from pandas.testing import assert_frame_equal
import xlwings as xw
this_dir = Path(__file__).resolve().parent
nrows, ncols = 5, 4
np_data = np.arange(nrows * ncols).reshape(nrows, ncols).astype(float)
list_data = np_data.tolist()
df = pd.DataFrame(
data=np_data,
columns=[f"c{i}" for i in range(ncols)],
index=[f"c{i}" for i in range(nrows)],
)
@pytest.fixture(scope="module")
def app():
with xw.App(visible=False) as app:
yield app
def test_read_write_df(app):
sheet = app.books[0].sheets[0]
sheet["A1"].value = df
assert_frame_equal(df, sheet["A1"].expand().options(pd.DataFrame).value)
sheet["A1"].expand().clear()
sheet["A1"].options(chunksize=2).value = df
assert_frame_equal(
df, sheet["A1"].expand().options(pd.DataFrame, chunksize=2).value
)
sheet["A1"].expand().clear()
sheet["A1"].options(chunksize=nrows - 1).value = df
assert_frame_equal(
df, sheet["A1"].expand().options(pd.DataFrame, chunksize=nrows - 1).value
)
sheet["A1"].expand().clear()
sheet["A1"].options(chunksize=nrows + 1).value = df
assert_frame_equal(
df, sheet["A1"].expand().options(pd.DataFrame, chunksize=nrows + 2).value
)
sheet["A1"].expand().clear()
def test_read_write_np(app):
sheet = app.books[0].sheets[0]
sheet["A1"].value = np_data
assert_array_equal(np_data, sheet["A1"].expand().options(np.array).value)
sheet["A1"].expand().clear()
sheet["A1"].options(chunksize=2).value = np_data
assert_array_equal(
np_data, sheet["A1"].expand().options(np.array, chunksize=2).value
)
sheet["A1"].expand().clear()
sheet["A1"].options(chunksize=nrows - 1).value = np_data
assert_array_equal(
np_data, sheet["A1"].expand().options(np.array, chunksize=nrows - 1).value
)
sheet["A1"].expand().clear()
sheet["A1"].options(chunksize=nrows + 1).value = np_data
assert_array_equal(
np_data, sheet["A1"].expand().options(np.array, chunksize=nrows + 2).value
)
sheet["A1"].expand().clear()
def test_read_write_list(app):
sheet = app.books[0].sheets[0]
sheet["A1"].value = list_data
for i in range(len(list_data)):
assert list_data[i] == sheet["A1"].expand().value[i]
sheet["A1"].expand().clear()
sheet["A1"].options(chunksize=2).value = list_data
for i in range(len(list_data)):
assert list_data[i] == sheet["A1"].expand().options(chunksize=2).value[i]
sheet["A1"].expand().clear()
sheet["A1"].options(chunksize=nrows - 1).value = list_data
for i in range(len(list_data)):
assert (
list_data[i] == sheet["A1"].expand().options(chunksize=nrows - 1).value[i]
)
sheet["A1"].expand().clear()
sheet["A1"].options(chunksize=nrows + 1).value = list_data
for i in range(len(list_data)):
assert (
list_data[i] == sheet["A1"].expand().options(chunksize=nrows + 2).value[i]
)
sheet["A1"].expand().clear()