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

59 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-22 02:17 +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 

36 

37def test_101_singleton_boolean_evaluation( ): 

38 ''' Global sentinel evaluates to False. ''' 

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

40 assert not module.absent 

41 assert False is bool( module.absent ) # noqa: E712 

42 

43 

44def test_102_singleton_string_representations( ): 

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

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

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

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

49 

50 

51def test_200_factory_instantiation( ): 

52 ''' Factory produces unique instances. ''' 

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

54 obj1 = module.AbsenceFactory( ) 

55 obj2 = module.AbsenceFactory( ) 

56 assert obj1 is not obj2 

57 assert obj1 != obj2 

58 

59 

60def test_201_factory_boolean_evaluation( ): 

61 ''' Factory instances evaluate to False. ''' 

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

63 obj = module.AbsenceFactory( ) 

64 assert not obj 

65 assert False is bool( obj ) # noqa: E712 

66 

67 

68def test_202_factory_default_strings( ): 

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

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

71 obj = module.AbsenceFactory( ) 

72 assert 'absence' == str( obj ) 

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

74 

75 

76def test_203_factory_custom_strings( ): 

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

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

79 obj = module.AbsenceFactory( 

80 repr_function = lambda self: 'custom_repr', 

81 str_function = lambda self: 'custom_str', 

82 ) 

83 assert 'custom_str' == str( obj ) 

84 assert 'custom_repr' == repr( obj ) 

85 

86 

87def test_204_factory_pickle( ): 

88 ''' Factory instances cannot be pickled. ''' 

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

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

91 obj = module.AbsenceFactory( ) 

92 with pytest.raises( exceptions.OperationValidityError ): 

93 pickle.dumps( obj ) 

94 

95 

96def test_300_is_absent_predicate( ): 

97 ''' is_absent predicate identifies global sentinel. ''' 

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

99 assert module.is_absent( module.absent ) 

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

101 assert not module.is_absent( None ) 

102 assert not module.is_absent( False ) 

103 

104 

105def test_301_is_absence_predicate( ): 

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

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

108 assert module.is_absence( module.absent ) 

109 assert module.is_absence( module.AbsenceFactory( ) ) 

110 assert not module.is_absence( None ) 

111 assert not module.is_absence( False ) 

112 

113 

114def test_900_docstring_sanity( ): 

115 ''' Classes have valid docstrings. ''' 

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

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

118 assert hasattr( class_, '__doc__' ) 

119 assert isinstance( class_.__doc__, str ) 

120 assert class_.__doc__