From 6e79158f28c7a125497fbba0c66890bb853ed4e0 Mon Sep 17 00:00:00 2001 From: Jakub Sobolewski Date: Sun, 19 Jan 2025 20:41:16 +0000 Subject: [PATCH] =?UTF-8?q?test:=20:test=5Ftube::x:=20Create=20specificati?= =?UTF-8?q?on=20for=20the=20registry=20=E2=80=93=20interface=20for=20persi?= =?UTF-8?q?stent=20storage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add unit tests for an object that the app will interact with to store data to a persistent storage. - I want to use a `registry` object to run operations on a `storage` object. The storage will be a CSV file, but it could as well be a database connection or a S3 bucket. - This is an implementation detail of the app, it doesn't change the behavior of the app – tests are added to `tests/testthat/`, no changes in acceptance tests are needed. --- tests/testthat/test-registry.R | 155 +++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 tests/testthat/test-registry.R diff --git a/tests/testthat/test-registry.R b/tests/testthat/test-registry.R new file mode 100644 index 0000000..c7a5cb2 --- /dev/null +++ b/tests/testthat/test-registry.R @@ -0,0 +1,155 @@ +box::use( + glue[glue], + testthat[...], # nolint + withr[with_tempdir], +) + +box::use( + app / registry, + app / transaction, +) + +# Fake storage +create_storage <- function() { + .storage <- glue::glue("store_session_{as.numeric(Sys.time())}.csv") + write.csv(data.frame(amount = numeric()), .storage, row.names = FALSE) + .storage +} + +describe("record", { + it("should record a transaction", { + with_tempdir({ + # Arrange + .storage <- create_storage() + .registry <- registry$new(.storage) + + # Act + .registry$record(transaction$new(100)) # nolint + + # Assert + expect_equal(nrow(read.csv(.storage)), 1) + }) + }) + + it("should record multiple transactions", { + with_tempdir({ + # Arrange + .storage <- create_storage() + .registry <- registry$new(.storage) + + # Act + .registry$record(transaction$new(100)) # nolint + .registry$record(transaction$new(100)) # nolint + + # Assert + expect_equal(nrow(read.csv(.storage)), 2) + }) + }) + + it("should throw an error if storage doesn't exist", { + # Arrange + with_tempdir({ + .storage <- create_storage() + }) + + # Act, Assert + expect_error( + registry$new(.storage), + regexp = glue("File {.storage} does not exist"), + fixed = TRUE + ) + }) +}) + +describe("get_total_positive", { + it("should return the total of positive transactions", { + with_tempdir({ + # Arrange + .storage <- create_storage() + .registry <- registry$new(.storage) + + # Act + .registry$record(transaction$new(100)) # nolint + .registry$record(transaction$new(-100)) # nolint + + # Assert + expect_equal(.registry$get_total_positive(), 100) + }) + }) + + it("should return 0 if there are no transactions", { + with_tempdir({ + # Arrange + .storage <- create_storage() + .registry <- registry$new(.storage) + + # Act + result <- .registry$get_total_positive() + + # Assert + expect_equal(result, 0) + }) + }) +}) + +describe("get_total_negative", { + it("should return the total of negative transactions", { + with_tempdir({ + # Arrange + .storage <- create_storage() + .registry <- registry$new(.storage) + + # Act + .registry$record(transaction$new(200)) # nolint + .registry$record(transaction$new(-100)) # nolint + + # Assert + expect_equal(.registry$get_total_negative(), 100) + }) + }) + + it("should return 0 if there are no transactions", { + with_tempdir({ + # Arrange + .storage <- create_storage() + .registry <- registry$new(.storage) + + # Act + result <- .registry$get_total_negative() + + # Assert + expect_equal(result, 0) + }) + }) +}) + +describe("get_total", { + it("should return the total of all transactions", { + with_tempdir({ + # Arrange + .storage <- create_storage() + .registry <- registry$new(.storage) + + # Act + .registry$record(transaction$new(100)) # nolint + .registry$record(transaction$new(-100)) # nolint + + # Assert + expect_equal(.registry$get_total(), 0) + }) + }) + + it("should return 0 if there are no transactions", { + with_tempdir({ + # Arrange + .storage <- create_storage() + .registry <- registry$new(.storage) + + # Act + result <- .registry$get_total() + + # Assert + expect_equal(result, 0) + }) + }) +})