Skip to content

Commit

Permalink
import_cpi updates and test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyclements committed May 1, 2024
1 parent b79b5b1 commit 748f6c0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion R/import_CPI.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#'
#' @examples
#' #cpi_data <- import_CPI()
import_CPI <- function(path = "C:/Users/lclem/Downloads/CPI_Data_DashboardExtract.xlsx", sheet = "Data_Price", skip = 2){
import_CPI <- function(path, sheet = "Data_Price", skip = 2){
data <- readxl::read_excel(path, sheet = sheet, skip = skip)
data <- data %>%
dplyr::filter(`Instrument Type` == "ETS") %>%
Expand Down
6 changes: 1 addition & 5 deletions man/import_CPI.Rd

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

27 changes: 27 additions & 0 deletions tests/testthat/test-import_CPI.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
library(readxl)
library(dplyr)
library(tidyr)
library(stringr)

# Assuming you have a test Excel file located in your test directory called "test_CPI_Data.xlsx"
path_to_test_file <- "tests/testthat/testdata/CPI_Data_DashboardExtract.xlsx"

test_data <- import_CPI(path = path_to_test_file, sheet = "Data_Price", skip = 2)

test_that("import_CPI returns a dataframe with correct structure and data", {
# Check if the output is a dataframe
expect_true(is.data.frame(test_data))

# Check for correct columns in the dataframe
expected_columns <- c("Jurisdiction", "Period", "Year", "Price ($)")
expect_true(all(expected_columns %in% names(test_data)))

# Here, check that only ETS instruments are included

# Check the Year and Period transformations
expect_true(all(!is.na(as.numeric(test_data$Year))))
expect_true(all(!is.na(as.numeric(test_data$Period))))

# Check for non-NA prices
expect_false(any(is.na(test_data$`Price ($)`)))
})
Binary file not shown.
Binary file added tests/testthat/testdata/cpi_data.rds
Binary file not shown.

0 comments on commit 748f6c0

Please sign in to comment.