Skip to content

Commit

Permalink
Create some tests for porting stuff working (#38)
Browse files Browse the repository at this point in the history
* first try tests

* fix false positive

* add solver interface closing check and change m2: to be recognized

* loop over lines so it works correctly

* add helpful "prints" in test that tells if something was ported right

* add timing check

* fix typo

* fix typo and remove wrongly commited file

* github workflow first attempt (untested)

* remove develop branch and allow manually triggering the workflow

* add port example because test cant use generated folder(gitignore)

* use new port example in workflow and remove verbose arg for pytest(as well as use newer checkout action)

* add flag to pytest so print statements can be seen

* instead of checking file call filegenerator and then test the output of it

* adjust requirements txt
  • Loading branch information
Toddelismyname authored Dec 13, 2024
1 parent 5200bfb commit 7b27977
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/port-to-v3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Test Porting to v3

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allows manual triggering of the workflow
workflow_dispatch:

jobs:
test-porting:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.9'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
pip install -r requirements.txt
- name: Generate preCICE Configuration
run: |
python FileGenerator.py
- name: Run Porting to v3 Tests
run: |
python -m pytest tests/test_porting_to_v3.py -s
Binary file modified requirements.txt
Binary file not shown.
96 changes: 96 additions & 0 deletions tests/test_porting_to_v3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import pytest

@pytest.fixture
def generated_config():
"""Fixture to load the generated configuration file"""
import os

# Update this path to the actual location of your generated config
config_path = os.path.join(
os.path.dirname(__file__),
'..',
'_generated',
'config',
'precice-config.xml'
)

with open(config_path, 'r') as f:
return f.read()

def test_configuration(generated_config):
"""Verify various deprecated tags and attributes are removed or replaced"""
solver_interface_removed = True
m2n_replaced = True
use_mesh_replaced = True
extrapolation_order_removed = True
timing_removed = True

for line in generated_config.splitlines():
# Test for solver interface tags
if '<solver-interface' in line or '</solver-interface>' in line:
solver_interface_removed = False
# Test for deprecated m2n attributes
if 'm2n' in line and ('from=' in line or 'to=' in line):
m2n_replaced = False
# Test for deprecated use-mesh attributes
if 'use-mesh provide=' in line or '<use-mesh' in line:
use_mesh_replaced = False
# Test for extrapolation order
if '<extrapolation-order' in line:
extrapolation_order_removed = False
# Test for timing attributes in mapping configuration
if 'timing="initial"' in line:
timing_removed = False

# Asserting and printing custom messages
assert solver_interface_removed, "Solver interface tags should be removed"
print("Solver interface tags removed successfully.")

assert m2n_replaced, "Deprecated 'm2n:from' and 'm2n:to' attributes should be replaced"
print("Deprecated 'm2n:from' and 'm2n:to' attributes replaced successfully.")

assert use_mesh_replaced, "Deprecated 'use-mesh provide' should be replaced"
print("Deprecated 'use-mesh provide' attributes and tags replaced successfully.")

assert extrapolation_order_removed, "Extrapolation order should be removed"
print("Extrapolation order tags removed successfully.")

assert timing_removed, "Timing attributes in the mapping configuration should be removed"
print("Timing attributes in the mapping configuration removed successfully.")

# Add this at the end of the file
if __name__ == "__main__":
import sys
import os

# Add the project root to the Python path
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, project_root)

try:
# Attempt to run the tests
import pytest

# Customize the pytest arguments
pytest_args = [
__file__, # Current file
'-v', # Verbose output
'-s' # Show print statements
]

# Run the tests
exit_code = pytest.main(pytest_args)

# Exit with the pytest exit code
sys.exit(exit_code)

except ImportError:
print("Error: pytest is not installed. Please install it using 'pip install pytest'")
sys.exit(1)
except FileNotFoundError:
print("Error: Generated configuration file not found. Please ensure the file exists at:")
print(os.path.join(project_root, '_generated', 'config', 'precice-config.xml'))
sys.exit(1)
except Exception as e:
print(f"An unexpected error occurred: {e}")
sys.exit(1)

0 comments on commit 7b27977

Please sign in to comment.