Skip to content

Commit c98d9f0

Browse files
authored
Merge pull request #774 from simvue-io/hotfix/fix-obj-get-count
Fix count limiting on object retrieval
2 parents 976b8f2 + 5f047de commit c98d9f0

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

simvue/api/objects/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def ids(
365365
"""
366366
_class_instance = cls(_read_only=True, _local=True)
367367
_count: int = 0
368-
for response in cls._get_all_objects(offset):
368+
for response in cls._get_all_objects(offset, count=count):
369369
if (_data := response.get("data")) is None:
370370
raise RuntimeError(
371371
f"Expected key 'data' for retrieval of {_class_instance.__class__.__name__.lower()}s"
@@ -404,7 +404,7 @@ def get(
404404
"""
405405
_class_instance = cls(_read_only=True, _local=True)
406406
_count: int = 0
407-
for _response in cls._get_all_objects(offset, **kwargs):
407+
for _response in cls._get_all_objects(offset, count=count, **kwargs):
408408
if count and _count > count:
409409
return
410410
if (_data := _response.get("data")) is None:
@@ -438,7 +438,7 @@ def count(cls, **kwargs) -> int:
438438

439439
@classmethod
440440
def _get_all_objects(
441-
cls, offset: int | None, **kwargs
441+
cls, offset: int | None, count: int | None, **kwargs
442442
) -> typing.Generator[dict, None, None]:
443443
_class_instance = cls(_read_only=True)
444444
_url = f"{_class_instance._base_url}"
@@ -448,7 +448,7 @@ def _get_all_objects(
448448
_label = _label[:-1]
449449

450450
for response in get_paginated(
451-
_url, headers=_class_instance._headers, offset=offset, **kwargs
451+
_url, headers=_class_instance._headers, offset=offset, count=count, **kwargs
452452
):
453453
yield get_json_from_response(
454454
response=response,

simvue/api/request.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ def get_paginated(
281281
timeout: int = DEFAULT_API_TIMEOUT,
282282
json: dict[str, typing.Any] | None = None,
283283
offset: int | None = None,
284+
count: int | None = None,
284285
**params,
285286
) -> typing.Generator[requests.Response, None, None]:
286287
"""Paginate results of a server query.
@@ -309,7 +310,7 @@ def get_paginated(
309310
url=url,
310311
headers=headers,
311312
params=(params or {})
312-
| {"count": MAX_ENTRIES_PER_PAGE, "start": _offset},
313+
| {"count": count or MAX_ENTRIES_PER_PAGE, "start": _offset},
313314
timeout=timeout,
314315
json=json,
315316
)
@@ -320,4 +321,5 @@ def get_paginated(
320321
yield _response
321322
_offset += MAX_ENTRIES_PER_PAGE
322323

323-
yield _response
324+
if count and _offset > count:
325+
break

tests/unit/test_folder.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,32 @@ def test_folder_creation_offline() -> None:
3838

3939
with _folder._local_staging_file.open() as in_f:
4040
_local_data = json.load(in_f)
41-
41+
4242
assert _folder._local_staging_file.name.split(".")[0] == _folder.id
4343
assert _local_data.get("path", None) == _path
44-
44+
4545
sender(_folder._local_staging_file.parents[1], 2, 10, ["folders"])
4646
time.sleep(1)
4747
client = Client()
48-
48+
4949
_folder_new = client.get_folder(_path)
5050
assert _folder_new.path == _path
51-
51+
5252
_folder_new.delete()
53-
53+
5454
assert not _folder._local_staging_file.exists()
5555

5656

57+
@pytest.mark.api
58+
@pytest.mark.online
59+
def test_get_folder() -> None:
60+
_uuid: str = f"{uuid.uuid4()}".split("-")[0]
61+
_folder_name = f"/simvue_unit_testing/{_uuid}"
62+
_folder_1 = Folder.new(path=f"{_folder_name}/dir_1")
63+
_folder_2 = Folder.new(path=f"{_folder_name}/dir_2")
64+
assert len(list(Folder._get_all_objects(count=2, offset=None))) == 2
65+
66+
5767
@pytest.mark.api
5868
@pytest.mark.online
5969
def test_folder_modification_online() -> None:
@@ -85,34 +95,34 @@ def test_folder_modification_offline() -> None:
8595
_tags = ["testing", "api"]
8696
_folder = Folder.new(path=_path, offline=True)
8797
_folder.commit()
88-
98+
8999
sender(_folder._local_staging_file.parents[1], 2, 10, ["folders"])
90100
time.sleep(1)
91-
101+
92102
client = Client()
93103
_folder_online = client.get_folder(_path)
94104
assert _folder_online.path == _path
95-
105+
96106
_folder_new = Folder(identifier=_folder.id)
97107
_folder_new.read_only(False)
98108
_folder_new.tags = _tags
99109
_folder_new.description = _description
100110
_folder_new.commit()
101-
111+
102112
with _folder._local_staging_file.open() as in_f:
103113
_local_data = json.load(in_f)
104114
assert _folder._local_staging_file.name.split(".")[0] == _folder.id
105115
assert _local_data.get("description", None) == _description
106116
assert _local_data.get("tags", None) == _tags
107-
117+
108118
sender(_folder._local_staging_file.parents[1], 2, 10, ["folders"])
109119
time.sleep(1)
110-
120+
111121
_folder_online.refresh()
112122
assert _folder_online.path == _path
113123
assert _folder_online.description == _description
114124
assert _folder_online.tags == _tags
115-
125+
116126
_folder_online.read_only(False)
117127
_folder_online.delete()
118128

tests/unit/test_run.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ def test_run_creation_offline() -> None:
3939
_local_data = json.load(in_f)
4040
assert _local_data.get("name") == f"simvue_offline_run_{_uuid}"
4141
assert _local_data.get("folder") == _folder_name
42-
42+
4343
sender(_run._local_staging_file.parents[1], 1, 10, ["folders", "runs"])
4444
time.sleep(1)
45-
45+
4646
# Get online ID and retrieve run
4747
_online_id = _run._local_staging_file.parents[1].joinpath("server_ids", f"{_run._local_staging_file.name.split('.')[0]}.txt").read_text()
4848
_online_run = Run(_online_id)
49-
49+
5050
assert _online_run.name == _run_name
5151
assert _online_run.folder == _folder_name
52-
52+
5353
_run.delete()
5454
_run._local_staging_file.parents[1].joinpath("server_ids", f"{_run._local_staging_file.name.split('.')[0]}.txt").unlink()
5555
client = Client()
@@ -117,40 +117,51 @@ def test_run_modification_offline() -> None:
117117
assert _new_run.ttl == 120
118118
assert _new_run.description == "Simvue test run"
119119
assert _new_run.name == "simvue_test_run"
120-
120+
121121
sender(_run._local_staging_file.parents[1], 1, 10, ["folders", "runs"])
122122
time.sleep(1)
123-
123+
124124
# Get online ID and retrieve run
125125
_online_id = _run._local_staging_file.parents[1].joinpath("server_ids", f"{_run._local_staging_file.name.split('.')[0]}.txt").read_text()
126126
_online_run = Run(_online_id)
127-
127+
128128
assert _online_run.ttl == 120
129129
assert _online_run.description == "Simvue test run"
130130
assert _online_run.name == "simvue_test_run"
131131
assert _online_run.folder == _folder_name
132-
132+
133133
# Now add a new set of tags in offline mode and send
134134
_new_run.tags = ["simvue", "test", "tag"]
135135
_new_run.commit()
136-
136+
137137
# Shouldn't yet be available in the online run since it hasnt been sent
138138
_online_run.refresh()
139139
assert _online_run.tags == []
140-
140+
141141
sender(_run._local_staging_file.parents[1], 1, 10, ["folders", "runs"])
142142
time.sleep(1)
143-
143+
144144
_online_run.refresh()
145145
assert sorted(_new_run.tags) == sorted(["simvue", "test", "tag"])
146146
assert sorted(_online_run.tags) == sorted(["simvue", "test", "tag"])
147-
147+
148148
_run.delete()
149149
_run._local_staging_file.parents[1].joinpath("server_ids", f"{_run._local_staging_file.name.split('.')[0]}.txt").unlink()
150150
client = Client()
151151
client.delete_folder(_folder_name, recursive=True, remove_runs=True)
152152

153153

154+
@pytest.mark.api
155+
@pytest.mark.online
156+
def test_get_run() -> None:
157+
_uuid: str = f"{uuid.uuid4()}".split("-")[0]
158+
_folder_name = f"/simvue_unit_testing/{_uuid}"
159+
_folder = Folder.new(path=_folder_name)
160+
_run_1 = Run.new(folder=_folder_name)
161+
_run_2 = Run.new(folder=_folder_name)
162+
assert len(list(Run._get_all_objects(count=2, offset=None))) == 2
163+
164+
154165
@pytest.mark.api
155166
@pytest.mark.online
156167
def test_run_get_properties() -> None:

0 commit comments

Comments
 (0)