Skip to content

Commit

Permalink
add google ods file #7
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkerlogue committed May 6, 2023
1 parent cadb40c commit 5c39b85
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
Binary file added inst/extdata/basic_example_google.ods
Binary file not shown.
95 changes: 95 additions & 0 deletions tests/testthat/test-ods_xml.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
example_file <- system.file("extdata", "basic_example.ods", package = "tidyods")
excel_file <- system.file("extdata", "basic_example_excel.ods", package = "tidyods")
google_file <- system.file("extdata", "basic_example_google.ods", package = "tidyods")

test_that("File handling errors", {
expect_error(unzip_ods_xml("nonexistent.csv"), regexp = "does not exist")
Expand Down Expand Up @@ -171,6 +172,100 @@ test_that("XML namespace (Excel)", {
expect_equal(xml2::xml_name(excel_ods, excel_ns), "office:document-content")
})

test_that("XML extraction (Google)", {
expect_silent(google_ods <- extract_ods_xml(google_file))
expect_type(google_ods, "list")
expect_s3_class(google_ods, c("xml_document", "xml_node"), exact = TRUE)
expect_length(google_ods, 2)
expect_named(google_ods, c("node", "doc"))
expect_equal(xml2::xml_length(google_ods), 4)
})

test_that("XML namespace (Google)", {
google_ods <- extract_ods_xml(google_file)
expect_silent(google_ns <- xml2::xml_ns(google_ods))
expect_type(google_ns, "character")
expect_s3_class(google_ns, "xml_namespace", exact = TRUE)
expect_length(google_ns, 35)
expect_named(
google_ns,
c("calcext",
"chart",
"css3t",
"dc",
"dom",
"dr3d",
"draw",
"drawooo",
"field",
"fo",
"form",
"formx",
"grddl",
"loext",
"math",
"meta",
"number",
"of",
"office",
"ooo",
"oooc",
"ooow",
"presentation",
"rpt",
"script",
"style",
"svg",
"table",
"tableooo",
"text",
"xforms",
"xhtml",
"xlink",
"xsd",
"xsi")
)
expect_equal(
as.character(google_ns),
c("urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0",
"urn:oasis:names:tc:opendocument:xmlns:chart:1.0",
"http://www.w3.org/TR/css3-text/",
"http://purl.org/dc/elements/1.1/",
"http://www.w3.org/2001/xml-events",
"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0",
"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
"http://openoffice.org/2010/draw",
"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0",
"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
"urn:oasis:names:tc:opendocument:xmlns:form:1.0",
"urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0",
"http://www.w3.org/2003/g/data-view#",
"urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0",
"http://www.w3.org/1998/Math/MathML",
"urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
"urn:oasis:names:tc:opendocument:xmlns:of:1.2",
"urn:oasis:names:tc:opendocument:xmlns:office:1.0",
"http://openoffice.org/2004/office",
"http://openoffice.org/2004/calc",
"http://openoffice.org/2004/writer",
"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",
"http://openoffice.org/2005/report",
"urn:oasis:names:tc:opendocument:xmlns:script:1.0",
"urn:oasis:names:tc:opendocument:xmlns:style:1.0",
"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
"urn:oasis:names:tc:opendocument:xmlns:table:1.0",
"http://openoffice.org/2009/table",
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"http://www.w3.org/2002/xforms",
"http://www.w3.org/1999/xhtml",
"http://www.w3.org/1999/xlink",
"http://www.w3.org/2001/XMLSchema",
"http://www.w3.org/2001/XMLSchema-instance")
)
expect_equal(xml2::xml_name(google_ods, google_ns), "office:document-content")
})


test_that("File size check passes", {
expect_invisible(check_xml_memory(example_file))
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-sheets.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ excel_file <- system.file("extdata", "basic_example_excel.ods", package = "tidyo
excel_ods <- extract_ods_xml(excel_file)
excel_ns <- xml2::xml_ns(excel_ods)

google_file <- system.file("extdata", "basic_example_google.ods", package = "tidyods")
google_ods <- extract_ods_xml(google_file)
google_ns <- xml2::xml_ns(google_ods)

sheet_paths <- c(
"/office:document-content/office:body/office:spreadsheet/table:table[1]",
"/office:document-content/office:body/office:spreadsheet/table:table[2]"
Expand All @@ -16,6 +20,7 @@ names(sheet_paths) <- c("penguins", "types")
test_that("Sheet names", {
expect_equal(ods_sheets(example_file), c("penguins", "types"))
expect_equal(ods_sheets(excel_file), c("penguins", "types"))
expect_equal(ods_sheets(google_file), c("penguins", "types"))
})

test_that("Sheet paths (LibreOffice)", {
Expand Down Expand Up @@ -61,3 +66,25 @@ test_that("Sheet paths (Excel)", {
regexp = "`sheet` must be the name of a sheet or index number"
)
})

test_that("Sheet paths (Google)", {
expect_equal(ods_sheet_paths(google_ods, google_ns), sheet_paths)
expect_equal(get_sheet_path(google_ods, 1, google_ns), sheet_paths[1])
expect_equal(get_sheet_path(google_ods, 2, google_ns), sheet_paths[2])
expect_equal(get_sheet_path(google_ods, "penguins", google_ns), sheet_paths[1])
expect_equal(get_sheet_path(google_ods, "types", google_ns), sheet_paths[2])

expect_error(get_sheet_path(google_ods, ns = google_ns))
expect_error(
get_sheet_path(google_ods, 1:2, google_ns),
regexp = "`sheet` must a character or numeric vector of length 1"
)
expect_error(
get_sheet_path(google_ods, "hello", google_ns),
regexp = "not a valid sheet name"
)
expect_error(
get_sheet_path(google_ods, TRUE, google_ns),
regexp = "`sheet` must be the name of a sheet or index number"
)
})

0 comments on commit 5c39b85

Please sign in to comment.