1# Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Tests for tf.framework.immutable_dict.""" 16 17from absl.testing import parameterized 18 19from tensorflow.python.framework import immutable_dict 20from tensorflow.python.framework import test_util 21from tensorflow.python.platform import googletest 22 23 24class ImmutableDictTest(test_util.TensorFlowTestCase, parameterized.TestCase): 25 26 def testGetItem(self): 27 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 28 self.assertEqual(d['x'], 1) 29 self.assertEqual(d['y'], 2) 30 with self.assertRaises(KeyError): 31 d['z'] # pylint: disable=pointless-statement 32 33 def testIter(self): 34 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 35 self.assertEqual(set(iter(d)), set(['x', 'y'])) 36 37 def testContains(self): 38 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 39 self.assertIn('x', d) 40 self.assertIn('y', d) 41 self.assertNotIn('z', d) 42 43 def testLen(self): 44 d1 = immutable_dict.ImmutableDict({}) 45 self.assertLen(d1, 0) # pylint: disable=g-generic-assert 46 d2 = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 47 self.assertLen(d2, 2) 48 49 def testRepr(self): 50 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 51 s = repr(d) 52 self.assertTrue(s == "ImmutableDict({'x': 1, 'y': 2})" or 53 s == "ImmutableDict({'y': 1, 'x': 2})") 54 55 def testGet(self): 56 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 57 self.assertEqual(d.get('x'), 1) 58 self.assertEqual(d.get('y'), 2) 59 self.assertIsNone(d.get('z')) 60 self.assertEqual(d.get('z', 'Foo'), 'Foo') 61 62 def testKeys(self): 63 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 64 self.assertEqual(set(d.keys()), set(['x', 'y'])) 65 66 def testValues(self): 67 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 68 self.assertEqual(set(d.values()), set([1, 2])) 69 70 def testItems(self): 71 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 72 self.assertEqual(set(d.items()), set([('x', 1), ('y', 2)])) 73 74 def testEqual(self): 75 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 76 self.assertEqual(d, {'x': 1, 'y': 2}) 77 78 def testNotEqual(self): 79 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 80 self.assertNotEqual(d, {'x': 1}) 81 82 def testSetItemFails(self): 83 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 84 with self.assertRaises(TypeError): 85 d['x'] = 5 # pylint: disable=unsupported-assignment-operation 86 with self.assertRaises(TypeError): 87 d['z'] = 5 # pylint: disable=unsupported-assignment-operation 88 89 def testDelItemFails(self): 90 d = immutable_dict.ImmutableDict({'x': 1, 'y': 2}) 91 with self.assertRaises(TypeError): 92 del d['x'] # pylint: disable=unsupported-delete-operation 93 with self.assertRaises(TypeError): 94 del d['z'] # pylint: disable=unsupported-delete-operation 95 96 97if __name__ == '__main__': 98 googletest.main() 99