-
Notifications
You must be signed in to change notification settings - Fork 5
/
SqlIdCoordinateSourceIT.groovy
190 lines (152 loc) · 5.81 KB
/
SqlIdCoordinateSourceIT.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
/*
* © 2022. 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.sql
import edu.ie3.datamodel.io.connectors.SqlConnector
import edu.ie3.datamodel.io.factory.timeseries.SqlIdCoordinateFactory
import edu.ie3.test.helper.TestContainerHelper
import edu.ie3.util.geo.CoordinateDistance
import edu.ie3.util.geo.GeoUtils
import org.locationtech.jts.geom.Coordinate
import org.testcontainers.containers.Container
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.spock.Testcontainers
import org.testcontainers.utility.DockerImageName
import org.testcontainers.utility.MountableFile
import spock.lang.Shared
import spock.lang.Specification
import tech.units.indriya.quantity.Quantities
import tech.units.indriya.unit.Units
@Testcontainers
class SqlIdCoordinateSourceIT extends Specification implements TestContainerHelper {
@Shared
PostgreSQLContainer postgisSQLContainer = new PostgreSQLContainer(DockerImageName.parse("postgis/postgis:14-3.3").asCompatibleSubstituteFor("postgres"))
@Shared
SqlIdCoordinateSource source
static String schemaName = "public"
static String coordinateTableName = "coordinates"
def setupSpec() {
// Copy sql import script into docker
MountableFile sqlImportFile = getMountableFile("_coordinates/coordinates.sql")
postgisSQLContainer.copyFileToContainer(sqlImportFile, "/home/coordinates.sql")
// Execute import script
Container.ExecResult res = postgisSQLContainer.execInContainer("psql", "-Utest", "-f/home/coordinates.sql")
assert res.stderr.empty
def connector = new SqlConnector(postgisSQLContainer.jdbcUrl, postgisSQLContainer.username, postgisSQLContainer.password)
def coordinatesFactory = new SqlIdCoordinateFactory()
source = new SqlIdCoordinateSource(connector, schemaName, coordinateTableName, coordinatesFactory)
}
def "A SqlIdCoordinateSource can read a single coordinate"(){
given:
def expectedValue = new Coordinate(7.438, 51.5)
when:
def receivedValue = source.getCoordinate(67775)
then:
def coordinate = receivedValue.get().coordinate
coordinate == expectedValue
}
def "A SqlIdCoordinateSource will return nothing if an id is not present"(){
given:
def receivedValue = source.getCoordinate(0)
expect:
receivedValue.isEmpty()
}
def "A SqlIdCoordinateSource can read a list of coordinates"(){
given:
def expectedValues = [
new Coordinate(7.438, 51.5),
new Coordinate(7.375, 51.5)
]
when:
int[] arr = new int[]{
67775, 531137
}
def receivedValues = source.getCoordinates(arr)
then:
ArrayList<Coordinate> points = receivedValues.coordinate
points == expectedValues
}
def "A SqlIdCoordinateSource can return the id of a point"(){
given:
int id = 67775
when:
def receivedValue = source.getId(GeoUtils.buildPoint(51.5, 7.438))
then:
receivedValue.get() == id
}
def "A SqlIdCoordinateSource will return nothing if a coordinate is not present"(){
given:
def coordinate = GeoUtils.buildPoint(0.0 ,0.0 )
when:
def receivedValue = source.getId(coordinate)
then:
receivedValue.isEmpty()
}
def "A SqlIdCoordinateSource can return all coordinates"() {
given:
def expectedValues = [
GeoUtils.buildPoint(51.5,7.438),
GeoUtils.buildPoint(51.5,7.375),
GeoUtils.buildPoint(51.438,7.438),
GeoUtils.buildPoint(51.438,7.375)
]
when:
def receivedValues = source.getAllCoordinates()
then:
receivedValues == expectedValues
}
def "A SqlIdCoordinateSource can return the nearest n coordinates if n coordinates are in the given radius"(){
given:
def basePoint = GeoUtils.buildPoint(51.5, 7.38)
def distance = Quantities.getQuantity(200000, Units.METRE)
when:
def actualDistances = source.getClosestCoordinates(basePoint, 3, distance)
then:
actualDistances.size() == 3
}
def "A SqlIdCoordinateSource will return the nearest m coordinates if less than n coordinates are in the given radius"(){
given:
def basePoint = GeoUtils.buildPoint(51.5, 7.38)
def distance = Quantities.getQuantity(1000, Units.METRE)
when:
def actualDistances = source.getClosestCoordinates(basePoint, 2, distance)
then:
actualDistances.size() == 1
}
def "A SqlIdCoordinateSource will return the nearest n coordinates of the nearest n neighbours if no coordinates are in the given radius" () {
given:
def basePoint = GeoUtils.buildPoint(39.617162, 1.438029)
def expectedValues = [
GeoUtils.buildPoint(51.5,7.438),
GeoUtils.buildPoint(51.5,7.375),
GeoUtils.buildPoint(51.438,7.438),
GeoUtils.buildPoint(51.438,7.375)
]
def distance = Quantities.getQuantity(1000, Units.METRE)
when:
def receivedValues = source.getClosestCoordinates(basePoint, 2, distance)
then:
for(CoordinateDistance coordinateDistance : receivedValues){
expectedValues.contains(coordinateDistance.coordinateB)
}
}
def "A SqlIdCoordinateSource will return the nearest n coordinates of all available coordinates if no coordinates are in the given radius and n is greater than the number of all coordinates"(){
given:
def basePoint = GeoUtils.buildPoint(39.617162, 1.438029)
def expectedValues = [
GeoUtils.buildPoint(51.5,7.438),
GeoUtils.buildPoint(51.5,7.375),
GeoUtils.buildPoint(51.438,7.438),
GeoUtils.buildPoint(51.438,7.375)
]
def distance = Quantities.getQuantity(1000, Units.METRE)
when:
def receivedValues = source.getClosestCoordinates(basePoint, 5, distance)
then:
for(CoordinateDistance coordinateDistance : receivedValues){
expectedValues.contains(coordinateDistance.coordinateB)
}
}
}