From 9d0c56f0f649a4b48bfa7b330d8f5c02a3b4d265 Mon Sep 17 00:00:00 2001 From: hynnot Date: Fri, 17 Jan 2025 17:10:13 +0100 Subject: [PATCH] Add E2E tests to aggregate observations endpoint and refactor with fixture --- .../oonimeasurements/tests/conftest.py | 19 +++++ .../oonimeasurements/tests/test_oonidata.py | 33 -------- .../tests/test_oonidata_aggregate_analysis.py | 48 +++++------- .../test_oonidata_aggregate_observations.py | 68 ++++++++++++++++ .../tests/test_oonidata_list_analysis.py | 70 ++++++++++------- .../tests/test_oonidata_list_observations.py | 77 +++++++++++-------- 6 files changed, 193 insertions(+), 122 deletions(-) delete mode 100644 ooniapi/services/oonimeasurements/tests/test_oonidata.py create mode 100644 ooniapi/services/oonimeasurements/tests/test_oonidata_aggregate_observations.py diff --git a/ooniapi/services/oonimeasurements/tests/conftest.py b/ooniapi/services/oonimeasurements/tests/conftest.py index 3cb1dcd5..df491432 100644 --- a/ooniapi/services/oonimeasurements/tests/conftest.py +++ b/ooniapi/services/oonimeasurements/tests/conftest.py @@ -150,3 +150,22 @@ def client_with_admin_role(client): jwt_token = create_session_token("0" * 16, "admin") client.headers = {"Authorization": f"Bearer {jwt_token}"} yield client + + +@pytest.fixture +def params_since_and_until_with_two_days(): + return set_since_and_until_params(since="2024-11-01", until="2024-11-02") + + +@pytest.fixture +def params_since_and_until_with_ten_days(): + return set_since_and_until_params(since="2024-11-01", until="2024-11-10") + + +def set_since_and_until_params(since, until): + params = { + "since": since, + "until": until + } + + return params diff --git a/ooniapi/services/oonimeasurements/tests/test_oonidata.py b/ooniapi/services/oonimeasurements/tests/test_oonidata.py deleted file mode 100644 index 54246155..00000000 --- a/ooniapi/services/oonimeasurements/tests/test_oonidata.py +++ /dev/null @@ -1,33 +0,0 @@ -def test_oonidata(client): - r = client.get("api/v1/observations?since=2024-11-01&until=2024-11-02") - j = r.json() - assert isinstance(j["results"], list), j - assert len(j["results"]) > 0 - for result in j["results"]: - assert "test_name" in result, result - assert "probe_cc" in result, result - - r = client.get("api/v1/analysis?since=2024-11-01&until=2024-11-02&probe_cc=IT") - j = r.json() - assert isinstance(j["results"], list), j - assert len(j["results"]) > 0 - for result in j["results"]: - assert result["probe_cc"] == "IT" - - r = client.get( - "api/v1/aggregation/analysis?since=2024-11-01&until=2024-11-10&probe_cc=IR" - ) - j = r.json() - assert isinstance(j["results"], list), j - assert len(j["results"]) > 0 - for result in j["results"]: - assert result["probe_cc"] == "IR" - - r = client.get( - "api/v1/aggregation/observations?since=2024-11-01&until=2024-11-10&probe_cc=IT" - ) - j = r.json() - assert isinstance(j["results"], list), j - assert len(j["results"]) > 0 - for result in j["results"]: - assert result["probe_cc"] == "IT" diff --git a/ooniapi/services/oonimeasurements/tests/test_oonidata_aggregate_analysis.py b/ooniapi/services/oonimeasurements/tests/test_oonidata_aggregate_analysis.py index f1bc4dca..1bff1fe4 100644 --- a/ooniapi/services/oonimeasurements/tests/test_oonidata_aggregate_analysis.py +++ b/ooniapi/services/oonimeasurements/tests/test_oonidata_aggregate_analysis.py @@ -1,8 +1,6 @@ import pytest route = "api/v1/aggregation/analysis" -since = "2024-11-01" -until = "2024-11-10" def test_oonidata_aggregation_analysis(client): @@ -13,8 +11,8 @@ def test_oonidata_aggregation_analysis(client): assert len(json["results"]) == 0 -def test_oonidata_aggregation_analysis_with_since_and_until(client): - response = client.get(f"{route}?since={since}&until={until}") +def test_oonidata_aggregation_analysis_with_since_and_until(client, params_since_and_until_with_two_days): + response = client.get(route, params=params_since_and_until_with_two_days) json = response.json() assert isinstance(json["results"], list), json @@ -35,11 +33,8 @@ def test_oonidata_aggregation_analysis_with_since_and_until(client): ("input", "stun://stun.voys.nl:3478"), ] ) -def test_oonidata_aggregation_analysis_with_filters(client, filter_param, filter_value): - params = { - "since": since, - "until": until - } +def test_oonidata_aggregation_analysis_with_filters(client, filter_param, filter_value, params_since_and_until_with_ten_days): + params = params_since_and_until_with_ten_days params[filter_param] = filter_value response = client.get(route, params=params) @@ -51,11 +46,12 @@ def test_oonidata_aggregation_analysis_with_filters(client, filter_param, filter assert result[filter_param] == filter_value, result -def test_oonidata_aggregation_analysis_filtering_by_probe_asn_as_a_string_with_since_and_until(client): +def test_oonidata_aggregation_analysis_filtering_by_probe_asn_as_a_string_with_since_and_until(client, params_since_and_until_with_ten_days): + params = params_since_and_until_with_ten_days probe_asn = 45758 - probe_asn_as_a_string = "AS" + str(probe_asn) + params["probe_asn"] = "AS" + str(probe_asn) - response = client.get(f"{route}?probe_asn={probe_asn_as_a_string}&since={since}&until={until}") + response = client.get(route, params=params) json = response.json() assert isinstance(json["results"], list), json @@ -74,12 +70,9 @@ def test_oonidata_aggregation_analysis_filtering_by_probe_asn_as_a_string_with_s "input", ] ) -def test_oonidata_aggregation_analysis_with_axis_x(client, field): - params = { - "since": since, - "until": until, - "axis_x": field - } +def test_oonidata_aggregation_analysis_with_axis_x(client, field, params_since_and_until_with_ten_days): + params = params_since_and_until_with_ten_days + params["axis_x"] = field response = client.get(route, params=params) @@ -100,12 +93,9 @@ def test_oonidata_aggregation_analysis_with_axis_x(client, field): "input", ] ) -def test_oonidata_aggregation_analysis_axis_y(client, field): - params = { - "since": since, - "until": until, - "axis_y": field - } +def test_oonidata_aggregation_analysis_axis_y(client, field, params_since_and_until_with_ten_days): + params = params_since_and_until_with_ten_days + params["axis_y"] = field response = client.get(route, params=params) @@ -127,12 +117,10 @@ def test_oonidata_aggregation_analysis_axis_y(client, field): ("auto", 9), ] ) -def test_oonidata_aggregation_analysis_time_grain(client, time_grain, total): - params = { - "since": since, - "until": until, - "time_grain": time_grain - } +def test_oonidata_aggregation_analysis_time_grain(client, time_grain, total, params_since_and_until_with_ten_days): + params = params_since_and_until_with_ten_days + params["group_by"] = "timestamp" + params["time_grain"] = time_grain response = client.get(route, params=params) diff --git a/ooniapi/services/oonimeasurements/tests/test_oonidata_aggregate_observations.py b/ooniapi/services/oonimeasurements/tests/test_oonidata_aggregate_observations.py new file mode 100644 index 00000000..2f4ca676 --- /dev/null +++ b/ooniapi/services/oonimeasurements/tests/test_oonidata_aggregate_observations.py @@ -0,0 +1,68 @@ +import pytest + +route = "api/v1/aggregation/observations" + + +def test_oonidata_aggregation_observations(client): + response = client.get(route) + + json = response.json() + assert isinstance(json["results"], list), json + assert len(json["results"]) == 0 + + +def test_oonidata_aggregation_observations_with_since_and_until(client, params_since_and_until_with_two_days): + response = client.get(route, params=params_since_and_until_with_two_days) + + json = response.json() + assert isinstance(json["results"], list), json + assert len(json["results"]) > 0 + + for result in json["results"]: + assert "observation_count" in result, result + assert "failure" in result, result + + +@pytest.mark.parametrize( + "filter_name, filter_value", + [ + ("probe_cc", "IT"), + ("probe_asn", 45758), + ("test_name", "whatsapp"), + ("hostname", "www.on-instant.com"), + ("ip", "64.233.190.139"), + ] +) +def test_oonidata_aggregation_observations_with_filters(client, filter_name, filter_value, params_since_and_until_with_ten_days): + params = params_since_and_until_with_ten_days + params[filter_name] = filter_value + + response = client.get(route, params=params) + + json = response.json() + assert isinstance(json["results"], list), json + assert len(json["results"]) > 0 + for result in json["results"]: + assert result[filter_name] == filter_value, result + + +@pytest.mark.parametrize( + "time_grain, total", + [ + ("hour", 215), + ("day", 9), + ("week", 2), + ("month", 1), + ("year", 1), + ("auto", 9), + ] +) +def test_oonidata_aggregation_observations_time_grain(client, time_grain, total, params_since_and_until_with_ten_days): + params = params_since_and_until_with_ten_days + params["group_by"] = "timestamp" + params["time_grain"] = time_grain + + response = client.get(route, params=params) + + json = response.json() + assert len(json["results"]) == total diff --git a/ooniapi/services/oonimeasurements/tests/test_oonidata_list_analysis.py b/ooniapi/services/oonimeasurements/tests/test_oonidata_list_analysis.py index ff1a1784..006b342a 100644 --- a/ooniapi/services/oonimeasurements/tests/test_oonidata_list_analysis.py +++ b/ooniapi/services/oonimeasurements/tests/test_oonidata_list_analysis.py @@ -1,8 +1,7 @@ import pytest route = "api/v1/analysis" -since = "2024-11-01" -until = "2024-11-02" + def test_oonidata_list_analysis(client): response = client.get(route) @@ -11,8 +10,9 @@ def test_oonidata_list_analysis(client): assert isinstance(json["results"], list), json assert len(json["results"]) == 0 -def test_oonidata_list_analysis_with_since_and_until(client): - response = client.get(f"{route}?since={since}&until={until}") + +def test_oonidata_list_analysis_with_since_and_until(client, params_since_and_until_with_two_days): + response = client.get(route, params=params_since_and_until_with_two_days) json = response.json() assert isinstance(json["results"], list), json @@ -21,6 +21,7 @@ def test_oonidata_list_analysis_with_since_and_until(client): assert "test_name" in result, result assert "probe_cc" in result, result + @pytest.mark.parametrize( "filter_param, filter_value", [ @@ -30,11 +31,8 @@ def test_oonidata_list_analysis_with_since_and_until(client): ("test_name", "web_connectivity"), ] ) -def test_oonidata_list_analysis_with_filters(client, filter_param, filter_value): - params = { - "since": since, - "until": until - } +def test_oonidata_list_analysis_with_filters(client, filter_param, filter_value, params_since_and_until_with_two_days): + params = params_since_and_until_with_two_days params[filter_param] = filter_value response = client.get(route, params=params) @@ -45,11 +43,13 @@ def test_oonidata_list_analysis_with_filters(client, filter_param, filter_value) for result in json["results"]: assert result[filter_param] == filter_value, result -def test_oonidata_list_analysis_filtering_by_probe_asn_as_a_string_with_since_and_until(client): + +def test_oonidata_list_analysis_filtering_by_probe_asn_as_a_string_with_since_and_until(client, params_since_and_until_with_two_days): + params = params_since_and_until_with_two_days probe_asn = 45758 - probe_asn_as_a_string = "AS" + str(probe_asn) + params["probe_asn"] = "AS" + str(probe_asn) - response = client.get(f"{route}?probe_asn={probe_asn_as_a_string}&since={since}&until={until}") + response = client.get(route, params=params) json = response.json() assert isinstance(json["results"], list), json @@ -57,8 +57,9 @@ def test_oonidata_list_analysis_filtering_by_probe_asn_as_a_string_with_since_an for result in json["results"]: assert result["probe_asn"] == probe_asn, result -def test_oonidata_list_analysis_order_default(client): - response = client.get(f"{route}?since={since}&until={until}") + +def test_oonidata_list_analysis_order_default(client, params_since_and_until_with_two_days): + response = client.get(route, params=params_since_and_until_with_two_days) json = response.json() assert isinstance(json["results"], list), json @@ -69,8 +70,12 @@ def test_oonidata_list_analysis_order_default(client): current_date = json["results"][i]["measurement_start_time"] assert previous_date >= current_date, f"The dates are not ordered: {previous_date} < {current_date}" -def test_oonidata_list_analysis_order_asc(client): - response = client.get(f"{route}?order=ASC&since={since}&until={until}") + +def test_oonidata_list_analysis_order_asc(client, params_since_and_until_with_two_days): + params = params_since_and_until_with_two_days + params["order"] = "ASC" + + response = client.get(route, params=params) json = response.json() assert isinstance(json["results"], list), json @@ -81,8 +86,9 @@ def test_oonidata_list_analysis_order_asc(client): current_date = json["results"][i]["measurement_start_time"] assert previous_date <= current_date, f"The dates are not ordered: {previous_date} > {current_date}" + @pytest.mark.parametrize( - "order_param, order", + "field, order", [ ("input", "asc"), ("probe_cc", "asc"), @@ -94,30 +100,38 @@ def test_oonidata_list_analysis_order_asc(client): ("test_name", "desc"), ] ) -def test_oonidata_list_analysis_order_by_field(client, order_param, order): - response = client.get(f"{route}?order_by={order_param}&order={order}&since={since}&until={until}") +def test_oonidata_list_analysis_order_by_field(client, field, order, params_since_and_until_with_two_days): + params = params_since_and_until_with_two_days + params['order_by'] = field + params['order'] = order + response = client.get(route, params=params) json = response.json() assert isinstance(json["results"], list), json assert len(json["results"]) > 0 for i in range(1, len(json["results"])): - assert order_param in json["results"][i], json["results"][i] - previous = json["results"][i - 1][order_param] - current = json["results"][i][order_param] + assert field in json["results"][i], json["results"][i] + previous = json["results"][i - 1][field] + current = json["results"][i][field] if order == "asc": - assert previous <= current, f"The {order_param} values are not ordered in ascending order: {previous} > {current}" + assert previous <= current, f"The {field} values are not ordered in ascending order: {previous} > {current}" else: - assert previous >= current, f"The {order_param} values are not ordered in descending order: {previous} < {current}" + assert previous >= current, f"The {field} values are not ordered in descending order: {previous} < {current}" + -def test_oonidata_list_analysis_limit_by_default(client): - response = client.get(f"{route}?since={since}&until={until}") +def test_oonidata_list_analysis_limit_by_default(client, params_since_and_until_with_two_days): + response = client.get(route, params=params_since_and_until_with_two_days) json = response.json() assert isinstance(json["results"], list), json assert len(json["results"]) == 100 -def test_oonidata_list_analysis_with_limit_and_offset(client): - response = client.get(f"{route}?limit=10&offset=10&since={since}&until={until}") + +def test_oonidata_list_analysis_with_limit_and_offset(client, params_since_and_until_with_two_days): + params = params_since_and_until_with_two_days + params["limit"] = 10 + + response = client.get(route, params=params) json = response.json() assert isinstance(json["results"], list), json diff --git a/ooniapi/services/oonimeasurements/tests/test_oonidata_list_observations.py b/ooniapi/services/oonimeasurements/tests/test_oonidata_list_observations.py index 86682219..0a86edd8 100644 --- a/ooniapi/services/oonimeasurements/tests/test_oonidata_list_observations.py +++ b/ooniapi/services/oonimeasurements/tests/test_oonidata_list_observations.py @@ -1,8 +1,7 @@ import pytest route = "api/v1/observations" -since = "2024-11-01" -until = "2024-11-02" + def test_oonidata_list_observations(client): response = client.get(route) @@ -11,8 +10,9 @@ def test_oonidata_list_observations(client): assert isinstance(json["results"], list), json assert len(json["results"]) == 0 -def test_oonidata_list_observations_with_since_and_until(client): - response = client.get(f"{route}?since={since}&until={until}") + +def test_oonidata_list_observations_with_since_and_until(client, params_since_and_until_with_two_days): + response = client.get(route, params=params_since_and_until_with_two_days) json = response.json() assert isinstance(json["results"], list), json @@ -21,8 +21,9 @@ def test_oonidata_list_observations_with_since_and_until(client): assert "test_name" in result, result assert "probe_cc" in result, result + @pytest.mark.parametrize( - "filter_param, filter_value", + "filter_name, filter_value", [ ("report_id", "20241101T233351Z_webconnectivity_DE_3209_n1_I7QVY7IdnaSfYmsb"), ("probe_asn", 45758), @@ -34,12 +35,9 @@ def test_oonidata_list_observations_with_since_and_until(client): ("engine_version", "3.20.0"), ] ) -def test_oonidata_list_observations_with_filters(client, filter_param, filter_value): - params = { - "since": since, - "until": until - } - params[filter_param] = filter_value +def test_oonidata_list_observations_with_filters(client, filter_name, filter_value, params_since_and_until_with_two_days): + params = params_since_and_until_with_two_days + params[filter_name] = filter_value response = client.get(route, params=params) @@ -47,13 +45,15 @@ def test_oonidata_list_observations_with_filters(client, filter_param, filter_va assert isinstance(json["results"], list), json assert len(json["results"]) > 0 for result in json["results"]: - assert result[filter_param] == filter_value, result + assert result[filter_name] == filter_value, result + -def test_oonidata_list_observations_filtering_by_probe_asn_as_a_string_with_since_and_until(client): +def test_oonidata_list_observations_filtering_by_probe_asn_as_a_string_with_since_and_until(client, params_since_and_until_with_two_days): + params = params_since_and_until_with_two_days probe_asn = 45758 - probe_asn_as_a_string = "AS" + str(probe_asn) + params["probe_asn"] = "AS" + str(probe_asn) - response = client.get(f"{route}?probe_asn={probe_asn_as_a_string}&since={since}&until={until}") + response = client.get(route, params=params) json = response.json() assert isinstance(json["results"], list), json @@ -61,8 +61,9 @@ def test_oonidata_list_observations_filtering_by_probe_asn_as_a_string_with_sinc for result in json["results"]: assert result["probe_asn"] == probe_asn, result -def test_oonidata_list_observations_order_default(client): - response = client.get(f"{route}?since={since}&until={until}") + +def test_oonidata_list_observations_order_default(client, params_since_and_until_with_two_days): + response = client.get(route, params=params_since_and_until_with_two_days) json = response.json() assert isinstance(json["results"], list), json @@ -73,8 +74,12 @@ def test_oonidata_list_observations_order_default(client): current_date = json["results"][i]["measurement_start_time"] assert previous_date >= current_date, f"The dates are not ordered: {previous_date} < {current_date}" -def test_oonidata_list_observations_order_asc(client): - response = client.get(f"{route}?order=ASC&since={since}&until={until}") + +def test_oonidata_list_observations_order_asc(client, params_since_and_until_with_two_days): + params = params_since_and_until_with_two_days + params["order"] = "ASC" + + response = client.get(route, params=params) json = response.json() assert isinstance(json["results"], list), json @@ -85,8 +90,9 @@ def test_oonidata_list_observations_order_asc(client): current_date = json["results"][i]["measurement_start_time"] assert previous_date <= current_date, f"The dates are not ordered: {previous_date} > {current_date}" + @pytest.mark.parametrize( - "order_param, order", + "field, order", [ ("input", "asc"), ("probe_cc", "asc"), @@ -98,30 +104,39 @@ def test_oonidata_list_observations_order_asc(client): ("test_name", "desc"), ] ) -def test_oonidata_list_observations_order_by_field(client, order_param, order): - response = client.get(f"{route}?order_by={order_param}&order={order}&since={since}&until={until}") +def test_oonidata_list_observations_order_by_field(client, field, order, params_since_and_until_with_two_days): + params = params_since_and_until_with_two_days + params['order_by'] = field + params['order'] = order + + response = client.get(route, params=params) json = response.json() assert isinstance(json["results"], list), json assert len(json["results"]) > 0 for i in range(1, len(json["results"])): - assert order_param in json["results"][i], json["results"][i] - previous = json["results"][i - 1][order_param] - current = json["results"][i][order_param] + assert field in json["results"][i], json["results"][i] + previous = json["results"][i - 1][field] + current = json["results"][i][field] if order == "asc": - assert previous <= current, f"The {order_param} values are not ordered in ascending order: {previous} > {current}" + assert previous <= current, f"The {field} values are not ordered in ascending order: {previous} > {current}" else: - assert previous >= current, f"The {order_param} values are not ordered in descending order: {previous} < {current}" + assert previous >= current, f"The {field} values are not ordered in descending order: {previous} < {current}" -def test_oonidata_list_observations_limit_by_default(client): - response = client.get(f"api/v1/observations?since={since}&until={until}") + +def test_oonidata_list_observations_limit_by_default(client, params_since_and_until_with_two_days): + response = client.get(route, params=params_since_and_until_with_two_days) json = response.json() assert isinstance(json["results"], list), json assert len(json["results"]) == 100 -def test_oonidata_list_observations_with_limit_and_offset(client): - response = client.get(f"{route}?limit=10&offset=10&since={since}&until={until}") + +def test_oonidata_list_observations_with_limit_and_offset(client, params_since_and_until_with_two_days): + params = params_since_and_until_with_two_days + params["limit"] = 10 + + response = client.get(route, params=params) json = response.json() assert isinstance(json["results"], list), json