[Bf-blender-cvs] [56d3cc9341] master: PyAPI: ID Property tests

Jacques Lucke noreply at git.blender.org
Sat Mar 18 17:53:48 CET 2017


Commit: 56d3cc9341862761454e927af04c120bbeabfa2a
Author: Jacques Lucke
Date:   Sun Mar 19 03:37:22 2017 +1100
Branches: master
https://developer.blender.org/rB56d3cc9341862761454e927af04c120bbeabfa2a

PyAPI: ID Property tests

===================================================================

M	tests/python/CMakeLists.txt
A	tests/python/bl_pyapi_idprop.py

===================================================================

diff --git a/tests/python/CMakeLists.txt b/tests/python/CMakeLists.txt
index b76c47fcf2..f7ca9b0213 100644
--- a/tests/python/CMakeLists.txt
+++ b/tests/python/CMakeLists.txt
@@ -83,11 +83,14 @@ add_test(script_pyapi_bpy_utils_units ${TEST_BLENDER_EXE}
 	--python ${CMAKE_CURRENT_LIST_DIR}/bl_pyapi_bpy_utils_units.py
 )
 
-# test running mathutils testing script
 add_test(script_pyapi_mathutils ${TEST_BLENDER_EXE}
 	--python ${CMAKE_CURRENT_LIST_DIR}/bl_pyapi_mathutils.py
 )
 
+add_test(script_pyapi_idprop ${TEST_BLENDER_EXE}
+	--python ${CMAKE_CURRENT_LIST_DIR}/bl_pyapi_idprop.py
+)
+
 # ------------------------------------------------------------------------------
 # MODELING TESTS
 add_test(bevel ${TEST_BLENDER_EXE}
diff --git a/tests/python/bl_pyapi_idprop.py b/tests/python/bl_pyapi_idprop.py
new file mode 100644
index 0000000000..0a9cb04457
--- /dev/null
+++ b/tests/python/bl_pyapi_idprop.py
@@ -0,0 +1,144 @@
+# Apache License, Version 2.0
+
+# ./blender.bin --background -noaudio --python tests/python/bl_pyapi_idprop.py -- --verbose
+import bpy
+import unittest
+from array import array
+
+
+class TestHelper:
+
+    @property
+    def id(self):
+        return self._id
+
+    def setUp(self):
+        self._id = bpy.context.scene
+        assert(len(self._id.keys()) == 0)
+
+    def tearDown(self):
+        for key in list(self._id.keys()):
+            del self._id[key]
+
+    def assertAlmostEqualSeq(self, list1, list2):
+        self.assertEqual(len(list1), len(list2))
+        for v1, v2 in zip(list1, list2):
+            self.assertAlmostEqual(v1, v2, places=5)
+
+
+class TestIdPropertyCreation(TestHelper, unittest.TestCase):
+
+    def test_name_empty(self):
+        self.id[""] = 4
+        self.assertEqual(self.id[""], 4)
+
+    def test_name_too_long(self):
+        with self.assertRaises(KeyError):
+            self.id["name" * 30] = 4
+
+    def test_int(self):
+        self.id["a"] = 2
+        self.assertEqual(self.id["a"], 2)
+        self.assertTrue(isinstance(self.id["a"], int))
+
+        with self.assertRaises(OverflowError):
+            self.id["a"] = 2 ** 31  # integer <= 2 ** 31-1
+
+    def test_double(self):
+        self.id["a"] = 2.5
+        self.assertEqual(self.id["a"], 2.5)
+        self.assertTrue(isinstance(self.id["a"], float))
+
+    def test_unicode(self):
+        self.id["a"] = "Hello World"
+        self.assertEqual(self.id["a"], "Hello World")
+        self.assertTrue(isinstance(self.id["a"], str))
+
+    def test_bytes(self):
+        self.id["a"] = b"Hello World"
+        self.assertEqual(self.id["a"], b"Hello World")
+        self.assertTrue(isinstance(self.id["a"], bytes))
+
+    def test_sequence_double_list(self):
+        mylist = [1.2, 3.4, 5.6]
+        self.id["a"] = mylist
+        self.assertEqual(self.id["a"].to_list(), mylist)
+        self.assertEqual(self.id["a"].typecode, "d")
+
+    def test_sequence_int_list(self):
+        mylist = [1, 2, 3]
+        self.id["a"] = mylist
+        self.assertEqual(self.id["a"].to_list(), mylist)
+        self.assertEqual(self.id["a"].typecode, "i")
+
+    def test_sequence_float_array(self):
+        mylist = [1.2, 3.4, 5.6]
+        self.id["a"] = array("f", mylist)
+        self.assertAlmostEqualSeq(self.id["a"].to_list(), mylist)
+        self.assertEqual(self.id["a"].typecode, "d")
+
+    def test_sequence_double_array(self):
+        mylist = [1.2, 3.4, 5.6]
+        self.id["a"] = array("d", mylist)
+        self.assertAlmostEqualSeq(self.id["a"].to_list(), mylist)
+        self.assertEqual(self.id["a"].typecode, "d")
+
+    def test_sequence_int_array(self):
+        mylist = [1, 2, 3]
+        self.id["a"] = array("i", mylist)
+        self.assertAlmostEqualSeq(self.id["a"].to_list(), mylist)
+        self.assertEqual(self.id["a"].typecode, "i")
+
+    def test_sequence_other_array(self):
+        mylist = [1, 2, 3]
+        self.id["a"] = array("Q", mylist)
+        self.assertEqual(self.id["a"].to_list(), mylist)
+
+    def test_sequence_mixed_numerical_type(self):
+        self.id["a"] = [1, 2, 3.4, 5]
+        self.assertAlmostEqualSeq(self.id["a"].to_list(), [1.0, 2.0, 3.4, 5.0])
+        self.assertEqual(self.id["a"].typecode, "d")
+
+    def test_sequence_str_list(self):
+        # I'm a bit surprised that this works
+        mylist = ["abc", "qwe"]
+        self.id["a"] = mylist
+        self.assertEqual(self.id["a"], mylist)
+
+    def test_sequence_mixed_type(self):
+        with self.assertRaises(TypeError):
+            mylist = ["abc", 3, "qwe", 3.4]
+            self.id["a"] = mylist
+
+    def test_mapping_simple(self):
+        mydict = {"1": 10, "2": "20", "3": 30.5}
+        self.id["a"] = mydict
+        self.assertEqual(self.id["a"]["1"], mydict["1"])
+        self.assertEqual(self.id["a"]["2"], mydict["2"])
+        self.assertEqual(self.id["a"]["3"], mydict["3"])
+
+    def test_mapping_complex(self):
+        mydict = {
+            "1": [1, 2, 3],
+            "2": {"1": "abc", "2": array("i", [4, 5, 6])},
+            "3": {"1": {"1": 10}, "2": b"qwe"},
+        }
+        self.id["a"] = mydict
+        self.assertEqual(self.id["a"]["1"].to_list(), [1, 2, 3])
+        self.assertEqual(self.id["a"]["2"]["1"], "abc")
+        self.assertEqual(self.id["a"]["2"]["2"].to_list(), [4, 5, 6])
+        self.assertEqual(self.id["a"]["3"]["1"]["1"], 10)
+        self.assertEqual(self.id["a"]["3"]["2"], b"qwe")
+
+        with self.assertRaises(KeyError):
+            a = self.id["a"]["2"]["a"]
+
+    def test_invalid_type(self):
+        with self.assertRaises(TypeError):
+            self.id["a"] = self
+
+
+if __name__ == '__main__':
+    import sys
+    sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])
+    unittest.main()




More information about the Bf-blender-cvs mailing list