-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest-cql-string.R
More file actions
98 lines (89 loc) · 3.48 KB
/
test-cql-string.R
File metadata and controls
98 lines (89 loc) · 3.48 KB
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
# Copyright 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
suppressPackageStartupMessages(library(sf, quietly = TRUE))
the_geom <- st_sf(st_sfc(st_point(c(1,1))))
test_that("bcdc_cql_string fails when an invalid arguments are given",{
expect_error(bcdc_cql_string(the_geom, "FOO"))
expect_error(bcdc_cql_string(quakes, "DWITHIN"))
})
test_that("bcdc_cql_string fails when used on an uncollected (promise) object", {
expect_error(
bcdc_cql_string(structure(list(), class = "bcdc_promise")),
"you need to use collect"
)
})
test_that("CQL function works", {
expect_s3_class(CQL("SELECT * FROM foo;"), c("CQL", "SQL"))
})
test_that("All cql geom predicate functions work", {
single_arg_functions <- c("EQUALS","DISJOINT","INTERSECTS",
"TOUCHES", "CROSSES", "WITHIN",
"CONTAINS", "OVERLAPS")
for (f in single_arg_functions) {
expect_equal(
do.call(f, list(the_geom)),
CQL(paste0(f, "({geom_name}, POINT (1 1))"))
)
}
expect_equal(
DWITHIN(the_geom, 1), #default units meters
CQL("DWITHIN({geom_name}, POINT (1 1), 1, meters)")
)
expect_equal(
DWITHIN(the_geom, 1, "meters"),
CQL("DWITHIN({geom_name}, POINT (1 1), 1, meters)")
)
expect_equal(
BEYOND(the_geom, 1, "feet"),
CQL("BEYOND({geom_name}, POINT (1 1), 1, feet)")
)
expect_equal(
RELATE(the_geom, "*********"),
CQL("RELATE({geom_name}, POINT (1 1), *********)")
)
expect_equal(
BBOX(c(1,2,1,2)),
CQL("BBOX({geom_name}, 1, 2, 1, 2)")
)
expect_equal(
BBOX(c(1,2,1,2), crs = 'EPSG:4326'),
CQL("BBOX({geom_name}, 1, 2, 1, 2, 'EPSG:4326')")
)
expect_equal(
BBOX(c(1,2,1,2), crs = 4326),
CQL("BBOX({geom_name}, 1, 2, 1, 2, 'EPSG:4326')")
)
})
test_that("CQL functions fail correctly", {
expect_error(EQUALS(quakes), "x is not a valid sf object")
expect_error(BEYOND(the_geom, "five"), "'distance' must be numeric")
expect_error(DWITHIN(the_geom, 5, "fathoms"), "'arg' should be one of")
expect_error(DWITHIN(the_geom, "10", "meters"), "must be numeric")
expect_error(RELATE(the_geom, "********"), "pattern") # 8 characters
expect_error(RELATE(the_geom, "********5"), "pattern") # invalid character
expect_error(RELATE(the_geom, rep("TTTTTTTTT", 2)), "pattern") # > length 1
expect_error(BBOX(c(1,2,3)), "numeric vector")
expect_error(BBOX(c("1","2","3", "4")), "numeric vector")
expect_error(BBOX(c(1,2,3,4), crs = c("EPSG:4326", "EPSG:3005")),
"must be a character string")
})
test_that("unsupported aggregation functions fail correctly", {
expect_error(filter(structure(list(cols_df = list(col_name = "x")), class = "bcdc_promise"), mean(x) > 5),
"not supported by this database")
})
test_that("passing an non-existent object to a geom predicate",{
skip_if_net_down()
skip_on_cran()
expect_error(bcdc_query_geodata("6a2fea1b-0cc4-4fc2-8017-eaf755d516da") %>%
filter(INTERSECTS(districts)),
'not found')
})