Skip to content

Commit

Permalink
Add tests for altrep wrap_real attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
trossi committed Oct 2, 2024
1 parent 0afed10 commit 45820b4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Binary file not shown.
Binary file not shown.
43 changes: 42 additions & 1 deletion rdata/tests/test_rdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,14 +649,55 @@ def test_altrep_deferred_string(self) -> None:

def test_altrep_wrap_real(self) -> None:
"""Test alternative representation of wrap_real."""
data = rdata.read_rda(
parsed = rdata.parser.parse_file(
TESTDATA_PATH / "test_altrep_wrap_real.rda",
)
obj = parsed.object.value[0] # taking value as it's an rda file
assert not obj.info.object
assert not obj.info.attributes
assert obj.attributes is None

data = rdata.conversion.convert(parsed)
np.testing.assert_equal(data, {
"test_altrep_wrap_real": [3],
})

def test_altrep_wrap_real_attributes(self) -> None:
"""Test alternative representation of wrap_real with attributes."""
# File created in R with
# a = .Internal(wrap_meta(c(1, 2, 3), 0, 0)); attr(a, "foo") = "bar"; saveRDS(a, file="test_altrep_wrap_real_attributes.rds") # noqa: E501
parsed = rdata.parser.parse_file(
TESTDATA_PATH / "test_altrep_wrap_real_attributes.rds",
)
obj = parsed.object
assert not obj.info.object
assert obj.info.attributes
assert obj.attributes is not None
assert obj.attributes.tag is not None
assert obj.attributes.tag.value.value == b"foo"
assert obj.attributes.value[0].value[0].value == b"bar"

data = rdata.conversion.convert(parsed)
np.testing.assert_equal(data, [1., 2., 3.])

def test_altrep_wrap_real_class_attribute(self) -> None:
"""Test alternative representation of wrap_real with class attribute."""
# File created in R with
# a = .Internal(wrap_meta(c(1, 2, 3), 0, 0)); attr(a, "class") = "Date"; saveRDS(a, file="test_altrep_wrap_real_class_attribute.rds") # noqa: E501
parsed = rdata.parser.parse_file(
TESTDATA_PATH / "test_altrep_wrap_real_class_attribute.rds",
)
obj = parsed.object
assert obj.info.object
assert obj.info.attributes
assert obj.attributes is not None
assert obj.attributes.tag is not None
assert obj.attributes.tag.value.value == b"class"
assert obj.attributes.value[0].value[0].value == b"Date"

data = rdata.conversion.convert(parsed)
np.testing.assert_equal(data, [1., 2., 3.])

def test_altrep_wrap_string(self) -> None:
"""Test alternative representation of wrap_string."""
data = rdata.read_rda(TESTDATA_PATH / "test_altrep_wrap_string.rda")
Expand Down

0 comments on commit 45820b4

Please sign in to comment.