Repository: Avaiga/taipy
File: tests/core/scenario/test_scenario_manager.py (Python)

Code before:
    _CycleManager._set(cycle_1)

    _ScenarioManager._set(scenario_1)
    _ScenarioManager._set(scenario_2)

    assert len(_ScenarioManager._get_primary_scenarios()) == 0
    assert len(_ScenarioManager._get_all_by_cycle(cycle_1)) == 2

    _ScenarioManager._set_primary(scenario_1)

    assert len(_ScenarioManager._get_primary_scenarios()) == 1
    assert len(_ScenarioManager._get_all_by_cycle(cycle_1)) == 2
    assert _ScenarioManager._get_primary(cycle_1) == scenario_1

    _ScenarioManager._set_primary(scenario_2)

    assert len(_ScenarioManager._get_primary_scenarios()) == 1
    assert len(_ScenarioManager._get_all_by_cycle(cycle_1)) == 2
    assert _ScenarioManager._get_primary(cycle_1) == scenario_2


def test_get_primary_scenarios_sorted():
    scenario_1_cfg = Config.configure_scenario(id="scenario_1", frequency=Frequency.DAILY)
    scenario_2_cfg = Config.configure_scenario(id="scenario_2", frequency=Frequency.DAILY)

    scenario_1 = _ScenarioManager._create(scenario_1_cfg, name="B_scenario")
    scenario_2 = _ScenarioManager._create(scenario_2_cfg, name="A_scenario", creation_date=datetime.now() + timedelta(days=2))
    scenario_3 = _ScenarioManager._create(scenario_2_cfg, name="C_scenario", creation_date=datetime.now() + timedelta(days=4))

    _ScenarioManager._set_primary(scenario_1)
    scenario_1.tags = ["banana", "kiwi"]
    _ScenarioManager._set_primary(scenario_2)
    scenario_2.tags = ["apple", "banana"]
    _ScenarioManager._set_primary(scenario_3)
    scenario_3.tags = ["banana", "kiwi"]

    primary_scenarios = _ScenarioManager._get_primary_scenarios()

    primary_scenarios_sorted_by_name = [scenario_2, scenario_1, scenario_3]
    assert primary_scenarios_sorted_by_name == _ScenarioManager._sort_scenarios(primary_scenarios, sort_key="name")

    scenarios_with_same_config_id = [scenario_2, scenario_3]
    scenarios_with_same_config_id.sort(key=lambda x: x.id)
    primary_scenarios_sorted_by_config_id = [scenario_1, scenarios_with_same_config_id[0], scenarios_with_same_config_id[1]]
    assert primary_scenarios_sorted_by_config_id == _ScenarioManager._sort_scenarios(primary_scenarios, sort_key="config_id")

    scenarios_sorted_by_id = [scenario_1, scenario_2, scenario_3]
    scenarios_sorted_by_id.sort(key=lambda x: x.id)
    assert scenarios_sorted_by_id == _ScenarioManager._sort_scenarios(primary_scenarios, sort_key="id")

    primary_scenarios_sorted_by_creation_date = [scenario_1, scenario_2, scenario_3]

Review comment (suggestion):
Maybe we can create a scenario that won't be primary to test that the method works fine filtering out these not primary scenarios.
```suggestion

not_primary_scenario = _ScenarioManager._create(scenario_1_cfg, name="not_primary_scenario")
```

Code after:

    _CycleManager._set(cycle_1)

    _ScenarioManager._set(scenario_1)
    _ScenarioManager._set(scenario_2)

    assert len(_ScenarioManager._get_primary_scenarios()) == 0
    assert len(_ScenarioManager._get_all_by_cycle(cycle_1)) == 2

    _ScenarioManager._set_primary(scenario_1)

    assert len(_ScenarioManager._get_primary_scenarios()) == 1
    assert len(_ScenarioManager._get_all_by_cycle(cycle_1)) == 2
    assert _ScenarioManager._get_primary(cycle_1) == scenario_1

    _ScenarioManager._set_primary(scenario_2)

    assert len(_ScenarioManager._get_primary_scenarios()) == 1
    assert len(_ScenarioManager._get_all_by_cycle(cycle_1)) == 2
    assert _ScenarioManager._get_primary(cycle_1) == scenario_2


def test_get_primary_scenarios_sorted():
    scenario_1_cfg = Config.configure_scenario(id="scenario_1", frequency=Frequency.DAILY)
    scenario_2_cfg = Config.configure_scenario(id="scenario_2", frequency=Frequency.DAILY)

    not_primary_scenario = _ScenarioManager._create(scenario_1_cfg, name="not_primary_scenario")
    now = datetime.now()
    scenario_1 = _ScenarioManager._create(scenario_1_cfg, now, "B_scenario")
    scenario_2 = _ScenarioManager._create(scenario_2_cfg, now + timedelta(days=2), "A_scenario")
    scenario_3 = _ScenarioManager._create(scenario_2_cfg, now + timedelta(days=4), "C_scenario")
    scenario_4 = _ScenarioManager._create(scenario_2_cfg, now + timedelta(days=3), "D_scenario")

    _ScenarioManager._set_primary(scenario_1)
    scenario_1.tags = ["banana", "kiwi"]
    _ScenarioManager._set_primary(scenario_2)
    scenario_2.tags = ["apple", "banana"]
    _ScenarioManager._set_primary(scenario_3)
    scenario_3.tags = ["banana", "kiwi"]
    _ScenarioManager._set_primary(scenario_4)

    all_scenarios = tp.get_scenarios()
    assert not_primary_scenario in all_scenarios

    primary_scenarios = _ScenarioManager._get_primary_scenarios()
    assert not_primary_scenario not in primary_scenarios

    primary_scenarios_sorted_by_name = [scenario_2, scenario_1, scenario_3, scenario_4]
    assert primary_scenarios_sorted_by_name == _ScenarioManager._sort_scenarios(
        primary_scenarios, descending=False, sort_key="name"
    )