Skip to content

Commit

Permalink
improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Sep 6, 2023
1 parent 049b7ee commit bcfbf79
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 4 deletions.
2 changes: 1 addition & 1 deletion R/add_labels_families.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ add_labels_families <- function(arrw,
year = parent.frame()$year,
lang = 'pt'){

# check languate input
# check input
checkmate::assert_string(lang, pattern = 'pt', na.ok = TRUE)
if (!(year %in% c(2000, 2010))) {stop('Labels for this data are only available for the years c(2000, 2010)')}

Expand Down
2 changes: 1 addition & 1 deletion R/add_labels_households.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ add_labels_households <- function(arrw,
year = parent.frame()$year,
lang = 'pt'){

# check languate input
# check input
checkmate::assert_string(lang, pattern = 'pt', na.ok = TRUE)
if (!(year %in% c(2000, 2010))) {stop('Labels for this data are only available for the years c(2000, 2010)')}

Expand Down
2 changes: 1 addition & 1 deletion R/add_labels_mortality.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ add_labels_mortality <- function(arrw,
year = parent.frame()$year,
lang = 'pt'){

# check languate input
# check input
checkmate::assert_string(lang, pattern = 'pt', na.ok = TRUE)
if (!(year %in% c(2010))) {stop('Labels for this data are only available for the year c(2010)')}

Expand Down
2 changes: 1 addition & 1 deletion R/add_labels_population.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ add_labels_population <- function(arrw,
year = parent.frame()$year,
lang = 'pt'){

# check languate input
# check input
checkmate::assert_string(lang, pattern = 'pt', na.ok = TRUE)
if (!(year %in% c(2010))) {stop('Labels for this data are only available for the year c(2010)')}

Expand Down
3 changes: 3 additions & 0 deletions tests/tests_rafa/test_rafa.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ Sys.setenv(NOT_CRAN = "true")
t1 <- covr::function_coverage(fun=read_mortality, test_file("tests/testthat/test_read_mortality.R"))
t1 <- covr::function_coverage(fun=censobr_cache, test_file("tests/testthat/test_censobr_cache.R"))
t1 <- covr::function_coverage(fun=censobr:::add_labels_emigration, test_file("tests/testthat/test_labels_emigration.R"))
t1 <- covr::function_coverage(fun=censobr:::add_labels_mortality, test_file("tests/testthat/test_labels_mortality.R"))
t1 <- covr::function_coverage(fun=censobr:::add_labels_households, test_file("tests/testthat/test_labels_households.R"))
t1


# nocov start

# nocov end
Expand Down
40 changes: 40 additions & 0 deletions tests/testthat/test_labels_families.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
context("add_labels_families")

# skip tests because they take too much time
skip_if(Sys.getenv("TEST_ONE") != "")
testthat::skip_on_cran()


# Reading the data -----------------------

test_that("add_labels_families", {

# sem labels
test1a <- read_families(year = 2000, add_labels = NULL) |>
filter(abbrev_state == 'RO')

# com labels
test1b <- censobr:::add_labels_families(arrw = test1a, year=2000, lang = 'pt') |>
filter(abbrev_state == 'RO')

test1a <- as.data.frame(test1a)
test1b <- as.data.frame(test1b)

# add labels
testthat::expect_true('01' %in% test1a$CODV0404_2)
testthat::expect_true('Casal sem filhos' %in% test1b$CODV0404_2)



})


# ERRORS and messages -----------------------
test_that("add_labels_families", {

# missing labels
testthat::expect_error(censobr:::add_labels_families(arrw = test1a, year=9999, lang = 'pt') )
testthat::expect_error(censobr:::add_labels_families(arrw = test1a, year=2000, lang = 9999) )
testthat::expect_error(censobr:::add_labels_families(arrw = test1a, year=2000, lang = 'banana') )

})
61 changes: 61 additions & 0 deletions tests/testthat/test_labels_households.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
context("add_labels_households")

# skip tests because they take too much time
skip_if(Sys.getenv("TEST_ONE") != "")
testthat::skip_on_cran()


# Reading the data -----------------------

test_that("add_labels_households", {

################################################################### 2010
# sem labels
test1a <- read_households(year = 2010, add_labels = NULL) |>
filter(abbrev_state == 'RO')

# com labels
test1b <- censobr:::add_labels_households(arrw = test1a, year=2010, lang = 'pt') |>
filter(abbrev_state == 'RO')

test1a <- as.data.frame(test1a)
test1b <- as.data.frame(test1b)

# add labels
testthat::expect_true('1' %in% test1a$V1006)
testthat::expect_true('Urbana' %in% test1b$V1006)



################################################################### 2000
# sem labels
test2a <- read_households(year = 2000, add_labels = NULL) |>
filter(abbrev_state == 'RO')

# com labels
test2b <- censobr:::add_labels_households(arrw = test2a, year=2000, lang = 'pt') |>
filter(abbrev_state == 'RO')

test2a <- as.data.frame(test2a)
test2b <- as.data.frame(test2b)

# add labels
testthat::expect_true('1' %in% test2a$V1006)
testthat::expect_true('Urbana' %in% test2b$V1006)


})


# ERRORS and messages -----------------------
test_that("add_labels_households", {

# missing labels
testthat::expect_error(censobr:::add_labels_households(arrw = test1a, year=9999, lang = 'pt') )
testthat::expect_error(censobr:::add_labels_households(arrw = test1a, year=2010, lang = 9999) )
testthat::expect_error(censobr:::add_labels_households(arrw = test1a, year=2010, lang = 'banana') )

testthat::expect_error(censobr:::add_labels_households(arrw = test1a, year=2000, lang = 9999) )
testthat::expect_error(censobr:::add_labels_households(arrw = test1a, year=2000, lang = 'banana') )

})
39 changes: 39 additions & 0 deletions tests/testthat/test_labels_mortality.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
context("add_labels_mortality")

# skip tests because they take too much time
skip_if(Sys.getenv("TEST_ONE") != "")
testthat::skip_on_cran()


# Reading the data -----------------------

test_that("add_labels_mortality", {

# sem labels
test1a <- read_mortality(year = 2010, add_labels = NULL) |>
filter(abbrev_state == 'RO')

# com labels
test1b <- censobr:::add_labels_mortality(arrw = test1a, year=2010, lang = 'pt') |>
filter(abbrev_state == 'RO')

test1a <- as.data.frame(test1a)
test1b <- as.data.frame(test1b)
# add labels
testthat::expect_true('1' %in% test1a$V0704)
testthat::expect_true('Feminino' %in% test1b$V0704)



})


# ERRORS and messages -----------------------
test_that("add_labels_mortality", {

# missing labels
testthat::expect_error(censobr:::add_labels_mortality(arrw = test1a, year=9999, lang = 'pt') )
testthat::expect_error(censobr:::add_labels_mortality(arrw = test1a, year=2010, lang = 9999) )
testthat::expect_error(censobr:::add_labels_mortality(arrw = test1a, year=2010, lang = 'banana') )

})
62 changes: 62 additions & 0 deletions tests/testthat/test_labels_population.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
context("add_labels_population")

# skip tests because they take too much time
skip_if(Sys.getenv("TEST_ONE") != "")
testthat::skip_on_cran()


# Reading the data -----------------------

test_that("add_labels_population", {

################################################################### 2010
# sem labels
test1a <- read_population(year = 2010, add_labels = NULL) |>
filter(abbrev_state == 'RO')

# com labels
test1b <- censobr:::add_labels_population(arrw = test1a, year=2010, lang = 'pt') |>
filter(abbrev_state == 'RO')

test1a <- as.data.frame(test1a)
test1b <- as.data.frame(test1b)

# add labels
testthat::expect_true('1' %in% test1a$V1006)
testthat::expect_true('Urbana' %in% test1b$V1006)



# ################################################################### 2000
# # sem labels
# test2a <- read_population(year = 2000, add_labels = NULL) |>
# filter(abbrev_state == 'RO')
#
# # com labels
# test2b <- censobr:::add_labels_population(arrw = test2a, year=2000, lang = 'pt') |>
# filter(abbrev_state == 'RO')
#
# test2a <- as.data.frame(test2a)
# test2b <- as.data.frame(test2b)
#
# # add labels
# testthat::expect_true('1' %in% test2a$V1006)
# testthat::expect_true('Urbana' %in% test2b$V1006)


})


# ERRORS and messages -----------------------
test_that("add_labels_population", {

# missing labels
testthat::expect_error(censobr:::add_labels_population(arrw = test1a, year=9999, lang = 'pt') )
testthat::expect_error(censobr:::add_labels_population(arrw = test1a, year=2010, lang = 9999) )
testthat::expect_error(censobr:::add_labels_population(arrw = test1a, year=2010, lang = 'banana') )

testthat::expect_error(censobr:::add_labels_population(arrw = test1a, year=2000, lang = 'pt') )
testthat::expect_error(censobr:::add_labels_population(arrw = test1a, year=2000, lang = 9999) )
testthat::expect_error(censobr:::add_labels_population(arrw = test1a, year=2000, lang = 'banana') )

})

0 comments on commit bcfbf79

Please sign in to comment.