Coverage for tests/test_000_frigid/test_013_dictionaries.py: 100%

70 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-24 04:09 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2# -*- coding: utf-8 -*- 

3 

4#============================================================================# 

5# # 

6# Licensed under the Apache License, Version 2.0 (the "License"); # 

7# you may not use this file except in compliance with the License. # 

8# You may obtain a copy of the License at # 

9# # 

10# http://www.apache.org/licenses/LICENSE-2.0 # 

11# # 

12# Unless required by applicable law or agreed to in writing, software # 

13# distributed under the License is distributed on an "AS IS" BASIS, # 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

15# See the License for the specific language governing permissions and # 

16# limitations under the License. # 

17# # 

18#============================================================================# 

19 

20 

21''' Assert correct function of internal dictionaries. ''' 

22 

23# mypy: ignore-errors 

24# pylint: disable=attribute-defined-outside-init 

25# pylint: disable=magic-value-comparison 

26# pylint: disable=missing-class-docstring 

27# pylint: disable=protected-access 

28# pylint: disable=unexpected-keyword-arg 

29# ruff: noqa: E711,E712 

30 

31 

32import pytest 

33 

34from types import MappingProxyType as DictionaryProxy 

35 

36from . import PACKAGE_NAME, cache_import_module 

37 

38 

39MODULE_QNAME = f"{PACKAGE_NAME}.__" 

40 

41exceptions = cache_import_module( f"{PACKAGE_NAME}.exceptions" ) 

42module = cache_import_module( MODULE_QNAME ) 

43 

44dictionary_posargs = ( ( ( 'foo', 1 ), ( 'bar', 2 ) ), { 'unicorn': True } ) 

45dictionary_nomargs = DictionaryProxy( dict( orb = False ) ) 

46 

47 

48def test_200_immutable_dictionary_instantiation( ): 

49 ''' Dictionary instantiates with various input types. ''' 

50 factory = module.ImmutableDictionary 

51 dct1 = factory( ) 

52 assert isinstance( dct1, factory ) 

53 dct2 = factory( *dictionary_posargs, **dictionary_nomargs ) 

54 assert isinstance( dct2, factory ) 

55 assert 1 == dct2[ 'foo' ] 

56 assert 2 == dct2[ 'bar' ] 

57 assert dct2[ 'unicorn' ] 

58 assert not dct2[ 'orb' ] 

59 assert ( 'foo', 'bar', 'unicorn', 'orb' ) == tuple( dct2.keys( ) ) 

60 assert ( 1, 2, True, False ) == tuple( dct2.values( ) ) 

61 

62 

63def test_201_immutable_dictionary_duplication( ): 

64 ''' Dictionary is duplicable. ''' 

65 factory = module.ImmutableDictionary 

66 odct = factory( *dictionary_posargs, **dictionary_nomargs ) 

67 ddct = odct.copy( ) 

68 assert odct == ddct 

69 assert id( odct ) != id( ddct ) 

70 

71 

72def test_202_immutable_dictionary_prevents_key_overwrite( ): 

73 ''' Dictionary prevents overwriting existing keys during creation. ''' 

74 with pytest.raises( exceptions.EntryImmutabilityError ): 

75 module.ImmutableDictionary( [ ( 'a', 1 ) ], { 'a': 2 } ) 

76 

77 

78def test_210_immutable_dictionary_entry_protection( ): 

79 ''' Dictionary prevents entry modification and deletion. ''' 

80 factory = module.ImmutableDictionary 

81 dct = factory( *dictionary_posargs, **dictionary_nomargs ) 

82 with pytest.raises( exceptions.EntryImmutabilityError ): 

83 dct[ 'foo' ] = 42 

84 with pytest.raises( exceptions.EntryImmutabilityError ): 

85 del dct[ 'foo' ] 

86 with pytest.raises( exceptions.EntryImmutabilityError ): 

87 dct[ 'baz' ] = 3.1415926535 

88 

89 

90def test_211_immutable_dictionary_operation_prevention( ): 

91 ''' Dictionary prevents all mutating operations. ''' 

92 factory = module.ImmutableDictionary 

93 dct = factory( *dictionary_posargs, **dictionary_nomargs ) 

94 with pytest.raises( exceptions.OperationInvalidity ): 

95 dct.clear( ) 

96 with pytest.raises( exceptions.OperationInvalidity ): 

97 dct.pop( 'foo' ) 

98 with pytest.raises( exceptions.OperationInvalidity ): 

99 dct.pop( 'foo', default = -1 ) 

100 with pytest.raises( exceptions.OperationInvalidity ): 

101 dct.popitem( ) 

102 with pytest.raises( exceptions.OperationInvalidity ): 

103 dct.update( baz = 42 ) 

104 

105 

106def test_212_immutable_dictionary_initialization_validation( ): 

107 ''' Dictionary properly handles various input types during creation. ''' 

108 factory = module.ImmutableDictionary 

109 dct1 = factory( { 'a': 1, 'b': 2 } ) 

110 assert 1 == dct1[ 'a' ] 

111 assert 2 == dct1[ 'b' ] 

112 dct2 = factory( [ ( 'c', 3 ), ( 'd', 4 ) ] ) 

113 assert 3 == dct2[ 'c' ] 

114 assert 4 == dct2[ 'd' ] 

115 dct3 = factory( e = 5, f = 6 ) 

116 assert 5 == dct3[ 'e' ] 

117 assert 6 == dct3[ 'f' ] 

118 dct4 = factory( { 'g': 7 }, [ ( 'h', 8 ) ], i = 9 ) 

119 assert 7 == dct4[ 'g' ] 

120 assert 8 == dct4[ 'h' ] 

121 assert 9 == dct4[ 'i' ] 

122 

123 

124def test_213_immutable_dictionary_behaviors( ): 

125 ''' Dictionary has proper immutability behavior. ''' 

126 factory = module.ImmutableDictionary 

127 dct = factory( a = 1 ) 

128 assert module.behavior_label in dct._behaviors_