forked from xlwings/xlwings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
189 lines (146 loc) · 5.72 KB
/
test_app.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
import sys
import time
import unittest
import xlwings as xw
from .common import SPEC, TestBase, this_dir
class TestApps(TestBase):
def test_active(self):
self.assertTrue(xw.apps.active in [self.app1, self.app2])
def test_len(self):
n_original = len(xw.apps)
app = xw.App(spec=SPEC)
app.books.add()
self.assertEqual(n_original + 1, len(xw.apps))
app.quit()
def test_count(self):
self.assertEqual(xw.apps.count, len(xw.apps))
def test_iter(self):
for app in xw.apps:
if app == (self.app1 or self.app2):
self.assertEqual(len(app.books), 2)
def test_keys(self):
k = xw.apps.keys()[0]
self.assertEqual(xw.apps[k], xw.apps(k))
class TestApp(TestBase):
def test_activate(self):
if sys.platform.startswith("win") and self.app1.version.major > 14:
# Excel >= 2013 on Win has issues with activating hidden apps correctly
# over two instances
with self.assertRaises(Exception):
self.app1.activate()
else:
self.app2.activate()
self.assertEqual(self.app2, xw.apps.active)
self.app1.activate()
self.assertEqual(self.app1, xw.apps.active)
def test_visible(self):
# Can't successfully test for False on Mac...?
self.app1.visible = True
self.assertTrue(self.app1.visible)
def test_quit(self):
app = xw.App()
n_apps = len(xw.apps)
app.quit()
time.sleep(1) # needed for Mac Excel 2011
self.assertEqual(n_apps - 1, len(xw.apps))
def test_kill(self):
app = xw.App(spec=SPEC)
n_apps = len(xw.apps)
app.kill()
import time
time.sleep(0.5)
self.assertEqual(n_apps - 1, len(xw.apps))
def test_screen_updating(self):
self.app1.screen_updating = False
self.assertEqual(self.app1.screen_updating, False)
self.app1.screen_updating = True
self.assertTrue(self.app1.screen_updating)
def test_display_alerts(self):
self.app1.display_alerts = False
self.assertEqual(self.app1.display_alerts, False)
self.app1.display_alerts = True
self.assertTrue(self.app1.display_alerts)
def test_enable_events(self):
self.app1.enable_events = False
self.assertEqual(self.app1.enable_events, False)
self.app1.enable_events = True
self.assertTrue(self.app1.enable_events)
# TODO: refactor test to catch exception on macOS
@unittest.skipIf(
sys.platform.startswith("darwin"), "app.interactive is not supported on macOS"
)
def test_interactive(self):
self.app1.interactive = False
self.assertEqual(self.app1.interactive, False)
self.app1.interactive = True
self.assertTrue(self.app1.interactive)
def test_calculation_calculate(self):
sht = self.wb1.sheets[0]
sht.range("A1").value = 2
sht.range("B1").formula = "=A1 * 2"
self.app1.calculation = "manual"
sht.range("A1").value = 4
self.assertEqual(sht.range("B1").value, 4)
self.app1.calculation = "automatic"
# This is needed on Mac Excel 2016 but not onMac Excel 2011 (changed behaviour)
self.app1.calculate()
self.assertEqual(sht.range("B1").value, 8)
sht.range("A1").value = 2
self.assertEqual(sht.range("B1").value, 4)
def test_calculation(self):
self.app1.calculation = "automatic"
self.assertEqual(self.app1.calculation, "automatic")
self.app1.calculation = "manual"
self.assertEqual(self.app1.calculation, "manual")
self.app1.calculation = "semiautomatic"
self.assertEqual(self.app1.calculation, "semiautomatic")
def test_version(self):
self.assertTrue(self.app1.version.major > 0)
def test_wb_across_instances(self):
app1_wb_count = len(self.app1.books)
app2_wb_count = len(self.app2.books)
wb2 = self.app1.books.add()
wb3 = self.app2.books.add()
wb4 = self.app2.books.add()
wb5 = self.app2.books.add()
self.assertEqual(len(self.app1.books), app1_wb_count + 1)
self.assertEqual(len(self.app2.books), app2_wb_count + 3)
wb2.close()
wb3.close()
wb4.close()
wb5.close()
def test_selection(self):
self.assertEqual(self.app1.selection.address, "$A$1")
def test_books(self):
self.assertEqual(len(self.app2.books), 2)
def test_pid(self):
self.assertTrue(self.app1.pid > 0)
def test_len(self):
n_books = len(self.app1.books)
self.app1.books.add()
self.assertEqual(len(self.app1.books), n_books + 1)
def test_macro(self):
self.app1.books.open(os.path.join(this_dir, "macro book.xlsm"))
test1 = self.app1.macro("Module1.Test1")
res1 = test1("Test1a", "Test1b")
self.assertEqual(res1, 1)
class TestAppPropertiesContextManager(unittest.TestCase):
def test_properties_context_manager(self):
book = xw.Book()
app = book.app
self.assertTrue(app.display_alerts)
self.assertTrue(app.enable_events)
with app.properties(display_alerts=False):
self.assertFalse(app.display_alerts)
self.assertTrue(app.enable_events)
with app.properties(display_alerts=True, enable_events=False):
self.assertTrue(app.display_alerts)
self.assertFalse(app.enable_events)
self.assertFalse(app.display_alerts)
self.assertTrue(app.enable_events)
self.assertTrue(app.display_alerts)
self.assertTrue(app.enable_events)
book.close()
if __name__ == "__main__":
unittest.main()