-
Notifications
You must be signed in to change notification settings - Fork 5
/
InfluxDbWeatherSourceCosmoIT.groovy
156 lines (135 loc) · 7.36 KB
/
InfluxDbWeatherSourceCosmoIT.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
/*
* © 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.influxdb
import edu.ie3.datamodel.io.connectors.InfluxDbConnector
import edu.ie3.datamodel.io.factory.timeseries.CosmoTimeBasedWeatherValueFactory
import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries
import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue
import edu.ie3.datamodel.models.value.WeatherValue
import edu.ie3.test.common.CosmoWeatherTestData
import edu.ie3.test.helper.TestContainerHelper
import edu.ie3.test.helper.WeatherSourceTestHelper
import edu.ie3.util.geo.GeoUtils
import edu.ie3.util.interval.ClosedInterval
import org.locationtech.jts.geom.Point
import org.testcontainers.containers.Container
import org.testcontainers.containers.InfluxDBContainer
import org.testcontainers.spock.Testcontainers
import org.testcontainers.utility.DockerImageName
import org.testcontainers.utility.MountableFile
import spock.lang.Shared
import spock.lang.Specification
@Testcontainers
class InfluxDbWeatherSourceCosmoIT extends Specification implements TestContainerHelper, WeatherSourceTestHelper {
@Shared
InfluxDBContainer influxDbContainer = new InfluxDBContainer(DockerImageName.parse("influxdb").withTag("1.8.10"))
.withAuthEnabled(false)
.withDatabase("test_weather")
@Shared
InfluxDbWeatherSource source
def setupSpec() {
// Copy import file into docker and then import it via influx CLI
// more information on file format and usage here: https://docs.influxdata.com/influxdb/v1.7/tools/shell/#import-data-from-a-file-with-import
MountableFile influxWeatherImportFile = getMountableFile("_weather/cosmo/weather.txt")
influxDbContainer.copyFileToContainer(influxWeatherImportFile, "/home/weather_cosmo.txt")
Container.ExecResult res = influxDbContainer.execInContainer("influx", "-import", "-path=/home/weather_cosmo.txt", "-precision=ms")
assert res.stderr.empty
def connector = new InfluxDbConnector(influxDbContainer.url, "test_weather", "test_scenario")
def weatherFactory = new CosmoTimeBasedWeatherValueFactory()
source = new InfluxDbWeatherSource(connector, CosmoWeatherTestData.coordinateSource, weatherFactory)
}
def "The test container can establish a valid connection"() {
when:
def connector = new InfluxDbConnector(influxDbContainer.url,"test_weather", "test_scenario")
then:
connector.isConnectionValid()
}
def "An InfluxDbWeatherSource 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 "An InfluxDbWeatherSource 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 timeseries_193186 = 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 timeseries_193187 = 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), timeseries_193186)
equalsIgnoreUUID(coordinateToTimeSeries.get(CosmoWeatherTestData.COORDINATE_193187), timeseries_193187)
}
def "An InfluxDbWeatherSource can read all weather data in a given time interval"() {
given:
def timeInterval = new ClosedInterval(CosmoWeatherTestData.TIME_15H, CosmoWeatherTestData.TIME_17H)
def timeseries_193186 = 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 timeseries_193187 = 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 timeseries_193188 = 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, timeseries_193186.entries)
equalsIgnoreUUID(coordinateToTimeSeries.get(CosmoWeatherTestData.COORDINATE_193187).entries, timeseries_193187.entries)
equalsIgnoreUUID(coordinateToTimeSeries.get(CosmoWeatherTestData.COORDINATE_193188).entries, timeseries_193188.entries)
}
def "An InfluxDbWeatherSource will return an equivalent to 'empty' when being unable to map a coordinate to its ID"() {
given:
def validCoordinate = CosmoWeatherTestData.COORDINATE_193186
def invalidCoordinate = GeoUtils.buildPoint(7d, 48d)
def time = CosmoWeatherTestData.TIME_15H
def timeInterval = new ClosedInterval(CosmoWeatherTestData.TIME_15H, CosmoWeatherTestData.TIME_17H)
def emptyTimeSeries = new IndividualTimeSeries(UUID.randomUUID(), Collections.emptySet())
def timeseries_193186 = 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>)
when:
def coordinateAtDate = source.getWeather(time, invalidCoordinate)
def coordinateInInterval = source.getWeather(timeInterval, invalidCoordinate)
def coordinatesToTimeSeries = source.getWeather(timeInterval, [
validCoordinate,
invalidCoordinate
])
then:
coordinateAtDate == Optional.empty()
equalsIgnoreUUID(coordinateInInterval, emptyTimeSeries)
coordinatesToTimeSeries.keySet() == [validCoordinate].toSet()
equalsIgnoreUUID(coordinatesToTimeSeries.get(validCoordinate), timeseries_193186)
}
}