コード例 #1
0
def test_wrong_key(monkeypatch, latitude, longitude):
    key = 'e1ff55658d4a2b28c4841e373c3b3d87'

    reposta_mock = {"error": "permission denied"}

    # Função que substitui o get retornando um objeto
    # com a resposta que eu defini
    def mock_get(*args, **kwargs):
        return MockResponse(reposta_mock)

    # Aplica o mock na função get do requests
    monkeypatch.setattr(requests, "get", mock_get)

    with pytest.raises(RequestError):
        get_temperature(latitude, longitude, key)
コード例 #2
0
def test_get_temperature_by_lat_lng():

    lat = -14.235004
    lng = -51.92528

    temp = get_temperature(lat, lng)
    return temp == 16
コード例 #3
0
def test_get_temperature_by_lat_lng(monkeypatch):
    def mockrequest(self):
        return MockResponse()

    monkeypatch.setattr(requests, 'get', mockrequest)
    result = get_temperature(None, None)

    assert result == 16
コード例 #4
0
def test_get_temperature_by_lat_lng(mock_get_request_data_success):
    lat = -12.235004
    lng = -59.92528

    get_temperature_expected_return = 16

    assert main.get_temperature(lat=lat,
                                lng=lng) == get_temperature_expected_return
コード例 #5
0
def test_when_temperature_is_none(monkeypatch):
    lat = -14.235004
    lng = -51.92528
    temperature = ''
    answer_expected = None
    atalho_mock(monkeypatch, temperature)
    result = get_temperature(lat, lng)
    assert result == answer_expected
コード例 #6
0
def test_get_temperature_by_lat_lng(lag, lng, expected):
    temp_json = {"currently": {"temperature": 62}}
    mock_patch = patch("main.requests.get")
    mock_value = mock_patch.start()
    mock_value.return_value.json.return_value = temp_json
    temp = get_temperature(lag, lng)
    mock_patch.stop()
    assert temp == expected, "Temperature was {}".format(temp)
コード例 #7
0
def test_get_temperature_no_lat_lng(monkeypatch):
    def mock_json(*args, **kwargs):
        return {"currently": {"temperature": None}}

    monkeypatch.setattr(requests.Response, "json", mock_json)

    result = get_temperature(None, None)
    assert result is None
コード例 #8
0
def test_get_temperature_by_lat_lng(faren, celsius):
    mock_patch = "main.requests.get"

    with mock.patch(mock_patch) as mp:
        mp.return_value.json.return_value = {'currently': {'temperature': faren}}
        resultado = get_temperature(64.1405435, -21.951502)

    assert resultado == celsius
コード例 #9
0
def test_get_temperature_by_lat_lng(mock_get, lat, lng, temperature, expected):

    temperature = {"currently": {"temperature": temperature}}

    mock_get.return_value.json.return_value = temperature
    result = get_temperature(lat, lng)

    assert result == expected
コード例 #10
0
def test_get_temperature_by_lat_lng():
    lat = -14.235004
    lng = -51.92528

    result = get_temperature(lat, lng)

    if result != expected:
        return "Failed"
コード例 #11
0
def test_positive_latitude(get_mock):
    temperature = {"currently": {"temperature": 86}}
    get_mock.return_value.json.return_value = temperature
    lat = 60
    lng = 0
    result = 30
    response = main.get_temperature(lat, lng)
    assert response == result
コード例 #12
0
def test_conversion_to_celsius(monkeypatch):
    lat = -14.235004
    lng = -51.92528
    temperature = 62
    conversion = int((temperature - 32) * 5.0 / 9.0)
    atalho_mock(monkeypatch, temperature)
    result = get_temperature(lat, lng)
    assert result == conversion
コード例 #13
0
def test_negative_lng(get_mock):
    temperature = {"currently": {"temperature": 75.20}}
    get_mock.return_value.json.return_value = temperature
    lat = 0
    lng = -150
    result = 24
    response = main.get_temperature(lat, lng)
    assert response == result
コード例 #14
0
def test_get_temperature_by_lat_lng(lat, lng, expect, monkeypatch):
    def mock_get(*args, **kwargs):
        return MockResponse()

    monkeypatch.setattr(requests, "get", mock_get)
    result = get_temperature(lat, lng)

    assert result == expect
コード例 #15
0
def test_without_latitude(monkeypatch, longitude, key):
    # Como na função esse valor se torna string
    # Então atribui uma string vazia para testar a omissão.
    latitude = ''

    reposta_mock = {"error": "Poorly formatted request"}

    # Função que substitui o get retornando um objeto
    # com a resposta que eu defini
    def mock_get(*args, **kwargs):
        return MockResponse(reposta_mock)

    # Aplica o mock na função get do requests
    monkeypatch.setattr(requests, "get", mock_get)

    with pytest.raises(RequestError):
        get_temperature(latitude, longitude, key)
コード例 #16
0
def test_get_temperature_by_lat_lng(monkeypatch, lat, lng, temperature,
                                    expected):
    def mock_get(*args, **kwargs):
        return MockResponse(temperature)

    monkeypatch.setattr(requests, "get", mock_get)

    response = get_temperature(lat, lng)
    assert response == expected, f'Deveria ter retornado response({expected})'
コード例 #17
0
def test_get_temperature_by_lat_lng(monkeypatch):
    lat = -14.235004
    lng = -51.92528
    temperature = 62
    expected = 16
    atalho_mock(monkeypatch, temperature)
    result = get_temperature(lat, lng)

    assert result == expected
コード例 #18
0
def test_get_temperature_by_lat_lng(mock_request, lat, lng, temperature,
                                    expected):
    mock_request.return_value.json.return_value = {
        "currently": {
            "temperature": temperature
        }
    }
    response = get_temperature(lat, lng)
    assert expected == response, "The result was {}".format(response)
コード例 #19
0
def test_get_temperature_without_lat_lang(monkeypatch):

    def mock_json(*args, **kwatgs):
        return {'currently': {'temperature': None}}

    monkeypatch.setattr(requests.Response, 'json', mock_json)
    result = get_temperature(None, None)

    assert result is None
コード例 #20
0
def test_get_temperature_by_valid_lat_lng(lat,lng,error):
    """
    :param lat: Latitude value
    :param lng: Longitude value
    :return: We expect some kind of error since at least one of the values are not valid
    """
    expected=get_temperature(lat,lng)

    assert expected == error
コード例 #21
0
def test_get_temperature_by_lat_lng(temperature, lat, lng, expected):
    temperature = {"currently": {"temperature": temperature}}

    mock_get_patcher = patch('main.requests.get')
    mock_get = mock_get_patcher.start()
    mock_get.return_value.json.return_value = temperature
    response = get_temperature(lat, lng)
    mock_get_patcher.stop()
    assert response == expected
コード例 #22
0
def test_get_temperature_by_lat_lng(monkeypatch):

    lat = -14.235004
    lng = -51.92528
    expected = 16

    monkeypatch.setattr(main, 'get_temperature',
                        get_temperature_substitute_function)
    assert main.get_temperature(lat, lng) == expected
コード例 #23
0
def test_get_temperature(monkeypatch, lat, lng, expected):
    def mock_get(*args, **kwargs):
        return MockResponse()

    monkeypatch.setattr(requests, 'get', mock_get)

    result = get_temperature(lat, lng)

    assert result == expected
コード例 #24
0
def test_get_temperature_by_lat_lng(monkeypatch):
    # calling the value.
    def mock_get(*args, **kwargs):
        return MockResponse()
    monkeypatch.setattr(requests, "get", mock_get) # changing the value 

    lat = -14.235004
    lng = -51.92528
    assert get_temperature(lat,lng) == 16 # Checking the value.
コード例 #25
0
def test_get_temperature_by_lat_lng(monkeypatch):
    expected_result = 16

    def mock_get(*args, **kwargs):
        return MockResponse()

    monkeypatch.setattr(requests, 'get', mock_get)
    result = get_temperature(-14.235004, -51.92528)

    assert result == expected_result
コード例 #26
0
def test_get_temperature_by_lat_lng(lat, long, temperature,
                                    converted_temperature):

    result_json = {'currently': {'temperature': temperature}}

    with mock.patch('main.requests.get') as mk:
        mk.return_value.json.return_value = result_json
        result = get_temperature(lat, long)

    assert result == converted_temperature
コード例 #27
0
def test_get_temperature_by_lat_lng(monkeypatch):
    lat = -3.10719
    lng = -60.0261

    expected = 26

    monkeypatch.setattr(main, 'get_temperature',
                        get_temperature_helper_function)

    assert main.get_temperature(lat, lng) == expected
コード例 #28
0
def test_get_temperature_invalid_lng(mock_requests):
    response = Mock()
    response.json.return_value = {
        "code": 400,
        "error": "The given location is invalid."
    }
    mock_requests.get.return_value = response
    # The valid range of latitude is -180 and +180
    invalid_lng = 200.856727
    assert get_temperature(LAT, invalid_lng) is None
コード例 #29
0
def test_get_temperature_invalid_lat(mock_requests):
    response = Mock()
    response.json.return_value = {
        "code": 400,
        "error": "The given location is invalid."
    }
    mock_requests.get.return_value = response
    # The valid range of latitude is -90 and +90
    invalid_lat = 100.487639
    assert get_temperature(invalid_lat, LNG) is None
コード例 #30
0
def test_temperature_returns_none(monkeypatch):
    def mock_get_temperature(url):
        return MockResponse(None)

    lat = 0.0
    lng = 0.0

    monkeypatch.setattr(requests, 'get', mock_get_temperature)
    response = get_temperature(lat, lng)

    assert response is None