Skip to content

Commit 49bd117

Browse files
committed
drop Unit type
1 parent faac8b7 commit 49bd117

5 files changed

Lines changed: 42 additions & 74 deletions

File tree

python/lib/sift_client/_internal/low_level_wrappers/units.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
ListUnitsRequest,
1010
ListUnitsResponse,
1111
)
12+
from sift.unit.v2.unit_pb2 import Unit as UnitProto
1213
from sift.unit.v2.unit_pb2_grpc import UnitServiceStub
1314

1415
from sift_client._internal.low_level_wrappers.base import DEFAULT_PAGE_SIZE, LowLevelClientBase
15-
from sift_client.sift_types.unit import Unit
1616
from sift_client.transport import WithGrpcClient
1717

1818
if TYPE_CHECKING:
@@ -36,7 +36,7 @@ def __init__(self, grpc_client: GrpcClient):
3636
"""
3737
super().__init__(grpc_client)
3838

39-
async def create_unit(self, name: str) -> Unit:
39+
async def create_unit(self, name: str) -> UnitProto:
4040
"""Create a new unit.
4141
4242
If a unit with the same name already exists, it is returned instead of creating a duplicate.
@@ -45,7 +45,7 @@ async def create_unit(self, name: str) -> Unit:
4545
name: The name of the unit.
4646
4747
Returns:
48-
The created Unit.
48+
The created unit proto, whose unit_id is used to reference the unit.
4949
5050
Raises:
5151
ValueError: If name is not provided.
@@ -55,8 +55,7 @@ async def create_unit(self, name: str) -> Unit:
5555

5656
request = CreateUnitRequest(name=name)
5757
response = await self._grpc_client.get_stub(UnitServiceStub).CreateUnit(request)
58-
grpc_unit = cast("CreateUnitResponse", response).unit
59-
return Unit._from_proto(grpc_unit)
58+
return cast("CreateUnitResponse", response).unit
6059

6160
async def list_units(
6261
self,
@@ -65,17 +64,17 @@ async def list_units(
6564
page_token: str | None = None,
6665
query_filter: str | None = None,
6766
order_by: str | None = None,
68-
) -> tuple[list[Unit], str]:
67+
) -> tuple[list[UnitProto], str]:
6968
"""List units with optional filtering and pagination.
7069
7170
Args:
7271
page_size: The maximum number of units to return.
7372
page_token: A page token for pagination.
74-
query_filter: A CEL filter string.
73+
query_filter: A CEL filter string (e.g. filtering on unit_id or abbreviated_name).
7574
order_by: How to order the retrieved units.
7675
7776
Returns:
78-
A tuple of (units, next_page_token).
77+
A tuple of (unit protos, next_page_token).
7978
"""
8079
request_kwargs: dict[str, Any] = {}
8180
if page_size is not None:
@@ -91,5 +90,4 @@ async def list_units(
9190
response = await self._grpc_client.get_stub(UnitServiceStub).ListUnits(request)
9291
response = cast("ListUnitsResponse", response)
9392

94-
units = [Unit._from_proto(unit) for unit in response.units]
95-
return units, response.next_page_token
93+
return list(response.units), response.next_page_token
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Tests for the Units low-level wrapper."""
2+
3+
from unittest.mock import AsyncMock, MagicMock
4+
5+
import pytest
6+
from sift.unit.v2.unit_pb2 import CreateUnitResponse
7+
from sift.unit.v2.unit_pb2 import Unit as UnitProto
8+
9+
from sift_client._internal.low_level_wrappers.units import UnitsLowLevelClient
10+
11+
12+
@pytest.mark.asyncio
13+
async def test_create_unit_rejects_empty_name():
14+
"""create_unit raises before making a request when name is empty."""
15+
client = UnitsLowLevelClient(grpc_client=MagicMock())
16+
17+
with pytest.raises(ValueError, match="name must be provided"):
18+
await client.create_unit("")
19+
20+
21+
@pytest.mark.asyncio
22+
async def test_create_unit_returns_created_unit_proto():
23+
"""create_unit unwraps the response and returns the unit proto (unit_id + abbreviated_name)."""
24+
stub = MagicMock()
25+
stub.CreateUnit = AsyncMock(
26+
return_value=CreateUnitResponse(unit=UnitProto(unit_id="u1", abbreviated_name="volts"))
27+
)
28+
grpc_client = MagicMock()
29+
grpc_client.get_stub.return_value = stub
30+
31+
unit = await UnitsLowLevelClient(grpc_client).create_unit("volts")
32+
33+
assert unit.unit_id == "u1"
34+
assert unit.abbreviated_name == "volts"

python/lib/sift_client/_tests/sift_types/test_unit.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

python/lib/sift_client/sift_types/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@
188188
TestStepCreate,
189189
TestStepType,
190190
)
191-
from sift_client.sift_types.unit import Unit
192191

193192
if "pytest" in sys.modules:
194193
# These are not test classes, so we need to set __test__ to False to avoid pytest warnings.
@@ -258,5 +257,4 @@
258257
"TestStep",
259258
"TestStepCreate",
260259
"TestStepType",
261-
"Unit",
262260
]

python/lib/sift_client/sift_types/unit.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)