-
Notifications
You must be signed in to change notification settings - Fork 5
/
SqlIdCoordinateSourceIconIT.groovy
158 lines (126 loc) · 4.85 KB
/
SqlIdCoordinateSourceIconIT.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
/*
* © 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.IconIdCoordinateFactory
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.locationtech.jts.geom.Point
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
import javax.measure.quantity.Length
@Testcontainers
class SqlIdCoordinateSourceIconIT 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/icon/coordinates.sql")
postgisSQLContainer.copyFileToContainer(sqlImportFile, "/home/coordinates_icon.sql")
// Execute import script
Container.ExecResult res = postgisSQLContainer.execInContainer("psql", "-Utest", "-f/home/coordinates_icon.sql")
assert res.stderr.empty
def connector = new SqlConnector(postgisSQLContainer.jdbcUrl, postgisSQLContainer.username, postgisSQLContainer.password)
def coordinatesFactory = new IconIdCoordinateFactory()
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 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.get(0) == expectedValues.get(0)
points.get(1) == expectedValues.get(1)
}
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 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:
for(Point point : receivedValues){
expectedValues.contains(point.coordinate)
}
}
def "A SqlIdCoordinateSource can return the nearest n coordinates if n coordinates are in the given radius"(){
given:
def basePoint = GeoUtils.buildPoint(39.617162, 1.438029)
def distance = Quantities.getQuantity(200000, Units.METRE)
when:
def actualDistances = source.getNearestCoordinates(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.getNearestCoordinates(basePoint, 2, distance)
then:
actualDistances.size() == 1
}
def "A SqlIdCoordinateSource will return the nearest n coordinates of all coordinates 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.getNearestCoordinates(basePoint, 3, distance)
then:
for(CoordinateDistance coordinateDistance : receivedValues){
expectedValues.contains(coordinateDistance.coordinateB)
}
}
}