Skip to content

Commit

Permalink
add some tests (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjyetman authored Apr 16, 2024
1 parent a43cd61 commit 748d7c9
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/testthat/test-determine_relevant_years.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test_that("returns appropriate length integer vector", {
market_share_target_reference_year <- 2023
time_horizon <- 5
output <- determine_relevant_years(market_share_target_reference_year, time_horizon)
expect_vector(output, ptype = integer())
expect_length(output, time_horizon + 1)
expect_contains(output, market_share_target_reference_year)
expect_length(output, length(unique(output)))
})
28 changes: 28 additions & 0 deletions tests/testthat/test-prepare_ar_company_id__country_of_domicile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
test_that("returns a data frame with expected columns and no `NA`s", {
input <- data.frame(
ar_company_id = c("123-AB", "456-CD"),
country_of_domicile = c("Power", "Oil&Gas")
)

output <- prepare_ar_company_id__country_of_domicile(input)

expect_s3_class(output, class = "data.frame")
expect_identical(names(output), c("ar_company_id", "country_of_domicile"))
expect_true(all(!is.na(output$ar_company_id)))
})

test_that("returns error if input is not a data frame", {
expect_error(prepare_ar_company_id__country_of_domicile("xxx"))
})

test_that("returns error if input does not have the necessary columns", {
expect_error(prepare_ar_company_id__country_of_domicile(data.frame(x = 1L)))
})

test_that("returns error if output has duplicated `ar_company_id`s", {
input <- data.frame(
ar_company_id = c("123-AB", "123-AB"),
country_of_domicile = c("Power", "Oil&Gas")
)
expect_error(prepare_ar_company_id__country_of_domicile(input))
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
test_that("returns a data frame with expected columns and no `NA`s", {
input <- data.frame(
ar_company_id = c("123-AB", "456-CD"),
credit_parent_ar_company_id = c("Power", "Oil&Gas")
)

output <- prepare_ar_company_id__credit_parent_ar_company_id(input)

expect_s3_class(output, class = "data.frame")
expect_identical(names(output), c("ar_company_id", "credit_parent_ar_company_id"))
expect_true(all(!is.na(output$ar_company_id)))
})

test_that("returns error if input is not a data frame", {
expect_error(prepare_ar_company_id__credit_parent_ar_company_id("xxx"))
})

test_that("returns error if input does not have the necessary columns", {
expect_error(prepare_ar_company_id__credit_parent_ar_company_id(data.frame(x = 1L)))
})

test_that("returns error if output has duplicated `ar_company_id`s", {
input <- data.frame(
ar_company_id = c("123-AB", "123-AB"),
credit_parent_ar_company_id = c("Power", "Oil&Gas")
)
expect_error(prepare_ar_company_id__credit_parent_ar_company_id(input))
})
29 changes: 29 additions & 0 deletions tests/testthat/test-prepare_company_id__creditor_company_id.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
test_that("returns a data frame with expected columns and no `NA`s", {
input <- data.frame(
company_id = c("123-AB", "456-CD"),
creditor_company_id = c("Power", "Oil&Gas")
)

output <- prepare_company_id__creditor_company_id(input)

expect_s3_class(output, class = "data.frame")
expect_identical(names(output), c("company_id", "creditor_company_id"))
expect_true(all(!is.na(output$company_id)))
expect_true(all(!is.na(output$creditor_company_id)))
})

test_that("returns error if input is not a data frame", {
expect_error(prepare_company_id__creditor_company_id("xxx"))
})

test_that("returns error if input does not have the necessary columns", {
expect_error(prepare_company_id__creditor_company_id(data.frame(x = 1L)))
})

test_that("returns error if output has duplicated `company_id`s", {
input <- data.frame(
company_id = c("123-AB", "123-AB"),
creditor_company_id = c("Power", "Oil&Gas")
)
expect_error(prepare_company_id__creditor_company_id(input))
})
29 changes: 29 additions & 0 deletions tests/testthat/test-prepare_factset_entity_id__ar_company_id.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
test_that("returns a data frame with expected columns and no `NA`s", {
input <- data.frame(
factset_id = c("123-AB", "456-CD"),
company_id = c("Power", "Oil&Gas")
)

output <- prepare_factset_entity_id__ar_company_id(input)

expect_s3_class(output, class = "data.frame")
expect_identical(names(output), c("factset_entity_id", "ar_company_id"))
expect_true(all(!is.na(output$factset_id)))
expect_true(all(!is.na(output$company_id)))
})

test_that("returns error if input is not a data frame", {
expect_error(prepare_factset_entity_id__ar_company_id("xxx"))
})

test_that("returns error if input does not have the necessary columns", {
expect_error(prepare_factset_entity_id__ar_company_id(data.frame(x = 1L)))
})

test_that("returns error if output has duplicated `factset_id`s", {
input <- data.frame(
factset_id = c("123-AB", "123-AB"),
company_id = c("Power", "Oil&Gas")
)
expect_error(prepare_factset_entity_id__ar_company_id(input))
})
29 changes: 29 additions & 0 deletions tests/testthat/test-prepare_factset_entity_id__credit_parent_id.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
test_that("returns a data frame with expected columns and no `NA`s", {
input <- data.frame(
factset_entity_id = c("123-AB", "456-CD"),
credit_parent_id = c("Power", "Oil&Gas")
)

output <- prepare_factset_entity_id__credit_parent_id(input)

expect_s3_class(output, class = "data.frame")
expect_identical(names(output), c("factset_entity_id", "credit_parent_id"))
expect_true(all(!is.na(output$factset_entity_id)))
expect_true(all(!is.na(output$credit_parent_id)))
})

test_that("returns error if input is not a data frame", {
expect_error(prepare_factset_entity_id__credit_parent_id("xxx"))
})

test_that("returns error if input does not have the necessary columns", {
expect_error(prepare_factset_entity_id__credit_parent_id(data.frame(x = 1L)))
})

test_that("returns error if output has duplicated `factset_entity_id`s", {
input <- data.frame(
factset_entity_id = c("123-AB", "123-AB"),
credit_parent_id = c("Power", "Oil&Gas")
)
expect_error(prepare_factset_entity_id__credit_parent_id(input))
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
test_that("returns a data frame with expected columns and no `NA`s", {
input <- data.frame(
factset_entity_id = c("123-AB", "456-CD"),
security_mapped_sector = c("Power", "Oil&Gas")
)

output <- prepare_factset_entity_id__security_mapped_sector(input)

expect_s3_class(output, class = "data.frame")
expect_identical(names(output), c("factset_entity_id", "security_mapped_sector"))
expect_true(all(!is.na(output$factset_entity_id)))
expect_true(all(!is.na(output$security_mapped_sector)))
})

test_that("returns error if input is not a data frame", {
expect_error(prepare_factset_entity_id__security_mapped_sector("xxx"))
})

test_that("returns error if input does not have the necessary columns", {
expect_error(prepare_factset_entity_id__security_mapped_sector(data.frame(x = 1L)))
})

test_that("returns error if output has duplicated `factset_entity_id`s", {
input <- data.frame(
factset_entity_id = c("123-AB", "123-AB"),
security_mapped_sector = c("Power", "Oil&Gas")
)
expect_error(prepare_factset_entity_id__security_mapped_sector(input))
})
18 changes: 18 additions & 0 deletions tests/testthat/test-prepare_total_fund_list.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test_that("returns a data frame with the proper column and no `NA`s", {
input <- data.frame(
factset_fund_id = c("XXX", NA, "ZZZ")
)

output <- prepare_total_fund_list(input)

expect_s3_class(output, class = "data.frame")
expect_true(all(!is.na(output$factset_fund_id)))
})

test_that("returns error if input is not a data frame", {
expect_error(prepare_total_fund_list("xxx"))
})

test_that("returns error if input does not have `factset_fund_id` column", {
expect_error(prepare_total_fund_list(data.frame(x = 1L)))
})
24 changes: 24 additions & 0 deletions tests/testthat/test-standardize_asset_type_names.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
test_that("returns a data frame with proper asset type names", {
input <- data.frame(
issue_type_code = "XXX",
asset_type = c(
"Listed Equity",
"Corporate Bond",
"Fund",
"Other",
"XXX"
)
)

proper_asset_type_names <- c(
"Equity",
"Bonds",
"Funds",
"Others"
)

output <- standardize_asset_type_names(input)

expect_s3_class(output, class = "data.frame")
expect_in(output$asset_type, proper_asset_type_names)
})

0 comments on commit 748d7c9

Please sign in to comment.