Skip to content

Commit 1900780

Browse files
committed
checking presence of ".template" variable and adding tests for check_guide
1 parent e3a40c2 commit 1900780

File tree

8 files changed

+169
-8
lines changed

8 files changed

+169
-8
lines changed

R/guide.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ validate_unique_names <- function(names_vector, name_type) {
5959
#' @noRd
6060
#'
6161
check_guide <- function(guide) {
62-
## NOTE: Most of the validation of a guide should be performed using the JSON schema
62+
## NOTE: Most of the validation of a guide should be performed using the schema validator before the guide is used
6363

6464
# Ensure translations are optional
6565
if (!"translations" %in% names(guide)) {
@@ -71,6 +71,17 @@ check_guide <- function(guide) {
7171
validate_plate_format(guide)
7272
}
7373

74+
# validate presence of .template variable in locations
75+
if (
76+
!any(sapply(guide$locations, function(loc) {
77+
isTRUE(loc$varname == ".template")
78+
}))
79+
) {
80+
rlang::abort(
81+
"The spreadsheet guide must contain the '.template' element with at least a version key."
82+
)
83+
}
84+
7485
# Validate each location in the guide
7586
lapply(guide$locations, validate_location, guide)
7687
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
guide.version: '1.0'
2+
template.name: test
3+
template.min.version: '1.0'
4+
template.max.version: ~
5+
locations:
6+
- sheet: description
7+
type: keyvalue
8+
varname: .template
9+
translate: false
10+
ranges:
11+
- A1:B1
12+
translations:
13+
- long: Version
14+
short: template.version
15+
- long: Version
16+
short: version.duplicate
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
guide.version: '1.0'
2+
template.name: test
3+
template.min.version: '1.0'
4+
template.max.version: ~
5+
locations:
6+
- sheet: description
7+
type: keyvalue
8+
varname: .template
9+
translate: false
10+
ranges:
11+
- A1:B1
12+
translations:
13+
- long: Version
14+
short: template.version
15+
- long: Version2
16+
short: template.version
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
guide.version: '1.0'
2+
template.name: test
3+
template.min.version: '1.0'
4+
template.max.version: ~
5+
plate.format: 192
6+
locations:
7+
- sheet: description
8+
type: keyvalue
9+
varname: .template
10+
translate: false
11+
ranges:
12+
- A1:B1
13+
- sheet: _data
14+
type: platedata
15+
translate: false
16+
varname: plate
17+
ranges:
18+
- A1:M9
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
guide.version: '1.0'
2+
template.name: test
3+
template.min.version: '1.0'
4+
template.max.version: ~
5+
locations:
6+
- sheet: description
7+
type: keyvalue
8+
varname: metadata
9+
translate: false
10+
ranges:
11+
- A1:B5
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
guide.version: '1.0'
2+
template.name: test
3+
template.min.version: '1.0'
4+
template.max.version: ~
5+
locations:
6+
- sheet: description
7+
type: keyvalue
8+
varname: .template
9+
translate: false
10+
ranges:
11+
- A1:B1
12+
translations:
13+
- long: File path
14+
short: filepath
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
guide.version: '1.0'
2+
template.name: test
3+
template.min.version: '1.0'
4+
template.max.version: ~
5+
locations:
6+
- sheet: description
7+
type: keyvalue
8+
varname: .template
9+
translate: false
10+
ranges:
11+
- A1:B1
12+
- sheet: description
13+
type: keyvalue
14+
varname: metadata
15+
translate: false
16+
ranges:
17+
- A2:B5

tests/testthat/test-guide.R

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
test_that("Reading a spreadsheet map works", {
1+
test_that("Reading a valid guide with platedata works", {
22
expect_no_error(read_guide(test_path("fixtures/guide_competition_1_0.yml")))
33
})
44

5+
test_that("Reading a valid guide without platedata works", {
6+
expect_no_error(read_guide(test_path(
7+
"fixtures/goodguides/guide_without_platedata.yml"
8+
)))
9+
})
10+
511
test_that("Function check_dim works", {
612
expect_no_error(check_dim("A1:B1", NA, 2))
713
expect_no_error(check_dim("A1:C10", 10, 3))
@@ -11,9 +17,61 @@ test_that("Function check_dim works", {
1117
expect_error(check_dim("A1:C3", NA, NA))
1218
})
1319

14-
#test_that("check_guide works for correct guides", {
15-
# expect_no_error(read_guide(test_path("fixtures/goodguides/guide_competition_9_3.yml")))
16-
# expect_no_error(read_guide(test_path("fixtures/goodguides/guide_without_platedata.yml")))
17-
# expect_no_error(read_guide(test_path("fixtures/goodguides/guide_with_two_plates.yml")))
18-
# expect_no_error(read_guide(test_path("fixtures/goodguides/guide_without_templatemetadata.yml")))
19-
#})
20+
test_that("check_guide: missing plate.format when platedata is present yields an error", {
21+
expect_error(
22+
read_guide(test_path("fixtures/erroneousguides/missing_plate_format.yml")),
23+
regexp = "plate.format"
24+
)
25+
})
26+
27+
test_that("check_guide: invalid plate.format value yields an error", {
28+
expect_error(
29+
read_guide(test_path("fixtures/erroneousguides/invalid_plate_format.yml")),
30+
regexp = "plate format"
31+
)
32+
})
33+
34+
test_that("check_guide: wrong platedata range dimensions yield an error", {
35+
expect_error(
36+
read_guide(test_path(
37+
"fixtures/erroneousguides/invalid_platedata_range.yml"
38+
)),
39+
regexp = "required dimensions"
40+
)
41+
})
42+
43+
test_that("check_guide: guide without a .template location yields an error", {
44+
expect_error(
45+
read_guide(test_path(
46+
"fixtures/erroneousguides/missing_template_location.yml"
47+
)),
48+
regexp = "\\.template"
49+
)
50+
})
51+
52+
test_that("read_guide: duplicate long names in translations yield an error", {
53+
expect_error(
54+
read_guide(test_path(
55+
"fixtures/erroneousguides/duplicate_long_translations.yml"
56+
)),
57+
regexp = "Duplicate keys in long"
58+
)
59+
})
60+
61+
test_that("read_guide: duplicate short names in translations yield an error", {
62+
expect_error(
63+
read_guide(test_path(
64+
"fixtures/erroneousguides/duplicate_short_translations.yml"
65+
)),
66+
regexp = "Duplicate keys in short"
67+
)
68+
})
69+
70+
test_that("read_guide: reserved 'File path' long name mapped to non-.sourcefile short name yields an error", {
71+
expect_error(
72+
read_guide(test_path(
73+
"fixtures/erroneousguides/reserved_filepath_translation.yml"
74+
)),
75+
regexp = "File path"
76+
)
77+
})

0 commit comments

Comments
 (0)