V23 Test a target roundtrip
Use your current course session. Preparation and course runner · R script ↓
Optional deep dive. Enable only the options named in each block, after preparing its packages or services.
V23a Define a custom source
Goal: Understand R's S3 extension protocol.
Prerequisites: H01; extras.
Create an in-memory source with a declared read capability. Register methods with their generics without editing package files. R's S3 method system here is unrelated to Amazon S3 storage.
training_step(
"V23a",
{
training_source <- structure(
list(data = policies),
class = "training_source"
)
dr_check_component.training_source <- function(x, ...) invisible(x)
dr_read_source.training_source <- function(source, ...) source$data
dr_inspect.training_source <- function(x, ...) {
list(type = "training_source")
}
dr_capabilities.training_source <- function(x, ...) {
dr_component_capabilities(read = TRUE)
}
for (generic in c(
"dr_check_component",
"dr_read_source",
"dr_inspect",
"dr_capabilities"
)) {
registerS3method(
generic,
"training_source",
get(paste0(generic, ".training_source")),
envir = asNamespace("dataraft.core")
)
}
print(dr_inspect(training_source))
},
needs = c("H01"),
enabled = training_options$extras,
optional = TRUE
)Expected result: A safe description without data contents.
Task: Should dr_inspect read the actual data?
Show solution
No. Inspection describes the component without executing it.
V23b Use the custom source
Goal: Plug an adapter into an ordinary product.
Prerequisites: V23a; adapters.
A custom adapter should work through the same product and contract interface.
training_step(
"V23b",
{
custom_product <- dr_product(
"policies.custom",
training_source,
contract = policy_contract
)
print(dr_collect(dr_run(custom_product, write = FALSE)))
print(dataraft.adapters::dr_test_adapter(
training_source,
expected = policies
))
},
needs = c("V23a"),
packages = c("dataraft.adapters"),
enabled = training_options$extras,
optional = TRUE
)Expected result: Six unchanged policy rows.
Task: Which new function did the product need to learn?
Show solution
None. The S3 protocols connect product and source.
V23 Test a target roundtrip
Goal: Read back what was written.
Prerequisites: H04; adapters; extras.
The target test needs read_back and a contract. A successful write alone does not prove a correct roundtrip.
training_step(
"V23",
{
conformance_path <- file.path(training_root, "adapter-conformance")
conformance <- dataraft.adapters::dr_test_adapter(
dataraft.adapters::dr_target_rds(conformance_path),
data = policies,
context = list(contract = policy_contract),
read_back = function(output) {
dr_read_source(dataraft.adapters::dr_source_rds(
conformance_path,
output$version
))
}
)
print(conformance)
},
needs = c("H04"),
packages = c("dataraft.adapters"),
enabled = training_options$extras,
optional = TRUE
)Expected result: A comparison of written and reread values.
Task: What additional failure test would be useful?
Show solution
A writer failure that demonstrably leaves no partially committed output.