Skip to content

Commit

Permalink
[#55] many new tests added for sfa_load_statements
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgomolka committed Mar 19, 2024
1 parent 1de62d5 commit 47fa546
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
6 changes: 3 additions & 3 deletions tests/testthat/test-sfa_load_companies.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ test_that("sfa_load_companies works with details = FALSE/TRUE", {

test_that("sfa_load_companies returns error if api key is incorrect", {
expect_error(
sfa_load_companies("invalid_api_key"),
"SimFin API Error 401: Invalid API Key - check the key again and also if you confirmed your e-mail on registration",
fixed = TRUE
sfa_load_companies("invalid_api_key"),
"SimFin API Error 401: Invalid API Key - check the key again and also if you confirmed your e-mail on registration",
fixed = TRUE
)
})
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
test_cases <- data.table::CJ(
ticker = list("GOOG", c("GOOG", "AAPL")),
# id = list(18, c(18, 111052)),
ticker = list("GOOG", NULL, c("GOOG", "AAPL")),
id = list(NULL, 59265, c(59265, 62747)), # MSFT, AMZN
statements = list("pl", "bs", "cf", "derived", c("pl", "bs", "cf", "derived")),
# period =
period = list(NULL, "q1", c("q1", "q2", "q3", "q4", "fy", "h1", "h2", "nine_month")),
sorted = FALSE
)
)[lengths(ticker) + lengths(id) != 0] # drop rows without ticker or id


test_cases[, indx := .I]
data.table::setcolorder(test_cases, "indx")
data.table::setindexv(test_cases, "indx")
test_cases[, key_ := do.call(paste, c(.SD, sep = "-")), .SDcols = names(test_cases)]

for (row in seq(nrow(test_cases))) {
test_that(paste("sfa_load_statement - Case:", test_cases[row, key_]), {
test_that(paste("sfa_load_statement - Case:", test_cases[row, key_]), {
res <- sfa_load_statements(
ticker = test_cases[[row, "ticker"]],
statements = test_cases[[row, "statements"]]
id = test_cases[[row, "id"]],
statements = test_cases[[row, "statements"]],
period = test_cases[[row, "period"]]
)
checkmate::expect_data_table(res)

# testing ticker parameter
for (ticker in test_cases[[row, "ticker"]]) {
expect_true(ticker %in% res[["ticker"]])
}

# testing id parameter
for (id in test_cases[[row, "id"]]) {
expect_true(id %in% res[["id"]])
}

# testing statements parameter
if ("pl" %in% test_cases[[row, "statements"]]) {
checkmate::expect_names(colnames(res), must.include = "revenue")
}
Expand All @@ -29,6 +45,37 @@ for (row in seq(nrow(test_cases))) {
if ("cf" %in% test_cases[[row, "derived"]]) {
checkmate::expect_names(colnames(res), must.include = "ebitda")
}

# testing period parameter
expected <- test_cases[[row, "period"]] |>
toupper() |>
sub(pattern = "NINE_MONTH", replacement = "9M") |>
sort()
if (is.null(test_cases[[row, "period"]])) {
expected <- c("9M", "FY", "H1", "H2", "Q1", "Q2", "Q3", "Q4")
}
if (all(test_cases[[row, "statements"]] == "bs")) {
# balance sheet statements are only quarterly
expected <- intersect(expected, c("Q1", "Q2", "Q3", "Q4")) |> sort()
}
if (all(test_cases[[row, "statements"]] == "derived")) {
expected <- intersect(expected, c("FY", "Q1", "Q2", "Q3", "Q4")) |> sort()
}
expect_identical(unique(res[["fiscal_period"]]) |> sort(), expected)
# testing period parameter
# if (is.null(test_cases[[row, "period"]])) {
# expected <- c("9M", "FY", "H1", "H2", "Q1", "Q2", "Q3", "Q4")
# if (test_cases[[row, "statements"]] == "bs") {
# # balance sheet statements are only quarterly
# expected <- c("Q1", "Q2", "Q3", "Q4")
# }
# expect_identical(unique(res[["fiscal_period"]]), expected)
# } else {
# expect_identical(
# test_cases[[row, "period"]] |> sort(),
# res[["fiscal_period"]] |> unique() |> tolower() |> sub(pattern = "9m", replacement = "nine_month") |> sort()
# )
# }
})
}

Expand Down

0 comments on commit 47fa546

Please sign in to comment.