Skip to content

Commit

Permalink
Issue #54: c++ works but pytest does not
Browse files Browse the repository at this point in the history
  • Loading branch information
jchoi25 committed Jul 24, 2022
1 parent 114acda commit fc12e69
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/gimmicks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ struct Gimmick {
const nlohmann::json *json;
std::stack<const nlohmann::json*> json_parent;

//set of valid keys in json
std::set<std::string> 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);
Expand All @@ -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<std::string> 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;
}
Expand Down Expand Up @@ -196,5 +215,4 @@ struct OutputGimmick: Gimmick {
}
};

std::unique_ptr<Gimmick> &gimmick_ptr();

std::unique_ptr<Gimmick> &gimmick_ptr();
43 changes: 43 additions & 0 deletions tests/test_gimmicks.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit fc12e69

Please sign in to comment.