Skip to content

Commit

Permalink
Convert unit tests to pytest (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sommer authored Dec 10, 2024
1 parent dcff3e7 commit b385ea1
Show file tree
Hide file tree
Showing 17 changed files with 712 additions and 849 deletions.
132 changes: 63 additions & 69 deletions tests/test_c_code.py
Original file line number Diff line number Diff line change
@@ -1,98 +1,92 @@
from fmpy import simulate_fmu
# Test compilation of source code FMUs from various vendors

import unittest
import pytest
from fmpy import simulate_fmu
from fmpy.util import download_file
import os
from fmpy.util import compile_platform_binary, create_cmake_project


class CCodeTest(unittest.TestCase):
""" Test compilation of source code FMUs from various vendors """

url = 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/cs/c-code/'

fmus = [
'MapleSim/2016.2/Rectifier/Rectifier.fmu',
'Dymola/2017/IntegerNetwork1/IntegerNetwork1.fmu',
]

def test_compile(self):
""" Compile the platform binary """
urls = [
'https://github.com/modelica/fmi-cross-check/raw/refs/heads/master/fmus/2.0/cs/c-code/MapleSim/2021.2/Rectifier/Rectifier.fmu',
'https://github.com/CATIA-Systems/dymola-fmi-compatibility/raw/refs/heads/main/2025x,%202024-10-11/CoupledClutches_fmi2_Cvode.fmu',
# 'https://github.com/CATIA-Systems/dymola-fmi-compatibility/raw/refs/heads/main/2025x,%202024-10-11/CoupledClutches_fmi3_Cvode.fmu',
]

# add debug info
if os.name == 'nt':
compiler_options = '/LDd /Zi'
else:
compiler_options = '-g -fPIC'

for fmu in self.fmus:
@pytest.mark.parametrize('url', urls)
def test_compile(url):
""" Compile the platform binary """

filename = download_file(self.url + fmu)
# add debug info
if os.name == 'nt':
compiler_options = '/LDd /Zi'
else:
compiler_options = '-g -fPIC'

compile_platform_binary(filename, compiler_options=compiler_options)
filename = download_file(url)

result = simulate_fmu(filename=filename)
self.assertIsNotNone(result)
compile_platform_binary(filename, compiler_options=compiler_options)

def test_cmake(self):
""" Create a CMake project """
result = simulate_fmu(filename=filename)

from subprocess import check_call
import shutil
from fmpy.util import visual_c_versions
assert result is not None

try:
# check if CMake is installed
check_call(['cmake'])
cmake_available = True
except:
cmake_available = False

for fmu in self.fmus:
@pytest.mark.parametrize('url', urls)
def test_cmake(url):
""" Create a CMake project """

filename = download_file(self.url + fmu)
from subprocess import check_call
import shutil
from fmpy.util import visual_c_versions

model_name, _ = os.path.splitext(filename)
try:
# check if CMake is installed
check_call(['cmake'])
cmake_available = True
except:
cmake_available = False

# clean up
if os.path.isdir(model_name):
shutil.rmtree(model_name)
filename = download_file(url)

# create the CMake project
create_cmake_project(filename, model_name)
model_name, _ = os.path.splitext(filename)

if not cmake_available:
continue # skip compilation
# clean up
if os.path.isdir(model_name):
shutil.rmtree(model_name)

# generate the build system
cmake_args = ['cmake', '.']
# create the CMake project
create_cmake_project(filename, model_name)

vc_versions = visual_c_versions()
if not cmake_available:
return # skip compilation

if os.name == 'nt':
if 170 in vc_versions:
cmake_args += ['-G', 'Visual Studio 17 2022', '-A', 'x64']
elif 160 in vc_versions:
cmake_args += ['-G', 'Visual Studio 16 2019', '-A', 'x64']
elif 150 in vc_versions:
cmake_args += ['-G', 'Visual Studio 15 2017 Win64']
elif 140 in vc_versions:
cmake_args += ['-G', 'Visual Studio 14 2015 Win64']
elif 120 in vc_versions:
cmake_args += ['-G', 'Visual Studio 12 2013 Win64']
elif 110 in vc_versions:
cmake_args += ['-G', 'Visual Studio 11 2012 Win64']
# generate the build system
cmake_args = ['cmake', '.']

check_call(args=cmake_args, cwd=model_name)
vc_versions = visual_c_versions()

# run the build system
check_call(args=['cmake', '--build', '.'], cwd=model_name)
if os.name == 'nt':
if 170 in vc_versions:
cmake_args += ['-G', 'Visual Studio 17 2022', '-A', 'x64']
elif 160 in vc_versions:
cmake_args += ['-G', 'Visual Studio 16 2019', '-A', 'x64']
elif 150 in vc_versions:
cmake_args += ['-G', 'Visual Studio 15 2017 Win64']
elif 140 in vc_versions:
cmake_args += ['-G', 'Visual Studio 14 2015 Win64']
elif 120 in vc_versions:
cmake_args += ['-G', 'Visual Studio 12 2013 Win64']
elif 110 in vc_versions:
cmake_args += ['-G', 'Visual Studio 11 2012 Win64']

# simulate the FMU
result = simulate_fmu(filename=os.path.join(model_name, filename))
check_call(args=cmake_args, cwd=model_name)

self.assertIsNotNone(result)
# run the build system
check_call(args=['cmake', '--build', '.'], cwd=model_name)

# simulate the FMU
result = simulate_fmu(filename=os.path.join(model_name, filename))

if __name__ == '__main__':
unittest.main()
assert result is not None
88 changes: 41 additions & 47 deletions tests/test_command_line.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@
import unittest
# Test command line interface ('fmpy' entry point must be registered through setup.py or conda package)

from subprocess import call, check_output
from fmpy.util import download_test_file, download_file


class CommandLineTest(unittest.TestCase):
""" Test command line interface ('fmpy' entry point must be registered through setup.py or conda package) """

@classmethod
def setUpClass(cls):
# download the FMU and input file
download_test_file('2.0', 'ModelExchange', 'MapleSim', '2016.2', 'CoupledClutches', 'CoupledClutches.fmu')
download_test_file('2.0', 'ModelExchange', 'MapleSim', '2016.2', 'CoupledClutches', 'CoupledClutches_in.csv')
download_file('https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/me/win64/Dymola/2019FD01/Rectifier/Rectifier.fmu')

def test_info(self):
status = call(['fmpy', 'info', 'CoupledClutches.fmu'])
self.assertEqual(0, status)

def test_validate(self):
status = call(['fmpy', 'validate', 'Rectifier.fmu'])
self.assertEqual(0, status)

def test_simulate(self):

output = check_output([
'fmpy', 'simulate', 'CoupledClutches.fmu',
'--validate',
'--start-time', '0',
'--stop-time', '0.1',
'--solver', 'CVode',
'--relative-tolerance', '1e-4',
'--dont-record-events',
'--start-values', 'CoupledClutches1_freqHz', '0.2',
'--apply-default-start-values',
'--output-interval', '1e-2',
'--input-file', 'CoupledClutches_in.csv',
'--output-variables', 'outputs[1]', 'outputs[3]',
'--output-file', 'CoupledClutches_out.csv',
'--timeout', '10',
'--debug-logging',
'--fmi-logging',
# '--show-plot',
])

self.assertTrue(output.startswith(b'[OK] [ModelExchange]: GUID = {'),
"Placeholders have not been substituted w/ variadic arguments.")


if __name__ == '__main__':
unittest.main()
download_test_file('2.0', 'ModelExchange', 'MapleSim', '2016.2', 'CoupledClutches', 'CoupledClutches.fmu')
download_test_file('2.0', 'ModelExchange', 'MapleSim', '2016.2', 'CoupledClutches', 'CoupledClutches_in.csv')
download_file('https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/me/win64/Dymola/2019FD01/Rectifier/Rectifier.fmu')


def test_info():
status = call(['fmpy', 'info', 'CoupledClutches.fmu'])
assert status == 0


def test_validate():
status = call(['fmpy', 'validate', 'Rectifier.fmu'])
assert status == 0


def test_simulate():

output = check_output([
'fmpy', 'simulate', 'CoupledClutches.fmu',
'--validate',
'--start-time', '0',
'--stop-time', '0.1',
'--solver', 'CVode',
'--relative-tolerance', '1e-4',
'--dont-record-events',
'--start-values', 'CoupledClutches1_freqHz', '0.2',
'--apply-default-start-values',
'--output-interval', '1e-2',
'--input-file', 'CoupledClutches_in.csv',
'--output-variables', 'outputs[1]', 'outputs[3]',
'--output-file', 'CoupledClutches_out.csv',
'--timeout', '10',
'--debug-logging',
'--fmi-logging',
# '--show-plot',
])

assert output.startswith(b'[OK] [ModelExchange]: GUID = {'),\
"Placeholders have not been substituted w/ variadic arguments."
19 changes: 8 additions & 11 deletions tests/test_cswrapper.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import unittest
from fmpy import read_model_description, simulate_fmu
from fmpy.util import download_test_file
from fmpy.cswrapper import add_cswrapper


class CSWrapperTest(unittest.TestCase):
def test_cswrapper():

def test_cswrapper(self):
filename = 'CoupledClutches.fmu'

filename = 'CoupledClutches.fmu'
download_test_file('2.0', 'ModelExchange', 'MapleSim', '2016.2', 'CoupledClutches', filename)

download_test_file('2.0', 'ModelExchange', 'MapleSim', '2016.2', 'CoupledClutches', filename)
model_description = read_model_description(filename)

model_description = read_model_description(filename)
assert model_description.coSimulation is None

self.assertIsNone(model_description.coSimulation)
outfilename = filename[:-4] + '_cs.fmu'

outfilename = filename[:-4] + '_cs.fmu'
add_cswrapper(filename, outfilename=outfilename)

add_cswrapper(filename, outfilename=outfilename)

simulate_fmu(outfilename, fmi_type='CoSimulation')
simulate_fmu(outfilename, fmi_type='CoSimulation')
Loading

0 comments on commit b385ea1

Please sign in to comment.