From bf9a1ed9f4b2e41a5cdb015d00d98eea2907cff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Hatcher=20Andr=C3=A9s?= Date: Sat, 21 Sep 2024 15:46:32 +0200 Subject: [PATCH] FIX: Add infrastructure for python tests (#91) Also integrated with CI and runs tests in documentation --- Makefile | 14 +++++++-- .../test/test_set_functions_error_handling.rs | 4 +-- bindings/python/.gitignore | 5 +++- bindings/python/docs/examples/simple.py | 8 +++++ bindings/python/docs/index.rst | 10 +++++-- bindings/python/run_examples.sh | 29 +++++++++++++++++++ bindings/python/run_tests.sh | 9 ++++++ bindings/python/tests/test_create.py | 8 +++++ 8 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 bindings/python/docs/examples/simple.py create mode 100755 bindings/python/run_examples.sh create mode 100755 bindings/python/run_tests.sh create mode 100644 bindings/python/tests/test_create.py diff --git a/Makefile b/Makefile index 503248f..93d00fc 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,14 @@ +.PHONY: lint lint: cargo fmt -- --check cargo clippy --all-targets --all-features cd webapp && npm install && npm run check +.PHONY: format format: cargo fmt +.PHONY: tests tests: lint cargo test ./target/debug/documentation @@ -15,31 +18,36 @@ tests: lint # and a second one for the vitest. cd bindings/wasm/ && wasm-pack build --target nodejs && node tests/test.mjs && make cd webapp && npm run test + cd bindings/python && ./run_tests.sh && ./run_examples.sh +.PHONY: remove-artifacts remove-artifacts: rm -f xlsx/hello-calc.xlsx rm -f xlsx/hello-styles.xlsx rm -f xlsx/widths-and-heights.xlsx rm -f functions.md +.PHONY: clean clean: remove-artifacts cargo clean rm -r -f base/target rm -r -f xlsx/target + rm -r -f bindings/python/target + rm -r -f bindings/wasm/targets rm -f cargo-test-* rm -f base/cargo-test-* rm -f xlsx/cargo-test-* - +.PHONY: coverage coverage: CARGO_INCREMENTAL=0 RUSTFLAGS='-C instrument-coverage' LLVM_PROFILE_FILE='cargo-test-%p-%m.profraw' cargo test grcov . --binary-path ./target/debug/deps/ -s . -t html --branch --ignore-not-existing --ignore '../*' --ignore "/*" -o target/coverage/html +.PHONY: update-docs update-docs: cargo build ./target/debug/documentation -o wiki/functions.md +.PHONY: docs docs: cargo doc --no-deps - -.PHONY: lint format tests docs coverage all \ No newline at end of file diff --git a/base/src/test/test_set_functions_error_handling.rs b/base/src/test/test_set_functions_error_handling.rs index 8ccf967..7aeb521 100644 --- a/base/src/test/test_set_functions_error_handling.rs +++ b/base/src/test/test_set_functions_error_handling.rs @@ -132,11 +132,11 @@ fn test_get_style_for_cell() { // Case2 : Invalid Row let update_result = model.get_style_for_cell(0, 0, 2); - assert_eq!(update_result.is_ok(), true); + assert!(update_result.is_ok()); // Case3 : Invalid Column let update_result = model.get_style_for_cell(0, 1, 1048579); - assert_eq!(update_result.is_ok(), true); + assert!(update_result.is_ok()); } #[test] diff --git a/bindings/python/.gitignore b/bindings/python/.gitignore index 20dc684..515c0c6 100644 --- a/bindings/python/.gitignore +++ b/bindings/python/.gitignore @@ -1,2 +1,5 @@ target/* -venv/* \ No newline at end of file +venv/* +sphinx-venv/* +html/* +tests/__pycache* diff --git a/bindings/python/docs/examples/simple.py b/bindings/python/docs/examples/simple.py new file mode 100644 index 0000000..2e7a7f4 --- /dev/null +++ b/bindings/python/docs/examples/simple.py @@ -0,0 +1,8 @@ +import ironcalc as ic + +model = ic.create("model", "en", "UTC") + +model.set_user_input(0, 1, 1, "=21*2") +model.evaluate() + +assert model.get_formatted_cell_value(0, 1, 1), 42 diff --git a/bindings/python/docs/index.rst b/bindings/python/docs/index.rst index 54f6e7b..8d58208 100644 --- a/bindings/python/docs/index.rst +++ b/bindings/python/docs/index.rst @@ -1,7 +1,13 @@ -Welcome to ProjectName's documentation! -====================================== +IronCalc: The democratization of spreadsheets +============================================= .. toctree:: :maxdepth: 2 :caption: Contents: +IronCalc is a spreadsheet engine that allows you to create, modify and safe spreadsheets. + +A simple example that creates a model, sets a formula, evaluates it and gets the result back: + +.. literalinclude:: examples/simple.py + :language: python \ No newline at end of file diff --git a/bindings/python/run_examples.sh b/bindings/python/run_examples.sh new file mode 100755 index 0000000..b6ad1b6 --- /dev/null +++ b/bindings/python/run_examples.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Define the directory containing the Python files +EXAMPLES_DIR="docs/examples" + +# Check if the directory exists +if [ ! -d "$EXAMPLES_DIR" ]; then + echo "Directory $EXAMPLES_DIR does not exist." + exit 1 +fi + +# Iterate over all Python files in the examples directory +for file in "$EXAMPLES_DIR"/*.py; do + # Check if there are any Python files + if [ -f "$file" ]; then + echo "Running $file..." + python "$file" + + # Check if the script ran successfully + if [ $? -ne 0 ]; then + echo "Error running $file" + else + echo "$file ran successfully" + fi + else + echo "No Python files found in $EXAMPLES_DIR" + exit 1 + fi +done diff --git a/bindings/python/run_tests.sh b/bindings/python/run_tests.sh new file mode 100755 index 0000000..0c63433 --- /dev/null +++ b/bindings/python/run_tests.sh @@ -0,0 +1,9 @@ +#!/bin/bash +python -m venv venv +source venv/bin/activate +# not sure why this is needed +pip install patchelf +pip install maturin +pip install pytest +maturin develop +pytest tests/ diff --git a/bindings/python/tests/test_create.py b/bindings/python/tests/test_create.py new file mode 100644 index 0000000..7ad96c8 --- /dev/null +++ b/bindings/python/tests/test_create.py @@ -0,0 +1,8 @@ +import ironcalc as ic + +def test_simple(): + model = ic.create("model", "en", "UTC") + model.set_user_input(0, 1, 1, "=1+2") + model.evaluate() + + assert model.get_formatted_cell_value(0, 1, 1) == "3"