json_db: fix StoredDict.__delitem__() to work similarly to .pop()
follow-up https://github.com/spesmilo/electrum/pull/10233 ("jsondb pointers")
This commit is contained in:
@@ -2,6 +2,7 @@ import contextlib
|
||||
import copy
|
||||
import traceback
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
import jsonpatch
|
||||
from jsonpatch import JsonPatchException
|
||||
@@ -88,6 +89,16 @@ class TestJsonpatch(ElectrumTestCase):
|
||||
fail_if_leaking_secret(ctx)
|
||||
|
||||
|
||||
def pop1_from_dict(d: dict, key: str) -> Any:
|
||||
return d.pop(key)
|
||||
|
||||
|
||||
def pop2_from_dict(d: dict, key: str) -> Any:
|
||||
val = d[key]
|
||||
del d[key]
|
||||
return val
|
||||
|
||||
|
||||
class TestJsonDB(ElectrumTestCase):
|
||||
|
||||
async def test_jsonpatch_replace_after_remove(self):
|
||||
@@ -109,31 +120,35 @@ class TestJsonDB(ElectrumTestCase):
|
||||
data = jpatch.apply(data)
|
||||
|
||||
async def test_jsondb_replace_after_remove(self):
|
||||
data = { 'a': {'b': {'c': 0}}, 'd': 3}
|
||||
db = JsonDB(repr(data))
|
||||
a = db.get_dict('a')
|
||||
# remove
|
||||
b = a.pop('b')
|
||||
self.assertEqual(len(db.pending_changes), 1)
|
||||
# replace item. this must not been written to db
|
||||
b['c'] = 42
|
||||
self.assertEqual(len(db.pending_changes), 1)
|
||||
patches = json.loads('[' + ','.join(db.pending_changes) + ']')
|
||||
jpatch = jsonpatch.JsonPatch(patches)
|
||||
data = jpatch.apply(data)
|
||||
self.assertEqual(data, {'a': {}, 'd': 3})
|
||||
for pop_from_dict in [pop1_from_dict, pop2_from_dict]:
|
||||
with self.subTest(pop_from_dict):
|
||||
data = { 'a': {'b': {'c': 0}}, 'd': 3}
|
||||
db = JsonDB(repr(data))
|
||||
a = db.get_dict('a')
|
||||
# remove
|
||||
b = pop_from_dict(a, 'b')
|
||||
self.assertEqual(len(db.pending_changes), 1)
|
||||
# replace item. this must not been written to db
|
||||
b['c'] = 42
|
||||
self.assertEqual(len(db.pending_changes), 1)
|
||||
patches = json.loads('[' + ','.join(db.pending_changes) + ']')
|
||||
jpatch = jsonpatch.JsonPatch(patches)
|
||||
data = jpatch.apply(data)
|
||||
self.assertEqual(data, {'a': {}, 'd': 3})
|
||||
|
||||
async def test_jsondb_replace_after_remove_nested(self):
|
||||
data = { 'a': {'b': {'c': 0}}, 'd': 3}
|
||||
db = JsonDB(repr(data))
|
||||
# remove
|
||||
a = db.data.pop('a')
|
||||
self.assertEqual(len(db.pending_changes), 1)
|
||||
b = a['b']
|
||||
# replace item. this must not be written to db
|
||||
b['c'] = 42
|
||||
self.assertEqual(len(db.pending_changes), 1)
|
||||
patches = json.loads('[' + ','.join(db.pending_changes) + ']')
|
||||
jpatch = jsonpatch.JsonPatch(patches)
|
||||
data = jpatch.apply(data)
|
||||
self.assertEqual(data, {'d': 3})
|
||||
for pop_from_dict in [pop1_from_dict, pop2_from_dict]:
|
||||
with self.subTest(pop_from_dict):
|
||||
data = { 'a': {'b': {'c': 0}}, 'd': 3}
|
||||
db = JsonDB(repr(data))
|
||||
# remove
|
||||
a = pop_from_dict(db.data, "a")
|
||||
self.assertEqual(len(db.pending_changes), 1)
|
||||
b = a['b']
|
||||
# replace item. this must not be written to db
|
||||
b['c'] = 42
|
||||
self.assertEqual(len(db.pending_changes), 1)
|
||||
patches = json.loads('[' + ','.join(db.pending_changes) + ']')
|
||||
jpatch = jsonpatch.JsonPatch(patches)
|
||||
data = jpatch.apply(data)
|
||||
self.assertEqual(data, {'d': 3})
|
||||
|
||||
Reference in New Issue
Block a user