MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_datastore

Function test_datastore

tests/test_misc.py:4005–4117  ·  view source on GitHub ↗
(node_factory)

Source from the content-addressed store, hash-verified

4003
4004
4005def test_datastore(node_factory):
4006 # Suppress plugins that use the datastore (keep list empty for assertions below).
4007 l1 = node_factory.get_node(options={"disable-plugin": ["cln-xpay", "bookkeeper", "bwatch"]})
4008
4009 # Starts empty
4010 assert l1.rpc.listdatastore() == {'datastore': []}
4011 assert l1.rpc.listdatastore('somekey') == {'datastore': []}
4012
4013 # Fail on empty array
4014 with pytest.raises(RpcError, match='should not be empty'):
4015 l1.rpc.listdatastore([])
4016
4017 # Add entries.
4018 somedata = b'somedata'.hex()
4019 somedata_expect = {'key': ['somekey'],
4020 'generation': 0,
4021 'hex': somedata,
4022 'string': 'somedata'}
4023
4024 # We should fail trying to insert into an empty array
4025 with pytest.raises(RpcError, match='should not be empty'):
4026 l1.rpc.datastore(key=[], hex=somedata)
4027
4028 assert l1.rpc.datastore(key='somekey', hex=somedata) == somedata_expect
4029
4030 assert l1.rpc.listdatastore() == {'datastore': [somedata_expect]}
4031 assert l1.rpc.listdatastore('somekey') == {'datastore': [somedata_expect]}
4032 assert l1.rpc.listdatastore('otherkey') == {'datastore': []}
4033
4034 # Cannot add by default.
4035 with pytest.raises(RpcError, match='already exists'):
4036 l1.rpc.datastore(key='somekey', hex=somedata)
4037
4038 with pytest.raises(RpcError, match='already exists'):
4039 l1.rpc.datastore(key='somekey', hex=somedata, mode="must-create")
4040
4041 # But can insist on replace.
4042 l1.rpc.datastore(key='somekey', hex=somedata[:-4], mode="must-replace")
4043 assert only_one(l1.rpc.listdatastore('somekey')['datastore'])['hex'] == somedata[:-4]
4044 # And append works.
4045 l1.rpc.datastore(key='somekey', hex=somedata[-4:-2], mode="must-append")
4046 assert only_one(l1.rpc.listdatastore('somekey')['datastore'])['hex'] == somedata[:-2]
4047 l1.rpc.datastore(key='somekey', hex=somedata[-2:], mode="create-or-append")
4048 assert only_one(l1.rpc.listdatastore('somekey')['datastore'])['hex'] == somedata
4049
4050 # Generation will have increased due to three ops above.
4051 somedata_expect['generation'] += 3
4052 assert l1.rpc.listdatastore() == {'datastore': [somedata_expect]}
4053
4054 # Can't replace or append non-existing records if we say not to
4055 with pytest.raises(RpcError, match='does not exist'):
4056 l1.rpc.datastore(key='otherkey', hex=somedata, mode="must-replace")
4057
4058 with pytest.raises(RpcError, match='does not exist'):
4059 l1.rpc.datastore(key='otherkey', hex=somedata, mode="must-append")
4060
4061 otherdata = b'otherdata'.hex()
4062 otherdata_expect = {'key': ['otherkey'],

Callers

nothing calls this directly

Calls 6

only_oneFunction · 0.90
listdatastoreMethod · 0.80
deldatastoreMethod · 0.80
restartMethod · 0.80
get_nodeMethod · 0.45
datastoreMethod · 0.45

Tested by

no test coverage detected