0ptr/nullptr/test_store.py

101 lines
2.4 KiB
Python
Raw Normal View History

2023-12-25 06:54:19 +00:00
import unittest
import tempfile
from nullptr.store import Store
from nullptr.models import Base
class Dummy(Base):
def define(self):
self.count: int = 0
self.data: str = ""
def update(self, d):
self.seta('count', d)
@classmethod
def ext(self):
return 'dum'
def f(self, detail=1):
r = super().f(detail)
if detail >2:
r += f' c:{self.count}'
return r
class TestStore(unittest.TestCase):
def setUp(self):
self.store_file = tempfile.NamedTemporaryFile()
self.s = Store(self.store_file.name, False)
def tearDown(self):
self.s.close()
self.store_file.close()
def reopen(self):
self.s.flush()
self.s.close()
self.s = Store(self.store_file.name, False)
def test_single(self):
dum = self.s.get(Dummy, "5", create=True)
dum.count = 1337
dum.data = "A" * 1000
self.reopen()
dum = self.s.get(Dummy, "5")
self.assertEqual(1337, dum.count)
def test_grow(self):
dum = self.s.get(Dummy, "5", create=True)
dum.data = "A"
dum2 = self.s.get(Dummy, "7",create=True)
self.reopen()
dum = self.s.get(Dummy, "5")
dum.data = "A" * 100
dum.count = 1337
self.reopen()
dum = self.s.get(Dummy, "5")
self.assertEqual(1337, dum.count)
def test_purge(self):
dum = self.s.get(Dummy, "5", create=True)
dum.data = "A"
dum2 = self.s.get(Dummy, "7",create=True)
dum2.count = 1337
self.s.flush()
self.s.purge(dum)
self.reopen()
dum2 = self.s.get(Dummy, "7")
self.assertEqual(1337, dum2.count)
def test_grow_last(self):
dum = self.s.get(Dummy, "5", create=True)
dum.data = "A"
dum2 = self.s.get(Dummy, "7",create=True)
self.reopen()
dum2 = self.s.get(Dummy, "7")
dum2.data = "A" * 100
dum2.count = 1337
dum3 = self.s.get(Dummy, "9",create=True)
dum3.count = 1338
self.reopen()
dum2 = self.s.get(Dummy, "7")
self.assertEqual(1337, dum2.count)
dum3 = self.s.get(Dummy, "9")
self.assertEqual(1338, dum3.count)
def test_purge_last(self):
dum = self.s.get(Dummy, "5", create=True)
dum.data = "A"
dum2 = self.s.get(Dummy, "7",create=True)
self.reopen()
dum2 = self.s.get(Dummy, "7")
self.s.purge(dum2)
dum3 = self.s.get(Dummy, "9",create=True)
dum3.count = 1338
self.reopen()
dum2 = self.s.get(Dummy, "7")
self.assertIsNone(dum2)
dum3 = self.s.get(Dummy, "9")
self.assertEqual(1338, dum3.count)