Skip to content

Commit

Permalink
Test snapshots do not fail when there are additional fields in API, f…
Browse files Browse the repository at this point in the history
…ixing problem of duplicate names (#118)
  • Loading branch information
nickdickinson committed Apr 18, 2024
1 parent 18cf609 commit 945c7b2
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 23 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Imports:
pillar (>= 1.8.1),
rlang (>= 1.1.0),
tibble (>= 3.2.1),
vctrs,
tidyselect (>= 1.2.0),
magrittr
Encoding: UTF-8
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,4 @@ importFrom(utils,head)
importFrom(utils,lsf.str)
importFrom(utils,read.table)
importFrom(utils,tail)
importFrom(vctrs,vec_as_names)
8 changes: 7 additions & 1 deletion R/records.R
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ getRecords.default <- getRecords.character
#' @param allReferenceFields include all the fields in referenced records; the
#' default is FALSE
#' @param columnNames Can be "pretty", "label", "id", c("code", "id), or c("code", "label"); default is "pretty".
#' @param .names_repair Treatment of problematic column names following the approach used in tibbles / vctrs. Default is "unique".
#' @param style a style to modify with one or more parameters
#'
#' @export
Expand All @@ -524,6 +525,7 @@ columnStyle <- function(
columnNames = "pretty",
recordId = TRUE,
lastEditedTime = TRUE,
.names_repair = "unique",
style) {
stopifnot(is.logical(referencedId))
stopifnot(is.logical(referencedKey))
Expand All @@ -547,7 +549,8 @@ columnStyle <- function(
"allReferenceFields" = allReferenceFields,
"columnNames" = columnNames,
"recordId" = recordId,
"lastEditedTime" = lastEditedTime
"lastEditedTime" = lastEditedTime,
".names_repair" = .names_repair
)
class(style) <- c("activityInfoColumnStyle", class(style))
}
Expand Down Expand Up @@ -819,6 +822,7 @@ varNames <- function(x, style, addNames) {
UseMethod("varNames")
}

#' @importFrom vctrs vec_as_names
#' @exportS3Method varNames activityInfoFormTree
varNames.activityInfoFormTree <- function(x, style = defaultColumnStyle(), addNames = FALSE) {
fmSchema <- x$forms[[x$root]]
Expand Down Expand Up @@ -855,6 +859,8 @@ varNames.activityInfoFormTree <- function(x, style = defaultColumnStyle(), addNa
elementVars(element = y, formTree = x, style = style, namedElement = FALSE)
})))

vrNames <- vctrs::vec_as_names(vrNames, repair = style[[".names_repair"]])

if(addNames) {
names(vrNames) <- vrNames
}
Expand Down
3 changes: 3 additions & 0 deletions man/columnStyle.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
11 changes: 0 additions & 11 deletions tests/testthat/_snaps/records.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,17 +533,6 @@
499 499 4_stuff False 2021-07-24
500 500 5_stuff False 2021-07-25

# Copying of schemas with extractSchemaFromFields()

structure(list(databaseId = "<id value>", elements = list(structure(list(
code = NULL, description = NULL, id = "<id value>", key = TRUE,
label = "Identifier number", relevanceCondition = "", required = TRUE,
tableVisible = TRUE, type = "FREE_TEXT", typeParameters = list(
barcode = FALSE), validationCondition = ""), class = c("activityInfoTextFieldSchema",
"activityInfoFormFieldSchema", "formField", "list"))), id = "<id value>",
label = "new form"), class = c("activityInfoFormSchema",
"formSchema", "list"))

# Reference field with shallow reference table should provide field based names

Code
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/_snaps/tableQuery.md

This file was deleted.

14 changes: 12 additions & 2 deletions tests/testthat/test-databases.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ testthat::test_that("addDatabase() and deleteDatabase() works", {
})

testthat::test_that("getDatabases() works", {
databases <- getDatabases()

# update snapshot; works for now
databases <- getDatabases() %>%
select("billingAccountId", "databaseId", "description", "label", "ownerId", "suspended")
databases <- canonicalizeActivityInfoObject(databases)


testthat::expect_snapshot(databases)
})

Expand Down Expand Up @@ -45,11 +49,13 @@ testthat::test_that("getDatabaseResources() works", {
subForms <- dbResources[dbResources$type == "SUB_FORM",]
})

dbResources <- dbResources[order(dbResources$id, dbResources$parentId, dbResources$label, dbResources$visibility),]
dbResources <- dbResources[order(dbResources$id, dbResources$parentId, dbResources$label, dbResources$visibility),] %>%
select(id, label, parentId, type, visibility)
dbResources$id <- substr(dbResources$id,1,9)
dbResources$parentId <- substr(dbResources$parentId,1,9)
row.names(dbResources) <- NULL
dbResources <- canonicalizeActivityInfoObject(dbResources, replaceId = FALSE)



testthat::expect_snapshot(dbResources)
Expand Down Expand Up @@ -114,8 +120,10 @@ simplifyUsers <- function(returnedUsers, additionalFields = list(), addedUsers =
message(sprintf(msg, paste(missingFields, collapse="', '")))
}
x["version"] <- NULL
x <- x[names(x) %in% expectedFields]
x <- x[sapply(x, is.atomic)]
x <- x[order(names(x))]

x
})
}
Expand All @@ -127,6 +135,7 @@ testthat::test_that("addDatabaseUser() and deleteDatabaseUser() and getDatabaseU

returnedUsers <- addTestUsers(database, tree, nUsers = 2)

# update snapshot; safe for now
expectActivityInfoSnapshot(simplifyUsers(returnedUsers, addedUsers = TRUE))

nUsers <- 2
Expand Down Expand Up @@ -156,6 +165,7 @@ testthat::test_that("addDatabaseUser() and deleteDatabaseUser() and getDatabaseU

testthat::expect_equal(class(users2), "data.frame")

# update snapshot; safe for now
expectActivityInfoSnapshot(simplifyUsers(users))

deleteTestUsers(database, returnedUsers)
Expand Down
5 changes: 4 additions & 1 deletion tests/testthat/test-formField.r
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ test_that("Test deleteFormField()", {
addFormField(textFieldSchema(label = "Text field 3", code = "txt3", id = "text3")) %>%
addFormField(textFieldSchema(label = "Text field 4", code = "txt4", id = "text4")) %>%
addFormField(textFieldSchema(label = "Text field 5", code = "txt5", id = "text5"))


## Safe snapshots because made from R
test1 <- fmSchm %>% deleteFormField(code = c("txt1", "txt3"))
expectActivityInfoSnapshot(test1)

Expand All @@ -35,6 +36,7 @@ test_that("Test deleteFormField()", {

test3 <- fmSchm %>% deleteFormField(label = c("Text field 1", "Text field 5"))
expectActivityInfoSnapshot(test3)
##

testthat::expect_warning({
fmSchm %>% deleteFormField(id = c("Text field 1", "Text field 5"))
Expand Down Expand Up @@ -245,6 +247,7 @@ testthat::test_that("migrateFieldData() works", {

recordsMinimal <- getRecords(newSchema, minimalColumnStyle()) %>% collect() %>% as.data.frame()

# should be a safe snapshot with minimalColumnStyle
testthat::expect_snapshot(recordsMinimal)
})

12 changes: 11 additions & 1 deletion tests/testthat/test-records.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ testthat::test_that("add, update, and deleteRecord() works", {
alice <- getRecord(form$id, alice$recordId)
assertthat::assert_that(alice$fields[[ageField$id]] == 25)

# It shouldn't be possible to add a record with an existing id
expect_error(
alice2 <- addRecord(formId = form$id, fieldValues = list(NAME = "Alice Duplicate", AGE = 25), recordId = alice$recordId)
)

# It is possible to add a record with a user provided id
eliza <- addRecord(formId = form$id, fieldValues = list(NAME = "Eliza", AGE = as.integer(format(Sys.Date(), "%Y")) - 1964), recordId = cuid())

# It shouldn't be possible to update or delete non-existant records
expect_error(updateRecord(form$id, recordId = "foobar", fieldValues = list(AGE = 25)))
expect_error(deleteRecord(form$id, recordId = "foobar"))
Expand Down Expand Up @@ -246,7 +254,9 @@ testthat::test_that("getRecords() works", {

identicalForm(schemaToCompare, newSchema)

expectActivityInfoSnapshot(newSchema)
# removing newSchema snapshot - not per se safe - should use new snapshot function
expectActivityInfoSnapshotCompare(newSchema, "extractSchemaFromFields")
#expectActivityInfoSnapshot(newSchema)

# no form schema elements to provide - expect warning
testthat::expect_warning({
Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/test-tableQuery.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ testthat::test_that("queryTable() returns a single column data.frame if the inpu
output <- do.call(activityinfo::queryTable, input_parameters2)
})


testthat::expect_true(inherits(output, "data.frame"))
testthat::expect_true(all(names_valid %in% colnames(output)))
testthat::expect_identical(length(colnames(output)), 1L)

testthat::expect_snapshot_value(deparse(output))

testthat::expect_true(all(output$Person.name %in% c("Alice","Bob")))
testthat::expect_equal(nrow(output), 2)

})


Expand Down

0 comments on commit 945c7b2

Please sign in to comment.