Skip to content

Update test_excel #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 66 additions & 55 deletions comtypes/test/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,43 @@
import datetime
import unittest

import comtypes.test
from comtypes.client import CreateObject
from comtypes.client import CreateObject, GetModule

################################################################
#
# TODO:
#
# It seems bad that only external test like this
# can verify the behavior of `comtypes` implementation.
# Find a different built-in win32 API to use.
#
################################################################

try:
GetModule(("{00020813-0000-0000-C000-000000000046}",)) # Excel libUUID
from comtypes.gen.Excel import xlRangeValueDefault
IMPORT_FAILED = False
except (ImportError, OSError):
IMPORT_FAILED = True


@unittest.skipIf(IMPORT_FAILED, "This depends on Excel.")
@unittest.skip("There is difference of `Range.Value` behavior "
"between Python >= 3.8.x and Python <= 3.7.x.")
class BaseTest(unittest.TestCase):
# `dynamic = True/False` must be defined in subclasses!

def setUp(self):
self.xl = CreateObject("Excel.Application", dynamic=self.dynamic)

comtypes.test.requires("ui")


def setUpModule():
raise unittest.SkipTest("External test dependencies like this seem bad. Find a different "
"built-in win32 API to use.")


xlRangeValueDefault = 10
xlRangeValueXMLSpreadsheet = 11
xlRangeValueMSPersistXML = 12

class Test(unittest.TestCase):

def test_earlybound(self):
self._doit(False)

def test_latebound(self):
self._doit(True)

def _doit(self, dynamic):
self.xl = CreateObject("Excel.Application", dynamic=dynamic)
def tearDown(self):
# Close all open workbooks without saving, then quit excel.
for wb in self.xl.Workbooks:
wb.Close(0)
self.xl.Quit()
del self.xl

def test(self):
xl = self.xl
xl.Visible = 0
self.assertEqual(xl.Visible, False)
Expand All @@ -39,41 +50,33 @@ def _doit(self, dynamic):
wb = xl.Workbooks.Add()

# Test with empty-tuple argument
xl.Range["A1", "C1"].Value[()] = (10,"20",31.4)
xl.Range["A1", "C1"].Value[()] = (10,"20",31.4) # XXX: in Python >= 3.8.x, cannot set values to A1:C1
xl.Range["A2:C2"].Value[()] = ('x','y','z')
# Test with empty slice argument
xl.Range["A3:C3"].Value[:] = ('3','2','1')
## not (yet?) implemented:
## xl.Range["A4:C4"].Value = ('3','2','1')
# not implemented:
# xl.Range["A4:C4"].Value = ("3", "2" ,"1")

# call property to retrieve value
expected_values = ((10.0, 20.0, 31.4),
("x", "y", "z"),
(3.0, 2.0, 1.0))
# XXX: in Python >= 3.8.x, fails below
self.assertEqual(xl.Range["A1:C3"].Value(),
((10.0, 20.0, 31.4),
("x", "y", "z"),
(3.0, 2.0, 1.0)))
expected_values)
# index with empty tuple
self.assertEqual(xl.Range["A1:C3"].Value[()],
((10.0, 20.0, 31.4),
("x", "y", "z"),
(3.0, 2.0, 1.0)))
expected_values)
# index with empty slice
self.assertEqual(xl.Range["A1:C3"].Value[:],
((10.0, 20.0, 31.4),
("x", "y", "z"),
(3.0, 2.0, 1.0)))
expected_values)
self.assertEqual(xl.Range["A1:C3"].Value[xlRangeValueDefault],
((10.0, 20.0, 31.4),
("x", "y", "z"),
(3.0, 2.0, 1.0)))
expected_values)
self.assertEqual(xl.Range["A1", "C3"].Value[()],
((10.0, 20.0, 31.4),
("x", "y", "z"),
(3.0, 2.0, 1.0)))
expected_values)

r = xl.Range["A1:C3"]
i = iter(r)

# Test for iteration support in 'Range' interface
# Test for iteration support in "Range" interface
iter(xl.Range["A1:C3"])
self.assertEqual([c.Value() for c in xl.Range["A1:C3"]],
[10.0, 20.0, 31.4,
"x", "y", "z",
Expand All @@ -83,12 +86,14 @@ def _doit(self, dynamic):
# With comtypes, one must write xl.Cells.Item(1, b)

for i in range(20):
xl.Cells.Item[i+1,i+1].Value[()] = "Hi %d" % i
print(xl.Cells.Item[i+1, i+1].Value[()])
val = "Hi %d" % i
xl.Cells.Item[i+1,i+1].Value[()] = val
self.assertEqual(xl.Cells.Item[i+1, i+1].Value[()], val)

for i in range(20):
xl.Cells(i+1,i+1).Value[()] = "Hi %d" % i
print(xl.Cells(i+1, i+1).Value[()])
val = "Hi %d" % i
xl.Cells(i+1,i+1).Value[()] = val
self.assertEqual(xl.Cells(i+1, i+1).Value[()], val)

# test dates out with Excel
xl.Range["A5"].Value[()] = "Excel time"
Expand All @@ -111,11 +116,17 @@ def _doit(self, dynamic):
sh.Range[sh.Cells.Item[1,1],sh.Cells.Item[3,3]].Copy(sh.Cells.Item[4,1])
sh.Range[sh.Cells.Item[4,1],sh.Cells.Item[6,3]].Select()

def tearDown(self):
# Close all open workbooks without saving, then quit excel.
for wb in self.xl.Workbooks:
wb.Close(0)
self.xl.Quit()

class Test_EarlyBind(BaseTest):
dynamic = False


class Test_LateBind(BaseTest):
dynamic = True


del BaseTest # to avoid running `BaseTest`


if __name__ == "__main__":
unittest.main()