Coverage for tests/test_000_accretive/test_400_modules.py: 100%
55 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-20 02:16 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-20 02:16 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
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#============================================================================#
21''' Assert correct function of modules. '''
23# mypy: ignore-errors
24# pylint: disable=attribute-defined-outside-init
25# pylint: disable=invalid-name,magic-value-comparison,protected-access
28import pytest
30from itertools import product
32from . import (
33 MODULES_QNAMES,
34 PACKAGE_NAME,
35 cache_import_module,
36)
39THESE_MODULE_QNAMES = tuple(
40 name for name in MODULES_QNAMES if name.endswith( '.modules' ) )
41THESE_CLASSES_NAMES = ( 'Module', )
43base = cache_import_module( f"{PACKAGE_NAME}.__" )
44exceptions = cache_import_module( f"{PACKAGE_NAME}.exceptions" )
47@pytest.mark.parametrize(
48 'module_qname, class_name',
49 product( THESE_MODULE_QNAMES, THESE_CLASSES_NAMES )
50)
51def test_100_instantiation( module_qname, class_name ):
52 ''' Class instantiates. '''
53 module = cache_import_module( module_qname )
54 Module = getattr( module, class_name )
55 obj = Module( 'foo' )
56 assert isinstance( obj, Module )
57 assert 'foo' == obj.__name__
60@pytest.mark.parametrize(
61 'module_qname, class_name',
62 product( THESE_MODULE_QNAMES, THESE_CLASSES_NAMES )
63)
64def test_101_accretion( module_qname, class_name ):
65 ''' Module accretes attributes. '''
66 module = cache_import_module( module_qname )
67 Module = getattr( module, class_name )
68 obj = Module( 'foo' )
69 obj.attr = 42
70 with pytest.raises( exceptions.AttributeImmutabilityError ):
71 obj.attr = -1
72 assert 42 == obj.attr
73 with pytest.raises( exceptions.AttributeImmutabilityError ):
74 del obj.attr
75 assert 42 == obj.attr
78@pytest.mark.parametrize(
79 'module_qname, class_name',
80 product( THESE_MODULE_QNAMES, THESE_CLASSES_NAMES )
81)
82def test_500_module_reclassification( module_qname, class_name ):
83 ''' Modules are correctly reclassified. '''
84 module = cache_import_module( module_qname )
85 Module = getattr( module, class_name )
86 from types import ModuleType as Module_
87 m1 = Module_( 'm1' )
88 m2 = Module_( 'm2' )
89 m3 = Module( 'm3' )
90 attrs = { 'bar': 42, 'orb': True, 'm1': m1, 'm2': m2, 'm3': m3 }
91 assert not isinstance( m1, Module )
92 assert not isinstance( m2, Module )
93 assert isinstance( m3, Module )
94 module.reclassify_modules( attrs )
95 assert isinstance( m1, Module )
96 assert isinstance( m2, Module )
97 assert isinstance( m3, Module )
100@pytest.mark.parametrize(
101 'module_qname, class_name',
102 product( THESE_MODULE_QNAMES, THESE_CLASSES_NAMES )
103)
104def test_900_docstring_sanity( module_qname, class_name ):
105 ''' Class has valid docstring. '''
106 module = cache_import_module( module_qname )
107 Object = getattr( module, class_name )
108 assert hasattr( Object, '__doc__' )
109 assert isinstance( Object.__doc__, str )
110 assert Object.__doc__
113@pytest.mark.parametrize(
114 'module_qname, class_name',
115 product( THESE_MODULE_QNAMES, THESE_CLASSES_NAMES )
116)
117def test_902_docstring_mentions_accretion( module_qname, class_name ):
118 ''' Class docstring mentions accretion. '''
119 module = cache_import_module( module_qname )
120 Object = getattr( module, class_name )
121 fragment = base.generate_docstring( 'module attributes accretion' )
122 assert fragment in Object.__doc__