Fix #5398 and #5395 - Fix tests failing due to problem creating connection for alembic
[larjonas-mediagoblin.git] / mediagoblin / tests / test_workbench.py
blobf3ff57ed669687f0f8d4ef179cd8bcb73da7b0c8
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 import os
18 import tempfile
21 from mediagoblin.tools import workbench
22 from mediagoblin.mg_globals import setup_globals
23 from mediagoblin.decorators import get_workbench
24 from mediagoblin.tests.test_storage import get_tmp_filestorage, cleanup_storage
27 class TestWorkbench(object):
28 def setup(self):
29 self.workbench_base = tempfile.mkdtemp(prefix='gmg_workbench_testing')
30 self.workbench_manager = workbench.WorkbenchManager(
31 self.workbench_base)
33 def teardown(self):
34 # If the workbench is empty, this should work.
35 os.rmdir(self.workbench_base)
37 def test_create_workbench(self):
38 workbench = self.workbench_manager.create()
39 assert os.path.isdir(workbench.dir)
40 assert workbench.dir.startswith(self.workbench_manager.base_workbench_dir)
41 workbench.destroy()
43 def test_joinpath(self):
44 this_workbench = self.workbench_manager.create()
45 tmpname = this_workbench.joinpath('temp.txt')
46 assert tmpname == os.path.join(this_workbench.dir, 'temp.txt')
47 this_workbench.destroy()
49 def test_destroy_workbench(self):
50 # kill a workbench
51 this_workbench = self.workbench_manager.create()
52 tmpfile_name = this_workbench.joinpath('temp.txt')
53 tmpfile = open(tmpfile_name, 'w')
54 with tmpfile:
55 tmpfile.write('lollerskates')
57 assert os.path.exists(tmpfile_name)
59 wb_dir = this_workbench.dir
60 this_workbench.destroy()
61 assert not os.path.exists(tmpfile_name)
62 assert not os.path.exists(wb_dir)
64 def test_localized_file(self):
65 tmpdir, this_storage = get_tmp_filestorage()
66 this_workbench = self.workbench_manager.create()
68 # Write a brand new file
69 filepath = ['dir1', 'dir2', 'ourfile.txt']
71 with this_storage.get_file(filepath, 'w') as our_file:
72 our_file.write('Our file')
74 # with a local file storage
75 filename = this_workbench.localized_file(this_storage, filepath)
76 assert filename == os.path.join(
77 tmpdir, 'dir1/dir2/ourfile.txt')
78 this_storage.delete_file(filepath)
79 cleanup_storage(this_storage, tmpdir, ['dir1', 'dir2'])
81 # with a fake remote file storage
82 tmpdir, this_storage = get_tmp_filestorage(fake_remote=True)
84 # ... write a brand new file, again ;)
85 with this_storage.get_file(filepath, 'w') as our_file:
86 our_file.write('Our file')
88 filename = this_workbench.localized_file(this_storage, filepath)
89 assert filename == os.path.join(
90 this_workbench.dir, 'ourfile.txt')
92 # fake remote file storage, filename_if_copying set
93 filename = this_workbench.localized_file(
94 this_storage, filepath, 'thisfile')
95 assert filename == os.path.join(
96 this_workbench.dir, 'thisfile.txt')
98 # fake remote file storage, filename_if_copying set,
99 # keep_extension_if_copying set to false
100 filename = this_workbench.localized_file(
101 this_storage, filepath, 'thisfile.text', False)
102 assert filename == os.path.join(
103 this_workbench.dir, 'thisfile.text')
105 this_storage.delete_file(filepath)
106 cleanup_storage(this_storage, tmpdir, ['dir1', 'dir2'])
107 this_workbench.destroy()
109 def test_workbench_decorator(self):
110 """Test @get_workbench decorator and automatic cleanup"""
111 # The decorator needs mg_globals.workbench_manager
112 setup_globals(workbench_manager=self.workbench_manager)
114 @get_workbench
115 def create_it(workbench=None):
116 # workbench dir exists?
117 assert os.path.isdir(workbench.dir)
118 return workbench.dir
120 benchdir = create_it()
121 # workbench dir has been cleaned up automatically?
122 assert not os.path.isdir(benchdir)