# 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)