Changeset View
Changeset View
Standalone View
Standalone View
swh/graph/tests/test_http_client.py
Show All 12 Lines | |||||
TEST_ORIGIN_ID = "swh:1:ori:{}".format( | TEST_ORIGIN_ID = "swh:1:ori:{}".format( | ||||
hashlib.sha1(b"https://example.com/swh/graph").hexdigest() | hashlib.sha1(b"https://example.com/swh/graph").hexdigest() | ||||
) | ) | ||||
def test_stats(graph_client): | def test_stats(graph_client): | ||||
stats = graph_client.stats() | stats = graph_client.stats() | ||||
assert stats["num_nodes"] == 21 | assert stats["num_nodes"] == 24 | ||||
assert stats["num_edges"] == 23 | assert stats["num_edges"] == 28 | ||||
assert isinstance(stats["compression_ratio"], float) | assert isinstance(stats["compression_ratio"], float) | ||||
assert isinstance(stats["bits_per_node"], float) | assert isinstance(stats["bits_per_node"], float) | ||||
assert isinstance(stats["bits_per_edge"], float) | assert isinstance(stats["bits_per_edge"], float) | ||||
assert isinstance(stats["avg_locality"], float) | assert isinstance(stats["avg_locality"], float) | ||||
assert stats["indegree_min"] == 0 | assert stats["indegree_min"] == 0 | ||||
assert stats["indegree_max"] == 3 | assert stats["indegree_max"] == 4 | ||||
assert isinstance(stats["indegree_avg"], float) | assert isinstance(stats["indegree_avg"], float) | ||||
assert stats["outdegree_min"] == 0 | assert stats["outdegree_min"] == 0 | ||||
assert stats["outdegree_max"] == 3 | assert stats["outdegree_max"] == 3 | ||||
assert isinstance(stats["outdegree_avg"], float) | assert isinstance(stats["outdegree_avg"], float) | ||||
def test_leaves(graph_client): | def test_leaves(graph_client): | ||||
actual = list(graph_client.leaves(TEST_ORIGIN_ID)) | actual = list(graph_client.leaves(TEST_ORIGIN_ID)) | ||||
Show All 27 Lines | |||||
def test_neighbors(graph_client): | def test_neighbors(graph_client): | ||||
actual = list( | actual = list( | ||||
graph_client.neighbors( | graph_client.neighbors( | ||||
"swh:1:rev:0000000000000000000000000000000000000009", direction="backward" | "swh:1:rev:0000000000000000000000000000000000000009", direction="backward" | ||||
) | ) | ||||
) | ) | ||||
expected = [ | expected = [ | ||||
"swh:1:snp:0000000000000000000000000000000000000022", | |||||
"swh:1:snp:0000000000000000000000000000000000000020", | "swh:1:snp:0000000000000000000000000000000000000020", | ||||
"swh:1:rel:0000000000000000000000000000000000000010", | "swh:1:rel:0000000000000000000000000000000000000010", | ||||
"swh:1:rev:0000000000000000000000000000000000000013", | "swh:1:rev:0000000000000000000000000000000000000013", | ||||
] | ] | ||||
assert set(actual) == set(expected) | assert set(actual) == set(expected) | ||||
def test_visit_nodes(graph_client): | def test_visit_nodes(graph_client): | ||||
▲ Show 20 Lines • Show All 139 Lines • ▼ Show 20 Lines | expected = [ | ||||
"swh:1:rev:0000000000000000000000000000000000000009", | "swh:1:rev:0000000000000000000000000000000000000009", | ||||
"swh:1:dir:0000000000000000000000000000000000000008", | "swh:1:dir:0000000000000000000000000000000000000008", | ||||
), | ), | ||||
( | ( | ||||
"swh:1:rev:0000000000000000000000000000000000000003", | "swh:1:rev:0000000000000000000000000000000000000003", | ||||
"swh:1:dir:0000000000000000000000000000000000000002", | "swh:1:dir:0000000000000000000000000000000000000002", | ||||
), | ), | ||||
] | ] | ||||
# As there are four valid answers (up to reordering), we cannot check for | # As there are four valid answers (up to reordering), we cannot check for | ||||
# equality. Instead, we check the client returned all edges but one. | # equality. Instead, we check the client returned either | ||||
# * all edges but one, or | |||||
# * all edges | |||||
# and the right answer depends on which edges were traversed, which is | |||||
# non-deterministic | |||||
assert set(actual).issubset(set(expected)) | assert set(actual).issubset(set(expected)) | ||||
assert len(actual) == 3 | assert 3 <= len(actual) <= 4 | ||||
def test_visit_edges_diamond_pattern(graph_client): | def test_visit_edges_diamond_pattern(graph_client): | ||||
actual = list( | actual = list( | ||||
graph_client.visit_edges( | graph_client.visit_edges( | ||||
"swh:1:rev:0000000000000000000000000000000000000009", | "swh:1:rev:0000000000000000000000000000000000000009", | ||||
edges="*", | edges="*", | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 140 Lines • ▼ Show 20 Lines | def test_count(graph_client): | ||||
assert actual == 4 | assert actual == 4 | ||||
actual = graph_client.count_visit_nodes( | actual = graph_client.count_visit_nodes( | ||||
"swh:1:rel:0000000000000000000000000000000000000010", edges="rel:rev,rev:rev" | "swh:1:rel:0000000000000000000000000000000000000010", edges="rel:rev,rev:rev" | ||||
) | ) | ||||
assert actual == 3 | assert actual == 3 | ||||
actual = graph_client.count_neighbors( | actual = graph_client.count_neighbors( | ||||
"swh:1:rev:0000000000000000000000000000000000000009", direction="backward" | "swh:1:rev:0000000000000000000000000000000000000009", direction="backward" | ||||
) | ) | ||||
assert actual == 3 | assert actual == 4 | ||||
@pytest.mark.parametrize("max_matching_nodes", [0, 1, 2, 3, 4, 5, 10, 1 << 31]) | @pytest.mark.parametrize("max_matching_nodes", [0, 1, 2, 3, 4, 5, 10, 1 << 31]) | ||||
def test_count_with_limit(graph_client, max_matching_nodes): | def test_count_with_limit(graph_client, max_matching_nodes): | ||||
actual = graph_client.count_leaves( | actual = graph_client.count_leaves( | ||||
TEST_ORIGIN_ID, max_matching_nodes=max_matching_nodes | TEST_ORIGIN_ID, max_matching_nodes=max_matching_nodes | ||||
) | ) | ||||
if max_matching_nodes == 0: | if max_matching_nodes == 0: | ||||
▲ Show 20 Lines • Show All 55 Lines • Show Last 20 Lines |