Skip to content

Commit 65168d6

Browse files
committed
chore: new storage test
1 parent 649a8f7 commit 65168d6

1 file changed

Lines changed: 305 additions & 0 deletions

File tree

src/appier/test/storage.py

Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Hive Appier Framework
5+
# Copyright (c) 2008-2024 Hive Solutions Lda.
6+
#
7+
# This file is part of Hive Appier Framework.
8+
#
9+
# Hive Appier Framework is free software: you can redistribute it and/or modify
10+
# it under the terms of the Apache License as published by the Apache
11+
# Foundation, either version 2.0 of the License, or (at your option) any
12+
# later version.
13+
#
14+
# Hive Appier Framework is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# Apache License for more details.
18+
#
19+
# You should have received a copy of the Apache License along with
20+
# Hive Appier Framework. If not, see <http://www.apache.org/licenses/>.
21+
22+
__author__ = "João Magalhães <joamag@hive.pt>"
23+
""" The author(s) of the module """
24+
25+
__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26+
""" The copyright for the module """
27+
28+
__license__ = "Apache License, Version 2.0"
29+
""" The license for the module """
30+
31+
import os
32+
import shutil
33+
import tempfile
34+
import unittest
35+
36+
import appier
37+
38+
39+
class FsEngineTest(unittest.TestCase):
40+
def setUp(self):
41+
self.temp_dir = tempfile.mkdtemp()
42+
43+
def tearDown(self):
44+
if os.path.exists(self.temp_dir):
45+
shutil.rmtree(self.temp_dir)
46+
47+
def test_store(self):
48+
file_data = b"Hello World"
49+
file = appier.File((b"test.txt", None, file_data))
50+
file.engine = "fs"
51+
file.params = {"file_path": os.path.join(self.temp_dir, "test.txt")}
52+
53+
appier.FsEngine.store(file)
54+
55+
file_path = os.path.join(self.temp_dir, "test.txt")
56+
self.assertEqual(os.path.exists(file_path), True)
57+
58+
handle = open(file_path, "rb")
59+
try:
60+
data = handle.read()
61+
finally:
62+
handle.close()
63+
64+
self.assertEqual(data, file_data)
65+
66+
def test_load(self):
67+
file_data = b"Hello World"
68+
file_path = os.path.join(self.temp_dir, "test.txt")
69+
70+
handle = open(file_path, "wb")
71+
try:
72+
handle.write(file_data)
73+
finally:
74+
handle.close()
75+
76+
file = appier.File((b"test.txt", None, file_data))
77+
file.engine = "fs"
78+
file.params = {"file_path": file_path}
79+
80+
appier.FsEngine.load(file)
81+
82+
self.assertEqual(file.size, len(file_data))
83+
self.assertEqual(file.hash != None, True)
84+
self.assertEqual(file.etag != None, True)
85+
86+
def test_delete(self):
87+
file_data = b"Hello World"
88+
file_path = os.path.join(self.temp_dir, "test.txt")
89+
90+
handle = open(file_path, "wb")
91+
try:
92+
handle.write(file_data)
93+
finally:
94+
handle.close()
95+
96+
self.assertEqual(os.path.exists(file_path), True)
97+
98+
file = appier.File((b"test.txt", None, file_data))
99+
file.engine = "fs"
100+
file.params = {"file_path": file_path}
101+
102+
appier.FsEngine.delete(file)
103+
104+
self.assertEqual(os.path.exists(file_path), False)
105+
106+
def test_read(self):
107+
file_data = b"Hello World"
108+
file_path = os.path.join(self.temp_dir, "test.txt")
109+
110+
handle = open(file_path, "wb")
111+
try:
112+
handle.write(file_data)
113+
finally:
114+
handle.close()
115+
116+
file = appier.File((b"test.txt", None, file_data))
117+
file.engine = "fs"
118+
file.params = {"file_path": file_path}
119+
120+
data = appier.FsEngine.read(file)
121+
122+
self.assertEqual(data, file_data)
123+
124+
def test_read_size(self):
125+
file_data = b"Hello World"
126+
file_path = os.path.join(self.temp_dir, "test.txt")
127+
128+
handle = open(file_path, "wb")
129+
try:
130+
handle.write(file_data)
131+
finally:
132+
handle.close()
133+
134+
file = appier.File((b"test.txt", None, file_data))
135+
file.engine = "fs"
136+
file.params = {"file_path": file_path}
137+
138+
data = appier.FsEngine.read(file, size=5)
139+
140+
self.assertEqual(data, b"Hello")
141+
142+
data = appier.FsEngine.read(file, size=6)
143+
144+
self.assertEqual(data, b" World")
145+
146+
data = appier.FsEngine.read(file, size=6)
147+
148+
self.assertEqual(data, b"")
149+
150+
def test_seek(self):
151+
file_data = b"Hello World"
152+
file_path = os.path.join(self.temp_dir, "test.txt")
153+
154+
handle = open(file_path, "wb")
155+
try:
156+
handle.write(file_data)
157+
finally:
158+
handle.close()
159+
160+
file = appier.File((b"test.txt", None, file_data))
161+
file.engine = "fs"
162+
file.params = {"file_path": file_path}
163+
164+
appier.FsEngine.seek(file, offset=6)
165+
166+
data = appier.FsEngine.read(file)
167+
168+
self.assertEqual(data, b"World")
169+
170+
def test_cleanup(self):
171+
file_data = b"Hello World"
172+
file_path = os.path.join(self.temp_dir, "test.txt")
173+
174+
handle = open(file_path, "wb")
175+
try:
176+
handle.write(file_data)
177+
finally:
178+
handle.close()
179+
180+
file = appier.File((b"test.txt", None, file_data))
181+
file.engine = "fs"
182+
file.params = {"file_path": file_path}
183+
184+
appier.FsEngine._handle(file)
185+
186+
self.assertEqual(hasattr(file, "_handle"), True)
187+
188+
appier.FsEngine.cleanup(file)
189+
190+
self.assertEqual(hasattr(file, "_handle"), False)
191+
192+
def test_is_seekable(self):
193+
self.assertEqual(appier.FsEngine.is_seekable(), True)
194+
195+
def test_file_path(self):
196+
file = appier.File((b"test.txt", None, b"Hello World"))
197+
file.engine = "fs"
198+
file.params = {"file_path": os.path.join(self.temp_dir, "custom.txt")}
199+
200+
file_path = appier.FsEngine._file_path(file)
201+
202+
self.assertEqual(file_path, os.path.join(self.temp_dir, "custom.txt"))
203+
204+
def test_file_path_default(self):
205+
file = appier.File((b"test.txt", None, b"Hello World"))
206+
file.engine = "fs"
207+
208+
file_path = appier.FsEngine._file_path(file, base=self.temp_dir)
209+
210+
expected_path = os.path.join(self.temp_dir, file.guid)
211+
self.assertEqual(file_path, expected_path)
212+
213+
def test_file_path_ensure(self):
214+
sub_dir = os.path.join(self.temp_dir, "sub", "directory")
215+
file = appier.File((b"test.txt", None, b"Hello World"))
216+
file.engine = "fs"
217+
file.params = {"file_path": os.path.join(sub_dir, "test.txt")}
218+
219+
self.assertEqual(os.path.exists(sub_dir), False)
220+
221+
file_path = appier.FsEngine._file_path(file, ensure=True)
222+
223+
self.assertEqual(os.path.exists(sub_dir), True)
224+
self.assertEqual(file_path, os.path.join(sub_dir, "test.txt"))
225+
226+
def test_compute(self):
227+
file_data = b"Hello World"
228+
file_path = os.path.join(self.temp_dir, "test.txt")
229+
230+
handle = open(file_path, "wb")
231+
try:
232+
handle.write(file_data)
233+
finally:
234+
handle.close()
235+
236+
file = appier.File((b"test.txt", None, file_data))
237+
file.engine = "fs"
238+
file.params = {"file_path": file_path}
239+
240+
appier.FsEngine._compute(file)
241+
242+
self.assertEqual(file.size, len(file_data))
243+
self.assertEqual(type(file.hash), str)
244+
self.assertEqual(type(file.etag), str)
245+
self.assertEqual(file.hash, file.etag)
246+
247+
def test_handle(self):
248+
file_data = b"Hello World"
249+
file_path = os.path.join(self.temp_dir, "test.txt")
250+
251+
handle = open(file_path, "wb")
252+
try:
253+
handle.write(file_data)
254+
finally:
255+
handle.close()
256+
257+
file = appier.File((b"test.txt", None, file_data))
258+
file.engine = "fs"
259+
file.params = {"file_path": file_path}
260+
261+
handle = appier.FsEngine._handle(file)
262+
263+
self.assertEqual(hasattr(file, "_handle"), True)
264+
self.assertEqual(handle != None, True)
265+
self.assertEqual(handle.closed, False)
266+
267+
appier.FsEngine.cleanup(file)
268+
269+
def test_integration(self):
270+
file_data = b"Hello World"
271+
file = appier.File((b"test.txt", None, file_data))
272+
file.engine = "fs"
273+
file.params = {"file_path": os.path.join(self.temp_dir, "test.txt")}
274+
275+
appier.FsEngine.store(file)
276+
277+
file2 = appier.File((b"test.txt", None, file_data))
278+
file2.engine = "fs"
279+
file2.params = {"file_path": os.path.join(self.temp_dir, "test.txt")}
280+
281+
appier.FsEngine.load(file2)
282+
283+
self.assertEqual(file2.size, len(file_data))
284+
285+
data = appier.FsEngine.read(file2)
286+
287+
self.assertEqual(data, file_data)
288+
289+
appier.FsEngine.delete(file2)
290+
291+
file_path = os.path.join(self.temp_dir, "test.txt")
292+
self.assertEqual(os.path.exists(file_path), False)
293+
294+
295+
class BaseEngineTest(unittest.TestCase):
296+
def test_is_stored(self):
297+
self.assertEqual(appier.BaseEngine.is_stored(), True)
298+
299+
300+
class StorageEngineTest(unittest.TestCase):
301+
def test_is_seekable(self):
302+
self.assertEqual(appier.StorageEngine.is_seekable(), False)
303+
304+
def test_is_stored(self):
305+
self.assertEqual(appier.StorageEngine.is_stored(), False)

0 commit comments

Comments
 (0)