def test_raises_for_unknown_response(mock_api): mock_api({}, 500, geocode="37.587093,55.733969") client = Client("well-known-key") with pytest.raises(UnexpectedResponse) as exc_info: client.address(Decimal("37.587093"), Decimal("55.733969")) assert "status_code=500, body=b'{}'" in exc_info.value.args
def test_raises_for_invalid_api_key(mock_api): mock_api( {"statusCode": 403, "error": "Forbidden", "message": "Invalid key"}, 403, geocode="37.587093,55.733969", api_key="unkown-api-key", ) client = Client("unkown-api-key") with pytest.raises(InvalidKey): client.address(Decimal("37.587093"), Decimal("55.733969"))
def test_returns_found_address(mock_api): mock_api("address_found", 200, geocode="37.587093,55.733969") client = Client("well-known-key") assert ( client.address(Decimal("37.587093"), Decimal("55.733969")) == "Россия, Москва, улица Льва Толстого, 16" )
def Geokoder(document): client = Client("9577d1e2-cf09-4dea-94cc-5c80d4de81e6") coordinates = client.coordinates("Москва Льва Толстого 16") assert coordinates == (Decimal("37.587093"), Decimal("55.733969")) address = client.address(Decimal("37.587093"), Decimal("55.733969")) assert address == "Россия, Москва, улица Льва Толстого, 16" d = client.coordinates(document) return d
def get_address_by_coordinates(coordinates: tuple) -> Optional[AnyStr]: """ Return address string value by coordinates :param coordinates: Coordinates (latitude, longitude) :return: string value """ client = Client('4d16304f-12ba-4134-ac9b-f0da5028a1f4') latitude = coordinates[0] longitude = coordinates[1] location = client.address(longitude, latitude) return location
def location(message): if message.location is not None: # по координатам получаем адрес и присылаем человеку, ему останется проверить адрес и внести правки. client = Client(YANDEX_API_KEY) address = client.address(Decimal(message.location.longitude), Decimal(message.location.latitude)) markup = types.ReplyKeyboardMarkup(row_width=1) yes_button = types.KeyboardButton(text='Да') no_button = types.KeyboardButton(text='Нет') markup.row(yes_button, no_button) bot.send_message( message.chat.id, address) # 'Ваш адрес: г. Волгодонск, ул. Моская, д. 92' bot.send_message(message.chat.id, 'Верно ли определён Ваш адрес?', reply_markup=markup) with open(settings.MEDIA_ROOT + f'{message.chat.id}.txt', 'a') as f: f.writelines([address + '\n']) bot.register_next_step_handler(message, check_address)
def test_raises_if_address_not_found(mock_api): mock_api("address_not_found", 200, geocode="337.587093,55.733969") client = Client("well-known-key") with pytest.raises(NothingFound, match='Nothing found for "337.587093 55.733969"'): client.address(Decimal("337.587093"), Decimal("55.733969"))