Skip to content

Commit

Permalink
validate setasdefault
Browse files Browse the repository at this point in the history
  • Loading branch information
trisongz committed Jan 2, 2024
1 parent b2d82ba commit a3aad68
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ lazyops/libs/stream_unzip/constants.py
tests/test_shelve.db.db
lazyops/libs/unzip_stream*
lazyops/libs/jsonstate*
tests/local.cache
tests/local.hash
tests/local.lock
2 changes: 1 addition & 1 deletion lazyops/libs/persistence/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def setdefault(self, key: str, default: Any = None):
if self.contains(key):
return self.get(key)
self.set(key, default)
return default
return default

async def asetdefault(self, key: str, default: Any = None):
"""
Expand Down
3 changes: 2 additions & 1 deletion lazyops/libs/persistence/backends/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def get(self, key: str, default: Optional[Any] = None, **kwargs) -> Optional[Any
self.sync()
if value := self.cache.get(self.get_key(key)):
try:
return self.decode_value(value, **kwargs) or default
result = self.decode_value(value, **kwargs)
return result if result is not None else default
except Exception as e:
logger.info(f'Error Decoding Value: |r|({type(value)}) {e}|e| {value}', colored = True)
self.delete(key)
Expand Down
7 changes: 4 additions & 3 deletions lazyops/libs/persistence/serializers/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
except ImportError:
_ujson_available = False

if _orjson_available:
default_json = orjson

elif _simdjson_available:
if _simdjson_available:
default_json = simdjson

elif _orjson_available:
default_json = orjson

elif _ujson_available:
default_json = ujson

Expand Down
2 changes: 1 addition & 1 deletion lazyops/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.2.64rc0'
VERSION = '0.2.64rc1'
33 changes: 32 additions & 1 deletion tests/test_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

from pydantic import BaseModel
from lazyops.libs.persistence import PersistentDict
from lazyops.libs.persistence.main import _DEBUG_ENABLED

_DEBUG_ENABLED = True

class X(BaseModel):
n: int
Expand Down Expand Up @@ -271,10 +274,18 @@ def test_json_advanced(backend: str = 'local', compression_level: int = None, is
i_int = random.randint(0, 1000)
y[f'x{i}'] = i_int
d[f'x{i}'] = X(n = i_int)


d.setdefault('xy', {})
d['xy'].update({
'x': 1,
'y': 2,
})

print(f'[{backend} - {compression_level} - {kwargs} - {d.base_key}] test json advanced: ', y, '=', list(d.keys()))
for k,v in d.items():
if k == 'xy':
print(f'[{backend} - {compression_level} - {kwargs} - {d.base_key}] test json advanced: {k} {v} {type(v)}')
continue
assert y[k] == v.n if is_obj else v['n'], f'[{backend} - {compression_level} - {kwargs}] test json advanced: {k} {y[k]} {v} {type(v)}'
d.clear()
assert len(d) == 0
Expand Down Expand Up @@ -303,6 +314,13 @@ def test_msgpack_advanced(backend: str = 'local', compression_level: int = None,
d[f'x{i}'] = X(n = i_int)


d.setdefault('xy', {})
d['xy'].update({
'x': 1,
'y': 2,
})


print(f'[{backend} - {d.base_key}] mutating: ', d['x5'].n, '=', d['x5'].n + 1)
d['x5'].n += 1
print(f'[{backend} - {d.base_key}] mutated: ', d['x5'].n)
Expand All @@ -317,6 +335,9 @@ def test_msgpack_advanced(backend: str = 'local', compression_level: int = None,

print(f'[{backend} - {compression_level} - {kwargs} - {d.base_key}] test msgpack advanced: ', y, '=', list(d.keys()))
for k,v in d.items():
if k == 'xy':
print(f'[{backend} - {compression_level} - {kwargs} - {d.base_key}] test msgpack advanced: {k} {v} {type(v)}')
continue
assert y[k] == v.n, f'[{backend} - {compression_level} - {kwargs}] test msgpack advanced: {k} {y[k]} {v} {type(v)}'
d.clear()
assert len(d) == 0
Expand All @@ -342,6 +363,12 @@ def test_pickle_advanced(backend: str = 'local', compression_level: int = None,
y[f'x{i}'] = i_int
d[f'x{i}'] = X(n = i_int)

d.setdefault('xy', {})
d['xy'].update({
'x': 1,
'y': 2,
})

print(f'[{backend} - {d.base_key}] mutating: ', d['x5'].n, '=', d['x5'].n + 1)
d['x5'].n += 1
print(f'[{backend} - {d.base_key}] mutated: ', d['x5'].n)
Expand All @@ -357,6 +384,10 @@ def test_pickle_advanced(backend: str = 'local', compression_level: int = None,

print(f'[{backend} - {compression_level} - {kwargs} - {d.base_key}] test pickle advanced: ', y, '=', list(d.keys()))
for k,v in d.items():
if k == 'xy':
print(f'[{backend} - {compression_level} - {kwargs} - {d.base_key}] test msgpack advanced: {k} {v} {type(v)}')
continue

assert y[k] == v.n, f'[{backend} - {compression_level} - {kwargs}] test pickle advanced: {k} {y[k]} {v} {type(v)}'
for key in d:
print(f'[{backend} - {compression_level} - {kwargs} - {d.base_key}] test pickle advanced: {key} {d[key]} {type(d[key])}')
Expand Down

0 comments on commit a3aad68

Please sign in to comment.