|
6 | 6 | from time import sleep, time |
7 | 7 | import sys |
8 | 8 | import pytest |
9 | | -from gridappsd import GridAPPSD, topics as t |
10 | | -# tm: added for run_simulation workaround |
| 9 | + |
| 10 | +from gridappsd import GridAPPSD |
11 | 11 | from gridappsd.simulation import Simulation |
12 | 12 | from gridappsd_docker import docker_up, docker_down |
13 | | - |
14 | | -logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) |
| 13 | +from gridappsd import GridAPPSD, topics as t |
15 | 14 |
|
16 | 15 | LOGGER = logging.getLogger(__name__) |
17 | 16 |
|
18 | | - |
19 | | -@contextmanager |
20 | | -def startup_containers(spec=None): |
21 | | - LOGGER.info('Starting gridappsd containers') |
22 | | - docker_up(spec) |
23 | | - LOGGER.info('Containers started') |
24 | | - |
25 | | - yield |
26 | | - |
27 | | - LOGGER.info('Stopping gridappsd containers') |
28 | | - # docker_down() |
29 | | - # LOGGER.info('Containers stopped') |
30 | | - |
31 | | - |
32 | | -@contextmanager |
33 | | -def gappsd() -> GridAPPSD: |
34 | | - gridappsd = GridAPPSD() |
35 | | - LOGGER.info('Gridappsd connected') |
36 | | - |
37 | | - yield gridappsd |
38 | | - |
39 | | - gridappsd.disconnect() |
40 | | - LOGGER.info('Gridappsd disconnected') |
41 | | - |
| 17 | +result_weather_data = [] |
| 18 | +result_timeseries_query = [] |
| 19 | +result_sensor_query = [] |
42 | 20 |
|
43 | 21 | @pytest.mark.parametrize("sim_config_file, sim_result_file", [ |
44 | 22 | ("9500-timeseries-config.json", "9500-simulation.json") |
45 | 23 | # ("123-config.json", "123-simulation.json"), |
46 | 24 | # ("13-node-config.json", "13-node-sim.json"), |
47 | 25 | # , ("t3-p1-config.json", "t3-p1.json"), |
48 | 26 | ]) |
49 | | -def test_timeseries_output(sim_config_file, sim_result_file): |
| 27 | +def test_timeseries_output(gridappsd_client, sim_config_file, sim_result_file): |
| 28 | + global result_weather_data |
| 29 | + global result_timeseries_query |
| 30 | + global result_sensor_query |
50 | 31 | simulation_id = None |
51 | 32 | sim_config_file = os.path.join(os.path.dirname(__file__), f"simulation_config_files/{sim_config_file}") |
52 | 33 | sim_result_file = os.path.join(os.path.dirname(__file__), f"simulation_baseline_files/{sim_result_file}") |
53 | | - # sim_test_config = os.path.join(os.path.dirname(__file__), f"simulation_baseline_files/{sim_test_file}") |
54 | 34 |
|
55 | 35 | assert os.path.exists(sim_config_file), f"File {sim_config_file} must exist to run simulation test" |
56 | | - # assert os.path.exists(sim_result_file), f"File {sim_result_file} must exist to run simulation test" |
57 | | - |
58 | | - with startup_containers(): |
59 | | - with gappsd() as gapps: |
60 | | - os.makedirs("/tmp/output", exist_ok=True) |
61 | | - with open("/tmp/output/simulation.json", 'w') as outfile: |
62 | | - sim_complete = False |
63 | | - rcvd_measurement = False |
64 | | - |
65 | | - def onmeasurement(sim, timestep, measurements): |
66 | | - nonlocal rcvd_measurement |
67 | | - # print(rcvd_measurement) |
68 | | - if not rcvd_measurement: |
69 | | - # print(f"A measurement happened at {timestep}") |
70 | | - LOGGER.info('Measurement received at %s', timestep) |
71 | | - outfile.write(f"{timestep}|{json.dumps(measurements)}\n") |
72 | | - |
73 | | - with open(sim_config_file) as fp: |
74 | | - run_config = json.load(fp) |
75 | | - starttime = run_config["simulation_config"]["start_time"] |
76 | | - # run_config["simulation_config"]["start_time"] = str(starttime) |
77 | | - print(run_config["simulation_config"]["start_time"]) |
78 | | - |
79 | | - def onfinishsimulation(sim): |
80 | | - nonlocal sim_complete |
81 | | - sim_complete = True |
82 | | - LOGGER.info('Simulation Complete') |
83 | | - |
84 | | - sim = Simulation(gapps, run_config) |
85 | | - sim.add_oncomplete_callback(onfinishsimulation) |
86 | | - sim.add_onmesurement_callback(onmeasurement) |
87 | | - sim.start_simulation() |
88 | | - LOGGER.info("Starting simulation") |
89 | | - print(sim.simulation_id) |
90 | | - |
91 | | - with open("./simulation_config_files/weather_data.json", 'r') as g: |
92 | | - LOGGER.info('Querying weather data from timeseries') |
93 | | - query1 = json.load(g) |
94 | | - |
95 | | - a = gapps.get_response(t.TIMESERIES, query1, timeout=60) |
96 | | - LOGGER.info('Weather data received ') |
97 | | - assert "Diffuse" in a["data"][0], "Weather data query does not have expected output" |
98 | | - LOGGER.info('Weather data query has expected output') |
99 | | - |
100 | | - while not sim_complete: |
101 | | - sleep(5) |
102 | | - |
103 | | - with open("./simulation_config_files/timeseries_query.json", 'r') as f: |
104 | | - query2 = json.load(f) |
105 | | - query2["queryFilter"]["simulation_id"] = sim.simulation_id |
106 | | - LOGGER.info('Querying simulation data from timeseries') |
107 | | - q = gapps.get_response(t.TIMESERIES, query2, timeout=300) |
108 | | - LOGGER.info('Simulation data received for Timeseries API') |
109 | | - file2 = open("./out-input.txt", 'w') |
110 | | - file2.write(json.dumps(q)) |
111 | | - assert "hasSimulationMessageType" in q["data"][0], "Simulation data query does not have expected output" |
112 | | - LOGGER.info('Simulation data query has expected output') |
113 | | - |
114 | | - with open("./simulation_config_files/sensor_query.json", 'r') as file: |
115 | | - sensor_query = json.load(file) |
116 | | - sensor_query["queryFilter"]["simulation_id"] = sim.simulation_id |
117 | | - LOGGER.info('Querying GridAPPS-D sensor simulator data from timeseries') |
118 | | - result = gapps.get_response(t.TIMESERIES, sensor_query, timeout=300) |
119 | | - assert "hasSimulationMessageType" in result["data"][0], "Sensor simulator data does not have expected output" |
120 | | - LOGGER.info('Query response received for GridAPPS-D sensor simulator data from timeseries') |
| 36 | + |
| 37 | + gapps = gridappsd_client |
| 38 | + sim_complete = False |
| 39 | + rcvd_measurement = False |
| 40 | + |
| 41 | + def onmeasurement(sim, timestep, measurements): |
| 42 | + LOGGER.info('Measurement received at %s', timestep) |
| 43 | + |
| 44 | + def onfinishsimulation(sim): |
| 45 | + nonlocal sim_complete |
| 46 | + sim_complete = True |
| 47 | + LOGGER.info('Simulation Complete') |
| 48 | + |
| 49 | + with open(sim_config_file) as fp: |
| 50 | + LOGGER.info('Loading config') |
| 51 | + run_config = json.load(fp) |
| 52 | + LOGGER.info(f'Simulation start time {run_config["simulation_config"]["start_time"]}') |
| 53 | + |
| 54 | + LOGGER.info('Starting the simulation') |
| 55 | + sim = Simulation(gapps, run_config) |
| 56 | + sim.start_simulation() |
| 57 | + LOGGER.info(f'Simulation id {sim.simulation_id}') |
| 58 | + |
| 59 | + LOGGER.info('sim.add_oncomplete_callback') |
| 60 | + sim.add_oncomplete_callback(onfinishsimulation) |
| 61 | + |
| 62 | + LOGGER.info('sim.add_onmeasurement_callback') |
| 63 | + sim.add_onmesurement_callback(onmeasurement) |
| 64 | + |
| 65 | + with open("./simulation_config_files/weather_data.json", 'r') as g: |
| 66 | + LOGGER.info('Querying weather data from timeseries') |
| 67 | + query1 = json.load(g) |
| 68 | + result_weather_data = gapps.get_response(t.TIMESERIES, query1, timeout=60) |
| 69 | + LOGGER.info('Weather data received ') |
| 70 | + |
| 71 | + while not sim_complete: |
| 72 | + LOGGER.info('Sleeping') |
| 73 | + sleep(5) |
| 74 | + else: |
| 75 | + with open("./simulation_config_files/timeseries_query.json", 'r') as f: |
| 76 | + query2 = json.load(f) |
| 77 | + query2["queryFilter"]["simulation_id"] = sim.simulation_id |
| 78 | + LOGGER.info('Querying simulation data from timeseries') |
| 79 | + result_timeseries_query = gapps.get_response(t.TIMESERIES, query2, timeout=300) |
| 80 | + LOGGER.info('Simulation data received for Timeseries API') |
| 81 | + |
| 82 | + with open("./simulation_config_files/sensor_query.json", 'r') as file: |
| 83 | + sensor_query = json.load(file) |
| 84 | + sensor_query["queryFilter"]["simulation_id"] = sim.simulation_id |
| 85 | + LOGGER.info('Querying GridAPPS-D sensor simulator data from timeseries') |
| 86 | + result_sensor_query = gapps.get_response(t.TIMESERIES, sensor_query, timeout=300) |
| 87 | + LOGGER.info('Simulation data received for sensor simulator') |
| 88 | + |
| 89 | +def test_weather_api(): |
| 90 | + global result_weather_data |
| 91 | + assert "Diffuse" in result_weather_data["data"][0], \ |
| 92 | + "Weather data query does not have expected output" |
| 93 | + LOGGER.info('Weather data query has expected output') |
| 94 | + |
| 95 | +def test_timeseries_simulation_api(): |
| 96 | + global result_timeseries_query |
| 97 | + assert "hasSimulationMessageType" in result_timeseries_query["data"][0], \ |
| 98 | + "Simulation data query does not have expected output" |
| 99 | + LOGGER.info('Simulation data query has expected output') |
| 100 | + |
| 101 | +def test_sensor_simulator_api(): |
| 102 | + global result_sensor_query |
| 103 | + assert "hasSimulationMessageType" in result_sensor_query["data"][0], \ |
| 104 | + "Sensor simulator data does not have expected output" |
| 105 | + LOGGER.info('Query response received for GridAPPS-D sensor simulator data from timeseries') |
0 commit comments