def setUp(self): self.tmp = tempfile.mkdtemp() self.stor = LocalFileStorage()
class TestLocalFileStorage(unittest.TestCase): def setUp(self): self.tmp = tempfile.mkdtemp() self.stor = LocalFileStorage() def tearDown(self): rm_local_tmp_dir(self.tmp) def test_read_write(self): fn = os.path.join(self.tmp, 'somefile.txt') with self.stor.open(fn, 'w') as fd: fd.write('stuff') with self.stor.open(fn, 'r') as fd: eq_(fd.read(), 'stuff') def test_non_ascii_filename(self): fn = os.path.join(self.tmp, u'Ivan Krsti\u0107.txt') with self.stor.open(fn, 'w') as fd: fd.write('stuff') with self.stor.open(fn, 'r') as fd: eq_(fd.read(), 'stuff') def test_non_ascii_content(self): fn = os.path.join(self.tmp, 'somefile.txt') with self.stor.open(fn, 'w') as fd: fd.write(u'Ivan Krsti\u0107.txt'.encode('utf8')) with self.stor.open(fn, 'r') as fd: eq_(fd.read().decode('utf8'), u'Ivan Krsti\u0107.txt') def test_make_file_dirs(self): dp = os.path.join(self.tmp, 'path', 'to') self.stor.open(os.path.join(dp, 'file.txt'), 'w').close() assert os.path.exists( self.stor.path(dp)), ('Directory not created: %r' % dp) def test_do_not_make_file_dirs_when_reading(self): fpath = os.path.join(self.tmp, 'file.txt') with open(fpath, 'w') as fp: fp.write('content') # Make sure this doesn't raise an exception. self.stor.open(fpath, 'r').close() def test_make_dirs_only_once(self): dp = os.path.join(self.tmp, 'path', 'to') with self.stor.open(os.path.join(dp, 'file.txt'), 'w') as fd: fd.write('stuff') # Make sure it doesn't try to make the dir twice with self.stor.open(os.path.join(dp, 'file.txt'), 'w') as fd: fd.write('stuff') with self.stor.open(os.path.join(dp, 'file.txt'), 'r') as fd: eq_(fd.read(), 'stuff') def test_delete_empty_dir(self): dp = os.path.join(self.tmp, 'path') os.mkdir(dp) self.stor.delete(dp) eq_(os.path.exists(dp), False) @raises(OSError) def test_cannot_delete_non_empty_dir(self): dp = os.path.join(self.tmp, 'path') with self.stor.open(os.path.join(dp, 'file.txt'), 'w') as fp: fp.write('stuff') self.stor.delete(dp) def test_delete_file(self): dp = os.path.join(self.tmp, 'path') fn = os.path.join(dp, 'file.txt') with self.stor.open(fn, 'w') as fp: fp.write('stuff') self.stor.delete(fn) eq_(os.path.exists(fn), False) eq_(os.path.exists(dp), True)
class TestLocalFileStorage(unittest.TestCase): def setUp(self): self.tmp = tempfile.mkdtemp() self.stor = LocalFileStorage() def tearDown(self): rm_local_tmp_dir(self.tmp) def test_read_write(self): fn = os.path.join(self.tmp, 'somefile.txt') with self.stor.open(fn, 'w') as fd: fd.write('stuff') with self.stor.open(fn, 'r') as fd: eq_(fd.read(), 'stuff') def test_non_ascii_filename(self): fn = os.path.join(self.tmp, u'Ivan Krsti\u0107.txt') with self.stor.open(fn, 'w') as fd: fd.write('stuff') with self.stor.open(fn, 'r') as fd: eq_(fd.read(), 'stuff') def test_non_ascii_content(self): fn = os.path.join(self.tmp, 'somefile.txt') with self.stor.open(fn, 'w') as fd: fd.write(u'Ivan Krsti\u0107.txt'.encode('utf8')) with self.stor.open(fn, 'r') as fd: eq_(fd.read().decode('utf8'), u'Ivan Krsti\u0107.txt') def test_make_file_dirs(self): dp = os.path.join(self.tmp, 'path', 'to') self.stor.open(os.path.join(dp, 'file.txt'), 'w').close() assert os.path.exists(self.stor.path(dp)), ( 'Directory not created: %r' % dp) def test_do_not_make_file_dirs_when_reading(self): fpath = os.path.join(self.tmp, 'file.txt') with open(fpath, 'w') as fp: fp.write('content') # Make sure this doesn't raise an exception. self.stor.open(fpath, 'r').close() def test_make_dirs_only_once(self): dp = os.path.join(self.tmp, 'path', 'to') with self.stor.open(os.path.join(dp, 'file.txt'), 'w') as fd: fd.write('stuff') # Make sure it doesn't try to make the dir twice with self.stor.open(os.path.join(dp, 'file.txt'), 'w') as fd: fd.write('stuff') with self.stor.open(os.path.join(dp, 'file.txt'), 'r') as fd: eq_(fd.read(), 'stuff') def test_delete_empty_dir(self): dp = os.path.join(self.tmp, 'path') os.mkdir(dp) self.stor.delete(dp) eq_(os.path.exists(dp), False) @raises(OSError) def test_cannot_delete_non_empty_dir(self): dp = os.path.join(self.tmp, 'path') with self.stor.open(os.path.join(dp, 'file.txt'), 'w') as fp: fp.write('stuff') self.stor.delete(dp) def test_delete_file(self): dp = os.path.join(self.tmp, 'path') fn = os.path.join(dp, 'file.txt') with self.stor.open(fn, 'w') as fp: fp.write('stuff') self.stor.delete(fn) eq_(os.path.exists(fn), False) eq_(os.path.exists(dp), True)