Coverage for tests / tests_config / tests_description / test_config_nets.py: 100%

68 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-09 16:40 +0100

1# SPDX-FileCopyrightText: Copyright INRIA 

2# 

3# SPDX-License-Identifier: LGPL-3.0-only 

4# 

5# Copyright INRIA 

6# 

7# This file is part of PhysioBlocks, a library mostly developed by the 

8# [Ananke project-team](https://team.inria.fr/ananke) at INRIA. 

9# 

10# Authors: 

11# - Colin Drieu 

12# - Dominique Chapelle 

13# - François Kimmig 

14# - Philippe Moireau 

15# 

16# PhysioBlocks is free software: you can redistribute it and/or modify it under the 

17# terms of the GNU Lesser General Public License as published by the Free Software 

18# Foundation, version 3 of the License. 

19# 

20# PhysioBlocks is distributed in the hope that it will be useful, but WITHOUT ANY 

21# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 

22# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 

23# 

24# You should have received a copy of the GNU Lesser General Public License along with 

25# PhysioBlocks. If not, see <https://www.gnu.org/licenses/>. 

26 

27from unittest.mock import PropertyMock, patch 

28 

29import pytest 

30 

31import physioblocks.registers.type_register as type_reg 

32from physioblocks.computing.models import ( 

33 Block, 

34 BlockMetaClass, 

35 Expression, 

36 ExpressionDefinition, 

37 TermDefinition, 

38) 

39from physioblocks.configuration import Configuration 

40from physioblocks.configuration.constants import ( 

41 BLOCK_FLUX_TYPE_ITEM_ID, 

42 BLOCKS_ITEM_ID, 

43 BOUNDARIES_ID, 

44 CONDITION_NAME_ID, 

45 CONDITION_TYPE_ID, 

46 FLUX_DOF_DEFINITION_ID, 

47 MODEL_COMPONENT_TYPE_ITEM_ID, 

48 NET_ID, 

49 NODES_ITEM_ID, 

50) 

51from physioblocks.configuration.description.nets import load_net_config, save_net_config 

52from physioblocks.description.blocks import BLOCK_DESCRIPTION_TYPE_ID, BlockDescription 

53from physioblocks.description.nets import BOUNDARY_CONDITION_ID, BoundaryCondition, Net 

54from tests.helpers.assertion_helpers import assert_net_equals 

55 

56BLOCK_DESC_ID = "Block" 

57FLUX_TYPE = "flux_type" 

58DOF_TYPE = "dof_type" 

59DOF_ID = "dof" 

60FLUX_ID = "flux" 

61 

62NODE_0_ID = "n0" 

63NODE_1_ID = "n1" 

64BLOCK_0_ID = "b0" 

65 

66 

67@pytest.fixture 

68def ref_expression(): 

69 return Expression(1, None) 

70 

71 

72@pytest.fixture 

73def ref_nodes(): 

74 return {0: [FLUX_TYPE], 1: [FLUX_TYPE]} 

75 

76 

77@pytest.fixture 

78def flux_definition(ref_expression): 

79 return ExpressionDefinition(ref_expression, [TermDefinition(DOF_ID, 1)]) 

80 

81 

82@pytest.fixture 

83def ref_flux_expressions(flux_definition): 

84 return {0: flux_definition, 1: flux_definition} 

85 

86 

87@pytest.fixture 

88def ref_config() -> Configuration: 

89 config = Configuration(NET_ID) 

90 

91 config[FLUX_DOF_DEFINITION_ID] = {FLUX_TYPE: DOF_TYPE} 

92 config[NODES_ITEM_ID] = [NODE_0_ID, NODE_1_ID] 

93 blocks = {} 

94 block_item = Configuration(BLOCK_DESCRIPTION_TYPE_ID) 

95 block_item[MODEL_COMPONENT_TYPE_ITEM_ID] = BLOCK_DESC_ID 

96 block_item[BLOCK_FLUX_TYPE_ITEM_ID] = FLUX_TYPE 

97 block_item[NODES_ITEM_ID] = {} 

98 block_item[NODES_ITEM_ID]["0"] = NODE_0_ID 

99 block_item[NODES_ITEM_ID]["1"] = NODE_1_ID 

100 blocks[BLOCK_0_ID] = block_item 

101 config[BLOCKS_ITEM_ID] = blocks 

102 config[BOUNDARIES_ID] = { 

103 NODE_0_ID: [ 

104 Configuration( 

105 BOUNDARY_CONDITION_ID, 

106 {CONDITION_TYPE_ID: FLUX_TYPE, CONDITION_NAME_ID: FLUX_ID}, 

107 ) 

108 ], 

109 NODE_1_ID: [ 

110 Configuration( 

111 BOUNDARY_CONDITION_ID, 

112 {CONDITION_TYPE_ID: DOF_TYPE, CONDITION_NAME_ID: DOF_ID}, 

113 ) 

114 ], 

115 } 

116 

117 return config 

118 

119 

120@pytest.fixture 

121@patch.multiple( 

122 "physioblocks.description.nets._flux_type_register", 

123 create=True, 

124 _fluxes_types={FLUX_TYPE: DOF_TYPE}, 

125 _dof_types={DOF_TYPE: FLUX_TYPE}, 

126) 

127def ref_net(ref_flux_expressions, ref_nodes) -> Net: 

128 with patch.multiple( 

129 BlockMetaClass, 

130 fluxes_expressions=PropertyMock(return_value=ref_flux_expressions), 

131 nodes=PropertyMock(return_value=ref_nodes), 

132 ): 

133 net = Net() 

134 net.add_node(NODE_0_ID) 

135 net.add_node(NODE_1_ID) 

136 net.add_block( 

137 BLOCK_0_ID, 

138 BlockDescription(BLOCK_0_ID, Block, FLUX_TYPE), 

139 {0: NODE_0_ID, 1: NODE_1_ID}, 

140 ) 

141 

142 net.set_boundary(NODE_0_ID, FLUX_TYPE, FLUX_ID) 

143 net.set_boundary(NODE_1_ID, DOF_TYPE, DOF_ID) 

144 return net 

145 

146 

147@patch.multiple( 

148 "physioblocks.description.nets._flux_type_register", 

149 create=True, 

150 _fluxes_types={FLUX_TYPE: DOF_TYPE}, 

151 _dof_types={DOF_TYPE: FLUX_TYPE}, 

152) 

153@patch.object( 

154 type_reg, 

155 attribute="__type_register", 

156 new={ 

157 BLOCK_DESCRIPTION_TYPE_ID: BlockDescription, 

158 BlockDescription: BLOCK_DESCRIPTION_TYPE_ID, 

159 BLOCK_DESC_ID: Block, 

160 Block: BLOCK_DESC_ID, 

161 BOUNDARY_CONDITION_ID: BoundaryCondition, 

162 BoundaryCondition: BOUNDARY_CONDITION_ID, 

163 }, 

164) 

165class TestNetConfiguration: 

166 def test_get_net_config( 

167 self, ref_net: Net, ref_config: Configuration, ref_flux_expressions, ref_nodes 

168 ): 

169 with patch.multiple( 

170 BlockMetaClass, 

171 fluxes_expressions=PropertyMock(return_value=ref_flux_expressions), 

172 nodes=PropertyMock(return_value=ref_nodes), 

173 ): 

174 net_configuration = save_net_config(ref_net) 

175 assert ref_config == net_configuration 

176 

177 def test_create_net_from_config( 

178 self, ref_net: Net, ref_config: Configuration, ref_nodes, ref_flux_expressions 

179 ): 

180 with patch.multiple( 

181 BlockMetaClass, 

182 fluxes_expressions=PropertyMock(return_value=ref_flux_expressions), 

183 nodes=PropertyMock(return_value=ref_nodes), 

184 ): 

185 net = load_net_config(ref_config, Net) 

186 assert_net_equals(net, ref_net)