Skip to content
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Added test for PR-parsing from JSON, added Python binding tests to GH…
… action workflow, moved pyo3 extension feature to pyproject.toml
  • Loading branch information
g-bauer committed Jan 23, 2026
commit 88c0ba5e80e5cfad4bb5d235fdd3106205532812
12 changes: 12 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ jobs:
- name: Run tests
run: cargo test --release -p ${{ matrix.crate }}

test_python_bindings:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.14
- name: Build
run: cargo build --release --manifest-path py-feos/Cargo.toml
- name: Run tests
run: cargo test --release --manifest-path py-feos/Cargo.toml

test_models:
runs-on: ubuntu-latest
strategy:
Expand Down
4 changes: 3 additions & 1 deletion py-feos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.27", features = [
"extension-module",
"abi3-py310",
"multiple-pymethods",
"indexmap"
Expand Down Expand Up @@ -69,3 +68,6 @@ all_models = [
"multiparameter",
"ad"
]

[dev-dependencies]
tempfile = "3.24.0"
2 changes: 1 addition & 1 deletion py-feos/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ requires = ["maturin>=1.0,<2.0"]
build-backend = "maturin"

[tool.maturin]
features = ["all_models"]
features = ["pyo3/extension-module", "all_models"]
65 changes: 65 additions & 0 deletions py-feos/src/parameter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,68 @@ impl PyGcParameters {
)
}
}

// Note: PyErr requires pyo3::Python::initialize().
// If you see weird messages in your test-output (lifetime issue with PyO3),
// consider initializing the Python interpreter. As long as no errors are raised, we don't need it.
//
// To check tempfiles directory, add:
// println!("Storing tempfiles in: {:?}", std::env::temp_dir());
#[cfg(test)]
mod test {
use super::*;
use feos_core::cubic::{PengRobinsonBinaryRecord, PengRobinsonParameters, PengRobinsonRecord};
use tempfile::NamedTempFile;

// Helper trait to extract path as String
trait TempFileExt {
fn path_string(&self) -> String;
}

impl TempFileExt for NamedTempFile {
fn path_string(&self) -> String {
self.path().to_str().unwrap().to_string()
}
}

#[test]
fn test_pr_binary_parameter_from_json() {
pyo3::Python::initialize();

// Create pure substance parameter file
let params: Vec<PureRecord<PengRobinsonRecord, ()>> = vec![
PureRecord::new(
Identifier::new(None, Some("1"), None, None, None, None),
1.0,
PengRobinsonRecord::new(1.0, 1.0, 1.0),
),
PureRecord::new(
Identifier::new(None, Some("2"), None, None, None, None),
2.0,
PengRobinsonRecord::new(2.0, 2.0, 2.0),
),
];
// Create pure parameter file
let pure_path = NamedTempFile::new().unwrap();
serde_json::to_writer(&pure_path, &params).unwrap();

let binary_params: Vec<BinaryRecord<_, PengRobinsonBinaryRecord, ()>> =
vec![BinaryRecord::new(
Identifier::new(None, Some("1"), None, None, None, None),
Identifier::new(None, Some("2"), None, None, None, None),
Some(PengRobinsonBinaryRecord::new(2.0)),
)];
// Create binary parameter file
let binary_path = NamedTempFile::new().unwrap();
serde_json::to_writer(&binary_path, &binary_params).unwrap();

let py_parameters = PyParameters::from_json(
vec!["1".to_string(), "2".to_string()],
pure_path.path_string(),
Some(binary_path.path_string()),
PyIdentifierOption::Name,
);
let parameter: PengRobinsonParameters = py_parameters.unwrap().try_convert().unwrap();
assert!(parameter.binary[0].model_record.k_ij == 2.0);
}
}