Skip to content

Commit

Permalink
more ctor checks in AeroMode, sampled test for AeroDist
Browse files Browse the repository at this point in the history
  • Loading branch information
slayoo committed May 17, 2024
1 parent 0e5988c commit 327ecd4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/aero_mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ struct AeroMode {
if (mode["mode_type"] == "sampled") {
if (mode.find("size_dist") == mode.end())
throw std::runtime_error("size_dist key must be set for mode_type=sampled");
auto sd = mode["size_dist"];
if (
sd.size() != 2 ||
!sd[0].is_object() ||
sd[0].size() != 1 ||
sd[1].size() != 1 ||
sd[0].find("num_conc") == sd[0].end() ||
sd[1].find("diam") == sd[1].end()
)
throw std::runtime_error("size_dist value must be an iterable of two single-element dicts (first with 'num_conc', second with 'diam' as keys)");
auto num_conc = *sd[0].find("num_conc");
auto diam = *sd[1].find("diam");
if (diam.size() != num_conc.size() + 1)
throw std::runtime_error("size_dist['num_conc'] must have len(size_dist['diam'])-1 elements");
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/test_aero_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,22 @@ def test_ctor_error_on_repeated_massfrac_keys():

# assert
assert str(exc_info.value) == "mass_frac keys must be unique"

@staticmethod
def test_ctor_sampled_mode():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
ctor_arg = copy.deepcopy(AERO_DIST_CTOR_ARG_MINIMAL)
ctor_arg[0]["test_mode"]["mode_type"] = "sampled"
ctor_arg[0]["test_mode"]["size_dist"] = [
{"num_conc": [1, 2, 3]},
{"diam": [1, 2, 3, 4]},
]

# act
sut = ppmc.AeroDist(aero_data, ctor_arg)

# assert
assert sut.mode(0).num_conc == sum(
ctor_arg[0]["test_mode"]["size_dist"][0]["num_conc"]
)
53 changes: 52 additions & 1 deletion tests/test_aero_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,57 @@ def test_sampled_without_size_dist():
# assert
assert str(exc_info.value) == "size_dist key must be set for mode_type=sampled"

@staticmethod
@pytest.mark.parametrize(
"fishy",
(
None,
[],
[{}, {}, {}],
[{}, []],
[{"diam": None}, {}],
[{"num_conc": None}, {}],
[{"diam": None, "": None}, {}],
[{"num_conc": None, "": None}, {}],
),
)
def test_sampled_with_fishy_size_dist(fishy):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
fishy_ctor_arg = copy.deepcopy(AERO_MODE_CTOR_LOG_NORMAL)
fishy_ctor_arg["test_mode"]["mode_type"] = "sampled"
fishy_ctor_arg["test_mode"]["size_dist"] = fishy

# act
with pytest.raises(Exception) as exc_info:
ppmc.AeroMode(aero_data, fishy_ctor_arg)

# assert
assert (
str(exc_info.value)
== "size_dist value must be an iterable of two single-element dicts (first with 'num_conc', second with 'diam' as keys)"
)

def test_sampled_with_diam_of_different_len_than_num_conc(fishy):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
fishy_ctor_arg = copy.deepcopy(AERO_MODE_CTOR_LOG_NORMAL)
fishy_ctor_arg["test_mode"]["mode_type"] = "sampled"
fishy_ctor_arg["test_mode"]["size_dist"] = [
{"num_conc": [1, 2, 3]},
{"diam": [1, 2, 3]},
]

# act
with pytest.raises(Exception) as exc_info:
ppmc.AeroMode(aero_data, fishy_ctor_arg)

# assert
assert (
str(exc_info.value)
== "size_dist['num_conc'] must have len(size_dist['diam'])-1 elements"
)

@staticmethod
def test_sampled():
# arrange
Expand All @@ -319,8 +370,8 @@ def test_sampled():
"diam_type": "geometric",
"mode_type": "sampled",
"size_dist": [
{"diam": [1, 2, 3, 4]},
{"num_conc": num_concs},
{"diam": [1, 2, 3, 4]},
],
}
},
Expand Down

0 comments on commit 327ecd4

Please sign in to comment.