Пример #1
0
def test_address_extractor_find_city_names():
    c1 = City("abc", "12345", None)
    c2 = City("def g", "12345", None)
    ae = AddressExtractor([c1, c2])

    assert ae.find_city_names("without city") == []
    assert ae.find_city_names("with def and g") == []
    assert ae.find_city_names("with the abc city") == [(c1, 9, 12)]
    assert ae.find_city_names("with the def g city") == [(c2, 9, 14)]
    assert ae.find_city_names("with def g and abc cities") == [
        (c2, 5, 10),
        (c1, 15, 18),
    ]
Пример #2
0
def test_address_extractor_find_nearby_postal_code(mocker):
    c = City("abc", "12345", None)
    ae = AddressExtractor([c], postal_code_search_distance=8)

    assert ae.find_nearby_postal_code("blah abc 12345", c, 5,
                                      8) == ("12345", 9, 14)
    assert ae.find_nearby_postal_code("abc fr12345-", c, 0,
                                      3) == ("12345", 6, 11)
    assert ae.find_nearby_postal_code("12345- abc fr", c, 7,
                                      10) == ("12345", 0, 5)
    assert ae.find_nearby_postal_code("blah abc 123456", c, 5, 8) is None
    assert ae.find_nearby_postal_code("blah abc foo 12345", c, 5, 8) is None
    assert ae.find_nearby_postal_code("12345 blah abc foo", c, 11, 14) is None
    # Search substring matching with postal code start
    assert ae.find_nearby_postal_code("foo 12345fr abc", c, 12,
                                      15) == ("12345", 4, 9)

    # Invalid postal code (not 5 digits)
    m_get_logger = mocker.patch(f"{module}.get_logger")
    c2 = City("abc", "12A42", None)
    assert ae.find_nearby_postal_code("foo 12A42 abc", c2, 12, 15) is None
    m_get_logger.assert_called_once_with(f"{module}.AddressExtractor")
    m_get_logger.return_value.error.assert_called_once_with(
        "postal code contains non-digit characters: %s", c2)
Пример #3
0
def test_load_cities_fr(mocker):
    m_gzip_open = mocker.patch(f"{module}.gzip.open")
    m_json_load = mocker.patch(
        f"{module}.json.load",
        return_value=[
            {
                "fields": {
                    "nom_de_la_commune": "PARIS",
                    "code_postal": "75000",
                    "coordonnees_gps": [48.866667, 2.333333],
                },
            },
            {
                "fields": {
                    "nom_de_la_commune": "POYA",
                    "code_postal": "98827"
                }
            },
        ],
    )

    res = load_cities_fr()

    m_gzip_open.assert_called_once_with(settings.OCR_CITIES_FR_PATH, "rb")
    m_json_load.assert_called_once_with(
        m_gzip_open.return_value.__enter__.return_value)
    assert res == {
        City("paris", "75000", (48.866667, 2.333333)),
        City("poya", "98827", None),
    }

    # Error with postal code with bad length
    mocker.resetall()
    m_json_load.return_value = [
        {
            "fields": {
                "nom_de_la_commune": "YOLO",
                "code_postal": "123"
            }
        },
    ]

    with pytest.raises(ValueError,
                       match="'123', invalid FR postal code for city 'yolo'"):
        load_cities_fr()

    m_gzip_open.assert_called_once_with(settings.OCR_CITIES_FR_PATH, "rb")
    m_json_load.assert_called_once_with(
        m_gzip_open.return_value.__enter__.return_value)

    # Error with non-digit postal code
    mocker.resetall()
    m_json_load.return_value = [
        {
            "fields": {
                "nom_de_la_commune": "YOLO",
                "code_postal": "12A42"
            }
        },
    ]

    with pytest.raises(
            ValueError,
            match="'12A42', invalid FR postal code for city 'yolo'"):
        load_cities_fr()

    m_gzip_open.assert_called_once_with(settings.OCR_CITIES_FR_PATH, "rb")
    m_json_load.assert_called_once_with(
        m_gzip_open.return_value.__enter__.return_value)
Пример #4
0
def cities():
    return [
        City("paris", "75000", (48.866667, 2.333333)),
        City("poya", "98827", None)
    ]