Skip to content

Commit

Permalink
tests fix
Browse files Browse the repository at this point in the history
  • Loading branch information
matveyvarg committed Jul 30, 2024
1 parent cadb1bc commit 0bc8e81
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions deker_server_adapters/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
EXCEPTION_CLASS_PARAM_NAME = "class"
LAST_MODIFIED_HEADER = "last-modified"
REBALANCING_STATUS = "rebalancing"
NORMAL_STATUS = "normal"


class ArrayType(Enum):
Expand Down
3 changes: 2 additions & 1 deletion deker_server_adapters/hash_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def get_node(self, string_key: str) -> T: # type: ignore[type-var]
"""
pos = self.get_node_pos(string_key)
if pos is None:
raise HashRingError(f"Couldn't find a position in {self.ring}")
msg = f"Couldn't find a position in {self.ring}"
raise HashRingError(msg)
return self.ring[self._sorted_keys[pos]]

def get_node_pos(self, string_key: str) -> Optional[int]:
Expand Down
11 changes: 8 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from deker_server_adapters.array_adapter import ServerArrayAdapter
from deker_server_adapters.cluster_config import apply_config
from deker_server_adapters.collection_adapter import ServerCollectionAdapter
from deker_server_adapters.consts import LAST_MODIFIED_HEADER
from deker_server_adapters.consts import LAST_MODIFIED_HEADER, NORMAL_STATUS, REBALANCING_STATUS
from deker_server_adapters.factory import AdaptersFactory
from deker_server_adapters.hash_ring import HashRing
from deker_server_adapters.httpx_client import HttpxClient
Expand All @@ -38,6 +38,11 @@ def mode(request) -> str:
return request.param


@pytest.fixture(params=[REBALANCING_STATUS, NORMAL_STATUS])
def status(request) -> str:
return request.param


@pytest.fixture()
def base_uri(mode, base_cluster_uri):
if mode == SINGLE_MODE:
Expand Down Expand Up @@ -236,6 +241,6 @@ def array_url_path(array: Array) -> str:


@pytest.fixture()
def mock_status(mode: str, request: pytest.FixtureRequest):
if mode == CLUSTER_MODE:
def mock_status(mode: str, request: pytest.FixtureRequest, status: str):
if mode == CLUSTER_MODE and status == REBALANCING_STATUS:
request.getfixturevalue("mocked_filestatus_check_unmoved")
15 changes: 9 additions & 6 deletions tests/plugins/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from deker.uri import Uri
from pytest_httpx import HTTPXMock

from deker_server_adapters.consts import REBALANCING_STATUS
from deker_server_adapters.models import Status


Expand All @@ -18,14 +19,14 @@ def nodes() -> List[Dict]:
"host": "localhost",
"port": 8000,
"protocol": "http",
"storage": "file:///tmp/deker_server"
"storage": "file:///tmp/deker_server",
},
{
"id": "8381202B-8C95-487A-B9B5-0B5270568040",
"host": "localhost",
"port": 8012,
"protocol": "http",
"storage": "file:///tmp/deker_server"
"storage": "file:///tmp/deker_server",
},
]

Expand All @@ -48,13 +49,14 @@ def base_cluster_uri(nodes_urls):


@pytest.fixture()
def mocked_ping(nodes: List[Dict]) -> Dict:
def mocked_ping(nodes: List[Dict], status: str) -> Dict:
return {
"mode": "cluster",
"cluster_status": status,
"this_id": "8381202B-8C95-487A-B9B5-0B527056804E",
"leader_id": "8381202B-8C95-487A-B9B5-0B527056804E",
"current": nodes,
"raft": nodes
"raft": nodes,
}


Expand All @@ -64,5 +66,6 @@ def mock_healthcheck(httpx_mock: HTTPXMock, mocked_ping):


@pytest.fixture()
def mocked_filestatus_check_unmoved(httpx_mock: HTTPXMock):
httpx_mock.add_response(method="GET", url=re.compile(r".*\/status.*"), text=Status.UNMOVED.value)
def mocked_filestatus_check_unmoved(httpx_mock: HTTPXMock, status: str):
if status == REBALANCING_STATUS:
httpx_mock.add_response(method="GET", url=re.compile(r".*\/status.*"), text=Status.UNMOVED.value)
7 changes: 5 additions & 2 deletions tests/test_cases/test_cluster/test_array_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re

from typing import TYPE_CHECKING, List
from unittest.mock import patch

import httpx
import numpy as np
Expand All @@ -15,6 +14,7 @@
from deker_server_adapters.array_adapter import ServerArrayAdapter
from deker_server_adapters.consts import LAST_MODIFIED_HEADER
from deker_server_adapters.errors import FilteringByIdInClusterIsForbidden
from deker_server_adapters.models import Status
from deker_server_adapters.utils.hashing import get_hash_key


Expand Down Expand Up @@ -78,7 +78,7 @@ def test_filter_by_id_is_not_allowed(collection_with_primary_attributes):
collection_with_primary_attributes.filter({"id": "foo"}).last()


def test_hash_updated(httpx_mock: HTTPXMock, server_array_adapter: ServerArrayAdapter, mocked_ping, array):
def test_hash_updated(httpx_mock: HTTPXMock, server_array_adapter: ServerArrayAdapter, mocked_ping, array, status: str):
class RequestCounter:
def __init__(self):
self.count = 0
Expand All @@ -96,6 +96,9 @@ def limited_mock_response(request, extensions, request_counter, mocked_ping, dat
else:
return httpx.Response(200, json=data)

elif "status" in str(request.url):
return httpx.Response(text=Status.UNMOVED.value, status_code=200)

return httpx.Response(404, json={"error": "Not found"})

httpx_mock.add_callback(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_cases/test_cluster/test_varray_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from deker_server_adapters.array_adapter import ServerArrayAdapter
from deker_server_adapters.consts import LAST_MODIFIED_HEADER
from deker_server_adapters.errors import FilteringByIdInClusterIsForbidden
from deker_server_adapters.models import Status
from deker_server_adapters.utils.hashing import get_hash_key
from deker_server_adapters.varray_adapter import ServerVarrayAdapter

Expand Down Expand Up @@ -102,6 +103,9 @@ def limited_mock_response(request, extensions, request_counter, mocked_ping, dat
else:
return httpx.Response(200, json=data)

elif "status" in str(request.url):
return httpx.Response(text=Status.UNMOVED.value)

return httpx.Response(404, json={"error": "Not found"})

httpx_mock.add_callback(
Expand Down

0 comments on commit 0bc8e81

Please sign in to comment.