Skip to content

Commit 75cc43d

Browse files
committed
use RuntimeError for when the box does not exists
1 parent 552553e commit 75cc43d

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

src/algopy_testing/models/box.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ def __bool__(self) -> bool:
4545
def key(self) -> algopy.Bytes:
4646
"""Provides access to the raw storage key"""
4747
if not self._key:
48-
raise ValueError("Box key is empty")
48+
raise RuntimeError("Box key is empty")
4949
return self._key
5050

5151
@property
5252
def value(self) -> _TValue:
5353
"""Retrieve the contents of the box. Fails if the box has not been created."""
5454
context = get_test_context()
5555
if not context.does_box_exist(self.key):
56-
raise ValueError("Box has not been created")
56+
raise RuntimeError("Box has not been created")
5757
return _cast_to_value_type(self._type, context.get_box(self.key))
5858

5959
@value.setter
@@ -100,7 +100,7 @@ def length(self) -> algopy.UInt64:
100100

101101
context = get_test_context()
102102
if not context.does_box_exist(self.key):
103-
raise ValueError("Box has not been created")
103+
raise RuntimeError("Box has not been created")
104104
return algopy.UInt64(len(context.get_box(self.key)))
105105

106106

@@ -129,7 +129,7 @@ def __bool__(self) -> bool:
129129
def key(self) -> algopy.Bytes:
130130
"""Provides access to the raw storage key"""
131131
if not self._key:
132-
raise ValueError("Box key is empty")
132+
raise RuntimeError("Box key is empty")
133133

134134
return self._key
135135

@@ -178,7 +178,7 @@ def extract(
178178
start_int = int(start_index)
179179
length_int = int(length)
180180
if not box_exists:
181-
raise ValueError("Box does not exist")
181+
raise RuntimeError("Box has not been created")
182182
if (start_int + length_int) > len(box_content):
183183
raise ValueError("Index out of bounds")
184184
result = box_content[start_int : start_int + length_int]
@@ -198,7 +198,7 @@ def resize(self, new_size: algopy.UInt64 | int) -> None:
198198
raise ValueError(f"Box size cannot exceed {MAX_BOX_SIZE}")
199199
box_content, box_exists = self._maybe()
200200
if not box_exists:
201-
raise ValueError("Box has not been created")
201+
raise RuntimeError("Box has not been created")
202202
if new_size_int > len(box_content):
203203
updated_content = box_content + b"\x00" * (new_size_int - len(box_content))
204204
else:
@@ -216,7 +216,7 @@ def replace(self, start_index: algopy.UInt64 | int, value: algopy.Bytes | bytes)
216216
context = get_test_context()
217217
box_content, box_exists = self._maybe()
218218
if not box_exists:
219-
raise ValueError("Box has not been created")
219+
raise RuntimeError("Box has not been created")
220220
start = int(start_index)
221221
length = len(value)
222222
if (start + length) > len(box_content):
@@ -252,7 +252,7 @@ def splice(
252252
insert_content = value.value if isinstance(value, algopy.Bytes) else value
253253

254254
if not box_exists:
255-
raise ValueError("Box has not been created")
255+
raise RuntimeError("Box has not been created")
256256

257257
if start > len(box_content):
258258
raise ValueError("Start index exceeds box size")
@@ -329,7 +329,7 @@ def length(self) -> algopy.UInt64:
329329

330330
box_content, box_exists = self._maybe()
331331
if not box_exists:
332-
raise ValueError("Box has not been created")
332+
raise RuntimeError("Box has not been created")
333333
return algopy.UInt64(len(box_content))
334334

335335

@@ -371,7 +371,7 @@ def __init__(
371371
def key_prefix(self) -> algopy.Bytes:
372372
"""Provides access to the raw storage key-prefix"""
373373
if not self._key_prefix:
374-
raise ValueError("Box key prefix is empty")
374+
raise RuntimeError("Box key prefix is empty")
375375
return self._key_prefix
376376

377377
def __getitem__(self, key: _TKey) -> _TValue:
@@ -380,7 +380,7 @@ def __getitem__(self, key: _TKey) -> _TValue:
380380
"""
381381
box_content, box_exists = self.maybe(key)
382382
if not box_exists:
383-
raise ValueError("Box has not been created")
383+
raise RuntimeError("Box has not been created")
384384
return box_content
385385

386386
def __setitem__(self, key: _TKey, value: _TValue) -> None:
@@ -442,7 +442,7 @@ def length(self, key: _TKey) -> algopy.UInt64:
442442
key_bytes = self._full_key(key)
443443
box_exists = context.does_box_exist(key_bytes)
444444
if not box_exists:
445-
raise ValueError("Box has not been created")
445+
raise RuntimeError("Box has not been created")
446446
box_content_bytes = context.get_box(key_bytes)
447447
return algopy.UInt64(len(box_content_bytes))
448448

src/algopy_testing/op.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ def extract(
10631063
length = int(c)
10641064
box_content = context.get_box(name_bytes)
10651065
if not box_content:
1066-
raise ValueError("Box does not exist")
1066+
raise RuntimeError("Box does not exist")
10671067
result = box_content[start : start + length]
10681068
return algopy.Bytes(result)
10691069

@@ -1111,7 +1111,7 @@ def replace(
11111111
new_content = c.value if isinstance(c, algopy.Bytes) else c
11121112
box_content = context.get_box(name_bytes)
11131113
if not box_content:
1114-
raise ValueError("Box does not exist")
1114+
raise RuntimeError("Box does not exist")
11151115
if start + len(new_content) > len(box_content):
11161116
raise ValueError("Replacement content exceeds box size")
11171117
updated_content = (
@@ -1130,7 +1130,7 @@ def resize(a: algopy.Bytes | bytes, b: algopy.UInt64 | int, /) -> None:
11301130
raise ValueError("Invalid box name or size")
11311131
box_content = context.get_box(name_bytes)
11321132
if not box_content:
1133-
raise ValueError("Box does not exist")
1133+
raise RuntimeError("Box does not exist")
11341134
if new_size > len(box_content):
11351135
updated_content = box_content + b"\x00" * (new_size - len(box_content))
11361136
else:
@@ -1155,7 +1155,7 @@ def splice(
11551155
box_content = context.get_box(name_bytes)
11561156

11571157
if not box_content:
1158-
raise ValueError("Box does not exist")
1158+
raise RuntimeError("Box does not exist")
11591159

11601160
if start > len(box_content):
11611161
raise ValueError("Start index exceeds box size")

tests/models/test_box.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ def test_init_with_key(
5959
)
6060
assert box.key == key_bytes
6161

62-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
62+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
6363
_ = box.value
6464

65-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
65+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
6666
_ = box.length
6767

6868

@@ -117,7 +117,7 @@ def test_value_deleter(
117117
del box.value
118118
assert not box
119119

120-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
120+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
121121
_ = box.value
122122

123123
op_box_content, op_box_exists = algopy.op.Box.get(key)

tests/models/test_box_map.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ def test_init_with_key_prefix(
6161
)
6262
assert box.key_prefix == key_prefix_bytes
6363

64-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
64+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
6565
_ = box[key_type()]
6666

67-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
67+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
6868
_ = box.length(key_type())
6969

7070

@@ -134,7 +134,7 @@ def test_value_deleter(
134134

135135
del box[key]
136136

137-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
137+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
138138
_ = box[key]
139139

140140
full_key = box._full_key(key)

tests/models/test_box_ref.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_init_with_key(
5454
)
5555
assert box.key == key_bytes
5656

57-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
57+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
5858
_ = box.length
5959

6060

@@ -101,13 +101,13 @@ def test_delete(
101101
assert box_existed
102102
assert not box
103103

104-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
104+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
105105
_ = box.length
106106

107-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
107+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
108108
box.resize(10)
109109

110-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
110+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
111111
box.replace(0, b"\x11")
112112

113113
assert not box.delete()
@@ -191,7 +191,7 @@ def test_replace_when_box_does_not_exists(
191191
) -> None:
192192
box = BoxRef(key=TEST_BOX_KEY)
193193

194-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
194+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
195195
box.replace(0, b"\x11")
196196

197197

@@ -377,7 +377,7 @@ def test_splice_when_box_does_not_exist(
377377
) -> None:
378378
box = BoxRef(key=TEST_BOX_KEY)
379379

380-
with pytest.raises(ValueError, match=BOX_NOT_CREATED_ERROR):
380+
with pytest.raises(RuntimeError, match=BOX_NOT_CREATED_ERROR):
381381
box.splice(0, 1, b"\x11")
382382

383383

0 commit comments

Comments
 (0)