|
| 1 | +# Return ---- |
| 2 | +test_that("upgrade_schema() returns a valid schema", { |
| 3 | + schema_v1 <- schema(example_package(version = "1.0"), "deployments") |
| 4 | + expect_no_error(check_schema(upgrade_schema(schema_v1, "deployments"))) |
| 5 | +}) |
| 6 | + |
| 7 | +# Functionality ---- |
| 8 | +test_that("upgrade_schema() upgrades a v1 schema to v2", { |
| 9 | + schema_v1 <- schema(example_package(version = "1.0"), "deployments") |
| 10 | + expect_identical(version(upgrade_schema(schema_v1, "deployments")), "2.0") |
| 11 | +}) |
| 12 | + |
| 13 | +test_that("upgrade_schema() leaves a v2 schema as is", { |
| 14 | + schema_v2 <- schema(example_package(version = "2.0"), "deployments") |
| 15 | + expect_identical(upgrade_schema(schema_v2, "deployments"), schema_v2) |
| 16 | +}) |
| 17 | + |
| 18 | +test_that("upgrade_schema() sets $schema to default v2 value", { |
| 19 | + schema_v1 <- schema(example_package(version = "1.0"), "deployments") |
| 20 | + expect_identical( |
| 21 | + upgrade_schema(schema_v1, "deployments")$`$schema`, |
| 22 | + "https://datapackage.org/profiles/2.0/tableschema.json" |
| 23 | + ) |
| 24 | +}) |
| 25 | + |
| 26 | +test_that("upgrade_schema() updates primaryKey", { |
| 27 | + schema_v1 <- schema(example_package(version = "1.0"), "deployments") |
| 28 | + |
| 29 | + # Update character |
| 30 | + schema_v1$primaryKey <- "deployment_id" |
| 31 | + expect_identical( |
| 32 | + upgrade_schema(schema_v1, "deployments")$primaryKey, |
| 33 | + list("deployment_id") |
| 34 | + ) |
| 35 | + |
| 36 | + # Leave undefined |
| 37 | + schema_v1$primaryKey <- NULL |
| 38 | + expect_null(upgrade_schema(schema_v1, "deployments")$primaryKey) |
| 39 | + |
| 40 | + # Leave list |
| 41 | + schema_v1$primaryKey <- list("deployment_id", "other_id") |
| 42 | + expect_identical( |
| 43 | + upgrade_schema(schema_v1, "deployments")$primaryKey, |
| 44 | + list("deployment_id", "other_id") |
| 45 | + ) |
| 46 | +}) |
| 47 | + |
| 48 | +test_that("upgrade_schema() updates foreignKeys", { |
| 49 | + schema_v1 <- schema(example_package(version = "1.0"), "media") |
| 50 | + # Media has fk to deployment.deployment_id and observations.observation_id |
| 51 | + |
| 52 | + # v1 foreignKeys become v2 |
| 53 | + expect_identical( |
| 54 | + upgrade_schema(schema_v1, "media")$foreignKeys, |
| 55 | + schema(example_package(version = "2.0"), "media")$foreignKeys, |
| 56 | + ) |
| 57 | + |
| 58 | + # Leave undefined |
| 59 | + schema_v1$foreignKeys <- NULL |
| 60 | + expect_null(upgrade_schema(schema_v1, "media")$foreignKeys) |
| 61 | + |
| 62 | + # Extensive example |
| 63 | + provided_keys <- list( |
| 64 | + fields = "not_in_list", # Ignore |
| 65 | + list( |
| 66 | + fields = "media_id", # Update to list |
| 67 | + custom = "custom", # Ignore |
| 68 | + reference = list( |
| 69 | + resource = "media", # Self-referential, remove |
| 70 | + fields = "media_id", # Update to list |
| 71 | + custom = "custom" # Ignore |
| 72 | + ) |
| 73 | + ), |
| 74 | + list( |
| 75 | + custom = "custom", # Ignore |
| 76 | + reference = list(custom = "custom") # Ignore |
| 77 | + ) |
| 78 | + ) |
| 79 | + expected_keys <- list( |
| 80 | + fields = "not_in_list", |
| 81 | + list( |
| 82 | + fields = list("media_id"), |
| 83 | + custom = "custom", |
| 84 | + reference = list( |
| 85 | + fields = list("media_id"), |
| 86 | + custom = "custom" |
| 87 | + ) |
| 88 | + ), |
| 89 | + list( |
| 90 | + custom = "custom", |
| 91 | + reference = list(custom = "custom") |
| 92 | + ) |
| 93 | + ) |
| 94 | + schema_v1$foreignKeys <- provided_keys |
| 95 | + expect_identical( |
| 96 | + upgrade_schema(schema_v1, "media")$foreignKeys, |
| 97 | + expected_keys |
| 98 | + ) |
| 99 | +}) |
0 commit comments