示例#1
0
 def testInternalAttributeIsUsed(self):
     """Tests that setter/getter properly uses the internal value."""
     MockClass = threading_utils.local_attributes(['attr'])(_DummyClass)
     obj = MockClass()
     with test_utils.mock_thread('thread'):
         obj.attr = 'internal-value'
     obj.attr = 'dummy_value'
     with test_utils.mock_thread('thread'):
         self.assertEqual(obj.attr, 'internal-value')
示例#2
0
 def testDefaultAttributeIsNotCallable(self):
     """Tests the error raised when provided default attribute not callable."""
     MockClass = threading_utils.local_attributes(['attr'])(_DummyClass)
     obj = MockClass()
     threading_utils.initialize_local_attributes(obj, attr='default-value')
     with self.assertRaisesRegexp(
             AttributeError, 'Default value initializer must be callable.'):
         # Calling the attribute is expected to initialize it with the default
         # value. Hence the pointless statement to run the getter.
         _ = obj.attr
示例#3
0
 def testAttributeNotInitialized(self):
     """Tests that error is raised when local value has not been initialized."""
     MockClass = threading_utils.local_attributes(['attr'])(_DummyClass)
     obj = MockClass()
     with self.assertRaisesRegexp(
             AttributeError,
             'Local value for attribute `attr` has not been set.*'):
         # Calling the attribute is expected to initialize it with the default
         # value. Hence the pointless statement to run the getter.
         _ = obj.attr
示例#4
0
 def testCallableAttribute(self):
     """Tests that internal value is properly called with callable attribute."""
     MockClass = threading_utils.local_attributes(['attr'])(_DummyClass)
     obj = MockClass()
     with test_utils.mock_thread('thread'):
         obj.attr = test.mock.Mock()
     obj.attr = test.mock.Mock()
     with test_utils.mock_thread('thread'):
         obj.attr.method()
         obj.attr.method.assert_called_once()
     obj.attr.method.assert_not_called()
示例#5
0
 def testMultipleAttributes(self):
     """Tests the class decorator with multiple local attributes."""
     MockClass = threading_utils.local_attributes(['attr1',
                                                   'attr2'])(_DummyClass)
     obj = MockClass()
     with test_utils.mock_thread('thread'):
         obj.attr1 = 10
         obj.attr2 = 20
     obj.attr1 = obj.attr = 'dummy_value'
     obj.attr2 = obj.attr = 'dummy_value'
     with test_utils.mock_thread('thread'):
         self.assertEqual(obj.attr1, 10)
         self.assertEqual(obj.attr2, 20)
示例#6
0
 def testLocalValueOverDefault(self):
     """Tests that getter uses internal value over default one."""
     MockClass = threading_utils.local_attributes(['attr'])(_DummyClass)
     obj = MockClass()
     mock_default_init = test.mock.Mock()
     threading_utils.initialize_local_attributes(obj,
                                                 attr=mock_default_init)
     with test_utils.mock_thread('thread'):
         obj.attr = 'internal-value'
     obj.attr = 'dummy_value'
     with test_utils.mock_thread('thread'):
         self.assertEqual(obj.attr, 'internal-value')
     mock_default_init.assert_not_called()
示例#7
0
 def testInternalAttributeIsInitializedOnce(self):
     """Tests that getter properly initializes the local value on first call."""
     MockClass = threading_utils.local_attributes(['attr'])(_DummyClass)
     obj = MockClass()
     mock_value = test.mock.Mock()
     mock_callable_init = test.mock.Mock(return_value=mock_value)
     threading_utils.initialize_local_attributes(obj,
                                                 attr=mock_callable_init)
     # Calling the attribute is expected to initialize it with the default value.
     # Hence the pointless statement to run the getter.
     _ = obj.attr
     mock_callable_init.assert_called_once()
     mock_value.assert_not_called()
     obj.attr()
     mock_callable_init.assert_called_once()
     mock_value.assert_called_once()
示例#8
0
 def testMultiThreads(self):
     """Tests that different threads create different local attributes."""
     MockClass = threading_utils.local_attributes(['attr'])(_DummyClass)
     obj = MockClass()
     # Initializes attribute in thread 1.
     with test_utils.mock_thread('thread_1'):
         obj.attr = 1
     # Initializes attribute in thread 2.
     with test_utils.mock_thread('thread_2'):
         obj.attr = 2
     # Reads attribute in thread 1.
     with test_utils.mock_thread('thread_1'):
         self.assertEqual(obj.attr, 1)
     # Reads attribute in thread 2.
     with test_utils.mock_thread('thread_2'):
         self.assertEqual(obj.attr, 2)
示例#9
0
 def testMultiThreadsMultipleAttributes(self):
     """Tests that different threads create different local attributes."""
     MockClass = threading_utils.local_attributes(['attr1',
                                                   'attr2'])(_DummyClass)
     obj = MockClass()
     # Initializes attribute in thread 1.
     with test_utils.mock_thread('thread_1'):
         obj.attr1 = 1
         obj.attr2 = 2
     with test_utils.mock_thread('thread_2'):
         obj.attr1 = 3
         obj.attr2 = 4
     with test_utils.mock_thread('thread_1'):
         self.assertEqual(obj.attr1, 1)
         self.assertEqual(obj.attr2, 2)
     with test_utils.mock_thread('thread_2'):
         self.assertEqual(obj.attr1, 3)
         self.assertEqual(obj.attr2, 4)