|
| 1 | +import csv |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | + |
| 7 | +from waveform_editor.waveform import Waveform |
| 8 | +from waveform_editor.waveform_exporter import WaveformExporter |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def waveform_list(): |
| 13 | + return [ |
| 14 | + { |
| 15 | + "user_type": "linear", |
| 16 | + "user_from": 0, |
| 17 | + "user_to": 8, |
| 18 | + "user_duration": 5, |
| 19 | + "line_number": 1, |
| 20 | + }, |
| 21 | + { |
| 22 | + "user_type": "sine-wave", |
| 23 | + "user_base": 8, |
| 24 | + "user_amplitude": 2, |
| 25 | + "user_frequency": 1, |
| 26 | + "user_duration": 4, |
| 27 | + "line_number": 2, |
| 28 | + }, |
| 29 | + { |
| 30 | + "user_type": "constant", |
| 31 | + "user_value": 8, |
| 32 | + "user_duration": 3, |
| 33 | + "line_number": 3, |
| 34 | + }, |
| 35 | + { |
| 36 | + "user_type": "smooth", |
| 37 | + "user_from": 8, |
| 38 | + "user_to": 0, |
| 39 | + "user_duration": 2, |
| 40 | + "line_number": 4, |
| 41 | + }, |
| 42 | + ] |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def waveform(waveform_list): |
| 47 | + return Waveform(waveform=waveform_list) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def times(): |
| 52 | + return np.array([0, 1, 2, 3, 4, 5]) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def exporter(waveform, times): |
| 57 | + return WaveformExporter(waveform, times) |
| 58 | + |
| 59 | + |
| 60 | +def test_parse_uri(exporter): |
| 61 | + """Test if URIs are parsed correctly.""" |
| 62 | + uri = "imas:hdf5?path=./testdb#ec_launchers/beam(1)/power_launched" |
| 63 | + parsed_uri = exporter.parse_uri(uri) |
| 64 | + assert parsed_uri == ( |
| 65 | + "imas:hdf5?path=./testdb", |
| 66 | + "ec_launchers", |
| 67 | + 0, |
| 68 | + "/beam(1)/power_launched", |
| 69 | + ) |
| 70 | + uri2 = "imas:hdf5?path=./testdb/test_loc#ec_launchers:1/beam(1)/power_launched" |
| 71 | + parsed_uri = exporter.parse_uri(uri2) |
| 72 | + assert parsed_uri == ( |
| 73 | + "imas:hdf5?path=./testdb/test_loc", |
| 74 | + "ec_launchers", |
| 75 | + 1, |
| 76 | + "/beam(1)/power_launched", |
| 77 | + ) |
| 78 | + |
| 79 | + uri3 = "imas:hdf5?path=./test_db#equilibrium/time_slice()/boundary/elongation" |
| 80 | + parsed_uri = exporter.parse_uri(uri3) |
| 81 | + assert parsed_uri == ( |
| 82 | + "imas:hdf5?path=./test_db", |
| 83 | + "equilibrium", |
| 84 | + 0, |
| 85 | + "/time_slice()/boundary/elongation", |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +def test_to_csv(exporter, tmp_path): |
| 90 | + """Test if exported CSV exists and is filled with correct times and values.""" |
| 91 | + file_path = tmp_path / "waveform.csv" |
| 92 | + exporter.to_csv(file_path) |
| 93 | + |
| 94 | + with open(file_path, newline="") as csvfile: |
| 95 | + reader = csv.reader(csvfile) |
| 96 | + rows = list(reader) |
| 97 | + |
| 98 | + assert Path(file_path).exists() |
| 99 | + assert len(rows) == len(exporter.times) + 1 # Header + data rows |
| 100 | + for i, (time, value) in enumerate(zip(exporter.times, exporter.values), start=1): |
| 101 | + assert float(rows[i][0]) == time |
| 102 | + assert float(rows[i][1]) == value |
| 103 | + |
| 104 | + |
| 105 | +def test_to_png(exporter, tmp_path): |
| 106 | + """Test if exported PNG exists.""" |
| 107 | + file_path = tmp_path / "waveform.png" |
| 108 | + exporter.to_png(file_path) |
| 109 | + assert Path(file_path).exists() |
| 110 | + |
| 111 | + |
| 112 | +# TODO: Write tests for exporting to IDS, this requires IMASPy as a dependency |
| 113 | +# def test_to_ids(exporter): |
| 114 | +# pass |
0 commit comments