|
| 1 | +""" |
| 2 | +Tests functions and classes from the odml terminology module. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import unittest |
| 7 | +import tempfile |
| 8 | + |
| 9 | +from glob import glob |
| 10 | +from time import sleep |
| 11 | +try: |
| 12 | + from urllib.request import pathname2url |
| 13 | +except ImportError: |
| 14 | + from urllib import pathname2url |
| 15 | + |
| 16 | +from odml import Document, save, Section, terminology |
| 17 | + |
| 18 | +CACHE_DIR = os.path.join(tempfile.gettempdir(), "odml.cache") |
| 19 | + |
| 20 | + |
| 21 | +class TestTerminology(unittest.TestCase): |
| 22 | + |
| 23 | + def setUp(self): |
| 24 | + """ |
| 25 | + Set up local temporary terminology files in a temporary folder |
| 26 | + """ |
| 27 | + tmp_dir = tempfile.mkdtemp("_odml") |
| 28 | + tmp_name = os.path.basename(tmp_dir) |
| 29 | + |
| 30 | + main_name = "%s_main.xml" % tmp_name |
| 31 | + main_file_path = os.path.join(tmp_dir, main_name) |
| 32 | + main_url = "file://%s" % pathname2url(main_file_path) |
| 33 | + |
| 34 | + include_name = "%s_include.xml" % tmp_name |
| 35 | + include_file_path = os.path.join(tmp_dir, include_name) |
| 36 | + include_url = "file://%s" % pathname2url(include_file_path) |
| 37 | + |
| 38 | + include_doc = Document() |
| 39 | + _ = Section(name="include_sec", type="test", parent=include_doc) |
| 40 | + save(include_doc, include_file_path) |
| 41 | + |
| 42 | + main_doc = Document() |
| 43 | + _ = Section(name="main_sec", type="test", include=include_url, parent=main_doc) |
| 44 | + save(main_doc, main_file_path) |
| 45 | + |
| 46 | + self.main_terminology_url = main_url |
| 47 | + self.temp_dir_base = tmp_name |
| 48 | + |
| 49 | + def tearDown(self): |
| 50 | + """ |
| 51 | + Remove all created files from the odml.cache to not cross pollute other tests. |
| 52 | + The created tmp directory should be cleaned up automatically upon startup. |
| 53 | + """ |
| 54 | + temp_file_glob = "*%s*" % self.temp_dir_base |
| 55 | + find_us = os.path.join(CACHE_DIR, temp_file_glob) |
| 56 | + |
| 57 | + for file_path in glob(find_us): |
| 58 | + os.remove(file_path) |
| 59 | + |
| 60 | + @staticmethod |
| 61 | + def _cache_files_map(file_filter="*"): |
| 62 | + """ |
| 63 | + Returns a dict mapping the basefilenames of cached odml files |
| 64 | + to their md5 hash and mtime. |
| 65 | +
|
| 66 | + :param file_filter: a valid glob to search for files in the odml cache directory. |
| 67 | + The cache directory is provided and must not be part of the glob. |
| 68 | + Default value is '*'. |
| 69 | +
|
| 70 | + :return: dict of the format {filename: [md5_hash, mtime]} |
| 71 | + """ |
| 72 | + temp_file_glob = os.path.join(CACHE_DIR, file_filter) |
| 73 | + |
| 74 | + curr_map = {} |
| 75 | + for file_path in glob(temp_file_glob): |
| 76 | + split_name = os.path.basename(file_path).split('.') |
| 77 | + file_mtime = os.path.getmtime(file_path) |
| 78 | + curr_map[split_name[1]] = [split_name[0], file_mtime] |
| 79 | + |
| 80 | + return curr_map |
| 81 | + |
| 82 | + def test_terminology_refresh(self): |
| 83 | + """ |
| 84 | + Test terminology cache refresh using local files to detach |
| 85 | + loading and resolving from the live online terminology repository. |
| 86 | + """ |
| 87 | + # Fetch current cache content specific to the two local terminologies |
| 88 | + # With the default file glob '*' all files in the odml cache directory would be |
| 89 | + # included in the test. |
| 90 | + file_filter = "*%s*" % self.temp_dir_base |
| 91 | + main_url = self.main_terminology_url |
| 92 | + |
| 93 | + # Initially load main and included file from temp directory into the odml cache directory |
| 94 | + terminology.load(main_url) |
| 95 | + |
| 96 | + orig_map = self._cache_files_map(file_filter) |
| 97 | + |
| 98 | + # Test cache content does not change |
| 99 | + terminology.load(main_url) |
| 100 | + load_map = self._cache_files_map(file_filter) |
| 101 | + |
| 102 | + self.assertEqual(len(orig_map), len(load_map)) |
| 103 | + for curr_file in orig_map: |
| 104 | + self.assertIn(curr_file, load_map) |
| 105 | + self.assertEqual(orig_map[curr_file], load_map[curr_file]) |
| 106 | + |
| 107 | + # Sleep is needed since the tests might be too fast to result in a different file mtime |
| 108 | + sleep(0.100) |
| 109 | + |
| 110 | + # Test refresh loads same cached files but changes them. |
| 111 | + # Different mtimes and id strings are sufficient. |
| 112 | + terminology.refresh(main_url) |
| 113 | + refresh_map = self._cache_files_map(file_filter) |
| 114 | + self.assertEqual(len(orig_map), len(refresh_map)) |
| 115 | + for curr_file in orig_map: |
| 116 | + self.assertIn(curr_file, refresh_map) |
| 117 | + # Check identical md5 hash |
| 118 | + self.assertEqual(orig_map[curr_file][0], refresh_map[curr_file][0]) |
| 119 | + # Check different mtime |
| 120 | + self.assertLess(orig_map[curr_file][1], refresh_map[curr_file][1]) |
0 commit comments