Tests: support running ID-property test without numpy
This commit is contained in:
parent
7b0b050dd5
commit
3d35d4a9e5
|
@ -4,9 +4,13 @@
|
||||||
import bpy
|
import bpy
|
||||||
import idprop
|
import idprop
|
||||||
import unittest
|
import unittest
|
||||||
import numpy as np
|
|
||||||
from array import array
|
from array import array
|
||||||
|
|
||||||
|
# Run if `numpy` is installed.
|
||||||
|
try:
|
||||||
|
import numpy as np
|
||||||
|
except ImportError:
|
||||||
|
np = None
|
||||||
|
|
||||||
class TestHelper:
|
class TestHelper:
|
||||||
|
|
||||||
|
@ -189,6 +193,34 @@ class TestIdPropertyGroupView(TestHelper, unittest.TestCase):
|
||||||
|
|
||||||
class TestBufferProtocol(TestHelper, unittest.TestCase):
|
class TestBufferProtocol(TestHelper, unittest.TestCase):
|
||||||
|
|
||||||
|
def test_copy(self):
|
||||||
|
self.id["a"] = array("i", [1, 2, 3, 4, 5])
|
||||||
|
self.id["b"] = self.id["a"]
|
||||||
|
self.assertEqual(self.id["a"].to_list(), self.id["b"].to_list())
|
||||||
|
|
||||||
|
def test_memview_attributes(self):
|
||||||
|
mylist = [1, 2, 3]
|
||||||
|
self.id["a"] = mylist
|
||||||
|
|
||||||
|
view1 = memoryview(self.id["a"])
|
||||||
|
view2 = memoryview(array("i", mylist))
|
||||||
|
|
||||||
|
self.assertEqualMemviews(view1, view2)
|
||||||
|
|
||||||
|
def assertEqualMemviews(self, view1, view2):
|
||||||
|
props_to_compare = (
|
||||||
|
"contiguous", "format", "itemsize", "nbytes", "ndim",
|
||||||
|
"readonly", "shape", "strides", "suboffsets"
|
||||||
|
)
|
||||||
|
for attr in props_to_compare:
|
||||||
|
self.assertEqual(getattr(view1, attr), getattr(view2, attr))
|
||||||
|
|
||||||
|
self.assertEqual(list(view1), list(view2))
|
||||||
|
self.assertEqual(view1.tobytes(), view2.tobytes())
|
||||||
|
|
||||||
|
|
||||||
|
if np is not None:
|
||||||
|
class TestBufferProtocol_Numpy(TestHelper, unittest.TestCase):
|
||||||
def test_int(self):
|
def test_int(self):
|
||||||
self.id["a"] = array("i", [1, 2, 3, 4, 5])
|
self.id["a"] = array("i", [1, 2, 3, 4, 5])
|
||||||
a = np.frombuffer(self.id["a"], self.id["a"].typecode)
|
a = np.frombuffer(self.id["a"], self.id["a"].typecode)
|
||||||
|
@ -221,30 +253,6 @@ class TestBufferProtocol(TestHelper, unittest.TestCase):
|
||||||
a[1:5] = [10, 20, 30, 40]
|
a[1:5] = [10, 20, 30, 40]
|
||||||
self.assertEqual(self.id["a"].to_list(), [1, 10, 20, 30, 40, 6, 7, 8])
|
self.assertEqual(self.id["a"].to_list(), [1, 10, 20, 30, 40, 6, 7, 8])
|
||||||
|
|
||||||
def test_copy(self):
|
|
||||||
self.id["a"] = array("i", [1, 2, 3, 4, 5])
|
|
||||||
self.id["b"] = self.id["a"]
|
|
||||||
self.assertEqual(self.id["a"].to_list(), self.id["b"].to_list())
|
|
||||||
|
|
||||||
def test_memview_attributes(self):
|
|
||||||
mylist = [1, 2, 3]
|
|
||||||
self.id["a"] = mylist
|
|
||||||
|
|
||||||
view1 = memoryview(self.id["a"])
|
|
||||||
view2 = memoryview(array("i", mylist))
|
|
||||||
|
|
||||||
self.assertEqualMemviews(view1, view2)
|
|
||||||
|
|
||||||
def assertEqualMemviews(self, view1, view2):
|
|
||||||
props_to_compare = (
|
|
||||||
"contiguous", "format", "itemsize", "nbytes", "ndim",
|
|
||||||
"readonly", "shape", "strides", "suboffsets"
|
|
||||||
)
|
|
||||||
for attr in props_to_compare:
|
|
||||||
self.assertEqual(getattr(view1, attr), getattr(view2, attr))
|
|
||||||
|
|
||||||
self.assertEqual(list(view1), list(view2))
|
|
||||||
self.assertEqual(view1.tobytes(), view2.tobytes())
|
|
||||||
|
|
||||||
class TestRNAData(TestHelper, unittest.TestCase):
|
class TestRNAData(TestHelper, unittest.TestCase):
|
||||||
|
|
||||||
|
@ -309,7 +317,6 @@ class TestRNAData(TestHelper, unittest.TestCase):
|
||||||
self.assertEqual(rna_data["default"], [1, 2])
|
self.assertEqual(rna_data["default"], [1, 2])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])
|
sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])
|
||||||
|
|
Loading…
Reference in New Issue