Skip to content

Commit 0977418

Browse files
committed
Finally updated commented tests to pass, as well as added a few additional tests.
1 parent 6053493 commit 0977418

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

R/utils.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ guess_geo_type = function(geo_value) {
3939
if (all(geo_value %in% state_values)) return("state")
4040

4141
# Else if all geo values are 2 letters, then use "nation"
42-
else if (all(grepl("[a-z]{2}", geo_value))) return("nation")
42+
else if (all(grepl("[a-z]{2}", geo_value))
43+
& !any(grepl("[a-z]{3}", geo_value))) return("nation")
4344

4445
# Else if all geo values are 5 numbers, then use "county"
45-
else if (all(grepl("[0-9]{5}", geo_value))) return("county")
46+
else if (all(grepl("[0-9]{5}", geo_value)) &
47+
!any(grepl("[0-9]{6}", geo_value))) return("county")
4648
}
4749

4850
else if (is.numeric(geo_value)) {
@@ -79,7 +81,7 @@ guess_time_type = function(time_value) {
7981

8082
# Else, if a Date class, then use "week" or "day" depending on gaps
8183
else if (inherits(time_value, "Date")) {
82-
return(ifelse(all(diff(sort(time_value)) == -7), "week", "day"))
84+
return(ifelse(all(diff(sort(time_value)) == 7), "week", "day"))
8385
}
8486

8587
# Else, check whether it's one of the tsibble classes

tests/testthat/test-utils.R

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,24 @@ test_that("guess_geo_type tests for different types of geo_value's",{
4949
# HRR regions
5050
hrr <- c(100,200)
5151

52+
# Long numbers should be custom
53+
long_nums <- c(123456789,111222333)
54+
5255
# Health regions in British Columbia
5356
bc <- c("Vancouver Coastal","Interior","Fraser",
5457
"Northern","Vancouver Island")
5558

56-
# Long numbers as strings that should also be custom
57-
long_nums <- c("123456789","111222333")
59+
# Long numbers as strings should also be custom
60+
long_num_strings <- c("123456789","111222333")
5861

5962
expect_equal(guess_geo_type(states),"state")
6063
expect_equal(guess_geo_type(nations),"nation")
6164
expect_equal(guess_geo_type(counties),"county")
6265
expect_equal(guess_geo_type(hhs),"hhs")
6366
expect_equal(guess_geo_type(hrr),"hrr")
64-
# expect_equal(guess_geo_type(bc),"custom") # EDIT LINE 42
65-
# expect_equal(guess_geo_type(long_nums),"custom") # EDIT LINE 45
67+
expect_equal(guess_geo_type(long_num_strings),"custom")
68+
expect_equal(guess_geo_type(bc),"custom")
69+
expect_equal(guess_geo_type(long_nums),"custom")
6670
})
6771

6872
test_that("guess_time_type works for different types",{
@@ -83,7 +87,7 @@ test_that("guess_time_type works for different types",{
8387
not_a_date <- "asdf"
8488

8589
expect_equal(guess_time_type(days),"day")
86-
# expect_equal(guess_time_type(weeks),"week") # EDIT LINE 82
90+
expect_equal(guess_time_type(weeks),"week")
8791

8892
expect_equal(guess_time_type(yearweeks),"yearweek")
8993
expect_equal(guess_time_type(yearmonths),"yearmonth")

0 commit comments

Comments
 (0)