Coverage for mpcforces_extractor\reader\test_modelreaders.py: 99%
70 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-06 21:34 +0100
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-06 21:34 +0100
1import unittest
2from unittest.mock import patch
3from mpcforces_extractor.reader.modelreaders import FemFileReader
4from mpcforces_extractor.datastructure.entities import Node, Element, Element1D
5from mpcforces_extractor.datastructure.rigids import MPC_CONFIG
6from mpcforces_extractor.datastructure.loads import Force, Moment
9class TestFemFileReader(unittest.TestCase):
10 # the __read_lines method is a private method, so we need to mock it and return propper file_content
11 @patch(
12 "mpcforces_extractor.reader.modelreaders.FemFileReader._FemFileReader__read_lines"
13 )
14 def test_init(self, mock_read_lines):
15 """
16 Test the init method. Make sure all variables are set correctly (correct type)
17 """
19 # Test the init method
20 mock_read_lines.return_value = []
21 fem_file_reader = FemFileReader("test.fem", 8)
22 self.assertEqual(fem_file_reader.file_path, "test.fem")
23 self.assertEqual(fem_file_reader.nodes_id2node, {})
24 self.assertEqual(fem_file_reader.rigid_elements, [])
25 self.assertEqual(fem_file_reader.node2property, {})
26 self.assertEqual(fem_file_reader.blocksize, 8)
28 @patch(
29 "mpcforces_extractor.reader.modelreaders.FemFileReader._FemFileReader__read_lines"
30 )
31 @patch(
32 "mpcforces_extractor.reader.modelreaders.FemFileReader._FemFileReader__read_nodes"
33 )
34 def test_split_line(self, mock_read_lines, mock_read_nodes):
35 """
36 Test the split_line method. Make sure the line is split correctly
37 """
38 mock_read_lines.return_value = []
39 mock_read_nodes.return_value = []
40 # Test the split_line method
41 fem_file_reader = FemFileReader("test.fem", 8)
42 line = "1234567890"
43 line_content = fem_file_reader.split_line(line)
44 self.assertEqual(line_content, ["12345678", "90"])
46 line = "123456789"
47 line_content = fem_file_reader.split_line(line)
48 self.assertEqual(line_content, ["12345678", "9"])
50 @patch(
51 "mpcforces_extractor.reader.modelreaders.FemFileReader._FemFileReader__read_lines"
52 )
53 def test_create_entities(self, mock_read_lines):
54 """
55 Test the create_entities method. Make sure the node2property is built correctly
56 """
58 # setup
59 Element1D.all_elements = []
61 mock_read_lines.return_value = [
62 "GRID 1 -16.889186.0 13.11648\n",
63 "GRID 2 -16.889186.0 13.11648\n",
64 "GRID 3 -16.889186.0 13.11648\n",
65 "GRID 4 -16.889186.0 13.11648\n",
66 "GRID 5 -16.889186.0 13.11648\n",
67 "GRID 6 -16.889186.0 13.11648\n",
68 "GRID 7 -16.889186.0 13.11648\n",
69 "\n",
70 "CHEXA 497 1 1 2 3\n",
71 "+ 4 5\n",
72 "CBAR 498 1 1 2\n",
73 "$$ test\n",
74 "RBE2 1 2 123456 3 4 5 6 7\n",
75 "RBE3 1 2 123456 3 4 5 6 7\n",
76 "\n",
77 ]
79 fem_file_reader = FemFileReader("test.fem", 8)
81 fem_file_reader.create_entities()
82 self.assertEqual(fem_file_reader.node2property, {1: 1, 2: 1, 3: 1, 4: 1, 5: 1})
83 self.assertTrue(Element.element_id2element[497] is not None)
84 self.assertTrue(Node.node_id2node[1] is not None)
85 self.assertTrue(Node.node_id2node[7] is not None)
86 for i in range(1, 8):
87 self.assertTrue(Node.node_id2node[i] is not None)
88 for i in range(1, 6):
89 self.assertTrue(
90 Node.node_id2node[i] in Element.element_id2element[497].nodes
91 )
92 print(len(Element1D.all_elements))
93 self.assertEqual(len(Element1D.all_elements), 1)
95 @patch(
96 "mpcforces_extractor.reader.modelreaders.FemFileReader._FemFileReader__read_lines"
97 )
98 def test_get_rigid_elements(self, mock_read_lines):
99 """
100 Test the get_rigid_elements method. Make sure the rigid elements are extracted correctly
101 """
102 mock_read_lines.return_value = [
103 "GRID 1 -16.889186.0 13.11648\n",
104 "GRID 2 -0.0 0.0 0.0 \n",
105 "GRID 3 -16.889186.0 13.11648\n",
106 "GRID 4 -16.889186.0 13.11648\n",
107 "GRID 5 -16.889186.0 13.11648\n",
108 "GRID 6 -16.889186.0 13.11648\n",
109 "GRID 7 -16.889186.0 13.11648\n",
110 "GRID 8 -16.889186.0 13.11648\n",
111 "$comment \n",
112 "CHEXA 497 1 1 2 3\n",
113 "+ 4 5\n",
114 "$$ breaking point\n",
115 "RBE2 1 2 123456 3 4 5 6 7\n",
116 "+ 8\n",
117 "RBE3 2 2 1234561.0 4 5 6\n",
118 "+ 8\n",
119 "\n",
120 ]
122 fem_file_reader = FemFileReader("test.fem", 8)
124 fem_file_reader.get_rigid_elements()
126 # dont care about the order of the dict
127 self.assertEqual(
128 fem_file_reader.rigid_elements[0].element_id, 1
129 ) # check the element_id
130 self.assertEqual(
131 fem_file_reader.rigid_elements[0].dofs, 123456
132 ) # check the dofs
133 self.assertEqual(fem_file_reader.rigid_elements[0].mpc_config, MPC_CONFIG.RBE2)
135 # RBE3
136 self.assertEqual(fem_file_reader.rigid_elements[1].element_id, 2)
137 self.assertEqual(fem_file_reader.rigid_elements[1].dofs, 123456)
138 self.assertEqual(fem_file_reader.rigid_elements[1].mpc_config, MPC_CONFIG.RBE3)
140 node_ids = [node.id for node in fem_file_reader.rigid_elements[0].nodes]
141 self.assertEqual(sorted(node_ids), [3, 4, 5, 6, 7, 8]) # check the nodes
143 self.assertEqual(
144 fem_file_reader.rigid_elements[0].master_node.id, 2
145 ) # check the master_node
146 self.assertEqual(
147 fem_file_reader.rigid_elements[0].master_node.coords, [0.0, 0.0, 0.0]
148 )
150 @patch(
151 "mpcforces_extractor.reader.modelreaders.FemFileReader._FemFileReader__read_lines"
152 )
153 def test_get_loads(self, mock_read_lines):
154 """
155 Test the get_loads method. Make sure the loads are extracted correctly
156 """
157 mock_read_lines.return_value = [
158 "GRID 1 -16.889186.0 13.116+2\n",
159 "GRID 2 -0.0 0.0 0.0 \n",
160 "GRID 3 -16.889186.0 13.116-8\n",
161 "GRID 4 -16.889186.0 13.11648\n",
162 "GRID 5 -16.889186.0 13.11648\n",
163 "GRID 6 -16.889186.0 13.11648\n",
164 "GRID 7 -16.889186.0 13.11648\n",
165 "GRID 8 -16.889186.0 13.11648\n",
166 "$comment \n",
167 "CHEXA 497 1 1 2 3\n",
168 "+ 4 5\n",
169 "$$ breaking point\n",
170 "FORCE 1 1 01.0 10000.00-1000.000.0 \n",
171 "MOMENT 2 2 01.0 10000.0010000.000.0 \n",
172 "$$ comment \n",
173 "\n",
174 ]
176 fem_file_reader = FemFileReader("test.fem", 8)
178 fem_file_reader.get_loads()
180 self.assertTrue(fem_file_reader.load_id2load[1] is not None)
181 self.assertTrue(fem_file_reader.load_id2load[2] is not None)
183 self.assertTrue(isinstance(fem_file_reader.load_id2load[1], Force))
184 self.assertTrue(isinstance(fem_file_reader.load_id2load[2], Moment))
187if __name__ == "__main__":
188 unittest.main()