|
1 | | -import pytest |
| 1 | +from contextlib import contextmanager |
| 2 | +from typing import Generator, List |
2 | 3 |
|
3 | 4 | import weaviate |
4 | 5 | from weaviate.collections.classes.config import ( |
5 | 6 | Configure, |
6 | 7 | DataType, |
7 | 8 | Property, |
8 | 9 | ) |
9 | | -from weaviate.util import parse_version_string |
10 | 10 |
|
11 | 11 |
|
| 12 | +COLLECTION_NAME_PREFIX = "Collection_test_cluster" |
12 | 13 | NODE_NAME = "node1" |
13 | 14 | NUM_OBJECT = 10 |
14 | 15 |
|
15 | 16 |
|
16 | | -@pytest.fixture(scope="module") |
17 | | -def client(): |
| 17 | +@contextmanager |
| 18 | +def get_weaviate_client( |
| 19 | + collection_names: List[str], |
| 20 | +) -> Generator[weaviate.WeaviateClient, None, None]: |
18 | 21 | client = weaviate.connect_to_local() |
19 | | - client.collections.delete_all() |
| 22 | + for collection_name in collection_names: |
| 23 | + client.collections.delete(collection_name) |
20 | 24 | yield client |
21 | | - client.collections.delete_all() |
| 25 | + for collection_name in collection_names: |
| 26 | + client.collections.delete(collection_name) |
| 27 | + client.close() |
22 | 28 |
|
23 | 29 |
|
24 | | -def test_rest_nodes_without_data(client: weaviate.WeaviateClient): |
| 30 | +def test_rest_nodes_without_data() -> None: |
25 | 31 | """get nodes status without data""" |
26 | | - resp = client.cluster.rest_nodes(output="verbose") |
27 | | - assert len(resp) == 1 |
28 | | - assert "gitHash" in resp[0] |
29 | | - assert resp[0]["name"] == NODE_NAME |
30 | | - assert resp[0]["shards"] is None |
31 | | - assert resp[0]["stats"]["objectCount"] == 0 |
32 | | - assert resp[0]["stats"]["shardCount"] == 0 |
33 | | - assert resp[0]["status"] == "HEALTHY" |
34 | | - assert "version" in resp[0] |
35 | | - |
36 | | - |
37 | | -def test_rest_nodes_with_data(client: weaviate.WeaviateClient): |
| 32 | + with get_weaviate_client([]) as client: |
| 33 | + resp = client.cluster.rest_nodes(output="verbose") |
| 34 | + assert len(resp) == 1 |
| 35 | + assert "gitHash" in resp[0] |
| 36 | + assert resp[0]["name"] == NODE_NAME |
| 37 | + assert resp[0]["shards"] is None |
| 38 | + assert resp[0]["stats"]["objectCount"] == 0 |
| 39 | + assert resp[0]["stats"]["shardCount"] == 0 |
| 40 | + assert resp[0]["status"] == "HEALTHY" |
| 41 | + assert "version" in resp[0] |
| 42 | + |
| 43 | + |
| 44 | +def test_rest_nodes_with_data() -> None: |
38 | 45 | """get nodes status with data""" |
39 | | - collection_name_1 = "Collection_1" |
40 | | - uncap_collection_name_1 = "collection_1" |
41 | | - collection = client.collections.create( |
42 | | - name=collection_name_1, |
43 | | - properties=[Property(name="Name", data_type=DataType.TEXT)], |
44 | | - vectorizer_config=Configure.Vectorizer.none(), |
45 | | - ) |
46 | | - collection.data.insert_many([{"Name": f"name {i}"} for i in range(NUM_OBJECT)]) |
47 | | - |
48 | | - collection_name_2 = "Collection_2" |
49 | | - collection = client.collections.create( |
50 | | - name=collection_name_2, |
51 | | - properties=[Property(name="Name", data_type=DataType.TEXT)], |
52 | | - vectorizer_config=Configure.Vectorizer.none(), |
53 | | - ) |
54 | | - collection.data.insert_many([{"Name": f"name {i}"} for i in range(NUM_OBJECT * 2)]) |
55 | | - |
56 | | - # server behaviour changed by https://github.com/weaviate/weaviate/pull/4203 |
57 | | - server_is_at_least_124 = parse_version_string( |
58 | | - client.get_meta()["version"] |
59 | | - ) > parse_version_string("1.24") |
60 | | - |
61 | | - resp = client.cluster.rest_nodes(output="verbose") |
62 | | - assert len(resp) == 1 |
63 | | - assert "gitHash" in resp[0] |
64 | | - assert resp[0]["name"] == NODE_NAME |
65 | | - assert len(resp[0]["shards"]) == 2 |
66 | | - assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT * 3 |
67 | | - assert resp[0]["stats"]["shardCount"] == 2 |
68 | | - assert resp[0]["status"] == "HEALTHY" |
69 | | - assert "version" in resp[0] |
70 | | - |
71 | | - shards = sorted(resp[0]["shards"], key=lambda x: x["class"]) |
72 | | - assert shards[0]["class"] == collection_name_1 |
73 | | - assert shards[0]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT |
74 | | - assert shards[1]["class"] == collection_name_2 |
75 | | - assert shards[1]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT * 2 |
76 | | - |
77 | | - resp = client.cluster.rest_nodes(collection=collection_name_1, output="verbose") |
78 | | - assert len(resp) == 1 |
79 | | - assert "gitHash" in resp[0] |
80 | | - assert resp[0]["name"] == NODE_NAME |
81 | | - assert len(resp[0]["shards"]) == 1 |
82 | | - assert resp[0]["stats"]["shardCount"] == 1 |
83 | | - assert resp[0]["status"] == "HEALTHY" |
84 | | - assert "version" in resp[0] |
85 | | - assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT |
86 | | - |
87 | | - resp = client.cluster.rest_nodes(uncap_collection_name_1, output="verbose") |
88 | | - assert len(resp) == 1 |
89 | | - assert "gitHash" in resp[0] |
90 | | - assert resp[0]["name"] == NODE_NAME |
91 | | - assert len(resp[0]["shards"]) == 1 |
92 | | - assert resp[0]["stats"]["shardCount"] == 1 |
93 | | - assert resp[0]["status"] == "HEALTHY" |
94 | | - assert "version" in resp[0] |
95 | | - assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT |
| 46 | + collection_name_1 = f"{COLLECTION_NAME_PREFIX}_rest_nodes_with_data_1" |
| 47 | + collection_name_2 = f"{COLLECTION_NAME_PREFIX}_rest_nodes_with_data_2" |
| 48 | + uncap_collection_name_1 = collection_name_1[0].lower() + collection_name_1[1:] |
| 49 | + |
| 50 | + with get_weaviate_client([collection_name_1, collection_name_2]) as client: |
| 51 | + collection = client.collections.create( |
| 52 | + name=collection_name_1, |
| 53 | + properties=[Property(name="Name", data_type=DataType.TEXT)], |
| 54 | + vectorizer_config=Configure.Vectorizer.none(), |
| 55 | + ) |
| 56 | + collection.data.insert_many([{"Name": f"name {i}"} for i in range(NUM_OBJECT)]) |
| 57 | + |
| 58 | + collection = client.collections.create( |
| 59 | + name=collection_name_2, |
| 60 | + properties=[Property(name="Name", data_type=DataType.TEXT)], |
| 61 | + vectorizer_config=Configure.Vectorizer.none(), |
| 62 | + ) |
| 63 | + collection.data.insert_many([{"Name": f"name {i}"} for i in range(NUM_OBJECT * 2)]) |
| 64 | + |
| 65 | + # server behaviour changed by https://github.com/weaviate/weaviate/pull/4203 |
| 66 | + server_is_at_least_124 = client._connection._weaviate_version.is_at_least(1, 24, 0) |
| 67 | + |
| 68 | + resp = client.cluster.rest_nodes(output="verbose") |
| 69 | + assert len(resp) == 1 |
| 70 | + assert "gitHash" in resp[0] |
| 71 | + assert resp[0]["name"] == NODE_NAME |
| 72 | + assert resp[0]["shards"] is not None and len(resp[0]["shards"]) == 2 |
| 73 | + assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT * 3 |
| 74 | + assert resp[0]["stats"]["shardCount"] == 2 |
| 75 | + assert resp[0]["status"] == "HEALTHY" |
| 76 | + assert "version" in resp[0] |
| 77 | + |
| 78 | + shards = sorted(resp[0]["shards"], key=lambda x: x["class"]) |
| 79 | + assert shards[0]["class"] == collection_name_1 |
| 80 | + assert shards[0]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT |
| 81 | + assert shards[1]["class"] == collection_name_2 |
| 82 | + assert shards[1]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT * 2 |
| 83 | + |
| 84 | + resp = client.cluster.rest_nodes(collection=collection_name_1, output="verbose") |
| 85 | + assert len(resp) == 1 |
| 86 | + assert "gitHash" in resp[0] |
| 87 | + assert resp[0]["name"] == NODE_NAME |
| 88 | + assert resp[0]["shards"] is not None and len(resp[0]["shards"]) == 1 |
| 89 | + assert resp[0]["stats"]["shardCount"] == 1 |
| 90 | + assert resp[0]["status"] == "HEALTHY" |
| 91 | + assert "version" in resp[0] |
| 92 | + assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT |
| 93 | + |
| 94 | + resp = client.cluster.rest_nodes(uncap_collection_name_1, output="verbose") |
| 95 | + assert len(resp) == 1 |
| 96 | + assert "gitHash" in resp[0] |
| 97 | + assert resp[0]["name"] == NODE_NAME |
| 98 | + assert resp[0]["shards"] is not None and len(resp[0]["shards"]) == 1 |
| 99 | + assert resp[0]["stats"]["shardCount"] == 1 |
| 100 | + assert resp[0]["status"] == "HEALTHY" |
| 101 | + assert "version" in resp[0] |
| 102 | + assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT |
0 commit comments