Ejemplo n.º 1
0
def test_get_data_retries_on_error(test_url, bad_response_mock):
    # GIVEN that the response is not 200
    with patch("urllib.request.urlopen", return_value=bad_response_mock):
        # WHEN calling get_data
        get_data(test_url)

    # THEN urllib.request.urlopen was called 6 times (first attempt + 5 retries)
    assert bad_response_mock.__enter__.call_count == 6
Ejemplo n.º 2
0
def test_get_data_sleeps_between_retries(test_url, sleep_mock):
    # GIVEN that upon parsing the response an exception is raised
    with patch("json.loads",
               side_effect=json.JSONDecodeError("Bad json",
                                                doc="my doc",
                                                pos=11)):
        # WHEN calling get_data
        get_data(test_url)

    # THEN sleep is only called 5 times (not 6)
    assert sleep_mock.call_count == 5
Ejemplo n.º 3
0
def test_get_data_sleeps_desired_amount_between_retries(test_url, sleep_mock):
    # GIVEN that upon parsing the response an exception is raised
    with patch("json.loads",
               side_effect=json.JSONDecodeError("Bad json",
                                                doc="my doc",
                                                pos=11)):
        # WHEN calling get_data with 4 retries and 2 seconds delay
        get_data(test_url, max_retries=4, delay_between_retries=2)

    # THEN sleep is only called 4 times
    assert sleep_mock.call_count == 4
    # each time with 2 seconds as argument
    sleep_mock.assert_has_calls([call(2)] * 4)
Ejemplo n.º 4
0
def test_get_data_gives_expected_result_for_test_json(test_url,
                                                      response_json_example):
    # WHEN calling get_data on the test url
    response = get_data(test_url)

    # THEN the response is exactly as expected (saved in the file)
    assert response_json_example == response  # comparing dicts
Ejemplo n.º 5
0
def test_get_data_returns_none_fora_bad_http_response_status(
        test_url, bad_response_mock):
    # GIVEN that the response is not 200
    with patch("urllib.request.urlopen", return_value=bad_response_mock):
        # WHEN calling get_data
        response = get_data(test_url)

    # THEN it returns None
    assert response is None
Ejemplo n.º 6
0
def test_get_data_returns_none_if_json_gives_exception(test_url):
    # GIVEN that upon parsing the response an exception is raised
    with patch("json.loads",
               side_effect=json.JSONDecodeError("Bad json",
                                                doc="my doc",
                                                pos=11)):
        # WHEN calling get_data
        response = get_data(test_url)

    # THEN the response is None
    assert response is None
Ejemplo n.º 7
0
def test_get_data_full_functionality(test_url, expected_json):
    data_centers = __data_centers_from_dict(get_data(test_url))

    for data_center in data_centers:
        data_center.remove_invalid_clusters()
        data_center.remove_invalid_records()
        data_center.sort_records()

    expected_data_centers = __data_centers_from_dict(expected_json)

    __assert_data_centers_eq(data_centers, expected_data_centers)
Ejemplo n.º 8
0
def test_get_data_returns_none_if_read_gives_exception(test_url):
    # GIVEN that the response is unreadable and it raises HTTPException
    response_mock = MagicMock(http.client.HTTPResponse)
    response_mock.status = 200
    response_mock.read.side_effect = http.client.HTTPException("Cannot read")
    urlopen_mock = MagicMock()
    urlopen_mock.__enter__.return_value = response_mock  # mocking the context manager
    with patch("urllib.request.urlopen", return_value=urlopen_mock):
        # WHEN calling get_data
        response = get_data(test_url)

    # THEN the response is None
    assert response is None
Ejemplo n.º 9
0
def main():
    """
    Main entry to our program.
    """

    data = get_data(URL)

    if not data:
        raise ValueError('No data to process')

    datacenters = [Datacenter(key, value) for key, value in data.items()]

    pass  # the rest of your logic here
Ejemplo n.º 10
0
def test_get_data_stops_retrying_if_succeeds_and_returns_dict(
        test_url, bad_response_mock):
    # GIVEN that http response is bad a couple of times and a good one the third time
    bad_response_mock.__enter__.side_effect = [
        bad_response_mock.response_mock,  # the response 400
        bad_response_mock.response_mock,  # the response 400 at first retry
        urllib.request.urlopen(test_url)  # second retry - does the actual call
    ]
    with patch("urllib.request.urlopen", return_value=bad_response_mock):
        # WHEN calling get_data
        response = get_data(test_url)

    # THEN response is a dict
    assert isinstance(response, dict)
    # and urllib.request.urlopen was called 3 times
    assert bad_response_mock.__enter__.call_count == 3
Ejemplo n.º 11
0
def test_get_data_returns_a_dict(test_url):
    # WHEN calling get_data on the test url
    response = get_data(test_url)

    # THEN it returns a valid dict object
    assert isinstance(response, dict)
Ejemplo n.º 12
0
def test_get_data_returns_none_for_nonexistent_url():
    # WHEN calling get_data with a nonexistent url
    # THEN it returns None
    assert get_data("http://this_url_does_not_exist.not_ever") is None
Ejemplo n.º 13
0
def test_get_data_returns_none_for_badly_formed_url():
    # WHEN calling get_data with a badly formed url
    # THEN it returns None
    assert get_data("/bad/url") is None