import sys import xml.etree.ElementTree import numpy as np import pytest from numpy.testing import assert_array_equal import tiledb from .common import DiskTestCase, assert_captured, has_pandas class AttributeTest(DiskTestCase): def test_minimal_attribute(self): attr = tiledb.Attr() self.assertEqual(attr, attr) self.assertTrue(attr.isanon) self.assertEqual(attr.name, "") self.assertEqual(attr.dtype, np.float64) self.assertFalse(attr.isvar) self.assertFalse(attr.isnullable) try: assert xml.etree.ElementTree.fromstring(attr._repr_html_()) is not None except: pytest.fail(f"Could not parse attr._repr_html_(). Saw {attr._repr_html_()}") def test_attribute_name_only(self, capfd): attr = tiledb.Attr("foo") attr.dump() assert_captured(capfd, "Name: foo") assert attr == attr assert attr.name == "foo" assert attr.dtype == np.float64, "default attribute type is float64" @pytest.mark.parametrize( "dtype, fill", [ (np.dtype(bytes), b"abc"), (str, "defg"), (np.float32, np.float32(0.4023573667780681)), (np.float64, np.float64(0.0560602549760851)), (np.dtype("M8[ns]"), np.timedelta64(11, "ns")), (np.dtype([("f0", "@\x00\x00\x00\x00\x00\x00$@", # representation of POLYGON ((3 1, 4 5, 2 2, 1 2, 3 1)) ( b"\x01\x03\x00\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08@" b"\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x14@" b"\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\xf0?" b"\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\xf0?" ), ], ) dom = tiledb.Domain(tiledb.Dim(domain=(0, 1), tile=2)) att = tiledb.Attr(dtype="wkb", var=True) schema = tiledb.ArraySchema(dom, (att,)) tiledb.DenseArray.create(self.path("foo"), schema) with tiledb.DenseArray(self.path("foo"), mode="w") as T: T[:] = A # read back the data with tiledb.DenseArray(self.path("foo"), mode="r") as T: for i in range(2): assert_array_equal(T[:][i].tobytes(), A[i])