| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- import pytest
- from gpustack.schemas import ModelInstance
- from gpustack.schemas.models import (
- DistributedServers,
- ModelInstanceSubordinateWorker,
- ModelInstanceStateEnum,
- )
- from gpustack.utils.attrs import get_attr, set_attr
- @pytest.mark.parametrize(
- "o, path, expected",
- [
- # Dict access
- (
- {"a": {"b": {"c": 42}}},
- "a.b.c",
- 42,
- ),
- # Dict access with list index
- (
- {"a": [{"b": {"c": 42}}]},
- "a.0.b.c",
- 42,
- ),
- # None access
- (
- None,
- "a.b.c",
- None,
- ),
- # Dict access with on-existent path
- (
- {"a": {"b": {"c": 42}}},
- "a.b.d",
- None,
- ),
- # List access
- (
- [1, 2, 3],
- "0",
- 1,
- ),
- # List of dicts access
- (
- [{"a": 1}, {"b": 2}],
- "0.a",
- 1,
- ),
- # Complex object access
- (
- ModelInstance(
- distributed_servers=DistributedServers(
- subordinate_workers=[
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.3",
- ),
- ],
- ),
- ),
- "distributed_servers.subordinate_workers.0.worker_ip",
- "192.168.50.3",
- ),
- # Complex object access with non-existent path
- (
- ModelInstance(
- distributed_servers=DistributedServers(
- subordinate_workers=[
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.3",
- ),
- ],
- ),
- ),
- "distributed_servers.subordinate_workers.0.name",
- None,
- ),
- ],
- )
- @pytest.mark.unit
- def test_get_attr(o, path, expected):
- actual = get_attr(o, path)
- assert (
- actual == expected
- ), f"Expected {expected} but got {actual} for path '{path}' in object {o}"
- @pytest.mark.parametrize(
- "o, path, value, expected",
- [
- # Dict access
- (
- {"a": {"b": {"c": 42}}},
- "a.b.c",
- 100,
- {"a": {"b": {"c": 100}}},
- ),
- # Dict access with list index
- (
- {"a": [{"b": {"c": 42}}]},
- "a.0.b.c",
- 100,
- {"a": [{"b": {"c": 100}}]},
- ),
- # None access
- (
- None,
- "a.b.c",
- 100,
- None,
- ),
- # Dict access with non-existent path: insert new item
- (
- {"a": {"b": {"c": 42}}},
- "a.b.d",
- 100,
- {"a": {"b": {"c": 42, "d": 100}}},
- ),
- # Dict access with non-existent path: nothing to do
- (
- {"a": {"b": {"c": 42}}},
- "a.d.c",
- 100,
- {"a": {"b": {"c": 42}}},
- ),
- # List access
- (
- [1, 2, 3],
- "0",
- 100,
- [100, 2, 3],
- ),
- # List of dicts access
- (
- [{"a": 1}, {"b": 2}],
- "0.a",
- 100,
- [{"a": 100}, {"b": 2}],
- ),
- # Complex object access
- (
- ModelInstance(
- distributed_servers=DistributedServers(
- subordinate_workers=[
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.3",
- state=ModelInstanceStateEnum.RUNNING,
- ),
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.5",
- state=ModelInstanceStateEnum.ERROR,
- ),
- ],
- ),
- ),
- "distributed_servers.subordinate_workers.0.worker_ip",
- "192.168.50.4",
- ModelInstance(
- distributed_servers=DistributedServers(
- subordinate_workers=[
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.4",
- state=ModelInstanceStateEnum.RUNNING,
- ),
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.5",
- state=ModelInstanceStateEnum.ERROR,
- ),
- ],
- ),
- ),
- ),
- # Complex object access: replace an item
- (
- ModelInstance(
- distributed_servers=DistributedServers(
- subordinate_workers=[
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.3",
- state=ModelInstanceStateEnum.RUNNING,
- ),
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.5",
- state=ModelInstanceStateEnum.ERROR,
- ),
- ],
- ),
- ),
- "distributed_servers.subordinate_workers.-1",
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.4",
- ),
- ModelInstance(
- distributed_servers=DistributedServers(
- subordinate_workers=[
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.3",
- state=ModelInstanceStateEnum.RUNNING,
- ),
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.4",
- ),
- ],
- ),
- ),
- ),
- # Complex object access with non-existent path: insert new item
- (
- ModelInstance(
- distributed_servers=DistributedServers(
- subordinate_workers=[
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.3",
- state=ModelInstanceStateEnum.RUNNING,
- ),
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.5",
- state=ModelInstanceStateEnum.ERROR,
- ),
- ],
- ),
- ),
- "distributed_servers.subordinate_workers.2",
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.4",
- ),
- ModelInstance(
- distributed_servers=DistributedServers(
- subordinate_workers=[
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.3",
- state=ModelInstanceStateEnum.RUNNING,
- ),
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.5",
- state=ModelInstanceStateEnum.ERROR,
- ),
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.4",
- ),
- ],
- ),
- ),
- ),
- # Complex object access with non-existent path: nothing to do
- (
- ModelInstance(
- distributed_servers=DistributedServers(
- subordinate_workers=[
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.3",
- state=ModelInstanceStateEnum.RUNNING,
- ),
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.5",
- state=ModelInstanceStateEnum.ERROR,
- ),
- ],
- ),
- ),
- "distributed_servers.subordinate_workers.0.name",
- "test",
- ModelInstance(
- distributed_servers=DistributedServers(
- subordinate_workers=[
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.3",
- state=ModelInstanceStateEnum.RUNNING,
- ),
- ModelInstanceSubordinateWorker(
- worker_ip="192.168.50.5",
- state=ModelInstanceStateEnum.ERROR,
- ),
- ],
- ),
- ),
- ),
- ],
- )
- @pytest.mark.unit
- def test_set_attr(o, path, value, expected):
- set_attr(o, path, value)
- actual = o
- assert (
- actual == expected
- ), f"Expected {expected} but got {actual} for path '{path}' in object {o}"
|