Debian Codegraph @

taskflow/tests/unit/persistence/test_memory_persistence.py

raw
1 # Copyright (C) 2013 Rackspace Hosting All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import contextlib
16
17 from taskflow import exceptions as exc
18 from taskflow.persistence import backends
19 from taskflow.persistence.backends import impl_memory
20 from taskflow import test
21 from taskflow.tests.unit.persistence import base
22
23
24 class MemoryPersistenceTest(test.TestCase, base.PersistenceTestMixin):
25 def setUp(self):
26 super().setUp()
27 self._backend = impl_memory.MemoryBackend({})
28
29 def _get_connection(self):
30 return self._backend.get_connection()
31
32 def tearDown(self):
33 conn = self._get_connection()
34 conn.clear_all()
35 self._backend = None
36 super().tearDown()
37
38 def test_memory_backend_entry_point(self):
39 conf = {'connection': 'memory:'}
40 with contextlib.closing(backends.fetch(conf)) as be:
41 self.assertIsInstance(be, impl_memory.MemoryBackend)
42
43 def test_memory_backend_fetch_by_name(self):
44 conf = {'connection': 'memory'} # note no colon
45 with contextlib.closing(backends.fetch(conf)) as be:
46 self.assertIsInstance(be, impl_memory.MemoryBackend)
47
48
49 class MemoryFilesystemTest(test.TestCase):
50
51 @staticmethod
52 def _get_item_path(fs, path):
53 # TODO(harlowja): is there a better way to do this??
54 return fs[path]
55
56 @staticmethod
57 def _del_item_path(fs, path):
58 # TODO(harlowja): is there a better way to do this??
59 del fs[path]
60
61 def test_set_get_ls(self):
62 fs = impl_memory.FakeFilesystem()
63 fs['/d'] = 'd'
64 fs['/c'] = 'c'
65 fs['/d/b'] = 'db'
66 self.assertEqual(2, len(fs.ls('/')))
67 self.assertEqual(1, len(fs.ls('/d')))
68 self.assertEqual('d', fs['/d'])
69 self.assertEqual('c', fs['/c'])
70 self.assertEqual('db', fs['/d/b'])
71
72 def test_ls_recursive(self):
73 fs = impl_memory.FakeFilesystem()
74 fs.ensure_path("/d")
75 fs.ensure_path("/c/d")
76 fs.ensure_path("/b/c/d")
77 fs.ensure_path("/a/b/c/d")
78 contents = fs.ls_r("/", absolute=False)
79 self.assertEqual([
80 'a',
81 'b',
82 'c',
83 'd',
84 'a/b',
85 'b/c',
86 'c/d',
87 'a/b/c',
88 'b/c/d',
89 'a/b/c/d',
90 ], contents)
91
92 def test_ls_recursive_absolute(self):
93 fs = impl_memory.FakeFilesystem()
94 fs.ensure_path("/d")
95 fs.ensure_path("/c/d")
96 fs.ensure_path("/b/c/d")
97 fs.ensure_path("/a/b/c/d")
98 contents = fs.ls_r("/", absolute=True)
99 self.assertEqual([
100 '/a',
101 '/b',
102 '/c',
103 '/d',
104 '/a/b',
105 '/b/c',
106 '/c/d',
107 '/a/b/c',
108 '/b/c/d',
109 '/a/b/c/d',
110 ], contents)
111
112 def test_ls_recursive_targeted(self):
113 fs = impl_memory.FakeFilesystem()
114 fs.ensure_path("/d")
115 fs.ensure_path("/c/d")
116 fs.ensure_path("/b/c/d")
117 fs.ensure_path("/a/b/c/d")
118 contents = fs.ls_r("/a/b", absolute=False)
119 self.assertEqual(['c', 'c/d'], contents)
120
121 def test_ls_targeted(self):
122 fs = impl_memory.FakeFilesystem()
123 fs.ensure_path("/d")
124 fs.ensure_path("/c/d")
125 fs.ensure_path("/b/c/d")
126 fs.ensure_path("/a/b/c/d")
127 contents = fs.ls("/a/b", absolute=False)
128 self.assertEqual(['c'], contents)
129
130 def test_ls_targeted_absolute(self):
131 fs = impl_memory.FakeFilesystem()
132 fs.ensure_path("/d")
133 fs.ensure_path("/c/d")
134 fs.ensure_path("/b/c/d")
135 fs.ensure_path("/a/b/c/d")
136 contents = fs.ls("/a/b", absolute=True)
137 self.assertEqual(['/a/b/c'], contents)
138
139 def test_ls_recursive_targeted_absolute(self):
140 fs = impl_memory.FakeFilesystem()
141 fs.ensure_path("/d")
142 fs.ensure_path("/c/d")
143 fs.ensure_path("/b/c/d")
144 fs.ensure_path("/a/b/c/d")
145 contents = fs.ls_r("/a/b", absolute=True)
146 self.assertEqual(['/a/b/c', '/a/b/c/d'], contents)
147
148 def test_ensure_path(self):
149 fs = impl_memory.FakeFilesystem()
150 pieces = ['a', 'b', 'c']
151 path = "/" + "/".join(pieces)
152 fs.ensure_path(path)
153 path = fs.root_path
154 for i, p in enumerate(pieces):
155 if i == 0:
156 path += p
157 else:
158 path += "/" + p
159 self.assertIsNone(fs[path])
160
161 def test_clear(self):
162 fs = impl_memory.FakeFilesystem()
163 paths = ['/b', '/c', '/a/b/c']
164 for p in paths:
165 fs.ensure_path(p)
166 for p in paths:
167 self.assertIsNone(self._get_item_path(fs, p))
168 fs.clear()
169 for p in paths:
170 self.assertRaises(exc.NotFound, self._get_item_path, fs, p)
171
172 def test_not_found(self):
173 fs = impl_memory.FakeFilesystem()
174 self.assertRaises(exc.NotFound, self._get_item_path, fs, '/c')
175
176 def test_bad_norms(self):
177 fs = impl_memory.FakeFilesystem()
178 self.assertRaises(ValueError, fs.normpath, '')
179 self.assertRaises(ValueError, fs.normpath, 'abc/c')
180 self.assertRaises(ValueError, fs.normpath, '../c')
181
182 def test_del_root_not_allowed(self):
183 fs = impl_memory.FakeFilesystem()
184 self.assertRaises(ValueError, fs.delete, "/", recursive=False)
185
186 def test_del_no_children_allowed(self):
187 fs = impl_memory.FakeFilesystem()
188 fs['/a'] = 'a'
189 self.assertEqual(1, len(fs.ls_r("/")))
190 fs.delete("/a")
191 self.assertEqual(0, len(fs.ls("/")))
192
193 def test_del_many_children_not_allowed(self):
194 fs = impl_memory.FakeFilesystem()
195 fs['/a'] = 'a'
196 fs['/a/b'] = 'b'
197 self.assertRaises(ValueError, fs.delete, "/", recursive=False)
198
199 def test_del_with_children_not_allowed(self):
200 fs = impl_memory.FakeFilesystem()
201 fs['/a'] = 'a'
202 fs['/a/b'] = 'b'
203 self.assertRaises(ValueError, fs.delete, "/a", recursive=False)
204
205 def test_del_many_children_allowed(self):
206 fs = impl_memory.FakeFilesystem()
207 fs['/a'] = 'a'
208 fs['/a/b'] = 'b'
209 self.assertEqual(2, len(fs.ls_r("/")))
210 fs.delete("/a", recursive=True)
211 self.assertEqual(0, len(fs.ls("/")))
212
213 def test_del_many_children_allowed_not_recursive(self):
214 fs = impl_memory.FakeFilesystem()
215 fs['/a'] = 'a'
216 fs['/a/b'] = 'b'
217 self.assertEqual(2, len(fs.ls_r("/")))
218 fs.delete("/a/b", recursive=False)
219 self.assertEqual(1, len(fs.ls("/")))
220 fs.delete("/a", recursive=False)
221 self.assertEqual(0, len(fs.ls("/")))
222
223 def test_link_loop_raises(self):
224 fs = impl_memory.FakeFilesystem()
225 fs['/b'] = 'c'
226 fs.symlink('/b', '/b')
227 self.assertRaises(ValueError, self._get_item_path, fs, '/b')
228
229 def test_ensure_linked_delete(self):
230 fs = impl_memory.FakeFilesystem()
231 fs['/b'] = 'd'
232 fs.symlink('/b', '/c')
233 self.assertEqual('d', fs['/b'])
234 self.assertEqual('d', fs['/c'])
235 del fs['/b']
236 self.assertRaises(exc.NotFound, self._get_item_path, fs, '/c')
237 self.assertRaises(exc.NotFound, self._get_item_path, fs, '/b')