From d0a310ba8c1930a8e22a2424ca7d5df6a62fb3a0 Mon Sep 17 00:00:00 2001 From: Peter Neuroth Date: Mon, 23 Oct 2023 18:45:08 +0200 Subject: [PATCH] test: Add test for datastoreusage Signed-off-by: Peter Neuroth --- tests/test_misc.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_misc.py b/tests/test_misc.py index 1a13e545acc2..092d9fac98ec 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -3316,6 +3316,35 @@ def test_datastore_keylist(node_factory): 'hex': b'ab2val2'.hex()}]} +def test_datastoreusage(node_factory): + l1: LightningNode = node_factory.get_node() + data = 'somedatatostoreinthedatastore' # len 29 + l1.rpc.datastore(key=["a", "b"], string=data) + assert l1.rpc.datastoreusage() == {'datastoreusage': {'key': '[]', 'total_bytes': (29 + 1 + 1)}} + assert l1.rpc.datastoreusage(key="a") == {'datastoreusage': {'key': '[a]', 'total_bytes': (29 + 1 + 1)}} + assert l1.rpc.datastoreusage(key=["a", "b"]) == {'datastoreusage': {'key': '[a,b]', 'total_bytes': (29 + 1 + 1)}} + + # add second leaf + l1.rpc.datastore(key=["a", "c"], string=data) + assert l1.rpc.datastoreusage() == {'datastoreusage': {'key': '[]', 'total_bytes': (29 + 1 + 1 + 29 + 1 + 1)}} + assert l1.rpc.datastoreusage(key=["a", "b"]) == {'datastoreusage': {'key': '[a,b]', 'total_bytes': (29 + 1 + 1)}} + assert l1.rpc.datastoreusage(key=["a", "c"]) == {'datastoreusage': {'key': '[a,c]', 'total_bytes': (29 + 1 + 1)}} + + # check that the key is also counted as stored data + l1.rpc.datastore(key=["a", "thisissomelongkeythattriestostore46bytesofdata"], string=data) + assert l1.rpc.datastoreusage() == {'datastoreusage': {'key': '[]', 'total_bytes': (29 + 1 + 46 + 62)}} + assert l1.rpc.datastoreusage(key=["a", "thisissomelongkeythattriestostore46bytesofdata"]) == {'datastoreusage': {'key': '[a,thisissomelongkeythattriestostore46bytesofdata]', 'total_bytes': (29 + 1 + 46)}} + + # check that the root is also counted + l1.rpc.datastore(key=["thisissomelongkeythattriestostore46bytesofdata", "a"], string=data) + assert l1.rpc.datastoreusage(key=["thisissomelongkeythattriestostore46bytesofdata", "a"]) == {'datastoreusage': {'key': '[thisissomelongkeythattriestostore46bytesofdata,a]', 'total_bytes': (29 + 1 + 46)}} + + # check really deep data + l1.rpc.datastore(key=["a", "d", "e", "f", "g"], string=data) + assert l1.rpc.datastoreusage(key=["a", "d", "e", "f", "g"]) == {'datastoreusage': {'key': '[a,d,e,f,g]', 'total_bytes': (29 + 1 + 1 + 1 + 1 + 1)}} + assert l1.rpc.datastoreusage() == {'datastoreusage': {'key': '[]', 'total_bytes': (29 + 1 + 1 + 1 + 1 + 1 + 214)}} + + @unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "This test requires sqlite3") def test_torv2_in_db(node_factory):