Skip to content

Commit

Permalink
Improve and test osmapi_objects()
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaspons committed Feb 23, 2024
1 parent 854715f commit af19900
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: osmapiR
Title: OpenStreetMap API
Version: 0.0.0.18
Version: 0.0.0.19
Authors@R:
person("Joan", "Maspons", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2286-8727"))
Expand Down
23 changes: 16 additions & 7 deletions R/osmapiR_objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,23 @@ new_osmapi_objects <- function(x) {
}


validate_osmapi_objects <- function(x) {
#' Validate `osmapi_objects`
#'
#' @param x An `osmapi_objects`
#' @param commited If `TRUE`, `x` must have columns `visible`, `version`, `changeset`, `timestamp`, `user` & `uid`.
#'
#' @return `x`
#' @noRd
validate_osmapi_objects <- function(x, commited = TRUE) {
stopifnot(inherits(x, "osmapi_objects"))
stopifnot(is.data.frame(x))

col_name <- c(
"type", "id", "visible", "version", "changeset", "timestamp", "user", "uid", "lat", "lon", "members", "tags"
)
names_ok <- setequal(names(x), col_name)
col_name <- c("type", "id", "lat", "lon", "members", "tags")
if (commited) {
col_name <- c(col_name, "visible", "version", "changeset", "timestamp", "user", "uid")
}
names_ok <- all(col_name %in% names(x))

if (!names_ok) {
stop("Missing columns: ", paste(setdiff(col_name, names(x)), collapse = ", "))
}
Expand Down Expand Up @@ -145,12 +154,12 @@ new_relation_members <- function(x) {

## tags_df ----

new_tags_df <- function(x) {
new_tags_df <- function(x = data.frame(key = character(), value = character())) {
stopifnot(is.data.frame(x))
stopifnot(c("key", "value") %in% colnames(x))
x$key <- as.character(x$key)
x$value <- as.character(x$value)

class(x) <- "tags_df"
class(x) <- c("tags_df", "data.frame")
x
}
4 changes: 2 additions & 2 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"codeRepository": "https://github.com/jmaspons/osmapiR",
"issueTracker": "https://github.com/jmaspons/osmapiR/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.0.0.18",
"version": "0.0.0.19",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down Expand Up @@ -112,7 +112,7 @@
"SystemRequirements": null
},
"keywords": ["openstreetmap", "OSM", "openstreetmap-api", "osmapi", "API", "osm", "r", "r-package"],
"fileSize": "1586.024KB",
"fileSize": "1599.885KB",
"readme": "https://github.com/jmaspons/osmapiR/blob/main/README.md",
"contIntegration": ["https://github.com/jmaspons/osmapiR/actions/workflows/R-CMD-check.yaml", "https://codecov.io/gh/jmaspons/osmapiR", "https://github.com/jmaspons/osmapiR/actions/workflows/pkgdown.yaml"],
"developmentStatus": "https://lifecycle.r-lib.org/articles/stages.html#experimental"
Expand Down
4 changes: 4 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ bcb
BEXER
BFPlFMA
Blackweir
bool
br
BugReports
byrow
cadedb
cb
cbind
Expand Down Expand Up @@ -53,6 +55,7 @@ codeRepository
colname
colspan
commentsL
commited
ComputerLanguage
config
Config
Expand Down Expand Up @@ -202,6 +205,7 @@ noRd
nowiki
nrow
nThis
num
nVJ
oauth
OAuth
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-osmapiR_objects.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
test_that("osmapi_objects works", {
x <- data.frame(
type = c("node", "node", "way", "relation"),
id = 1:4,
lat = c(0, 1, NA, NA),
lon = c(0, 1, NA, NA),
name = c(NA, NA, "My way", "Our relation"),
type.1 = c(NA, NA, NA, "Column clash!")
)
x$members <- list(
NULL, NULL, 1:2,
matrix(
c("node", "1", "node", "2", "way", "3"),
nrow = 3, ncol = 2, byrow = TRUE, dimnames = list(NULL, c("type", "ref"))
)
)

objs <- list()
objs$tag_ch <- osmapi_objects(x, tag_columns = "name")
objs$tag_num <- osmapi_objects(x, tag_columns = 5)
objs$tag_bool <- osmapi_objects(x, tag_columns = c(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE))
objs$tag_ch_named <- osmapi_objects(x, tag_columns = c(type = "type.1"))
objs$tag_num_named <- osmapi_objects(x, tag_columns = c(type = 6))

x$name <- NULL
x$tags <- list(
new_tags_df(),
new_tags_df(),
new_tags_df(data.frame(key = "name", value = "May way")),
new_tags_df(data.frame(key = "name", value = "Our relation"))
)
objs$tags <- osmapi_objects(x)

lapply(objs, function(x) {
expect_s3_class(
validate_osmapi_objects(x, commited = FALSE),
class = c("osmapi_objects", "data.frame"),
exact = TRUE
)
})
})

0 comments on commit af19900

Please sign in to comment.