-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Macr0Nerd/add_tests
Add tests
- Loading branch information
Showing
9 changed files
with
171 additions
and
30 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
simulation_id = "Pytest Simulation" | ||
algorithms_directory = "examples/algorithms/" | ||
nodes = [ { node_id = "node_1", algorithm = { file = "simple.py", object = "AlwaysCooperate" } }, | ||
{ node_id = "node_2", algorithm = { file = "simple.py", object = "AlwaysDefect" } }, | ||
{ node_id = "node_3", algorithm = { file = "tit_for_tat.py", object = "TitForTat" } }, ] | ||
rounds_data = "examples/rounds/pytest.json" | ||
rounds_output = "examples/rounds/pytest.json" | ||
simulation = { object = "StandardSimulation" } | ||
simulation_arguments = { rounds = 10 } | ||
simulation_output = "examples/results/pytest.json" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"node_1": 30, "node_2": 100, "node_3": 30} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"node_1:node_2": [{"node_1": true, "node_2": false}, {"node_1": true, "node_2": false}, {"node_1": true, "node_2": false}, {"node_1": true, "node_2": false}, {"node_1": true, "node_2": false}, {"node_1": true, "node_2": false}, {"node_1": true, "node_2": false}, {"node_1": true, "node_2": false}, {"node_1": true, "node_2": false}, {"node_1": true, "node_2": false}], "node_1:node_3": [{"node_1": true, "node_3": true}, {"node_1": true, "node_3": true}, {"node_1": true, "node_3": true}, {"node_1": true, "node_3": true}, {"node_1": true, "node_3": true}, {"node_1": true, "node_3": true}, {"node_1": true, "node_3": true}, {"node_1": true, "node_3": true}, {"node_1": true, "node_3": true}, {"node_1": true, "node_3": true}], "node_2:node_3": [{"node_2": false, "node_3": true}, {"node_2": false, "node_3": true}, {"node_2": false, "node_3": true}, {"node_2": false, "node_3": true}, {"node_2": false, "node_3": true}, {"node_2": false, "node_3": true}, {"node_2": false, "node_3": true}, {"node_2": false, "node_3": true}, {"node_2": false, "node_3": true}, {"node_2": false, "node_3": true}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import json | ||
|
||
import pytest | ||
|
||
import project_dilemma.config | ||
from project_dilemma.config import load_configuration, ProjectDilemmaConfig | ||
from project_dilemma.interfaces import Node | ||
from project_dilemma.object_loaders import create_nodes, load_algorithms, load_rounds, load_simulation | ||
from project_dilemma.simulations import StandardSimulation | ||
|
||
from algorithms.simple import AlwaysCooperate, AlwaysDefect | ||
from algorithms.tit_for_tat import TitForTat | ||
|
||
|
||
@pytest.fixture | ||
def test_configuration_loading(monkeypatch): | ||
def mock_args(): | ||
return {'config': 'examples/config/pytest.toml'} | ||
|
||
monkeypatch.setattr(project_dilemma.config, 'arguments', mock_args) | ||
|
||
expected: ProjectDilemmaConfig = { | ||
'simulation_id': 'Pytest Simulation', | ||
'algorithms_directory': 'examples/algorithms/', | ||
'nodes': [ | ||
{'node_id': 'node_1', 'algorithm': {'file': 'simple.py', 'object': 'AlwaysCooperate'}}, | ||
{'node_id': 'node_2', 'algorithm': {'file': 'simple.py', 'object': 'AlwaysDefect'}}, | ||
{'node_id': 'node_3', 'algorithm': {'file': 'tit_for_tat.py', 'object': 'TitForTat'}}, | ||
], | ||
'rounds_data': 'examples/rounds/pytest.json', | ||
'rounds_output': 'examples/rounds/pytest.json', | ||
'simulation': {'object': 'StandardSimulation'}, | ||
'simulation_arguments': {'rounds': 10}, | ||
'simulation_output': 'examples/results/pytest.json' | ||
} | ||
|
||
actual = load_configuration() | ||
|
||
assert expected == actual | ||
|
||
return actual | ||
|
||
|
||
@pytest.fixture | ||
def test_object_loading(test_configuration_loading): | ||
with open('examples/rounds/pytest.json', 'r') as f: | ||
expected_rounds = json.load(f) | ||
|
||
actual_rounds = load_rounds(test_configuration_loading) | ||
|
||
assert expected_rounds == actual_rounds | ||
|
||
expected_algorithm_map = { | ||
'AlwaysCooperate': AlwaysCooperate, | ||
'AlwaysDefect': AlwaysDefect, | ||
'TitForTat': TitForTat | ||
} | ||
|
||
actual_algorithm_map = load_algorithms(test_configuration_loading) | ||
|
||
assert False not in [ | ||
algo.algorithm_id == actual_algorithm_map[aid].algorithm_id for aid, algo in expected_algorithm_map.items() | ||
] | ||
|
||
expected_simulation = StandardSimulation | ||
|
||
actual_simulation = load_simulation(test_configuration_loading) | ||
|
||
assert expected_simulation == actual_simulation | ||
|
||
expected_nodes = [ | ||
Node('node_1', AlwaysCooperate), | ||
Node('node_2', AlwaysDefect), | ||
Node('node_3', TitForTat) | ||
] | ||
|
||
actual_nodes = create_nodes(test_configuration_loading, expected_algorithm_map) | ||
|
||
assert expected_nodes == actual_nodes | ||
|
||
return actual_simulation( | ||
simulation_id=test_configuration_loading['simulation_id'], | ||
nodes=actual_nodes, | ||
**test_configuration_loading['simulation_arguments'] | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def test_simulation_run(test_object_loading): | ||
with open('examples/rounds/pytest.json', 'r') as f: | ||
expected_rounds = json.load(f) | ||
|
||
actual_rounds = test_object_loading.run_simulation() | ||
|
||
assert expected_rounds == actual_rounds | ||
|
||
|
||
def test_simulation_process(test_object_loading): | ||
with open('examples/rounds/pytest.json', 'r') as f: | ||
rounds = json.load(f) | ||
|
||
with open('examples/results/pytest.json', 'r') as f: | ||
expected_results = json.load(f) | ||
|
||
test_object_loading.simulation_rounds = rounds | ||
|
||
actual_results = test_object_loading.process_results() | ||
|
||
assert expected_results == actual_results | ||
|
||
|