1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2024 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Tests for the sanitizer that splits multivalue lists.
 
  12 from nominatim_db.tokenizer.place_sanitizer import PlaceSanitizer
 
  13 from nominatim_db.data.place_info import PlaceInfo
 
  15 from nominatim_db.errors import UsageError
 
  19     @pytest.fixture(autouse=True)
 
  20     def setup_country(self, def_config):
 
  21         self.config = def_config
 
  24     def run_sanitizer_on(self, **kwargs):
 
  25         place = PlaceInfo({'name': kwargs})
 
  26         name, _ = PlaceSanitizer([{'step': 'split-name-list'}], self.config).process_names(place)
 
  28         return sorted([(p.name, p.kind, p.suffix) for p in name])
 
  31     def sanitize_with_delimiter(self, delimiter, name):
 
  32         place = PlaceInfo({'name': {'name': name}})
 
  33         san = PlaceSanitizer([{'step': 'split-name-list', 'delimiters': delimiter}],
 
  35         name, _ = san.process_names(place)
 
  37         return sorted([p.name for p in name])
 
  40     def test_simple(self):
 
  41         assert self.run_sanitizer_on(name='ABC') == [('ABC', 'name', None)]
 
  42         assert self.run_sanitizer_on(name='') == [('', 'name', None)]
 
  45     def test_splits(self):
 
  46         assert self.run_sanitizer_on(name='A;B;C') == [('A', 'name', None),
 
  49         assert self.run_sanitizer_on(short_name=' House, boat ') == [('House', 'short_name', None),
 
  50                                                                      ('boat', 'short_name', None)]
 
  53     def test_empty_fields(self):
 
  54         assert self.run_sanitizer_on(name='A;;B') == [('A', 'name', None),
 
  56         assert self.run_sanitizer_on(name='A; ,B') == [('A', 'name', None),
 
  58         assert self.run_sanitizer_on(name=' ;B') == [('B', 'name', None)]
 
  59         assert self.run_sanitizer_on(name='B,') == [('B', 'name', None)]
 
  62     def test_custom_delimiters(self):
 
  63         assert self.sanitize_with_delimiter(':', '12:45,3') == ['12', '45,3']
 
  64         assert self.sanitize_with_delimiter('\\', 'a;\\b!#@ \\') == ['a;', 'b!#@']
 
  65         assert self.sanitize_with_delimiter('[]', 'foo[to]be') == ['be', 'foo', 'to']
 
  66         assert self.sanitize_with_delimiter(' ', 'morning  sun') == ['morning', 'sun']
 
  69     def test_empty_delimiter_set(self):
 
  70         with pytest.raises(UsageError):
 
  71             self.sanitize_with_delimiter('', 'abc')
 
  74 def test_no_name_list(def_config):
 
  75     place = PlaceInfo({'address': {'housenumber': '3'}})
 
  76     name, address = PlaceSanitizer([{'step': 'split-name-list'}], def_config).process_names(place)
 
  79     assert len(address) == 1