Skip to content

Commit

Permalink
#86dt6db2x - Improve new get methods to avoid returning misleading se…
Browse files Browse the repository at this point in the history
…ntinel values
  • Loading branch information
Mirella de Medeiros committed Apr 30, 2024
1 parent 1adf40b commit 441cd41
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
12 changes: 12 additions & 0 deletions boa3_test/test_sc/interop_test/storage/StorageTryGetExists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from boa3.builtin.compile_time import public
from boa3.sc.storage import try_get_int, put_int


@public
def put_value(key: bytes, value: int):
put_int(key, value)


@public
def get_value(key: bytes) -> bool:
return try_get_int(key)[1]
12 changes: 12 additions & 0 deletions boa3_test/test_sc/interop_test/storage/StorageTryGetValue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from boa3.builtin.compile_time import public
from boa3.sc.storage import try_get_int, put_int


@public
def put_value(key: bytes, value: int):
put_int(key, value)


@public
def get_value(key: bytes) -> int:
return try_get_int(key)[0]
50 changes: 48 additions & 2 deletions boa3_test/tests/compiler_tests/test_interop/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def test_storage_get_str_key(self):
def test_storage_get_mismatched_type(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StorageGetMismatchedType.py')

async def test_storage_get_check(self):
await self.set_up_contract('StorageGetCheck.py', compile_if_found=True)
async def test_storage_try_get(self):
await self.set_up_contract('StorageTryGet.py')

key = b'example_key'
value = 42
Expand All @@ -80,6 +80,52 @@ async def test_storage_get_check(self):
self.assertIn(key, contract_storage)
self.assertEqual(value, contract_storage[key])

async def test_storage_try_get_value(self):
await self.set_up_contract('StorageTryGetValue.py')

key = b'example_key'
value = 42

expected = 0
result, _ = await self.call('get_value', [key], return_type=int)
self.assertEqual(expected, result)

contract_storage = await self.get_storage(values_post_processor=storage.as_int)
self.assertNotIn(key, contract_storage)

result, _ = await self.call('put_value', [key, value], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)

result, _ = await self.call('get_value', [key], return_type=int)
self.assertEqual(value, result)

contract_storage = await self.get_storage(values_post_processor=storage.as_int)
self.assertIn(key, contract_storage)
self.assertEqual(value, contract_storage[key])

async def test_storage_try_get_exists(self):
await self.set_up_contract('StorageTryGetExists.py')

key = b'example_key'
value = 42

contract_storage = await self.get_storage(values_post_processor=storage.as_int)
self.assertNotIn(key, contract_storage)

expected = key in contract_storage
result, _ = await self.call('get_value', [key], return_type=bool)
self.assertEqual(expected, result)

result, _ = await self.call('put_value', [key, value], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)

contract_storage = await self.get_storage(values_post_processor=storage.as_int)
self.assertIn(key, contract_storage)

expected = key in contract_storage
result, _ = await self.call('get_value', [key], return_type=bool)
self.assertEqual(expected, result)

async def test_storage_put_bytes_key_bytes_value(self):
await self.set_up_contract('StoragePutBytesKeyBytesValue.py')

Expand Down

0 comments on commit 441cd41

Please sign in to comment.