|
| 1 | +import pytest |
| 2 | +from virtual_lab import Simulation, Model, prefs |
| 3 | +from virtual_lab.const import ColorCodingStrategies |
| 4 | + |
| 5 | +def test_color_coding(): |
| 6 | + """ |
| 7 | + Initializes a Simulation and a Model with a number of variables which exceeds the |
| 8 | + number of colors defined in settings.prefs. The color_coding of the Simulation should then be set into |
| 9 | + "cycle" and "monocrome" modes and the colors should either be cycled through or all set to black. |
| 10 | + So to check if the test is successful, we check if the colors are either all black or if they are cycling through the preset colors |
| 11 | + in the ColorCoding class. |
| 12 | + """ |
| 13 | + # Create a Simulation and a Model with the default color coding |
| 14 | + sim = Simulation(load_preferences = False) |
| 15 | + |
| 16 | + # Default case (not reliant on the model) |
| 17 | + assert sim.color_coding.strategy == ColorCodingStrategies.CYCLE |
| 18 | + # Check if the colors were loaded correctly |
| 19 | + assert sim.color_coding.available_colors == prefs.color_presets |
| 20 | + |
| 21 | + N_colors = len(prefs.color_presets["var"]) |
| 22 | + model = Model("model", {f"var_{k}": 0 for k in range(N_colors + 1)}) |
| 23 | + # Add the model to the simulation |
| 24 | + sim.add_model(model) |
| 25 | + |
| 26 | + # Check after adding a new model with the correct number of variables to induce one cycle: |
| 27 | + assert sim.color_coding.available_colors['var'] == prefs.color_presets['var'] |
| 28 | + |
| 29 | + # Create a number of variables which exceeds the number of colors defined in settings.prefs |
| 30 | + for i in range(len(model.variables) - 1): |
| 31 | + model.add_variable(f"var{i}", 0) |
| 32 | + |
| 33 | + # Check if the colors are cycling correctly |
| 34 | + assert sim.color_coding.available_colors['var'] == prefs.color_presets['var'] |
| 35 | + |
| 36 | + sim.color_coding.monocrome() |
| 37 | + |
| 38 | + assert sim.color_coding.strategy == ColorCodingStrategies.MONOCROME |
| 39 | + # Check if the colors are all set to black |
| 40 | + for var_type in sim.color_coding.available_colors: |
| 41 | + assert sim.color_coding.available_colors[var_type] == ["#000000"] |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == '__main__': |
| 45 | + test_color_coding() |
| 46 | + |
0 commit comments