|
1 | 1 | import pytest |
2 | 2 |
|
| 3 | +from arangoasync.errno import INVALID_DISJOINT_SMART_EDGE |
3 | 4 | from arangoasync.exceptions import ( |
4 | 5 | DocumentDeleteError, |
| 6 | + DocumentInsertError, |
5 | 7 | EdgeCollectionListError, |
6 | 8 | EdgeDefinitionDeleteError, |
7 | 9 | EdgeDefinitionListError, |
|
19 | 21 | from tests.helpers import generate_col_name, generate_graph_name |
20 | 22 |
|
21 | 23 |
|
| 24 | +@pytest.mark.asyncio |
| 25 | +@pytest.mark.parametrize("creation_api", ["options", "legacy"]) |
| 26 | +@pytest.mark.parametrize( |
| 27 | + "smart_field,disjoint", |
| 28 | + [(None, False), ("region", False), ("region", True)], |
| 29 | + ids=["enterprise", "smart", "disjoint-smart"], |
| 30 | +) |
| 31 | +async def test_graph_create_smart_options( |
| 32 | + db, cluster, skip_tests, smart_field, disjoint, creation_api |
| 33 | +): |
| 34 | + if "enterprise" in skip_tests: |
| 35 | + pytest.skip("Only for ArangoDB enterprise edition") |
| 36 | + if not cluster: |
| 37 | + pytest.skip("Only for ArangoDB clusters") |
| 38 | + |
| 39 | + # Create an EnterpriseGraph, SmartGraph, or Disjoint SmartGraph. |
| 40 | + graph_name = generate_graph_name() |
| 41 | + vertices = generate_col_name() |
| 42 | + edges = generate_col_name() |
| 43 | + options = GraphOptions( |
| 44 | + number_of_shards=3, |
| 45 | + smart_graph_attribute=smart_field, |
| 46 | + is_smart=True, |
| 47 | + is_disjoint=disjoint, |
| 48 | + ) |
| 49 | + kwargs = {} |
| 50 | + if creation_api == "legacy": |
| 51 | + options = GraphOptions( |
| 52 | + number_of_shards=3, |
| 53 | + smart_graph_attribute=smart_field, |
| 54 | + is_smart=False, |
| 55 | + is_disjoint=not disjoint, |
| 56 | + ) |
| 57 | + kwargs = {"is_smart": True, "is_disjoint": disjoint} |
| 58 | + original_options = options.to_dict().copy() |
| 59 | + graph = await db.create_graph( |
| 60 | + graph_name, |
| 61 | + edge_definitions=[{"collection": edges, "from": [vertices], "to": [vertices]}], |
| 62 | + options=options, |
| 63 | + **kwargs, |
| 64 | + ) |
| 65 | + try: |
| 66 | + assert options.to_dict() == original_options |
| 67 | + # Check that the server honored the graph creation options. |
| 68 | + properties = await graph.properties() |
| 69 | + assert properties.is_smart is True |
| 70 | + assert properties.is_disjoint is disjoint |
| 71 | + assert (properties.smart_graph_attribute or "") == (smart_field or "") |
| 72 | + assert properties.number_of_shards == 3 |
| 73 | + |
| 74 | + # Add two vertices in one region and a third in another. |
| 75 | + first = await graph.insert_vertex(vertices, {"region": "eu"}) |
| 76 | + second = await graph.insert_vertex(vertices, {"region": "eu"}) |
| 77 | + third = await graph.insert_vertex(vertices, {"region": "us"}) |
| 78 | + # All three graph types must allow edges within a region. |
| 79 | + edge = await graph.insert_edge( |
| 80 | + edges, {"_from": first["_id"], "_to": second["_id"]} |
| 81 | + ) |
| 82 | + assert await graph.has_edge(edge["_id"]) |
| 83 | + |
| 84 | + # Only the disjoint graph must reject edges between regions. |
| 85 | + cross_region_edge = {"_from": first["_id"], "_to": third["_id"]} |
| 86 | + if disjoint: |
| 87 | + with pytest.raises(DocumentInsertError) as err: |
| 88 | + await graph.insert_edge(edges, cross_region_edge) |
| 89 | + assert err.value.error_code == INVALID_DISJOINT_SMART_EDGE |
| 90 | + else: |
| 91 | + edge = await graph.insert_edge(edges, cross_region_edge) |
| 92 | + assert await graph.has_edge(edge["_id"]) |
| 93 | + finally: |
| 94 | + # Remove the graph and its collections even if an assertion fails. |
| 95 | + await db.delete_graph(graph_name, drop_collections=True) |
| 96 | + |
| 97 | + |
22 | 98 | @pytest.mark.asyncio |
23 | 99 | async def test_graph_basic(db, bad_db): |
24 | 100 | graph1_name = generate_graph_name() |
@@ -60,8 +136,8 @@ async def test_graph_properties(db, bad_graph, cluster, skip_tests): |
60 | 136 | # Create a graph |
61 | 137 | name = generate_graph_name() |
62 | 138 | is_smart = cluster and "enterprise" not in skip_tests |
63 | | - options = GraphOptions(number_of_shards=3) |
64 | | - graph = await db.create_graph(name, is_smart=is_smart, options=options) |
| 139 | + options = GraphOptions(number_of_shards=3, is_smart=is_smart) |
| 140 | + graph = await db.create_graph(name, options=options) |
65 | 141 |
|
66 | 142 | with pytest.raises(GraphPropertiesError): |
67 | 143 | await bad_graph.properties() |
|
0 commit comments