import logging
import unittest
import tempfile
import uuid
import shutil

from whee.fs import FsStore

logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()


class TestFs(unittest.TestCase):

    def setUp(self):
        self.base = tempfile.mkdtemp()
        uu = uuid.uuid4()
        self.store = FsStore(base=self.base, dbname=str(uu), levels=2)


    def tearDown(self):
        shutil.rmtree(self.base)


    def test_get_put(self):
        r = self.store.have(b'foo')
        self.assertFalse(r)
        self.store.put(b'foo', b'bar')
        r = self.store.have(b'foo')
        self.assertTrue(r)
        r = self.store.get(b'foo')
        self.assertEqual(r, b'bar')
        r = self.store.get(b'foo'.hex())
        self.assertEqual(r, b'bar')
        with self.assertRaises(FileExistsError):
            self.store.put(b'foo', b'baz')
        self.store.put(b'foo', b'baz', exist_ok=True)
        self.store.delete(b'foo')
        with self.assertRaises(FileNotFoundError):
            self.store.delete(b'foo')


    def test_short(self):
        self.store.put(b'f', b'oo')
        self.store.put(b'fo', b'o')
        self.store.put(b'foo', b'')


if __name__ == '__main__':
    unittest.main()
