summaryrefslogtreecommitdiff
path: root/muck/mem_tests.py
blob: e858a33510ad2ac0cbe1099925dbd56d9b7f6722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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 <http://www.gnu.org/licenses/>.


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)