+def test_fetch_existing_words_phrases_basic(special_phrases_importer, word_table,
+ temp_db_cursor):
+ """
+ Check for the fetch_existing_words_phrases() method.
+ It should return special phrase term added to the word
+ table.
+ """
+ query ="""
+ INSERT INTO word VALUES(99999, 'lookup_token', 'normalized_word',
+ 'class', 'type', null, 0, 'near');
+ """
+ temp_db_cursor.execute(query)
+
+ assert not special_phrases_importer.words_phrases_to_delete
+ special_phrases_importer._fetch_existing_words_phrases()
+ contained_phrase = special_phrases_importer.words_phrases_to_delete.pop()
+ assert contained_phrase == ('normalized_word', 'class', 'type', 'near')
+
+@pytest.mark.parametrize("house_type", ['house', 'postcode'])
+def test_fetch_existing_words_phrases_special_cases(special_phrases_importer, word_table,
+ house_type, temp_db_cursor):
+ """
+ Check for the fetch_existing_words_phrases() method.
+ It should return nothing as the terms added correspond
+ to a housenumber and postcode term.
+ """
+ query ="""
+ INSERT INTO word VALUES(99999, 'lookup_token', 'normalized_word',
+ 'place', %s, null, 0, 'near');
+ """
+ temp_db_cursor.execute(query, (house_type,))
+
+ special_phrases_importer._fetch_existing_words_phrases()
+ assert not special_phrases_importer.words_phrases_to_delete
+
+def test_fetch_existing_place_classtype_tables(special_phrases_importer, temp_db_cursor):
+ """
+ Check for the fetch_existing_place_classtype_tables() method.
+ It should return the table just created.
+ """
+ temp_db_cursor.execute('CREATE TABLE place_classtype_testclasstypetable()')
+
+ special_phrases_importer._fetch_existing_place_classtype_tables()
+ contained_table = special_phrases_importer.table_phrases_to_delete.pop()
+ assert contained_table == 'place_classtype_testclasstypetable'
+
+def test_check_sanity_class(special_phrases_importer):
+ """
+ Check for _check_sanity() method.
+ If a wrong class or type is given, an UsageError should raise.
+ If a good class and type are given, nothing special happens.
+ """
+
+ assert not special_phrases_importer._check_sanity('en', '', 'type')
+ assert not special_phrases_importer._check_sanity('en', 'class', '')
+
+ assert special_phrases_importer._check_sanity('en', 'class', 'type')
+
+def test_load_white_and_black_lists(special_phrases_importer):
+ """
+ Test that _load_white_and_black_lists() well return
+ black list and white list and that they are of dict type.
+ """
+ black_list, white_list = special_phrases_importer._load_white_and_black_lists()
+
+ assert isinstance(black_list, dict) and isinstance(white_list, dict)
+
+def test_convert_php_settings(special_phrases_importer):
+ """
+ Test that _convert_php_settings_if_needed() convert the given
+ php file to a json file.
+ """
+ php_file = (TEST_BASE_DIR / 'testfiles' / 'phrase_settings.php').resolve()
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ temp_settings = (Path(temp_dir) / 'phrase_settings.php').resolve()
+ copyfile(php_file, temp_settings)
+ special_phrases_importer._convert_php_settings_if_needed(temp_settings)
+
+ assert (Path(temp_dir) / 'phrase_settings.json').is_file()
+
+def test_convert_settings_wrong_file(special_phrases_importer):
+ """
+ Test that _convert_php_settings_if_needed() raise an exception
+ if the given file is not a valid file.
+ """
+ with pytest.raises(UsageError, match='random_file is not a valid file.'):
+ special_phrases_importer._convert_php_settings_if_needed('random_file')
+
+def test_convert_settings_json_already_exist(special_phrases_importer):
+ """
+ Test that if we give to '_convert_php_settings_if_needed' a php file path
+ and that a the corresponding json file already exists, it is returned.
+ """
+ php_file = (TEST_BASE_DIR / 'testfiles' / 'phrase_settings.php').resolve()
+ json_file = (TEST_BASE_DIR / 'testfiles' / 'phrase_settings.json').resolve()
+
+ returned = special_phrases_importer._convert_php_settings_if_needed(php_file)
+
+ assert returned == json_file
+
+def test_convert_settings_giving_json(special_phrases_importer):
+ """
+ Test that if we give to '_convert_php_settings_if_needed' a json file path
+ the same path is directly returned
+ """
+ json_file = (TEST_BASE_DIR / 'testfiles' / 'phrase_settings.json').resolve()
+
+ returned = special_phrases_importer._convert_php_settings_if_needed(json_file)
+
+ assert returned == json_file
+
+def test_process_amenity_with_operator(special_phrases_importer, getorcreate_amenityoperator_funcs,
+ temp_db_conn, word_table):
+ """
+ Test that _process_amenity() execute well the
+ getorcreate_amenityoperator() SQL function and that
+ the 2 differents operators are well handled.
+ """