X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/d4c7bf20a2a36894536ed5c92652c953d7eacb80..16daa57e4757e4daeffec1e61630f989727dc563:/test/python/test_tokenizer_icu_rule_loader.py?ds=sidebyside diff --git a/test/python/test_tokenizer_icu_rule_loader.py b/test/python/test_tokenizer_icu_rule_loader.py index bb30dc6e..6ec53edc 100644 --- a/test/python/test_tokenizer_icu_rule_loader.py +++ b/test/python/test_tokenizer_icu_rule_loader.py @@ -1,16 +1,27 @@ """ Tests for converting a config file to ICU rules. """ -import pytest from textwrap import dedent +import pytest +import yaml + from nominatim.tokenizer.icu_rule_loader import ICURuleLoader from nominatim.errors import UsageError from icu import Transliterator @pytest.fixture -def cfgfile(tmp_path, suffix='.yaml'): +def test_config(def_config, tmp_path): + project_dir = tmp_path / 'project_dir' + project_dir.mkdir() + def_config.project_dir = project_dir + + return def_config + + +@pytest.fixture +def cfgrules(test_config): def _create_config(*variants, **kwargs): content = dedent("""\ normalization: @@ -27,22 +38,21 @@ def cfgfile(tmp_path, suffix='.yaml'): content += '\n'.join((" - " + s for s in variants)) + '\n' for k, v in kwargs: content += " {}: {}\n".format(k, v) - fpath = tmp_path / ('test_config' + suffix) - fpath.write_text(dedent(content)) - return fpath + (test_config.project_dir / 'icu_tokenizer.yaml').write_text(content) + + return test_config return _create_config -def test_empty_rule_file(tmp_path): - fpath = tmp_path / ('test_config.yaml') - fpath.write_text(dedent("""\ +def test_empty_rule_set(test_config): + (test_config.project_dir / 'icu_tokenizer.yaml').write_text(dedent("""\ normalization: transliteration: variants: """)) - rules = ICURuleLoader(fpath) + rules = ICURuleLoader(test_config) assert rules.get_search_rules() == '' assert rules.get_normalization_rules() == '' assert rules.get_transliteration_rules() == '' @@ -51,19 +61,16 @@ def test_empty_rule_file(tmp_path): CONFIG_SECTIONS = ('normalization', 'transliteration', 'variants') @pytest.mark.parametrize("section", CONFIG_SECTIONS) -def test_missing_normalization(tmp_path, section): - fpath = tmp_path / ('test_config.yaml') - with fpath.open('w') as fd: - for name in CONFIG_SECTIONS: - if name != section: - fd.write(name + ':\n') +def test_missing_section(section, test_config): + rule_cfg = { s: {} for s in CONFIG_SECTIONS if s != section} + (test_config.project_dir / 'icu_tokenizer.yaml').write_text(yaml.dump(rule_cfg)) with pytest.raises(UsageError): - ICURuleLoader(fpath) + ICURuleLoader(test_config) -def test_get_search_rules(cfgfile): - loader = ICURuleLoader(cfgfile()) +def test_get_search_rules(cfgrules): + loader = ICURuleLoader(cfgrules()) rules = loader.get_search_rules() trans = Transliterator.createFromRules("test", rules) @@ -77,24 +84,24 @@ def test_get_search_rules(cfgfile): assert trans.transliterate(" проспект ") == " prospekt " -def test_get_normalization_rules(cfgfile): - loader = ICURuleLoader(cfgfile()) +def test_get_normalization_rules(cfgrules): + loader = ICURuleLoader(cfgrules()) rules = loader.get_normalization_rules() trans = Transliterator.createFromRules("test", rules) assert trans.transliterate(" проспект-Prospekt ") == " проспект prospekt " -def test_get_transliteration_rules(cfgfile): - loader = ICURuleLoader(cfgfile()) +def test_get_transliteration_rules(cfgrules): + loader = ICURuleLoader(cfgrules()) rules = loader.get_transliteration_rules() trans = Transliterator.createFromRules("test", rules) assert trans.transliterate(" проспект-Prospekt ") == " prospekt Prospekt " -def test_transliteration_rules_from_file(tmp_path): - cfgpath = tmp_path / ('test_config.yaml') +def test_transliteration_rules_from_file(test_config): + cfgpath = test_config.project_dir / ('icu_tokenizer.yaml') cfgpath.write_text(dedent("""\ normalization: transliteration: @@ -102,10 +109,10 @@ def test_transliteration_rules_from_file(tmp_path): - !include transliteration.yaml variants: """)) - transpath = tmp_path / ('transliteration.yaml') + transpath = test_config.project_dir / ('transliteration.yaml') transpath.write_text('- "x > y"') - loader = ICURuleLoader(cfgpath) + loader = ICURuleLoader(test_config) rules = loader.get_transliteration_rules() trans = Transliterator.createFromRules("test", rules) @@ -115,11 +122,11 @@ def test_transliteration_rules_from_file(tmp_path): class TestGetReplacements: @pytest.fixture(autouse=True) - def setup_cfg(self, cfgfile): - self.cfgfile = cfgfile + def setup_cfg(self, cfgrules): + self.cfgrules = cfgrules def get_replacements(self, *variants): - loader = ICURuleLoader(self.cfgfile(*variants)) + loader = ICURuleLoader(self.cfgrules(*variants)) rules = loader.get_replacement_pairs() return set((v.source, v.replacement) for v in rules) @@ -129,7 +136,7 @@ class TestGetReplacements: '~foo~ -> bar', 'fo~ o -> bar']) def test_invalid_variant_description(self, variant): with pytest.raises(UsageError): - ICURuleLoader(self.cfgfile(variant)) + ICURuleLoader(self.cfgrules(variant)) def test_add_full(self): repl = self.get_replacements("foo -> bar")