Skip to content

Commit

Permalink
test: 🧪❌ Create specification for the registry – interface for persis…
Browse files Browse the repository at this point in the history
…tent storage

- 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.
  • Loading branch information
Jakub Sobolewski committed Jan 20, 2025
1 parent c98ffb9 commit 6e79158
Showing 1 changed file with 155 additions and 0 deletions.
155 changes: 155 additions & 0 deletions tests/testthat/test-registry.R
Original file line number Diff line number Diff line change
@@ -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)
})
})
})

0 comments on commit 6e79158

Please sign in to comment.