Skip to content

Commit

Permalink
Add E2E tests to aggregate observations endpoint and refactor with fi…
Browse files Browse the repository at this point in the history
…xture
  • Loading branch information
hynnot committed Jan 17, 2025
1 parent a57e53f commit 9d0c56f
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 122 deletions.
19 changes: 19 additions & 0 deletions ooniapi/services/oonimeasurements/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 0 additions & 33 deletions ooniapi/services/oonimeasurements/tests/test_oonidata.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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",
[
Expand All @@ -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)
Expand All @@ -45,20 +43,23 @@ 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
assert len(json["results"]) > 0
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
Expand All @@ -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
Expand All @@ -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"),
Expand All @@ -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
Expand Down
Loading

0 comments on commit 9d0c56f

Please sign in to comment.