-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_eml.R
More file actions
371 lines (260 loc) · 13 KB
/
Copy pathtest_eml.R
File metadata and controls
371 lines (260 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
context("EML")
mn <- env_load()$mn
test_that("a dataTable and otherEntity can be added from a pid", {
if (!is_token_set(mn)) {
skip("No token set. Skipping test.")
}
data_path <- tempfile()
writeLines(LETTERS, data_path)
pid1 <- publish_object(mn, data_path, "text/csv")
pid2 <- publish_object(mn, data_path, "text/csv")
eml_path <- file.path(system.file(package = "arcticdatautils"), "example-eml.xml")
doc <- EML::read_eml(eml_path)
dummy_factors <- c("factor 1", "factor 2")
dummy_attributes <- create_dummy_attributes_dataframe(10, dummy_factors)
dummy_enumeratedDomain <- create_dummy_enumeratedDomain_dataframe(dummy_factors)
dummy_attributeList <- EML::set_attributes(dummy_attributes, factors = dummy_enumeratedDomain)
dummy_entityName <- "Test_Name"
dummy_entityDescription <- "Test_Description"
# Create an otherEntity
OE <- pid_to_eml_entity(mn, pid1,
entityName = dummy_entityName,
entityDescription = dummy_entityDescription,
attributeList = dummy_attributeList)
expect_true(OE$entityName == dummy_entityName)
expect_true(OE$entityDescription == dummy_entityDescription)
# Create a dataTable
DT <- pid_to_eml_entity(mn, pid2,
entity_type = "dataTable",
entityName = dummy_entityName,
entityDescription = dummy_entityDescription,
attributeList = dummy_attributeList)
expect_true(DT$entityName == dummy_entityName)
expect_true(DT$entityDescription == dummy_entityDescription)
doc$dataset$otherEntity <- OE
expect_true(EML::eml_validate(doc))
doc$dataset$dataTable <- DT
expect_true(EML::eml_validate(doc))
unlink(data_path)
})
test_that("eml_otherEntity_to_dataTable fails gracefully", {
if (!is_token_set(mn)) {
skip("No token set. Skipping test.")
}
doc <- read_eml(system.file("example-eml.xml", package = "arcticdatautils"))
# incorrect inputs
expect_error(eml_otherEntity_to_dataTable("dummy input"))
expect_error(eml_otherEntity_to_dataTable(doc, "1"))
# subscripts out of bounds
expect_error(eml_otherEntity_to_dataTable(doc, 2))
# Duplicate entityNames found
doc$dataset$otherEntity <- list(doc$dataset$otherEntity, doc$dataset$otherEntity)
expect_error(eml_otherEntity_to_dataTable(doc, 1))
})
test_that("eml_otherEntity_to_dataTable works when every object is boxed", {
if (!is_token_set(mn)) {
skip("No token set. Skipping test.")
}
doc <- read_eml(system.file("example-eml.xml", package = "arcticdatautils"))
doc$dataset$otherEntity$attributeList <- EML::set_attributes(create_dummy_attributes_dataframe(1))
otherEntity <- doc$dataset$otherEntity
doc <- eml_otherEntity_to_dataTable(doc, 1)
# test that otherEntity was removed
expect_length(doc$dataset$otherEntity, 0)
# test that dataTable was added
expect_equal(otherEntity$entityName, doc$dataset$dataTable[[1]]$entityName)
expect_equivalent(otherEntity$physical, doc$dataset$dataTable[[1]]$physical)
})
test_that("eml_oe_to_dt works in all cases", {
me <- list(individualName = list(givenName = "Kristen", surName = "Peach"))
attributes <- data.frame(attributeName = 'length_1',
attributeDefinition = 'def1',
measurementScale = 'ratio',
domain = 'numericDomain',
unit = 'meter',
numberType = 'real',
stringsAsFactors = FALSE)
att_list <- EML::set_attributes(attributes)
oe1 <- list(entityName = 'other entity', attributeList = att_list, entityType = "other")
oe2 <- list(entityName = 'other entity2', attributeList = att_list, entityType = "other")
dT1 = list(entityName = "data table", attributeList = att_list)
dT2 = list(entityName = "data table2", attributeList = att_list)
#Testing if it works when there are 0 dataTables and 1 otherEntity
doc_zero_dts <- list(packageId = 'id', system = 'system',
dataset = list(title = 'A Mimimal Valid EML Dataset',
creator = me,
contact = me,
otherEntity = oe1))
write_eml(doc_zero_dts, "~/test.xml")
doc_zero_dts <- read_eml("~/test.xml")
doc_zero_dts <- eml_otherEntity_to_dataTable(doc_zero_dts, 1)
expect_true(eml_validate(doc_zero_dts))
#Testing if it works when there are 2 dataTables and 1 otherEntity
doc_two_dts <- list(packageId = 'id', system = 'system',
dataset = list(title = 'A Mimimal Valid EML Dataset',
creator = me,
contact = me,
dataTable = list(dT1, dT2),
otherEntity = oe1))
write_eml(doc_two_dts, "~/test.xml")
doc_two_dts <- read_eml("~/test.xml")
doc_two_dts <- eml_otherEntity_to_dataTable(doc_two_dts, 1)
expect_true(eml_validate(doc_two_dts))
#Testing if it works when there are 2 dataTables and 1 boxed otherEntity
doc_two_dts_boxed_oe <- list(packageId = 'id', system = 'system',
dataset = list(title = 'A Mimimal Valid EML Dataset',
creator = me,
contact = me,
dataTable = list(dT1, dT2),
otherEntity = list(oe1)))
write_eml(doc_two_dts_boxed_oe, "~/test.xml")
doc_two_dts_boxed_oe <- read_eml("~/test.xml")
doc_two_dts_boxed_oe <- eml_otherEntity_to_dataTable(doc_two_dts_boxed_oe, 1)
expect_true(eml_validate(doc_two_dts_boxed_oe))
#Testing if it works when there are 2 otherEntities and 0 dataTables
doc_two_oes <- list(packageId = 'id', system = 'system',
dataset = list(title = 'A Mimimal Valid EML Dataset',
creator = me,
contact = me,
otherEntity = list(oe1, oe2)))
write_eml(doc_two_oes, "~/test.xml")
doc_two_oes <- read_eml("~/test.xml")
doc_two_oes <- eml_otherEntity_to_dataTable(doc_two_oes, 1)
expect_true(eml_validate(doc_two_oes))
#Testing if it works when there are two otherEntities and 1 dataTable
doc_two_oes_one_dt <- list(packageId = 'id', system = 'system',
dataset = list(title = 'A Mimimal Valid EML Dataset',
creator = me,
contact = me,
dataTable = dT1,
otherEntity = list(oe1, oe2)))
write_eml(doc_two_oes_one_dt, "~/test.xml")
doc_two_oes_one_dt <- read_eml("~/test.xml")
doc_two_oes_one_dt <- eml_otherEntity_to_dataTable(doc_two_oes_one_dt, 1)
expect_true(eml_validate(doc_two_oes_one_dt))
})
test_that('reorder_pids orders pids correctly', {
me <- list(individualName = list(givenName = "Jeanette", surName = "Clark"))
oe1 <- list(entityName = "object one", entityType = "other")
oe2 <- list(entityName = "object two", entityType = "other")
doc <- list(packageId = "an id", system = "a system",
dataset = list(
title = "A Mimimal Valid EML Dataset",
creator = me,
contact = me,
otherEntity = list(oe1, oe2)))
pid_list <- list("object two" = "some identifier2", "object one" = "some identifier1")
ordered_pids <- reorder_pids(pid_list, doc)
entity_names <- eml_get_simple(doc, "entityName")
expect_equal(names(ordered_pids), entity_names)
})
test_that('reorder_pids fails gracefully', {
me <- list(individualName = list(givenName = "Jeanette", surName = "Clark"))
oe1 <- list(entityName = "object one", entityType = "other")
doc <- list(packageId = "an id", system = "a system",
dataset = list(
title = "A Mimimal Valid EML Dataset",
creator = me,
contact = me,
otherEntity = list(oe1)))
pid_list <- list("object two" = "some identifier2", "object one" = "some identifier1")
expect_error(reorder_pids(pid_list, doc))
})
test_that('eml_nsf_to_project generates a valid project section', {
# for a single award, EML 2.1.1
awards <- "1203146"
proj <- eml_nsf_to_project(awards)
me <- list(individualName = list(givenName = "Jeanette", surName = "Clark"))
doc <- list(packageId = "id", system = "system",
dataset = list(title = "A Mimimal Valid EML Dataset",
creator = me,
contact = me))
doc$dataset$project <- proj
expect_true(eml_validate(doc))
# for multiple awards, EML 2.1.1
awards <- c("1203146", "1203473", "1603116")
proj <- eml_nsf_to_project(awards)
me <- list(individualName = list(givenName = "Jeanette", surName = "Clark"))
doc <- list(packageId = "id", system = "system",
dataset = list(title = "A Mimimal Valid EML Dataset",
creator = me,
contact = me))
doc$dataset$project <- proj
expect_true(eml_validate(doc))
# for multiple awards, EML 2.2.0
awards <- c("1203146", "1203473", "1603116")
emld::eml_version("eml-2.2.0")
proj <- eml_nsf_to_project(awards, eml_version = "2.2")
me <- list(individualName = list(givenName = "Jeanette", surName = "Clark"))
doc <- list(packageId = "id", system = "system",
dataset = list(title = "A Mimimal Valid EML Dataset",
creator = me,
contact = me))
doc$dataset$project <- proj
expect_true(eml_validate(doc))
})
test_that('eml_nsf_to_project handles bad funding numbers gracefully', {
awards <- c("abcdef", "1203473", "12345")
expect_warning(proj <- eml_nsf_to_project(awards), "this award will not be included in the project section")
me <- list(individualName = list(givenName = "Jeanette", surName = "Clark"))
doc <- list(packageId = "id", system = "system",
dataset = list(title = "A Mimimal Valid EML Dataset",
creator = me,
contact = me))
doc$dataset$project <- proj
expect_true(eml_validate(doc))
})
test_that('eml_nsf_to_project fails gracefully', {
awards <- c("abcdef", "12345")
expect_error(suppressWarnings(proj <- eml_nsf_to_project(awards)), "No valid award numbers were found")
})
test_that('eml_nsf_to_project parses two-word last names correctly', {
proj <- eml_nsf_to_project("1822406", eml_version = "2.2")
expect_true("Maria" %in% eml_get_simple(proj$personnel, "givenName"))
expect_true("Val Martin" %in% eml_get_simple(proj$personnel, "surName"))
})
test_that('Data object physical created for an EML', {
if (!is_token_set(mn)) {
skip("No token set. Skipping test.")
}
cn_staging <- CNode('STAGING')
mn_test <- getMNode(cn_staging, 'urn:node:mnTestARCTIC')
pkg <- arcticdatautils::get_package(mn_test, 'resource_map_urn:uuid:8e0cf450-44d2-4c9d-aa48-09cb32538d2b')
doc <- EML::read_eml(getObject(mn_test, pkg$metadata))
csv1_physical <- arcticdatautils::pid_to_eml_physical(mn_test, pkg$data[4], num_header_lines = sample(1:100,1))
attributes1 <- arcticdatautils::create_dummy_attributes_dataframe(2)
attributeList1 <- set_attributes(attributes1)
dataTable <- list(entityName = "dummy1.csv",
entityDescription = "test csv",
physical = csv1_physical,
attributeList = attributeList1)
doc$dataset$dataTable[[1]] <- dataTable
expect_true(EML::eml_validate(doc))
})
test_that('Valid publisher information can be added', {
me <- list(individualName = list(givenName = "Jeanette", surName = "Clark"))
doc <- list(packageId = "id", system = "system",
dataset = list(title = "A Mimimal Valid EML Dataset",
creator = me,
contact = me))
doc <- eml_add_publisher(doc)
expect_true(EML::eml_validate(doc))
expect_equal(doc$dataset$publisher$organizationName, "NSF Arctic Data Center")
})
test_that('Identifier systems can be added', {
me <- list(individualName = list(givenName = "Jeanette", surName = "Clark"))
doc <- list(packageId = "id", system = "system",
dataset = list(title = "A Mimimal Valid EML Dataset",
creator = me,
contact = me,
otherEntity = list(list(entityName = "name",
entityType = "other",
id = "urn:uuid:asldkfjh"),
list(entityName = "name2",
entityType = "other",
id = "arctic-data.2.2"))))
doc <- eml_add_entity_system(doc)
expect_true(EML::eml_validate(doc))
expect_equal(doc$dataset$otherEntity[[1]]$system, "https://tools.ietf.org/html/rfc4122")
expect_equal(doc$dataset$otherEntity[[2]]$system, "https://search.dataone.org")
})