Skip to content

Commit

Permalink
Fix tests, annotations, signatures based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tibor-reiss committed Sep 25, 2024
1 parent 784d9df commit 7bb00eb
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 76 deletions.
159 changes: 83 additions & 76 deletions integration/test_cluster.py
Original file line number Diff line number Diff line change
@@ -1,95 +1,102 @@
import pytest
from contextlib import contextmanager
from typing import Generator, List

import weaviate
from weaviate.collections.classes.config import (
Configure,
DataType,
Property,
)
from weaviate.util import parse_version_string


COLLECTION_NAME_PREFIX = "Collection_test_cluster"
NODE_NAME = "node1"
NUM_OBJECT = 10


@pytest.fixture(scope="module")
def client():
@contextmanager
def get_weaviate_client(
collection_names: List[str],
) -> Generator[weaviate.WeaviateClient, None, None]:
client = weaviate.connect_to_local()
client.collections.delete_all()
for collection_name in collection_names:
client.collections.delete(collection_name)
yield client
client.collections.delete_all()
for collection_name in collection_names:
client.collections.delete(collection_name)
client.close()


def test_rest_nodes_without_data(client: weaviate.WeaviateClient):
def test_rest_nodes_without_data() -> None:
"""get nodes status without data"""
resp = client.cluster.rest_nodes(output="verbose")
assert len(resp) == 1
assert "gitHash" in resp[0]
assert resp[0]["name"] == NODE_NAME
assert resp[0]["shards"] is None
assert resp[0]["stats"]["objectCount"] == 0
assert resp[0]["stats"]["shardCount"] == 0
assert resp[0]["status"] == "HEALTHY"
assert "version" in resp[0]


def test_rest_nodes_with_data(client: weaviate.WeaviateClient):
with get_weaviate_client([]) as client:
resp = client.cluster.rest_nodes(output="verbose")
assert len(resp) == 1
assert "gitHash" in resp[0]
assert resp[0]["name"] == NODE_NAME
assert resp[0]["shards"] is None
assert resp[0]["stats"]["objectCount"] == 0
assert resp[0]["stats"]["shardCount"] == 0
assert resp[0]["status"] == "HEALTHY"
assert "version" in resp[0]


def test_rest_nodes_with_data() -> None:
"""get nodes status with data"""
collection_name_1 = "Collection_1"
uncap_collection_name_1 = "collection_1"
collection = client.collections.create(
name=collection_name_1,
properties=[Property(name="Name", data_type=DataType.TEXT)],
vectorizer_config=Configure.Vectorizer.none(),
)
collection.data.insert_many([{"Name": f"name {i}"} for i in range(NUM_OBJECT)])

collection_name_2 = "Collection_2"
collection = client.collections.create(
name=collection_name_2,
properties=[Property(name="Name", data_type=DataType.TEXT)],
vectorizer_config=Configure.Vectorizer.none(),
)
collection.data.insert_many([{"Name": f"name {i}"} for i in range(NUM_OBJECT * 2)])

# server behaviour changed by https://github.com/weaviate/weaviate/pull/4203
server_is_at_least_124 = parse_version_string(
client.get_meta()["version"]
) > parse_version_string("1.24")

resp = client.cluster.rest_nodes(output="verbose")
assert len(resp) == 1
assert "gitHash" in resp[0]
assert resp[0]["name"] == NODE_NAME
assert len(resp[0]["shards"]) == 2
assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT * 3
assert resp[0]["stats"]["shardCount"] == 2
assert resp[0]["status"] == "HEALTHY"
assert "version" in resp[0]

shards = sorted(resp[0]["shards"], key=lambda x: x["class"])
assert shards[0]["class"] == collection_name_1
assert shards[0]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT
assert shards[1]["class"] == collection_name_2
assert shards[1]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT * 2

resp = client.cluster.rest_nodes(collection=collection_name_1, output="verbose")
assert len(resp) == 1
assert "gitHash" in resp[0]
assert resp[0]["name"] == NODE_NAME
assert len(resp[0]["shards"]) == 1
assert resp[0]["stats"]["shardCount"] == 1
assert resp[0]["status"] == "HEALTHY"
assert "version" in resp[0]
assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT

resp = client.cluster.rest_nodes(uncap_collection_name_1, output="verbose")
assert len(resp) == 1
assert "gitHash" in resp[0]
assert resp[0]["name"] == NODE_NAME
assert len(resp[0]["shards"]) == 1
assert resp[0]["stats"]["shardCount"] == 1
assert resp[0]["status"] == "HEALTHY"
assert "version" in resp[0]
assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT
collection_name_1 = f"{COLLECTION_NAME_PREFIX}_rest_nodes_with_data_1"
collection_name_2 = f"{COLLECTION_NAME_PREFIX}_rest_nodes_with_data_2"
uncap_collection_name_1 = collection_name_1[0].lower() + collection_name_1[1:]

with get_weaviate_client([collection_name_1, collection_name_2]) as client:
collection = client.collections.create(
name=collection_name_1,
properties=[Property(name="Name", data_type=DataType.TEXT)],
vectorizer_config=Configure.Vectorizer.none(),
)
collection.data.insert_many([{"Name": f"name {i}"} for i in range(NUM_OBJECT)])

collection = client.collections.create(
name=collection_name_2,
properties=[Property(name="Name", data_type=DataType.TEXT)],
vectorizer_config=Configure.Vectorizer.none(),
)
collection.data.insert_many([{"Name": f"name {i}"} for i in range(NUM_OBJECT * 2)])

# server behaviour changed by https://github.com/weaviate/weaviate/pull/4203
server_is_at_least_124 = client._connection._weaviate_version.is_at_least(1, 24, 0)

resp = client.cluster.rest_nodes(output="verbose")
assert len(resp) == 1
assert "gitHash" in resp[0]
assert resp[0]["name"] == NODE_NAME
assert resp[0]["shards"] is not None and len(resp[0]["shards"]) == 2
assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT * 3
assert resp[0]["stats"]["shardCount"] == 2
assert resp[0]["status"] == "HEALTHY"
assert "version" in resp[0]

shards = sorted(resp[0]["shards"], key=lambda x: x["class"])
assert shards[0]["class"] == collection_name_1
assert shards[0]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT
assert shards[1]["class"] == collection_name_2
assert shards[1]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT * 2

resp = client.cluster.rest_nodes(collection=collection_name_1, output="verbose")
assert len(resp) == 1
assert "gitHash" in resp[0]
assert resp[0]["name"] == NODE_NAME
assert resp[0]["shards"] is not None and len(resp[0]["shards"]) == 1
assert resp[0]["stats"]["shardCount"] == 1
assert resp[0]["status"] == "HEALTHY"
assert "version" in resp[0]
assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT

resp = client.cluster.rest_nodes(uncap_collection_name_1, output="verbose")
assert len(resp) == 1
assert "gitHash" in resp[0]
assert resp[0]["name"] == NODE_NAME
assert resp[0]["shards"] is not None and len(resp[0]["shards"]) == 1
assert resp[0]["stats"]["shardCount"] == 1
assert resp[0]["status"] == "HEALTHY"
assert "version" in resp[0]
assert resp[0]["stats"]["objectCount"] == 0 if server_is_at_least_124 else NUM_OBJECT
6 changes: 6 additions & 0 deletions weaviate/collections/cluster/sync.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Literal, Optional, overload

from weaviate.cluster.types import Node as NodeREST
from weaviate.collections.classes.cluster import Node, Shards, Stats
from weaviate.collections.cluster.cluster import _ClusterBase

Expand All @@ -25,3 +26,8 @@ class _Cluster(_ClusterBase):
*,
output: Literal["verbose"],
) -> List[Node[Shards, Stats]]: ...
def rest_nodes(
self,
collection: Optional[str] = None,
output: Optional[Literal["minimal", "verbose"]] = None,
) -> List[NodeREST]: ...

0 comments on commit 7bb00eb

Please sign in to comment.