def setUp(self): super(Test, self).setUp() ## Make objects filechecker = NamesChecker(['write', 'read']) dirchecker = NamesChecker(['__setitem__', 'get']) defineChecker(File, filechecker) defineChecker(Directory, dirchecker) root = self.root = Directory() root['test'] = Directory() root['test2'] = Directory() root['f'] = File('contents of\nf') root['g'] = File('contents of\ng') self.__view = FTPView(root, None) ## declare adapters ztapi.provideAdapter(IContainer, IContainerItemRenamer, ContainerItemRenamer)
class Test(PlacelessSetup, TestCase): def setUp(self): super(Test, self).setUp() ## Make objects filechecker = NamesChecker(['write', 'read']) dirchecker = NamesChecker(['__setitem__', 'get']) defineChecker(File, filechecker) defineChecker(Directory, dirchecker) root = self.root = Directory() root['test'] = Directory() root['test2'] = Directory() root['f'] = File('contents of\nf') root['g'] = File('contents of\ng') self.__view = FTPView(root, None) ## declare adapters ztapi.provideAdapter(IContainer, IContainerItemRenamer, ContainerItemRenamer) def test_type(self): self.assertEqual(self.__view.type('test'), 'd') self.assertEqual(self.__view.type('f'), 'f') self.assertEqual(self.__view.type('missing'), None) def test_names(self): names = self.__view.names() names.sort() self.assertEqual(names, ['f', 'g', 'test', 'test2']) def test_ls(self): ls = map(norm_info, self.__view.ls()) ls.sort(lambda i1, i2: cmp(i1['name'], i2['name'])) self.assertEqual( ls, [{ 'name': 'f', 'type': 'f', 'size': 13, 'mtime': datetime.datetime(1990, 1, 2),'nlinks': 1, 'owner_name': 'na', 'owner_readable': 1, 'owner_writable': 1, 'group_name': 'na', 'group_readable': 1, 'group_writable': 1, 'other_readable': 0, 'other_writable': 0, }, { 'name': 'g', 'type': 'f', 'size': 13, 'mtime': datetime.datetime(1990, 1, 2), 'nlinks': 1, 'owner_name': 'na', 'owner_readable': 1, 'owner_writable': 1, 'group_name': 'na', 'group_readable': 1, 'group_writable': 1, 'other_writable': 0, 'other_readable': 0, }, { 'name': 'test', 'type': 'd', 'size': 0, 'mtime': datetime.datetime(1990, 1, 1), 'nlinks': 1, 'owner_name': 'na', 'owner_readable': 1, 'owner_writable': 1, 'group_name': 'na', 'group_readable': 1, 'group_writable': 1, 'other_writable': 0, 'other_readable': 0, }, { 'name': 'test2', 'type': 'd', 'size': 0, 'mtime': datetime.datetime(1990, 1, 1), 'nlinks': 1, 'owner_name': 'na', 'owner_readable': 1, 'owner_writable': 1, 'group_name': 'na', 'group_readable': 1, 'group_writable': 1, 'other_readable': 0, 'other_writable': 0, }]) def test_readfile(self): f = StringIO() self.__view.readfile('f', f) self.assertEqual(f.getvalue(), 'contents of\nf') f = StringIO() self.__view.readfile('f', f, 3) self.assertEqual(f.getvalue(), 'tents of\nf') f = StringIO() self.__view.readfile('f', f, end=6) self.assertEqual(f.getvalue(), 'conten') f = StringIO() self.__view.readfile('f', f, 3, 9) self.assertEqual(f.getvalue(), 'contents of\nf'[3:9]) def test_lsinfo(self): self.assertEqual( norm_info(self.__view.lsinfo('test')), { 'name': 'test', 'type': 'd', 'size': 0, 'mtime': datetime.datetime(1990, 1, 1), 'nlinks': 1, 'owner_name': 'na', 'owner_readable': 1, 'owner_writable': 1, 'group_name': 'na', 'group_readable': 1, 'group_writable': 1, 'other_readable': 0, 'other_writable': 0, }) self.assertEqual( norm_info(self.__view.lsinfo('f')), { 'name': 'f', 'type': 'f', 'size': 13, 'mtime': datetime.datetime(1990, 1, 2), 'nlinks': 1, 'owner_name': 'na', 'owner_readable': 1, 'owner_writable': 1, 'group_name': 'na', 'group_readable': 1, 'group_writable': 1, 'other_readable': 0, 'other_writable': 0, }) def test_mtime(self): self.assertEqual(self.__view.mtime('test'), Directory.modified) self.assertEqual(self.__view.mtime('f'), File.modified) def test_size(self): self.assertEqual(self.__view.size('test'), 0) self.assertEqual(self.__view.size('f'), len(self.__view.context['f'].data)) def test_mkdir(self): self.__view.mkdir('test3') names = self.__view.names() names.sort() self.assertEqual(names, ['f', 'g', 'test', 'test2', 'test3']) self.assertEqual( norm_info(self.__view.lsinfo('test3')), { 'name': 'test3', 'type': 'd', 'size': 0, 'mtime': datetime.datetime(1990, 1, 1), 'nlinks': 1, 'owner_name': 'na', 'owner_readable': 1, 'owner_writable': 1, 'group_name': 'na', 'group_readable': 1, 'group_writable': 1, 'other_writable': 0, 'other_readable': 0, }) def test_remove(self): self.__view.remove('g') names = self.__view.names() names.sort() self.assertEqual(names, ['f', 'test', 'test2']) def test_rmdir(self): self.__view.rmdir('test2') names = self.__view.names() names.sort() self.assertEqual(names, ['f', 'g', 'test']) def test_rename(self): self.__view.rename('test2', 'spam') names = self.__view.names() names.sort() self.assertEqual(names, ['f', 'g', 'spam', 'test']) def test_writefile_new(self): self.__view.writefile('foo', StringIO('foo contents')) self.assertEqual(self.__view.context['foo'].data, 'foo contents') def test_writefile_over(self): self.__view.writefile('f', StringIO('foo contents')) self.assertEqual(self.__view.context['f'].data, 'foo contents') self.__view.writefile('f', StringIO(' more'), append=True) self.assertEqual(self.__view.context['f'].data, 'foo contents more') self.__view.writefile('f', StringIO('xxxxxx'), start=3, end=9) self.assertEqual(self.__view.context['f'].data, 'fooxxxxxxnts more') self.__view.writefile('f', StringIO('yyy'), start=5) self.assertEqual(self.__view.context['f'].data, 'fooxxyyy') def test_writeable(self): self.assert_(self.__view.writable('f')) self.assert_(self.__view.writable('notthere')) self.assert_(not self.__view.writable('test')) def test_readable(self): self.assert_(self.__view.readable('f')) self.assert_(not self.__view.readable('notthere')) self.assert_(self.__view.readable('test')) root = self.root root['nondir'] = NonDirectoryContainer() self.assertEqual(self.__view.readable('nondir'), False) def test_read_unicode(self): root = self.root root['uf'] = File(u'unicode contents of\nuf') f = UnicodeStringIO() self.__view.readfile('uf', f) ## the value of f a non-unicode value - since it got encoded in the ## readfile method. self.assertEqual(f.getvalue(), 'unicode contents of\nuf')