From 34c7431562b14775f883f19bb43c11762be22050 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 19 Oct 2018 10:09:34 +0300 Subject: Add: muck/mem.py --- muck/mem.py | 56 ++++++++++++++++++++++ muck/mem_tests.py | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 muck/mem.py create mode 100644 muck/mem_tests.py diff --git a/muck/mem.py b/muck/mem.py new file mode 100644 index 0000000..437085d --- /dev/null +++ b/muck/mem.py @@ -0,0 +1,56 @@ +# 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 MemoryStore: + + def __init__(self): + self._dict = {} + + def __len__(self): + return len(self._dict) + + def as_dict(self): + return self._dict + + def change(self, chg): + funcs = { + 'create': self._create, + 'update': self._update, + 'delete': self._delete, + } + op = chg.get_op() + func = funcs[op] + func(chg) + + def _create(self, chg): + rid = chg.get_id() + if rid in self._dict: + raise muck.Error('Resource {} already exists'.format(rid)) + self._dict[rid] = (chg.get_meta(), chg.get_res()) + + def _update(self, chg): + rid = chg.get_id() + if rid not in self._dict: + raise muck.Error('Resource {} does not exist'.format(rid)) + self._dict[rid] = (chg.get_meta(), chg.get_res()) + + def _delete(self, chg): + rid = chg.get_id() + if rid not in self._dict: + raise muck.Error('Resource {} does not exist'.format(rid)) + del self._dict[rid] diff --git a/muck/mem_tests.py b/muck/mem_tests.py new file mode 100644 index 0000000..e858a33 --- /dev/null +++ b/muck/mem_tests.py @@ -0,0 +1,138 @@ +# 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 unittest + +import muck + + +class MemoryStoreTests(unittest.TestCase): + + def test_is_initially_empty(self): + ms = muck.MemoryStore() + 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) + ms = muck.MemoryStore() + ms.change(chg) + 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) + ms = muck.MemoryStore() + ms.change(chg) + 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) + + ms = muck.MemoryStore() + ms.change(create) + ms.change(update) + 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) + + ms = muck.MemoryStore() + with self.assertRaises(muck.Error): + ms.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) + + ms = muck.MemoryStore() + ms.change(create) + ms.change(delete) + 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) + + ms = muck.MemoryStore() + with self.assertRaises(muck.Error): + ms.change(delete) -- cgit v1.2.1