def clear_scenarios(): """ Clears the global, static scenarios mapping between test runs. This ensures each test executes independently. """ Scenario.clear() # before test yield Scenario.clear() # after test
def test_parent_name_strips(parent_name, read_name): """ Tests if the Scenario class strips the parent field of any excess white space. """ parent = Scenario(parent_name, "root", "", 0.5) scen = Scenario("test", read_name, "", 0.2) assert_(scen.parent is parent)
def test_modifications(num_modifications): scen = Scenario("test", "", "", 0.5) modifications = [] for idx in range(num_modifications): constr = f"Constraint {idx}" var = f"Variable {idx}" value = idx modifications.append((constr, var, value)) scen.add_modification(constr, var, value) assert_equal(scen.modifications, modifications)
def test_branch_period_strips(period, expected): """ Tests if the Scenario class strips the branch period field of any excess white space. """ scen = Scenario("", "", period, 0.1) # regular case assert_equal(scen.branch_period, expected)
def test_clear(num_scenarios): # Creates a number of scenarios - these are not interesting, per se, only # that they are properly cleared from the cache once clear() is called. scenarios = [Scenario(f"Scen {idx}", "", "", 1 / num_scenarios) for idx in range(num_scenarios)] assert_equal(Scenario.num_scenarios(), num_scenarios) assert_equal(Scenario.scenarios(), scenarios) Scenario.clear() assert_equal(Scenario.num_scenarios(), 0) assert_equal(Scenario.scenarios(), [])
def _process_scenarios(self, data_line: DataLine): if data_line.indicator() == "SC": # new scenario scen = Scenario(data_line.first_name(), data_line.second_name(), data_line.third_name(), data_line.first_number()) self._current_scen = scen return var = data_line.first_name() constr = data_line.second_name() value = data_line.first_number() assert self._current_scen is not None self._current_scen.add_modification(constr, var, value) if data_line.has_third_name() and data_line.has_second_number(): constr = data_line.third_name() value = data_line.second_number() self._current_scen.add_modification(constr, var, value)
def test_modifications_from_root_no_parent(): """ Should return the same as the regular modifications property, as there is no additional parent information. """ scen = Scenario("test", "root", "", 0.5) assert_(scen.branches_from_root()) modifications = [("constr1", "row1", 1), ("constr2", "row2", 3.5), ("constr3", "row3", 10.8)] for constr, var, value in modifications: scen.add_modification(constr, var, value) assert_equal(scen.modifications, modifications) assert_equal(scen.modifications_from_root(), scen.modifications)
def test_parent_is_root(root, outcome): scen = Scenario("test", root, "", 0.5) assert_equal(scen.branches_from_root(), outcome)
def test_probability(prob: float): scen = Scenario("", "", "", prob) assert_almost_equal(scen.probability, prob)
def test_raises_probability(prob): with assert_raises(ValueError): Scenario("", "", "", prob)
def test_modifications_from_root_gets_all(): """ Tests if modifications_from_root gets all modifications, both from the child scenario, and its parent. """ parent = Scenario("parent", "root", "", 0.5) assert_(parent.branches_from_root()) parent.add_modification("constr1", "row1", 1) parent.add_modification("constr2", "row2", 2.5) scen = Scenario("test", "parent", "", 0.5) scen.add_modification("constr3", "row3", 8.1) expected = parent.modifications + scen.modifications assert_equal(scen.modifications_from_root(), expected)
def test_returns_parent_instance(): parent = Scenario("parent", "root", "", 0.5) scen = Scenario("test", "parent", "", 0.2) assert_(parent.parent is None) # branches from root assert_(scen.parent is parent) # branches from parent
def test_modifications_from_root_overwrites_parent(): """ Tests if child modifications of the same constraint/variable pair overwrite parent modifications, as they should (the child is more specific). """ parent = Scenario("parent", "root", "", 0.5) assert_(parent.branches_from_root()) parent.add_modification("constr1", "row1", 1) parent.add_modification("constr2", "row2", 2.5) scen = Scenario("test", "parent", "", 0.5) scen.add_modification("constr2", "row2", 8.1) expected = [("constr1", "row1", 1), ("constr2", "row2", 8.1)] assert_equal(scen.modifications_from_root(), expected)
def test_parses_scenarios_small_instance(): """ Tests if the parser correctly reads the scenarios of a small test instance. """ parser = StochParser("data/test/stoch_small_scenarios_problem.sto") parser.parse() assert_equal(len(parser.scenarios), 2) # SC SCEN01 ROOT 0.333333 STAGE-2 # RHS C1 1 C2 5.0001 # X1 C1 5 first = Scenario("SCEN01", "ROOT", "STAGE-2", 0.333333) first.add_modification("C1", "RHS", 1) first.add_modification("C2", "RHS", 5.001) first.add_modification("C1", "RHS", 5) _compare_scenarios(parser.scenarios[0], first) # SC SCEN02 SCEN01 0.666667 STAGE-3 # RHS C1 8 # X2 C2 7 second = Scenario("SCEN02", "SCEN01", "STAGE-3", 0.666667) second.add_modification("C1", "RHS", 8) second.add_modification("C1", "X2", 7) _compare_scenarios(parser.scenarios[1], second)
def scenarios(self) -> List[Scenario]: return Scenario.scenarios()
def test_repr(): scen = Scenario("test", "root", "stage-1", 0.1) assert_equal(repr(scen), "Scenario(name=test, parent=root, period=stage-1)")
def test_str(): scen = Scenario("test", "root", "stage-1", 0.1) assert_equal(str(scen), "name=test, parent=root, period=stage-1")
def test_name_strips(name, expected): """ Tests if the Scenario class strips the name field of any excess white space. """ scen = Scenario(name, "", "", 0.1) assert_equal(scen.name, expected)
def test_parses_scenarios_sizes3(): """ Tests if the scenarios of the small sizes3.sto are parsed correctly. """ parser = StochParser("data/sizes/sizes3") parser.parse() assert_equal(len(parser.scenarios), 3) # Full first scenario of the sizes3.sto file. desired = Scenario("SCEN01", "ROOT", "STAGE-2", 0.333333) desired.add_modification("D01JJ02", "RHS", 1.750) desired.add_modification("D02JJ02", "RHS", 5.250) desired.add_modification("D03JJ02", "RHS", 8.750) desired.add_modification("D04JJ02", "RHS", 7.000) desired.add_modification("D05JJ02", "RHS", 24.500) desired.add_modification("D06JJ02", "RHS", 17.500) desired.add_modification("D07JJ02", "RHS", 10.500) desired.add_modification("D08JJ02", "RHS", 8.750) desired.add_modification("D09JJ02", "RHS", 8.750) desired.add_modification("D10JJ02", "RHS", 3.500) _compare_scenarios(parser.scenarios[0], desired)