From c3395677552b8178ae107f5595b8da83f542fb2a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 20 Oct 2018 11:30:58 +0300 Subject: Add: Store --- muck/__init__.py | 1 + muck/store.py | 30 +++++++++++ muck/store_tests.py | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 muck/store.py create mode 100644 muck/store_tests.py diff --git a/muck/__init__.py b/muck/__init__.py index 69831f1..d14c8e1 100644 --- a/muck/__init__.py +++ b/muck/__init__.py @@ -23,3 +23,4 @@ from .change import ( ) from .mem import MemoryStore from .pers import PersistentStore +from .store import Store diff --git a/muck/store.py b/muck/store.py new file mode 100644 index 0000000..59a74da --- /dev/null +++ b/muck/store.py @@ -0,0 +1,30 @@ +# Copyright (C) 2018 Lars Wirzenius +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +import muck + + +class Store: + + def __init__(self, dirname): + self._pers = muck.PersistentStore(dirname) + self._ms = muck.MemoryStore() + + def change(self, chg): + self._ms.change(chg) + self._pers.append_change(chg) + + def get_memory_store(self): + return self._ms diff --git a/muck/store_tests.py b/muck/store_tests.py new file mode 100644 index 0000000..eee729b --- /dev/null +++ b/muck/store_tests.py @@ -0,0 +1,147 @@ +# Copyright (C) 2018 Lars Wirzenius +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +import shutil +import tempfile +import unittest + +import muck + + +class StoreTests(unittest.TestCase): + + def setUp(self): + self.tempdir = tempfile.mkdtemp() + self.st = muck.Store(self.tempdir) + + def tearDown(self): + shutil.rmtree(self.tempdir) + + def test_is_initially_empty(self): + ms = self.st.get_memory_store() + self.assertEqual(len(ms), 0) + self.assertEqual(ms.as_dict(), {}) + + def test_creates_resource(self): + meta = { + 'id': 'id-1', + 'rev': 'rev-1', + } + + res = { + 'foo': 'bar', + } + + chg = muck.CreateChange(meta=meta, res=res) + self.st.change(chg) + + ms = self.st.get_memory_store() + self.assertEqual(len(ms), 1) + self.assertEqual( + ms.as_dict(), + { + 'id-1': (meta, res), + }) + + def test_wont_create_resource_with_conflicting_id(self): + meta = { + 'id': 'id-1', + 'rev': 'rev-1', + } + + res = { + 'foo': 'bar', + } + + chg = muck.CreateChange(meta=meta, res=res) + self.st.change(chg) + + ms = self.st.get_memory_store() + with self.assertRaises(muck.Error): + ms.change(chg) + + def test_updates_resource(self): + meta_1 = { + 'id': 'id-1', + 'rev': 'rev-1', + } + + res_v1 = { + 'foo': 'bar', + } + + meta_2 = dict(meta_1) + meta_2['rev'] = 'rev-2' + + res_v2 = dict(res_v1) + res_v2['foo'] = 'yo' + + create = muck.CreateChange(meta=meta_1, res=res_v1) + update = muck.UpdateChange(meta=meta_2, res=res_v2) + + self.st.change(create) + self.st.change(update) + + ms = self.st.get_memory_store() + self.assertEqual(len(ms), 1) + self.assertEqual( + ms.as_dict(), + { + 'id-1': (meta_2, res_v2), + }) + + def test_refuses_to_update_resource_that_didnt_exist(self): + meta = { + 'id': 'id-1', + 'rev': 'rev-1', + } + + res = { + 'foo': 'bar', + } + + update = muck.UpdateChange(meta=meta, res=res) + with self.assertRaises(muck.Error): + self.st.change(update) + + def test_deletes_resource(self): + meta = { + 'id': 'id-1', + 'rev': 'rev-1', + } + + res = { + 'foo': 'bar', + } + + create = muck.CreateChange(meta=meta, res=res) + delete = muck.DeleteChange(meta=meta) + + self.st.change(create) + self.st.change(delete) + + ms = self.st.get_memory_store() + self.assertEqual(len(ms), 0) + self.assertEqual(ms.as_dict(), {}) + + def test_refuses_to_delete_resource_that_doesnt_exist(self): + meta = { + 'id': 'id-1', + 'rev': 'rev-1', + } + + delete = muck.DeleteChange(meta=meta) + with self.assertRaises(muck.Error): + self.st.change(delete) -- cgit v1.2.1