Skip to content

Commit

Permalink
update tests and increment version
Browse files Browse the repository at this point in the history
  • Loading branch information
fbenke-pik committed Jul 26, 2024
1 parent e6c7c8f commit 803cf38
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ValidationKey: '2027862'
ValidationKey: '2072720'
AutocreateReadme: no
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
AcceptedNotes: ~
AcceptedNotes: 'Namespace in Imports field not imported from: ‘crayon’'
allowLinterWarnings: yes
enforceVersionUpdate: no
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'GDPuc: Easily Convert GDP Data'
version: 1.0.2
date-released: '2024-06-07'
version: 1.0.4
date-released: '2024-07-26'
abstract: Convert GDP time series data from one unit to another. All common GDP units
are included, i.e. current and constant local currency units, US$ via market exchange
rates and international dollars via purchasing power parities.
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: GDPuc
Title: Easily Convert GDP Data
Version: 1.0.2
Date: 2024-06-07
Version: 1.0.4
Date: 2024-07-26
Authors@R:
person("Johannes", "Koch", , "jokoch@pik-potsdam.de", role = c("aut", "cre"))
Description: Convert GDP time series data from one unit to
Expand Down Expand Up @@ -42,4 +42,4 @@ VignetteBuilder:
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
12 changes: 10 additions & 2 deletions R/check_user_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ check_user_input <- function(gdp,
verbose,
return_cfs) {

check_gdp(gdp)
require_year_column <- any(grepl("current", c(unit_in, unit_out)))

check_gdp(gdp, require_year_column)
check_unit_in_out(unit_in, unit_out)
source <- check_source(source)
check_use_USA_deflator_for_all(use_USA_deflator_for_all, unit_in, unit_out)
Expand All @@ -27,7 +29,7 @@ check_user_input <- function(gdp,


# Check 'gdp' input parameter
check_gdp <- function(gdp) {
check_gdp <- function(gdp, require_year_column) {
if (is.data.frame(gdp)) {
if (! "value" %in% colnames(gdp)) {
abort("Invalid 'gdp' argument. 'gdp' does not have the required 'value' column.")
Expand All @@ -38,6 +40,12 @@ check_gdp <- function(gdp) {
} else if (inherits(gdp, "magpie")) {
# Check for magclass package
rlang::check_installed("magclass", reason = "in order for magpie objects to be recognized.")

# Check if there is years info
if (is.null(magclass::getYears(gdp)) && require_year_column) {
abort("Invalid 'gdp' argument. No year information in {crayon::bold('magpie')} object.")
}

} else {
abort("Invalid 'gdp' argument. 'gdp' is neither a data-frame nor a 'magpie' object.")
}
Expand Down
2 changes: 1 addition & 1 deletion R/transform_user_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ transform_user_input <- function(gdp, unit_in, unit_out, source, use_USA_deflato

tb <- tibble::as_tibble(gdp)

if (is.null(getItems(gdp, dim = 2))){
if (is.null(magclass::getItems(gdp, dim = 2))) {
tb <- tb %>% tibble::add_column(year = 0, .after = 1)
}

Expand Down
2 changes: 1 addition & 1 deletion man/convertGDP.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions tests/testthat/test-01_check_user_input.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
test_that("gdp argument", {
gdp <- tibble::tibble("iso3c" = "EUR", "year" = 2010, "value2" = 100)
expect_error(check_user_input(gdp), "Invalid 'gdp' argument. 'gdp' does not have the required 'value' column.")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = "constant 2017 Int$PPP"),
"Invalid 'gdp' argument. 'gdp' does not have the required 'value' column.")

gdp <- tibble::tibble("iso3c" = "EUR", "year" = 2007, "value" = "100")
expect_error(check_user_input(gdp), "Invalid 'gdp' argument. The 'value' column is not numeric.")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = "constant 2017 Int$PPP"),
"Invalid 'gdp' argument. The 'value' column is not numeric.")

gdp <- "blabla"
expect_error(check_user_input(gdp), "Invalid 'gdp' argument. 'gdp' is neither a data-frame nor a 'magpie' object.")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = "constant 2017 Int$PPP"),
"Invalid 'gdp' argument. 'gdp' is neither a data-frame nor a 'magpie' object.")
gdp <- array()
expect_error(check_user_input(gdp), "Invalid 'gdp' argument. 'gdp' is neither a data-frame nor a 'magpie' object.")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = "constant 2017 Int$PPP"),
"Invalid 'gdp' argument. 'gdp' is neither a data-frame nor a 'magpie' object.")

if (rlang::is_installed("magclass")) {
gdp <- magclass::new.magpie()
expect_error(check_user_input(gdp), "Invalid 'gdp' argument. No year information in magpie object.")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = "constant 2017 Int$PPP"),
"Invalid 'gdp' argument. No year information in magpie object.")
}
})

test_that("unit arguments", {
gdp <- tibble::tibble("iso3c" = "EUR", "year" = 2010, "value" = 100)
expect_error(check_user_input(gdp, unit_in = "blabla"), "Invalid 'unit_in' argument")
expect_error(check_user_input(gdp, unit_in = "blabla", unit_out = "blabla"), "Invalid 'unit_in' argument")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = 2), "Invalid 'unit_out' argument")
})

Expand Down
9 changes: 8 additions & 1 deletion tests/testthat/test-05_convertGDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,21 @@ test_that("convertGDP magpie object", {
years = c(2001, 2002),
names = c("ssp1", "ssp2"),
fill = 100)
gdp_in3 <- magclass::new.magpie("USA",
years = NULL,
names = c("ssp1", "ssp2"),
fill = 100)

gdp_conv <- convertGDP(gdp_in, "current LCU", "constant 2017 Int$PPP")
gdp_conv2 <- convertGDP(gdp_in2, "current LCU", "constant 2017 Int$PPP")

gdp_conv3 <- convertGDP(gdp_in3, "constant 2005 LCU", "constant 2017 Int$PPP")

expect_s4_class(gdp_conv, "magpie")
expect_s4_class(gdp_conv2, "magpie")
expect_s4_class(gdp_conv3, "magpie")
expect_mapequal(magclass::getSets(gdp_in), magclass::getSets(gdp_conv))
expect_mapequal(magclass::getSets(gdp_in2), magclass::getSets(gdp_conv2))
expect_mapequal(magclass::getSets(gdp_in3), magclass::getSets(gdp_conv3))
})


Expand Down

0 comments on commit 803cf38

Please sign in to comment.