Coverage for test/unit/test_project.py: 96%

50 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-12 17:09 +0000

1import os 

2import shutil 

3import tempfile 

4import unittest 

5from pathlib import Path 

6 

7from bim2sim.plugins import Plugin 

8from bim2sim.project import Project 

9from bim2sim.sim_settings import PlantSimSettings 

10from bim2sim.utilities.types import IFCDomain 

11 

12sample_root = Path(__file__).parent.parent.parent / \ 

13 'test/resources/hydraulic/ifc' 

14ifc_paths = { 

15 IFCDomain.hydraulic: 

16 sample_root / 

17 'KM_DPM_Vereinshaus_Gruppe62_Heizung_with_pumps.ifc'} 

18 

19 

20class PluginDummy(Plugin): 

21 name = "Dummy" 

22 sim_settings = PlantSimSettings 

23 tasks = [] 

24 

25 

26class BaseTestProject(unittest.TestCase): 

27 

28 def setUp(self): 

29 self.directory = tempfile.TemporaryDirectory(prefix='bim2sim_') 

30 self.path = os.path.join(self.directory.name, 'proj') 

31 

32 def tearDown(self): 

33 try: 

34 self.directory.cleanup() 

35 except PermissionError: 

36 pass 

37 

38 

39class TestProject(BaseTestProject): 

40 

41 def test_create_remove(self): 

42 """Test creation and deletion of Project""" 

43 

44 project = Project.create( 

45 self.path, 

46 ifc_paths, 

47 PluginDummy 

48 ) 

49 

50 self.assertTrue(os.path.samefile(self.path, project.paths.root)) 

51 self.assertTrue(os.path.exists(project.paths.config)) 

52 self.assertTrue(os.path.exists(project.paths.ifc_base)) 

53 

54 project.delete() 

55 

56 self.assertFalse(os.path.exists(project.paths.root), 

57 f"Project folder {project.paths} not deleted!") 

58 

59 def test_double_create(self): 

60 """Test creating two projects in same dir""" 

61 project = Project.create( 

62 self.path, 

63 ifc_paths, 

64 PluginDummy 

65 ) 

66 self.assertTrue(os.path.exists(project.paths.ifc_base)) 

67 project.finalize(True) 

68 

69 domain = list(ifc_paths.keys())[0].name 

70 shutil.rmtree(project.paths.ifc_base / domain) 

71 self.assertFalse(os.path.exists(project.paths.ifc_base / domain)) 

72 

73 project2 = Project.create( 

74 self.path, 

75 ifc_paths, 

76 PluginDummy 

77 ) 

78 self.assertTrue(os.path.exists(project2.paths.ifc_base / domain)) 

79 project2.finalize(True) 

80 

81 def test_reuse_project_folder_change_plugin(self): 

82 """Tests to create project, reuse folder with other Plugin""" 

83 project = Project.create( 

84 self.path, 

85 ifc_paths, 

86 PluginDummy 

87 ) 

88 self.assertEqual(project.plugin_cls, PluginDummy) 

89 project.finalize(True) 

90 

91 project2 = Project.create( 

92 self.path, 

93 ifc_paths, 

94 'aixlib' 

95 ) 

96 from bim2sim.plugins.PluginAixLib.bim2sim_aixlib import PluginAixLib 

97 project2.finalize(True) 

98 self.assertEqual(project2.plugin_cls.__name__, PluginAixLib.__name__)