Coverage for tests/test_000_absence/test_100_objects.py: 100%

60 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-21 04:54 +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 absence objects. ''' 

22 

23 

24import pickle 

25 

26import pytest 

27 

28from . import PACKAGE_NAME, cache_import_module 

29 

30 

31def test_100_singleton_identity( ): 

32 ''' Global sentinel maintains identity. ''' 

33 module = cache_import_module( f"{PACKAGE_NAME}.objects" ) 

34 assert module.absent is module.AbsentSingleton( ) 

35 assert module.absent is module.absent 

36 

37 

38def test_101_singleton_boolean_evaluation( ): 

39 ''' Global sentinel evaluates to False. ''' 

40 module = cache_import_module( f"{PACKAGE_NAME}.objects" ) 

41 assert not module.absent 

42 assert False == bool( module.absent ) # noqa: E712 

43 

44 

45def test_102_singleton_string_representations( ): 

46 ''' Global sentinel has expected string representations. ''' 

47 module = cache_import_module( f"{PACKAGE_NAME}.objects" ) 

48 assert 'absent' == str( module.absent ) 

49 assert 'absence.absent' == repr( module.absent ) 

50 

51 

52def test_200_factory_instantiation( ): 

53 ''' Factory produces unique instances. ''' 

54 module = cache_import_module( f"{PACKAGE_NAME}.objects" ) 

55 obj1 = module.AbsenceFactory( ) 

56 obj2 = module.AbsenceFactory( ) 

57 assert obj1 is not obj2 

58 assert obj1 != obj2 

59 

60 

61def test_201_factory_boolean_evaluation( ): 

62 ''' Factory instances evaluate to False. ''' 

63 module = cache_import_module( f"{PACKAGE_NAME}.objects" ) 

64 obj = module.AbsenceFactory( ) 

65 assert not obj 

66 assert False == bool( obj ) # noqa: E712 

67 

68 

69def test_202_factory_default_strings( ): 

70 ''' Factory instances have expected default string representations. ''' 

71 module = cache_import_module( f"{PACKAGE_NAME}.objects" ) 

72 obj = module.AbsenceFactory( ) 

73 assert 'absence' == str( obj ) 

74 assert 'absence.AbsenceFactory( )' == repr( obj ) 

75 

76 

77def test_203_factory_custom_strings( ): 

78 ''' Factory instances support custom string representations. ''' 

79 module = cache_import_module( f"{PACKAGE_NAME}.objects" ) 

80 obj = module.AbsenceFactory( 

81 repr_function = lambda self: 'custom_repr', 

82 str_function = lambda self: 'custom_str', 

83 ) 

84 assert 'custom_str' == str( obj ) 

85 assert 'custom_repr' == repr( obj ) 

86 

87 

88def test_204_factory_pickle( ): 

89 ''' Factory instances cannot be pickled. ''' 

90 module = cache_import_module( f"{PACKAGE_NAME}.objects" ) 

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

92 obj = module.AbsenceFactory( ) 

93 with pytest.raises( exceptions.OperationValidityError ): 

94 pickle.dumps( obj ) 

95 

96 

97def test_300_is_absent_predicate( ): 

98 ''' is_absent predicate identifies global sentinel. ''' 

99 module = cache_import_module( f"{PACKAGE_NAME}.objects" ) 

100 assert module.is_absent( module.absent ) 

101 assert not module.is_absent( module.AbsenceFactory( ) ) 

102 assert not module.is_absent( None ) 

103 assert not module.is_absent( False ) 

104 

105 

106def test_301_is_absence_predicate( ): 

107 ''' is_absence predicate identifies all absence types. ''' 

108 module = cache_import_module( f"{PACKAGE_NAME}.objects" ) 

109 assert module.is_absence( module.absent ) 

110 assert module.is_absence( module.AbsenceFactory( ) ) 

111 assert not module.is_absence( None ) 

112 assert not module.is_absence( False ) 

113 

114 

115def test_900_docstring_sanity( ): 

116 ''' Classes have valid docstrings. ''' 

117 module = cache_import_module( f"{PACKAGE_NAME}.objects" ) 

118 for class_ in ( module.AbsentSingleton, module.AbsenceFactory ): 

119 assert hasattr( class_, '__doc__' ) 

120 assert isinstance( class_.__doc__, str ) 

121 assert class_.__doc__