-
Notifications
You must be signed in to change notification settings - Fork 5
/
CsvWeatherSourceCosmoTest.groovy
229 lines (201 loc) · 9.59 KB
/
CsvWeatherSourceCosmoTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io.source.csv
import static edu.ie3.datamodel.models.StandardUnits.*
import edu.ie3.datamodel.io.factory.timeseries.CosmoTimeBasedWeatherValueFactory
import edu.ie3.datamodel.io.naming.FileNamingStrategy
import edu.ie3.datamodel.io.source.IdCoordinateSource
import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries
import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue
import edu.ie3.datamodel.models.value.SolarIrradianceValue
import edu.ie3.datamodel.models.value.TemperatureValue
import edu.ie3.datamodel.models.value.WeatherValue
import edu.ie3.datamodel.models.value.WindValue
import edu.ie3.test.common.CosmoWeatherTestData
import edu.ie3.test.helper.WeatherSourceTestHelper
import edu.ie3.util.TimeUtil
import edu.ie3.util.geo.GeoUtils
import edu.ie3.util.interval.ClosedInterval
import org.locationtech.jts.geom.Coordinate
import org.locationtech.jts.geom.Point
import spock.lang.Shared
import spock.lang.Specification
import tech.units.indriya.quantity.Quantities
class CsvWeatherSourceCosmoTest extends Specification implements CsvTestDataMeta, WeatherSourceTestHelper {
@Shared
CsvWeatherSource source
@Shared
IdCoordinateSource coordinateSource
def setupSpec() {
coordinateSource = CosmoWeatherTestData.coordinateSource
def weatherFactory = new CosmoTimeBasedWeatherValueFactory()
source = new CsvWeatherSource(";", weatherCosmoFolderPath, new FileNamingStrategy(), coordinateSource, weatherFactory)
}
def "A CsvWeatherSource can read and correctly parse a single value for a specific date and coordinate"() {
given:
def expectedTimeBasedValue = new TimeBasedValue(CosmoWeatherTestData.TIME_15H, CosmoWeatherTestData.WEATHER_VALUE_193186_15H)
when:
def optTimeBasedValue = source.getWeather(CosmoWeatherTestData.TIME_15H, CosmoWeatherTestData.COORDINATE_193186)
then:
optTimeBasedValue.present
equalsIgnoreUUID(optTimeBasedValue.get(), expectedTimeBasedValue)
}
def "A CsvWeatherSource can read multiple time series values for multiple coordinates"() {
given:
def coordinates = [
CosmoWeatherTestData.COORDINATE_193186,
CosmoWeatherTestData.COORDINATE_193187
]
def timeInterval = new ClosedInterval(CosmoWeatherTestData.TIME_16H, CosmoWeatherTestData.TIME_17H)
def timeSeries193186 = new IndividualTimeSeries(null,
[
new TimeBasedValue(CosmoWeatherTestData.TIME_16H, CosmoWeatherTestData.WEATHER_VALUE_193186_16H),
new TimeBasedValue(CosmoWeatherTestData.TIME_17H, CosmoWeatherTestData.WEATHER_VALUE_193186_17H)
]
as Set<TimeBasedValue>)
def timeSeries193187 = new IndividualTimeSeries(null,
[
new TimeBasedValue(CosmoWeatherTestData.TIME_16H, CosmoWeatherTestData.WEATHER_VALUE_193187_16H)
] as Set<TimeBasedValue>)
when:
Map<Point, IndividualTimeSeries<WeatherValue>> coordinateToTimeSeries = source.getWeather(timeInterval, coordinates)
then:
coordinateToTimeSeries.keySet().size() == 2
equalsIgnoreUUID(coordinateToTimeSeries.get(CosmoWeatherTestData.COORDINATE_193186), timeSeries193186)
equalsIgnoreUUID(coordinateToTimeSeries.get(CosmoWeatherTestData.COORDINATE_193187), timeSeries193187)
}
def "A CsvWeatherSource can read all weather data in a given time interval"() {
given:
def timeInterval = new ClosedInterval(CosmoWeatherTestData.TIME_15H, CosmoWeatherTestData.TIME_17H)
def timeSeries193186 = new IndividualTimeSeries(null,
[
new TimeBasedValue(CosmoWeatherTestData.TIME_15H, CosmoWeatherTestData.WEATHER_VALUE_193186_15H),
new TimeBasedValue(CosmoWeatherTestData.TIME_16H, CosmoWeatherTestData.WEATHER_VALUE_193186_16H),
new TimeBasedValue(CosmoWeatherTestData.TIME_17H, CosmoWeatherTestData.WEATHER_VALUE_193186_17H)
] as Set<TimeBasedValue>)
def timeSeries193187 = new IndividualTimeSeries(null,
[
new TimeBasedValue(CosmoWeatherTestData.TIME_15H, CosmoWeatherTestData.WEATHER_VALUE_193187_15H),
new TimeBasedValue(CosmoWeatherTestData.TIME_16H, CosmoWeatherTestData.WEATHER_VALUE_193187_16H)
] as Set<TimeBasedValue>)
def timeSeries193188 = new IndividualTimeSeries(null,
[
new TimeBasedValue(CosmoWeatherTestData.TIME_15H, CosmoWeatherTestData.WEATHER_VALUE_193188_15H)
] as Set<TimeBasedValue>)
when:
Map<Point, IndividualTimeSeries<WeatherValue>> coordinateToTimeSeries = source.getWeather(timeInterval)
then:
coordinateToTimeSeries.keySet().size() == 3
equalsIgnoreUUID(coordinateToTimeSeries.get(CosmoWeatherTestData.COORDINATE_193186).entries, timeSeries193186.entries)
equalsIgnoreUUID(coordinateToTimeSeries.get(CosmoWeatherTestData.COORDINATE_193187).entries, timeSeries193187.entries)
equalsIgnoreUUID(coordinateToTimeSeries.get(CosmoWeatherTestData.COORDINATE_193188).entries, timeSeries193188.entries)
}
def "The CsvWeatherSource is able to build a single WeatherValue from field to value mapping"() {
given:
def defaultCoordinate = GeoUtils.DEFAULT_GEOMETRY_FACTORY.createPoint(new Coordinate(7.4116482, 51.4843281))
def coordinateSource = Mock(IdCoordinateSource)
coordinateSource.getCoordinate(_) >> { args -> args[0] == 5 ? Optional.of(defaultCoordinate) : Optional.empty() }
def weatherFactory = new CosmoTimeBasedWeatherValueFactory()
def source = new CsvWeatherSource(";", weatherCosmoFolderPath, new FileNamingStrategy(), coordinateSource, weatherFactory)
def fieldToValues = [
"uuid" : "71a79f59-eebf-40c1-8358-ba7414077d57",
"time" : "2020-10-16T12:40:42Z",
"coordinateId" : "5",
"directIrradiance" : "1.234",
"diffuseIrradiance": "5.678",
"temperature" : "9.1011",
"windVelocity" : "12.1314",
"windDirection" : "15.1617"
]
def expectedValue = new TimeBasedValue(
UUID.fromString("71a79f59-eebf-40c1-8358-ba7414077d57"),
TimeUtil.withDefaults.toZonedDateTime("2020-10-16T12:40:42+01:00"),
new WeatherValue(
defaultCoordinate,
new SolarIrradianceValue(
Quantities.getQuantity(1.234, SOLAR_IRRADIANCE),
Quantities.getQuantity(5.678, SOLAR_IRRADIANCE)
),
new TemperatureValue(
Quantities.getQuantity(9.1011, TEMPERATURE)
),
new WindValue(
Quantities.getQuantity(12.1314, WIND_DIRECTION),
Quantities.getQuantity(15.1617, WIND_VELOCITY)
)
)
)
when:
def actual = source.buildWeatherValue(fieldToValues)
then:
actual.present
actual.get() == expectedValue
}
def "The CsvWeatherSource returns no WeatherValue, if the coordinate field is empty"() {
given:
def defaultCoordinate = GeoUtils.DEFAULT_GEOMETRY_FACTORY.createPoint(new Coordinate(7.4116482, 51.4843281))
def coordinateSource = Mock(IdCoordinateSource)
coordinateSource.getCoordinate(_) >> { args -> args[0] == 5 ? Optional.of(defaultCoordinate) : Optional.empty() }
def weatherFactory = new CosmoTimeBasedWeatherValueFactory()
def source = new CsvWeatherSource(";", weatherCosmoFolderPath, new FileNamingStrategy(), coordinateSource, weatherFactory)
def fieldToValues = [
"uuid" : "71a79f59-eebf-40c1-8358-ba7414077d57",
"time" : "2020-10-16T12:40:42Z",
"coordinateid" : "",
"directirradiance" : "1.234",
"diffuseirradiance": "5.678",
"temperature" : "9.1011",
"windvelocity" : "12.1314",
"winddirection" : "15.1617"
]
when:
def actual = source.buildWeatherValue(fieldToValues)
then:
actual.empty
}
def "The CsvWeatherSource returns no WeatherValue, if the coordinate field is missing"() {
given:
def defaultCoordinate = GeoUtils.DEFAULT_GEOMETRY_FACTORY.createPoint(new Coordinate(7.4116482, 51.4843281))
def coordinateSource = Mock(IdCoordinateSource)
coordinateSource.getCoordinate(_) >> { args -> args[0] == 5 ? Optional.of(defaultCoordinate) : Optional.empty() }
def weatherFactory = new CosmoTimeBasedWeatherValueFactory()
def source = new CsvWeatherSource(";", weatherCosmoFolderPath, new FileNamingStrategy(), coordinateSource, weatherFactory)
def fieldToValues = [
"uuid" : "71a79f59-eebf-40c1-8358-ba7414077d57",
"time" : "2020-10-16T12:40:42Z",
"directirradiance" : "1.234",
"diffuseirradiance": "5.678",
"temperature" : "9.1011",
"windvelocity" : "12.1314",
"winddirection" : "15.1617"
]
when:
def actual = source.buildWeatherValue(fieldToValues)
then:
actual.empty
}
def "The CsvWeatherSource returns no WeatherValue, if the coordinate cannot be obtained"() {
given:
def coordinateSource = Mock(IdCoordinateSource)
coordinateSource.getCoordinate(_) >> Optional.empty()
def weatherFactory = new CosmoTimeBasedWeatherValueFactory()
def source = new CsvWeatherSource(";", weatherCosmoFolderPath, new FileNamingStrategy(), coordinateSource, weatherFactory)
def fieldToValues = [
"uuid" : "71a79f59-eebf-40c1-8358-ba7414077d57",
"time" : "2020-10-16T12:40:42Z",
"coordinateid" : "6",
"directirradiance" : "1.234",
"diffuseirradiance": "5.678",
"temperature" : "9.1011",
"windvelocity" : "12.1314",
"winddirection" : "15.1617"
]
when:
def actual = source.buildWeatherValue(fieldToValues)
then:
actual.empty
}
}