paleolimbot commented on code in PR #378:
URL: https://github.com/apache/arrow-nanoarrow/pull/378#discussion_r1491506115


##########
python/tests/test_c_array.py:
##########
@@ -0,0 +1,298 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import pytest
+from nanoarrow._lib import NanoarrowException
+from nanoarrow.c_lib import CArrayBuilder
+
+import nanoarrow as na
+
+
+def test_c_array_builder_init():
+    builder = CArrayBuilder.allocate()
+    builder.init_from_type(na.Type.INT32.value)
+
+    with pytest.raises(RuntimeError, match="CArrayBuilder is already 
initialized"):
+        builder.init_from_type(na.Type.INT32.value)
+
+    with pytest.raises(RuntimeError, match="CArrayBuilder is already 
initialized"):
+        builder.init_from_schema(na.c_schema(na.int32()))
+
+
+def test_c_array_from_c_array():
+    c_array = na.c_array([1, 2, 3], na.int32())
+    c_array_from_c_array = na.c_array(c_array)
+    assert c_array_from_c_array.length == c_array.length
+    assert c_array_from_c_array.buffers == c_array.buffers
+
+
+def test_c_array_from_capsule_protocol():
+    class CArrayWrapper:
+        def __init__(self, obj):
+            self.obj = obj
+
+        def __arrow_c_array__(self, *args, **kwargs):
+            return self.obj.__arrow_c_array__(*args, **kwargs)
+
+    c_array = na.c_array([1, 2, 3], na.int32())
+    c_array_wrapper = CArrayWrapper(c_array)
+    c_array_from_protocol = na.c_array(c_array_wrapper)
+    assert c_array_from_protocol.length == c_array.length
+    assert c_array_from_protocol.buffers == c_array.buffers
+
+
+def test_c_array_from_old_pyarrow():
+    # Simulate a pyarrow Array with no __arrow_c_array__
+    class MockLegacyPyarrowArray:
+        def __init__(self, obj):
+            self.obj = obj
+
+        def _export_to_c(self, *args):
+            return self.obj._export_to_c(*args)
+
+    MockLegacyPyarrowArray.__module__ = "pyarrow.lib"
+
+    pa = pytest.importorskip("pyarrow")
+    array = MockLegacyPyarrowArray(pa.array([1, 2, 3], pa.int32()))
+
+    c_array = na.c_array(array)
+    assert c_array.length == 3
+    assert c_array.schema.format == "i"
+
+    # Make sure that this heuristic won't result in trying to import
+    # something else that has an _export_to_c method
+    with pytest.raises(TypeError, match="Can't convert object of type 
DataType"):
+        not_array = pa.int32()
+        assert hasattr(not_array, "_export_to_c")
+        na.c_array(not_array)
+
+
+def test_c_array_from_bare_capsule():
+    c_array = na.c_array([1, 2, 3], na.int32())
+
+    # Check from bare capsule without supplying a schema
+    schema_capsule, array_capsule = c_array.__arrow_c_array__()
+    del schema_capsule
+    c_array_from_capsule = na.c_array(array_capsule)
+    assert c_array_from_capsule.length == c_array.length
+    assert c_array_from_capsule.buffers == c_array.buffers
+
+    # Check from bare capsule supplying a schema
+    schema_capsule, array_capsule = c_array.__arrow_c_array__()
+    c_array_from_capsule = na.c_array(array_capsule, schema_capsule)
+    assert c_array_from_capsule.length == c_array.length
+    assert c_array_from_capsule.buffers == c_array.buffers
+
+
+def test_c_array_type_not_supported():
+    with pytest.raises(TypeError, match="Can't convert object of type 
NoneType"):
+        na.c_array(None)
+
+
+def test_c_array_from_pybuffer_uint8():
+    data = b"abcdefg"
+    c_array = na.c_array(data)
+    assert c_array.length == len(data)
+    assert c_array.null_count == 0
+    assert c_array.offset == 0
+    assert na.c_schema_view(c_array.schema).type == "uint8"
+
+    c_array_view = na.c_array_view(c_array)
+    assert list(c_array_view.buffer(1)) == list(data)
+
+
+def test_c_array_from_pybuffer_string():
+    data = b"abcdefg"
+    buffer = na.c_buffer(data).set_format("c")

Review Comment:
   This is pretty much just for testing, so I made it internal. I don't think 
it should be easy to blindly reinterpret cast a buffer but there are a few code 
paths that are hard to test if I can't do it in a test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to