class TestSwiftObjects(TestCase): def setUp(self): self.data = 'zażółć gęślą jaźń\n' * 1000 self.file = File(0600, self.data) self.swift = Swift(**conn_opts) self.swift.mkdir("/") def test_put_get(self): self.swift.put('/file', self.file) self.assertEqual(self.swift.get('/file').contents, self.data) def test_put_fail(self): with self.assertRaises(NoSuchFileOrDirectory) as cm: self.swift.put('/dir/file', self.file) self.assertEqual(str(cm.exception), '/dir') with self.assertRaises(ValueError) as cm: self.swift.put('/file/', self.file) def test_put_rm(self): self.swift.put('/file', self.file) self.swift.rm('/file') with self.assertRaises(NoSuchFileOrDirectory) as cm: self.swift.get('/file') self.assertEqual(str(cm.exception), '/file') def test_file_ctime(self): self.swift.put('/file', self.file) self.file.touch() self.assertLessEqual(self.swift.get('/file').ctime, self.file.ctime) def tearDown(self): self.swift.rm("/", recursive=True, force=True)
class TestSwiftObjects(TestCase) : def setUp( self ) : self.data = 'zażółć gęślą jaźń\n'*1000 self.file = File(0600,self.data) self.swift = Swift(**conn_opts) self.swift.mkdir("/") def test_put_get( self ) : self.swift.put( '/file' , self.file ) self.assertEqual( self.swift.get( '/file' ).contents , self.data ) def test_put_fail( self ) : with self.assertRaises(NoSuchFileOrDirectory) as cm : self.swift.put( '/dir/file' , self.file ) self.assertEqual(str(cm.exception),'/dir') with self.assertRaises(ValueError) as cm : self.swift.put( '/file/' , self.file ) def test_put_rm( self ) : self.swift.put('/file' , self.file ) self.swift.rm('/file') with self.assertRaises(NoSuchFileOrDirectory) as cm : self.swift.get('/file') self.assertEqual(str(cm.exception),'/file') def test_file_ctime( self ) : self.swift.put('/file' , self.file ) self.file.touch() self.assertLessEqual(self.swift.get('/file').ctime,self.file.ctime) def tearDown( self ) : self.swift.rm("/",recursive=True,force=True)
class TestSwiftDirsAndObjects(TestCase): def setUp(self): self.swift = Swift(**conn_opts) self.f1 = File(0644, 'zażółć') self.f2 = File(0644, 'zażółć' * 2) self.d1f2 = File(0644, 'zażółć' * 3) self.d1d3f1 = File(0644, 'zażółć' * 4) self.swift.mkdir('/dir1/dir2', parents=True) self.swift.mkdir('/dir1/dir3') self.swift.put('/file1', self.f1) self.swift.put('/file2', self.f2) self.swift.put('/dir1/file2', self.d1f2) self.swift.put('/dir1/dir3/file1', self.d1d3f1) def test_tree_with_files_contents(self): d = self.swift.get('/') self.assertEqual(d.children['file1'].contents, self.f1.contents) self.assertEqual(d.children['file2'].contents, self.f2.contents) self.assertEqual(d.children['dir1'].children['file2'].contents, self.d1f2.contents) def test_tree_with_files_children_names(self): d = self.swift.get('/') self.assertIn('file1', d.children.names()) self.assertIn('file2', d.children.names()) self.assertIn('dir1', d.children.names()) def tearDown(self): self.swift.rm("/", recursive=True, force=True)
class TestSwiftDirsAndObjects(TestCase) : def setUp( self ) : self.swift = Swift(**conn_opts) self.f1 = File(0644,'zażółć') self.f2 = File(0644,'zażółć'*2) self.d1f2 = File(0644,'zażółć'*3) self.d1d3f1 = File(0644,'zażółć'*4) self.swift.mkdir( '/dir1/dir2' , parents=True ) self.swift.mkdir( '/dir1/dir3' ) self.swift.put( '/file1' , self.f1 ) self.swift.put( '/file2' , self.f2 ) self.swift.put( '/dir1/file2' , self.d1f2 ) self.swift.put( '/dir1/dir3/file1' , self.d1d3f1 ) def test_tree_with_files_contents( self ) : d = self.swift.get('/') self.assertEqual( d.children['file1'].contents , self.f1.contents ) self.assertEqual( d.children['file2'].contents , self.f2.contents ) self.assertEqual( d.children['dir1'].children['file2'].contents , self.d1f2.contents ) def test_tree_with_files_children_names( self ) : d = self.swift.get('/') self.assertIn('file1',d.children.names() ) self.assertIn('file2',d.children.names() ) self.assertIn('dir1',d.children.names() ) def tearDown( self ) : self.swift.rm("/",recursive=True,force=True)
class TestEmptySwift(TestCase): def setUp(self): self.swift = Swift(**conn_opts) def test_put_file_in_root(self): data = "contents" path = "/test_put_file_in_root" self.assertIsNone(self.swift.put(path, File(0644, data))) self.assertEqual(self.swift.get(path).contents, data) def test_put_empty_file_in_not_existing_dir(self): with self.assertRaises(NoSuchFileOrDirectory): self.swift.put("/doesnt_exist/file", File(0644, 'foo')) def test_empty_root(self): root = self.swift.get("/") # self.assertIs(root, Dir()) # orly? self.assertFalse(any(root.children)) def test_make_directory(self): self.assertIsNone(self.swift.mkdir("/test_make_directory")) self.assertIsNotNone(self.swift.get("/").children["test_make_directory"]) def test_put_file_in_subdirectory(self): dir = "/test_put_file_in_subdirectory" data = "foobar" self.swift.mkdir(dir) self.assertIsNone(self.swift.put(dir + "/file", File(0644, data))) self.assertEqual(self.swift.get(dir + "/file").contents, data) def test_new_directory_name_in_children_names(self): self.assertIsNone(self.swift.mkdir("/another_directory")) self.assertIn("another_directory", self.swift.get("/").children.names()) def tearDown( self ) : self.swift.rm("/",recursive=True)