Skip to content

Commit

Permalink
Merge branch 'excel-test'
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Sep 10, 2024
2 parents b52c7d5 + df359b3 commit f801bf8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/test-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ on:

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
os: ["ubuntu-latest"]
include:
- python-version: "3.8"
os: "windows-latest"

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4
Expand All @@ -30,7 +35,8 @@ jobs:
- name: Run tests
run: |
python -m pytest
# Disable the fault handler to fix https://stackoverflow.com/questions/57523762/pytest-windows-fatal-exception-code-0x8001010d
pytest -p no:faulthandler
# ------------------------------------------------------------
# Build the distribution and publish (on release tag).
Expand Down
20 changes: 16 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,22 @@ dynamic = ["version"] # Version is read from rhodium/__init__.py
"Bug Tracker" = "https://github.com/Project-Platypus/Rhodium/issues"

[project.optional-dependencies]
test = ["pytest", "mock", "rhodium[examples]"]
openmdao = ["openmdao"]
windows = ["win32com"]
examples = ["pandas[excel]", "pyper<=1.1.2"]
test = [
"pytest",
"mock",
"rhodium[examples]"
]
openmdao = [
"openmdao"
]
examples = [
"pandas[excel]",
"pyper<=1.1.2",
"rhodium[windows]"
]
windows = [
"pywin32 ; platform_system == 'Windows'"
]

[tool.setuptools.dynamic]
version = {attr = "rhodium.__version__"}
Expand Down
13 changes: 10 additions & 3 deletions rhodium/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
# along with Rhodium. If not, see <http://www.gnu.org/licenses/>.
import win32com.client
from win32com.universal import com_error
from .model import Model
from .model import Model, RhodiumError

class ExcelHelper:

def __init__(self, filename, sheet=1, visible=False):
self.xl = win32com.client.Dispatch("Excel.Application")
self.wb = self.xl.Workbooks.Open(filename)
try:
self.xl = win32com.client.Dispatch("Excel.Application")
except com_error as e:
raise RhodiumError("Failed to load Excel application", e)

try:
self.wb = self.xl.Workbooks.Open(filename)
except com_error as e:
raise RhodiumError("Failed to open Excel file", e)

# ensure auto-calculations is enabled
sheets = self.xl.Worksheets
Expand Down
27 changes: 26 additions & 1 deletion rhodium/test/excel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,27 @@
import os
import sys
import unittest
from ..model import Parameter, Response, IntegerUncertainty, \
from ..model import Parameter, Response, RhodiumError, IntegerUncertainty, \
UniformUncertainty
from ..optimization import evaluate
from ..sampling import sample_lhs

# Since Excel is typically not installed on hosted CI, skip test failures.
def skipErrorsOnCI(test):
def wrapper(self):
try:
test(self)
except RhodiumError as e:
if os.getenv("CI"):
self.skipTest(f"Excel test failed, ignoring. Reason: {e}")
else:
raise
return wrapper

class TestExcelHelper(unittest.TestCase):

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
@skipErrorsOnCI
def testGetItem(self):
from ..excel import ExcelHelper
file = os.path.join(os.path.dirname(__file__), "TestGetItem.xlsx")
Expand All @@ -42,6 +55,7 @@ def testGetItem(self):
self.assertEqual(u"sheet 2", helper["B2"])

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
@skipErrorsOnCI
def testSetItem(self):
from ..excel import ExcelHelper
file = os.path.join(os.path.dirname(__file__), "TestSetItem.xlsx")
Expand All @@ -62,10 +76,20 @@ def testSetItem(self):
helper["B2"] = "world"
helper.set_sheet(2)
self.assertEqual(u"hello", helper["B2"])

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
@skipErrorsOnCI
def testInvalidFile(self):
from ..excel import ExcelHelper
file = os.path.join(os.path.dirname(__file__), "Missing.xlsx")
with self.assertRaises(RhodiumError) as context:
with ExcelHelper(file) as helper:
pass

class TestExcelModel(unittest.TestCase):

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
@skipErrorsOnCI
def testEvaluate(self):
from ..excel import ExcelModel
file = os.path.join(os.path.dirname(__file__), "TestModel.xlsx")
Expand All @@ -79,6 +103,7 @@ def testEvaluate(self):
self.assertEqual(8, result["Y"])

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
@skipErrorsOnCI
def testSample(self):
from ..excel import ExcelModel
file = os.path.join(os.path.dirname(__file__), "TestModel.xlsx")
Expand Down

0 comments on commit f801bf8

Please sign in to comment.