-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add mocked unit tests using httptest2
resolves #13
- Loading branch information
Showing
10 changed files
with
75,090 additions
and
58 deletions.
There are no files selected for viewing
15,191 changes: 15,191 additions & 0 deletions
15,191
tests/testthat/gmhcommunities.entrata.com/api/v1/leases-7c11b9-POST.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
# ------------------------------------------------------------------------ | ||
# | ||
# Title : Test Helpers | ||
# By : Jimmy Briggs | ||
# Date : 2024-08-28 | ||
# | ||
# ------------------------------------------------------------------------ | ||
|
||
|
||
# mock response ----------------------------------------------------------- | ||
|
||
# Helper function to create a mock response | ||
create_mock_response <- function(status_code, body) { | ||
structure( | ||
list( | ||
status_code = status_code, | ||
headers = list(`Content-Type` = "application/json"), | ||
body = jsonlite::toJSON(body, auto_unbox = TRUE) | ||
), | ||
class = "response" | ||
) | ||
} | ||
|
||
# package root ------------------------------------------------------------ | ||
|
||
test_package_root <- function() { | ||
|
||
hold <- tryCatch( | ||
rprojroot::find_package_root_file(), | ||
error = function(e) NULL | ||
) | ||
|
||
if (!is.null(hold)) { return(hold) } | ||
|
||
pkg <- testthat::testing_package() | ||
|
||
hold <- tryCatch( | ||
rprojroot::find_package_root_file( | ||
path = file.path("..", "..", pkg) | ||
), | ||
error = function(e) NULL | ||
) | ||
|
||
if (!is.null(hold)) { return(hold) } | ||
|
||
rlang::abort("Could not find package root") | ||
|
||
} | ||
|
||
|
15,191 changes: 15,191 additions & 0 deletions
15,191
tests/testthat/leases/gmhcommunities.entrata.com/api/v1/leases-7c11b9-POST.json
Large diffs are not rendered by default.
Oops, something went wrong.
44,397 changes: 44,397 additions & 0 deletions
44,397
tests/testthat/leases/gmhcommunities.entrata.com/api/v1/leases-beef19-d43330-POST.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
# ------------------------------------------------------------------------ | ||
# | ||
# Title : Entrata Leases Setup | ||
# By : Jimmy Briggs | ||
# Date : 2024-08-28 | ||
# | ||
# ------------------------------------------------------------------------ | ||
|
||
cfg <- config::get("entrata", file = here::here("config.yml")) | ||
|
||
test_prop_ids <- c( | ||
"739084", | ||
"641240", | ||
"676055", | ||
"952515", | ||
"518041", | ||
"518042", | ||
"833617", | ||
"1197887", | ||
"1143679", | ||
"1311849" | ||
) | ||
|
||
# # Mock data for testing | ||
# mock_response <- function() { | ||
# list( | ||
# response = list( | ||
# result = list( | ||
# leases = list( | ||
# lease = list( | ||
# list( | ||
# id = "12345", | ||
# move_in_date = "01/01/2023", | ||
# is_month_to_month = "f", | ||
# lease_intervals = list( | ||
# list( | ||
# lease_interval_id = "67890", | ||
# lease_interval_type_name = "New Lease", | ||
# lease_interval_status_type_name = "Active", | ||
# interval_start_date = "01/01/2023", | ||
# interval_end_date = "12/31/2023" | ||
# ) | ||
# ), | ||
# customers = list( | ||
# list( | ||
# customer_id = "11111", | ||
# name_full = "John Doe", | ||
# email_address = "[email protected]" | ||
# ) | ||
# ), | ||
# scheduled_charges = list( | ||
# list( | ||
# id = "22222", | ||
# amount = "1000.00", | ||
# start_date = "01/01/2023", | ||
# end_date = "12/31/2023" | ||
# ) | ||
# ), | ||
# unit_spaces = list( | ||
# list( | ||
# unit_space_id = "33333", | ||
# unit_space = "Apartment 101" | ||
# ) | ||
# ) | ||
# ) | ||
# ) | ||
# ) | ||
# ) | ||
# ) | ||
# ) | ||
# } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
# ------------------------------------------------------------------------ | ||
# | ||
# Title : Setup for Entrata Request Tests | ||
# By : Jimmy Briggs | ||
# Date : 2024-08-28 | ||
# | ||
# ------------------------------------------------------------------------ | ||
|
||
cfg <- config::get("entrata", file = here::here("config.yml")) | ||
|
||
# status request ---------------------------------------------------------- | ||
|
||
mock_req_body <- list( | ||
auth = list( | ||
type = "basic" | ||
), | ||
requestId = 15, | ||
method = list( | ||
name = "getStatus", | ||
version = "r1", | ||
params = list() | ||
) | ||
) | ||
|
||
mock_res_success <- list( | ||
response = list( | ||
requestId = "15", | ||
code = 200, | ||
result = list( | ||
status = "Success", | ||
message = "API service is available and running." | ||
) | ||
) | ||
) | ||
|
||
mock_res_error <- list( | ||
response = list( | ||
requestId = "15", | ||
error = list( | ||
code = 113, | ||
message = "Username and/or password is incorrect." | ||
) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,14 @@ | ||
# setup ------------------------------------------------------------------- | ||
|
||
library(httr2) | ||
library(httptest2) | ||
library(httr2, warn.conflicts = FALSE) | ||
library(httptest2, warn.conflicts = FALSE) | ||
|
||
options(httptest2.verbose = TRUE) | ||
Sys.setlocale("LC_COLLATE", "C") | ||
|
||
cfg <- config::get("entrata", file = here::here("config.yml")) | ||
|
||
mock_req_body <- list( | ||
auth = list( | ||
type = "basic" | ||
), | ||
requestId = 15, | ||
method = list( | ||
name = "getStatus", | ||
version = "r1", | ||
params = list() | ||
) | ||
) | ||
|
||
mock_res_success <- list( | ||
response = list( | ||
requestId = "15", | ||
code = 200, | ||
result = list( | ||
status = "Success", | ||
message = "API service is available and running." | ||
) | ||
) | ||
options( | ||
httptest2.verbose = TRUE, | ||
httptest.debug.trace = TRUE, | ||
warn = 1 | ||
) | ||
|
||
mock_res_error <- list( | ||
response = list( | ||
requestId = "15", | ||
error = list( | ||
code = 113, | ||
message = "Username and/or password is incorrect." | ||
) | ||
) | ||
) | ||
cfg <- config::get("entrata", file = here::here("config.yml")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
# httptest2::start_capturing(simplify = TRUE) | ||
# | ||
# test_prop_ids <- c("739084") | ||
# | ||
# prop_ids <- paste(test_prop_ids, collapse = ",") | ||
# | ||
# res <- entrata( | ||
# endpoint = "leases", | ||
# method = "getLeases", | ||
# method_params = list("propertyId" = prop_ids), | ||
# method_version = "r2", | ||
# perform = TRUE | ||
# ) | ||
# | ||
# httptest2::stop_capturing() | ||
|
||
httptest2::with_mock_dir("leases", { | ||
|
||
test_that("Can call leases endpoint getLeases method", { | ||
test_prop_ids <- c("739084") | ||
prop_ids <- paste(test_prop_ids, collapse = ",") | ||
res <- entrata( | ||
endpoint = "leases", | ||
method = "getLeases", | ||
method_params = list("propertyId" = prop_ids), | ||
method_version = "r2", | ||
perform = TRUE | ||
) | ||
expect_equal(res$status_code, 200) | ||
}) | ||
|
||
}) | ||
|
||
httptest2::with_mock_dir("leases", { | ||
|
||
test_that("entrata_leases function works correctly", { | ||
result <- entrata_leases(property_ids = c("641240")) | ||
expect_s3_class(result, "data.frame") | ||
expect_true("lease_id" %in% names(result)) | ||
expect_true("move_in_date" %in% names(result)) | ||
expect_true("customer_name" %in% names(result)) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,79 @@ | ||
httptest2::with_mock_dir( | ||
dir = "status", | ||
test_that("Can ping API status endpoint", { | ||
res <- entrata( | ||
endpoint = "status", | ||
method = "getStatus", | ||
perform = TRUE, | ||
config = cfg | ||
) | ||
expect_equal(res$status_code, 200) | ||
}) | ||
) | ||
|
||
httptest2::with_mock_api({ | ||
test_that("Can ping API", { | ||
res <- entrata( | ||
endpoint = "status", | ||
method = "getStatus", | ||
perform = TRUE, | ||
config = cfg | ||
) | ||
expect_equal(res$status_code, 200) | ||
}) | ||
|
||
# ------------------------------------------------------------------------ | ||
# | ||
# Title : Entrata Request Tests | ||
# By : Jimmy Briggs | ||
# Date : 2024-08-28 | ||
# | ||
# ------------------------------------------------------------------------ | ||
|
||
test_that("entrata function creates a valid request object", { | ||
|
||
req <- entrata( | ||
endpoint = "status", | ||
method = "getStatus", | ||
config = cfg | ||
) | ||
|
||
expect_s3_class(req, "httr2_request") | ||
expect_equal(req$url, "https://gmhcommunities.entrata.com/api/v1/status") | ||
expect_equal(req$method, "POST") | ||
expect_true("Authorization" %in% names(req$headers)) | ||
|
||
body_data <- req$body$data | ||
expect_equal(body_data$method$name, "getStatus") | ||
expect_equal(body_data$method$version, "r1") | ||
expect_equal(body_data$requestId, 15) | ||
}) | ||
|
||
test_that("entrata function handles different endpoints and methods", { | ||
|
||
req1 <- entrata(endpoint = "status", method = "getStatus", config = cfg) | ||
req2 <- entrata(endpoint = "properties", method = "getProperties", config = cfg) | ||
|
||
expect_match(req1$url, "/status$") | ||
expect_match(req2$url, "/properties$") | ||
|
||
body1 <- req1$body$data | ||
body2 <- req2$body$data | ||
|
||
expect_equal(body1$method$name, "getStatus") | ||
expect_equal(body2$method$name, "getProperties") | ||
}) | ||
|
||
test_that("entrata function respects custom configurations", { | ||
|
||
custom_cfg <- list( | ||
username = "custom_user", | ||
password = "custom_pass", | ||
base_url = "https://custom.entrata.com" | ||
) | ||
|
||
req <- entrata(endpoint = "status", method = "getStatus", config = custom_cfg) | ||
|
||
expect_match(req$url, "^https://custom.entrata.com") | ||
expect_match(req$headers$Authorization, "^Basic Y3VzdG9tX3VzZXI6Y3VzdG9tX3Bhc3M=") | ||
}) | ||
|
||
test_that("entrata function respects dry_run parameter", { | ||
expect_message( | ||
entrata(endpoint = "status", method = "getStatus", dry_run = TRUE, config = cfg), | ||
"Dry Run: Request will not be performed" | ||
) | ||
}) | ||
|
||
test_that("entrata function handles timeout parameter", { | ||
req <- entrata(endpoint = "status", method = "getStatus", timeout = 30, config = cfg) | ||
expect_equal(req$options$timeout_ms / 1000, 30) | ||
}) | ||
|
||
test_that("entrata function respects enable_retry parameter", { | ||
req <- entrata(endpoint = "status", method = "getStatus", enable_retry = TRUE, config = cfg) | ||
expected_names <- c("retry_max_tries", "retry_max_wait", "retry_is_transient", "retry_backoff") | ||
expect_true(all(expected_names %in% names(req$policies))) | ||
expect_true(req$policies$retry_max_tries > 0) | ||
expect_true(req$policies$retry_max_wait > 0) | ||
expect_identical(typeof(req$policies$retry_is_transient), "closure") | ||
expect_identical(typeof(req$policies$retry_backoff), "closure") | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
httptest2::with_mock_dir( | ||
dir = "status", | ||
test_that("Can ping API status endpoint", { | ||
res <- entrata( | ||
endpoint = "status", | ||
method = "getStatus", | ||
perform = TRUE, | ||
config = cfg | ||
) | ||
expect_equal(res$status_code, 200) | ||
}) | ||
) |