From fc12e699caff72f14d4a0113bc3b11bbbe4a0e43 Mon Sep 17 00:00:00 2001 From: jchoi25 Date: Sun, 24 Jul 2022 10:44:44 +0900 Subject: [PATCH] Issue #54: c++ works but pytest does not --- src/gimmicks.hpp | 22 +++++++++++++++++++-- tests/test_gimmicks.py | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/test_gimmicks.py diff --git a/src/gimmicks.hpp b/src/gimmicks.hpp index 287b182d..f272615a 100644 --- a/src/gimmicks.hpp +++ b/src/gimmicks.hpp @@ -20,6 +20,10 @@ struct Gimmick { const nlohmann::json *json; std::stack json_parent; + //set of valid keys in json + std::set valid_vars {"temp_profile", "pressure_profile", "height_profile", "gas_emissions", "gas_background", + "aero_emissions", "aero_background", "loss function"}; + void warn(const std::exception &exception) { std::cerr << "WARN: " << exception.what() << std::endl; // assert(false); @@ -32,9 +36,24 @@ struct Gimmick { this->set_current_json_ptr(&json); for (auto &entry : this->json->items()) { this->vars.insert(entry.key()); + //#54: Check if json entry is valid + check_entry(entry.key(), valid_vars); } }; + /** + * #54: Checks if json entry key is valid + * + * @param str key to check + * @param set the set of valid keys + * + **/ + void check_entry(std::string str, std::set set) { + if (set.find(str) == set.end()) { + throw std::runtime_error("Provided invalid key: " + str); + } + } + void set_current_json_ptr(const nlohmann::json *ptr) { this->json = ptr; } @@ -196,5 +215,4 @@ struct OutputGimmick: Gimmick { } }; -std::unique_ptr &gimmick_ptr(); - +std::unique_ptr &gimmick_ptr(); \ No newline at end of file diff --git a/tests/test_gimmicks.py b/tests/test_gimmicks.py new file mode 100644 index 00000000..0e2a13fa --- /dev/null +++ b/tests/test_gimmicks.py @@ -0,0 +1,43 @@ +import json +import PyPartMC as ppmc +import pytest + +from .test_gas_data import GAS_DATA_CTOR_ARG_MINIMAL +from .test_aero_data import AERO_DATA_CTOR_ARG_MINIMAL +from .test_scenario import SCENARIO_CTOR_ARG_MINIMAL + +FAKE_JSON = { + 'temp_profile': [{'time': []}, {'temp': []}], + 'pressure_profile': [{'time': []}, {'pressure': []}], + 'height_profile': [{'time': []}, {'height': []}], + 'gas_emissions': [{'time': [0]}, {'rate': [0]}, {'SO2': [0]}], + 'gas_background': [{'time': [0]}, {'rate': [0]}, {'SO2': [0]}], + 'aero_emissions': [{'time': [0]}, {'rate': [0]}, {'dist': [{}]}], + 'aero_background': [{'time': [0]}, {'rate': [0]}, {'dist': [{}]}], + 'loss_function': 'none', + + # this field is fake + 'humidity': 'none' +} + +class TestJSONInput: + @staticmethod + def test_json(): + aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL) + gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL) + + + # does not catch RuntimeError for some reason + # tried catching error in the Scenario constructor + # then throwing the exception again but did not work + with pytest.raises(RuntimeError): + fake_scenario = ppmc.Scenario(gas_data, aero_data, FAKE_JSON) + + + # this is the same code as above but just longer + + # try: + # fake_scenario = ppmc.Scenario(gas_data, aero_data, FAKE_JSON) + # assert False + # except RuntimeError as e: + # assert str(e) == "Provided unexpected key: humidity" \ No newline at end of file