Coverage for bim2sim/plugins/PluginEnergyPlus/test/unit/task/test_hash_function.py: 0%

39 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-17 11:04 +0000

1import unittest 

2import tempfile 

3import shutil 

4import logging 

5from pathlib import Path 

6from bim2sim.plugins.PluginEnergyPlus.bim2sim_energyplus.utils.utils_hash_function import ( 

7 generate_hash, 

8 add_hash_into_idf, 

9) 

10 

11 

12logger = logging.getLogger(__name__) 

13# Set up logger to print to console 

14if not logger.handlers: 

15 _handler = logging.StreamHandler() 

16 logger.addHandler(_handler) 

17logger.setLevel(logging.INFO) 

18logger.propagate = False 

19 

20 

21test_rsrc_path = (Path( 

22 __file__).parent.parent.parent.parent.parent.parent.parent 

23 / 'test/resources') 

24 

25 

26class TestHashFunction(unittest.TestCase): 

27 

28 def setUp(self): 

29 self.test_dir = tempfile.TemporaryDirectory() 

30 

31 def tearDown(self): 

32 self.test_dir.cleanup() 

33 

34 def test_generate_hash_and_write_to_idf(self): 

35 """Unit test: generate IFC hash and prepend to IDF.""" 

36 

37 # Locate IFC test file 

38 ifc_path = test_rsrc_path / 'arch/ifc/AC20-FZK-Haus.ifc' 

39 

40 # Use predefined expected correct hash for AC20-FZK-Haus.ifc in the resources folder 

41 expected_hash = "b0b6cbe8e780f537417a7d67bc98966d2a9cd8644472e77fd3f9f1e5bb1fa97b" 

42 

43 # Use utils_hash_function to generate hash line and compare with expected hash 

44 hash_line = generate_hash(str(ifc_path)) 

45 logger.info("Generated hash line: %s", hash_line.strip()) 

46 self.assertTrue(hash_line.startswith('! IFC_GEOMETRY_HASH:'), "Hash line prefix incorrect") 

47 self.assertIn(expected_hash, hash_line, "Hash content mismatch") 

48 self.assertIn(ifc_path.name, hash_line, "IFC filename not included") 

49 

50 # Prepare a temp copy of Minimal.idf 

51 plugin_data_dir = Path(__file__).parents[3] / 'data' 

52 minimal_idf = plugin_data_dir / 'Minimal.idf' 

53 tmp_idf_path = Path(self.test_dir.name) / 'Minimal.idf' 

54 shutil.copyfile(minimal_idf, tmp_idf_path) 

55 

56 # Ensure there is no hash in the first line 

57 with open(tmp_idf_path, 'r', encoding='utf-8') as f: 

58 original_first_line = f.readline() 

59 self.assertNotIn("IFC_GEOMETRY_HASH", original_first_line, 

60 f"IDF file already contains hash in first line: '{original_first_line.strip()}'") 

61 

62 # Add hash to IDF 

63 add_hash_into_idf(hash_line, str(tmp_idf_path)) 

64 with open(tmp_idf_path, 'r', encoding='utf-8') as f: 

65 new_first_line = f.readline() 

66 

67 self.assertEqual(new_first_line, hash_line) 

68 

69 

70if __name__ == '__main__': 

71 unittest.main()