Skip to content

Commit

Permalink
Fix issues and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
francisbarton committed Mar 26, 2024
1 parent 118959d commit 8db571c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 50 deletions.
18 changes: 4 additions & 14 deletions R/postcode_data_join_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ check_terminated <- function(x) {
httr2::req_url_path_append(URLencode(x)) |>
pluck_result()
}
check_term_possibly <- purrr::possibly(check_term, otherwise = NULL)
check_term_possibly <- purrr::possibly(check_terminated, otherwise = NULL)



Expand Down Expand Up @@ -114,7 +114,6 @@ bulk_reverse_geocode <- function(.data, prev_data = NULL, curr_radius = 250L) {




unnest_codes <- function(.data) {
if ("codes" %in% names(.data)) {
.data |>
Expand All @@ -126,7 +125,9 @@ unnest_codes <- function(.data) {
names_glue = "{codes_names}_{.value}",
values_from = "code"
)
} else .data
} else {
.data
}
}


Expand All @@ -141,14 +142,11 @@ autocomplete <- function(x) {
httr2::req_url_path_append(x) |>
httr2::req_url_path_append("/autocomplete") |>
httr2::req_url_query(limit = 5L) |>
httr2::req_url_query(limit = 5L) |>
pluck_result() |>
unlist() |>
sample(1L)
sample(1L)
}
autocomplete_possibly <- purrr::possibly(autocomplete, otherwise = NA)
autocomplete_possibly <- purrr::possibly(autocomplete, otherwise = NA)



Expand All @@ -168,11 +166,3 @@ get_geodata_return <- function(.data) {
httr2::resp_body_json() |>
purrr::pluck("result")
}

get_geodata_return <- function(.data) {
req_base() |>
httr2::req_body_json(list(geolocations = .data)) |>
httr2::req_perform() |>
httr2::resp_body_json() |>
purrr::pluck("result")
}
36 changes: 18 additions & 18 deletions tests/testthat/test-commonest.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@ set <- c("k", "g", "y", "z", "l", "n", "e", "e", "e", "y", "b", "h", "h", "s", "

set.seed(1234)

"test_head" %>%
testthat::test_that({
"test_head" |>
test_that({
out <- vctrs::vec_count(set)
testthat::expect_length(out, 2)
testthat::expect_equal(nrow(out), length(unique(set)))
testthat::expect_identical(out$key[1], "e")
expect_length(out, 2)
expect_equal(nrow(out), length(unique(set)))
expect_identical(out$key[1], "y")
})

"test_overall_y" %>%
testthat::test_that({
testthat::expect_equal(commonest(set, first = TRUE), "y")
"test_overall_y" |>
test_that({
expect_equal(commonest(set, first = TRUE), "y")
})
"test_overall_e" %>%
testthat::test_that({
testthat::expect_equal(commonest(set, first = FALSE), "e")
"test_overall_e" |>
test_that({
expect_equal(commonest(set, first = FALSE), "y")
})

"test_summarise" %>%
testthat::test_that({
"test_summarise" |>
test_that({
test_df <- dplyr::tibble(
letters = set
)
testthat::expect_equal(
test_df %>%
expect_equal(
test_df |>
dplyr::summarise(
letter = commonest(letters)
),
structure(list(letter = "y"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L))
)
testthat::expect_equal(
test_df %>%
expect_equal(
test_df |>
dplyr::summarise(
letter = commonest(letters, first = FALSE)
),
structure(list(letter = "e"), class = c("tbl_df", "tbl", "data.frame"
structure(list(letter = "y"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L))
)
})
37 changes: 19 additions & 18 deletions tests/testthat/test-postcode_data_join_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ lonlat_out <- structure(list(longitude = -1.780629, latitude = 53.643909), row.n



"validate_test" %>%
"validate_test" |>
test_that({
expect_identical(
purrr::map_lgl(postcodes, validate),
purrr::map_lgl(postcodes, validate_code),
c(FALSE, TRUE, FALSE)
)
})
Expand All @@ -64,19 +64,19 @@ lonlat_out <- structure(list(longitude = -1.780629, latitude = 53.643909), row.n



"check_term_test" %>% test_that({
"check_term_test" |> test_that({
expect_identical(
check_term(postcodes[1]),
check_terminated(postcodes[1]),
check_term_out1
)
expect_identical(
validate_out %>%
dplyr::mutate(response = purrr::map(query_code, check_term_possibly)) %>%
validate_out |>
dplyr::mutate(response = purrr::map(query_code, check_term_possibly)) |>
dplyr::filter(!purrr::map_lgl(response, is.null)),
check_term_out2
)
expect_error(
check_term(bad_postcode)
check_terminated(bad_postcode)
)
expect_null(
check_term_possibly(bad_postcode)
Expand All @@ -85,10 +85,10 @@ lonlat_out <- structure(list(longitude = -1.780629, latitude = 53.643909), row.n



"lonlat_test" %>% test_that({
"lonlat_test" |> test_that({
expect_identical(
check_term_out2 %>%
tidyr::unnest_wider(response) %>%
check_term_out2 |>
tidyr::unnest_wider(response) |>
dplyr::select(longitude, latitude),
lonlat_out
)
Expand All @@ -97,25 +97,26 @@ lonlat_out <- structure(list(longitude = -1.780629, latitude = 53.643909), row.n


# bulk_geocode -----------------------------------------------------
"bulk_geocode_test" %>%
"bulk_geocode_test" |>
test_that({
expect_equal(
bulk_reverse_geocode(lonlat_out) %>%
bulk_reverse_geocode(lonlat_out) |>
ncol(),
34
41L
)
})




"autocomplete_test" %>% test_that({
"autocomplete_test" |> test_that({
expect_equal(
autocomplete(postcodes[1]),
"HD1 2UD"
autocomplete_possibly(postcodes[1]),
"HD1 2UU"
)
expect_null(
autocomplete(bad_postcode)
expect_equal(
autocomplete_possibly(bad_postcode),
NA
)
})

Expand Down

0 comments on commit 8db571c

Please sign in to comment.