PyAPI: ID Property tests
[blender.git] / tests / python / bl_pyapi_idprop.py
1 # Apache License, Version 2.0
2
3 # ./blender.bin --background -noaudio --python tests/python/bl_pyapi_idprop.py -- --verbose
4 import bpy
5 import unittest
6 from array import array
7
8
9 class TestHelper:
10
11     @property
12     def id(self):
13         return self._id
14
15     def setUp(self):
16         self._id = bpy.context.scene
17         assert(len(self._id.keys()) == 0)
18
19     def tearDown(self):
20         for key in list(self._id.keys()):
21             del self._id[key]
22
23     def assertAlmostEqualSeq(self, list1, list2):
24         self.assertEqual(len(list1), len(list2))
25         for v1, v2 in zip(list1, list2):
26             self.assertAlmostEqual(v1, v2, places=5)
27
28
29 class TestIdPropertyCreation(TestHelper, unittest.TestCase):
30
31     def test_name_empty(self):
32         self.id[""] = 4
33         self.assertEqual(self.id[""], 4)
34
35     def test_name_too_long(self):
36         with self.assertRaises(KeyError):
37             self.id["name" * 30] = 4
38
39     def test_int(self):
40         self.id["a"] = 2
41         self.assertEqual(self.id["a"], 2)
42         self.assertTrue(isinstance(self.id["a"], int))
43
44         with self.assertRaises(OverflowError):
45             self.id["a"] = 2 ** 31  # integer <= 2 ** 31-1
46
47     def test_double(self):
48         self.id["a"] = 2.5
49         self.assertEqual(self.id["a"], 2.5)
50         self.assertTrue(isinstance(self.id["a"], float))
51
52     def test_unicode(self):
53         self.id["a"] = "Hello World"
54         self.assertEqual(self.id["a"], "Hello World")
55         self.assertTrue(isinstance(self.id["a"], str))
56
57     def test_bytes(self):
58         self.id["a"] = b"Hello World"
59         self.assertEqual(self.id["a"], b"Hello World")
60         self.assertTrue(isinstance(self.id["a"], bytes))
61
62     def test_sequence_double_list(self):
63         mylist = [1.2, 3.4, 5.6]
64         self.id["a"] = mylist
65         self.assertEqual(self.id["a"].to_list(), mylist)
66         self.assertEqual(self.id["a"].typecode, "d")
67
68     def test_sequence_int_list(self):
69         mylist = [1, 2, 3]
70         self.id["a"] = mylist
71         self.assertEqual(self.id["a"].to_list(), mylist)
72         self.assertEqual(self.id["a"].typecode, "i")
73
74     def test_sequence_float_array(self):
75         mylist = [1.2, 3.4, 5.6]
76         self.id["a"] = array("f", mylist)
77         self.assertAlmostEqualSeq(self.id["a"].to_list(), mylist)
78         self.assertEqual(self.id["a"].typecode, "d")
79
80     def test_sequence_double_array(self):
81         mylist = [1.2, 3.4, 5.6]
82         self.id["a"] = array("d", mylist)
83         self.assertAlmostEqualSeq(self.id["a"].to_list(), mylist)
84         self.assertEqual(self.id["a"].typecode, "d")
85
86     def test_sequence_int_array(self):
87         mylist = [1, 2, 3]
88         self.id["a"] = array("i", mylist)
89         self.assertAlmostEqualSeq(self.id["a"].to_list(), mylist)
90         self.assertEqual(self.id["a"].typecode, "i")
91
92     def test_sequence_other_array(self):
93         mylist = [1, 2, 3]
94         self.id["a"] = array("Q", mylist)
95         self.assertEqual(self.id["a"].to_list(), mylist)
96
97     def test_sequence_mixed_numerical_type(self):
98         self.id["a"] = [1, 2, 3.4, 5]
99         self.assertAlmostEqualSeq(self.id["a"].to_list(), [1.0, 2.0, 3.4, 5.0])
100         self.assertEqual(self.id["a"].typecode, "d")
101
102     def test_sequence_str_list(self):
103         # I'm a bit surprised that this works
104         mylist = ["abc", "qwe"]
105         self.id["a"] = mylist
106         self.assertEqual(self.id["a"], mylist)
107
108     def test_sequence_mixed_type(self):
109         with self.assertRaises(TypeError):
110             mylist = ["abc", 3, "qwe", 3.4]
111             self.id["a"] = mylist
112
113     def test_mapping_simple(self):
114         mydict = {"1": 10, "2": "20", "3": 30.5}
115         self.id["a"] = mydict
116         self.assertEqual(self.id["a"]["1"], mydict["1"])
117         self.assertEqual(self.id["a"]["2"], mydict["2"])
118         self.assertEqual(self.id["a"]["3"], mydict["3"])
119
120     def test_mapping_complex(self):
121         mydict = {
122             "1": [1, 2, 3],
123             "2": {"1": "abc", "2": array("i", [4, 5, 6])},
124             "3": {"1": {"1": 10}, "2": b"qwe"},
125         }
126         self.id["a"] = mydict
127         self.assertEqual(self.id["a"]["1"].to_list(), [1, 2, 3])
128         self.assertEqual(self.id["a"]["2"]["1"], "abc")
129         self.assertEqual(self.id["a"]["2"]["2"].to_list(), [4, 5, 6])
130         self.assertEqual(self.id["a"]["3"]["1"]["1"], 10)
131         self.assertEqual(self.id["a"]["3"]["2"], b"qwe")
132
133         with self.assertRaises(KeyError):
134             a = self.id["a"]["2"]["a"]
135
136     def test_invalid_type(self):
137         with self.assertRaises(TypeError):
138             self.id["a"] = self
139
140
141 if __name__ == '__main__':
142     import sys
143     sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])
144     unittest.main()