def test_files(self): import tempfile a = Node() file_content = 'some text ABCDE' file_content_different = 'other values 12345' with tempfile.NamedTemporaryFile() as f: f.write(file_content) f.flush() a.add_path(f.name, 'file1.txt') a.add_path(f.name, 'file2.txt') self.assertEquals(set(a.get_folder_list()), set(['file1.txt', 'file2.txt'])) with open(a.get_abs_path('file1.txt')) as f: self.assertEquals(f.read(), file_content) with open(a.get_abs_path('file2.txt')) as f: self.assertEquals(f.read(), file_content) b = a.copy() self.assertNotEquals(a.uuid, b.uuid) # Check that the content is there self.assertEquals(set(b.get_folder_list()), set(['file1.txt', 'file2.txt'])) with open(b.get_abs_path('file1.txt')) as f: self.assertEquals(f.read(), file_content) with open(b.get_abs_path('file2.txt')) as f: self.assertEquals(f.read(), file_content) # I overwrite a file and create a new one in the copy only with tempfile.NamedTemporaryFile() as f: f.write(file_content_different) f.flush() b.add_path(f.name, 'file2.txt') b.add_path(f.name, 'file3.txt') # I check the new content, and that the old one has not changed self.assertEquals(set(a.get_folder_list()), set(['file1.txt', 'file2.txt'])) with open(a.get_abs_path('file1.txt')) as f: self.assertEquals(f.read(), file_content) with open(a.get_abs_path('file2.txt')) as f: self.assertEquals(f.read(), file_content) self.assertEquals(set(b.get_folder_list()), set(['file1.txt', 'file2.txt', 'file3.txt'])) with open(b.get_abs_path('file1.txt')) as f: self.assertEquals(f.read(), file_content) with open(b.get_abs_path('file2.txt')) as f: self.assertEquals(f.read(), file_content_different) with open(b.get_abs_path('file3.txt')) as f: self.assertEquals(f.read(), file_content_different) # This should in principle change the location of the files, # so I recheck a.store() # I now copy after storing c = a.copy() # I overwrite a file and create a new one in the copy only with tempfile.NamedTemporaryFile() as f: f.write(file_content_different) f.flush() c.add_path(f.name, 'file1.txt') c.add_path(f.name, 'file4.txt') self.assertEquals(set(a.get_folder_list()), set(['file1.txt', 'file2.txt'])) with open(a.get_abs_path('file1.txt')) as f: self.assertEquals(f.read(), file_content) with open(a.get_abs_path('file2.txt')) as f: self.assertEquals(f.read(), file_content) self.assertEquals(set(c.get_folder_list()), set(['file1.txt', 'file2.txt', 'file4.txt'])) with open(c.get_abs_path('file1.txt')) as f: self.assertEquals(f.read(), file_content_different) with open(c.get_abs_path('file2.txt')) as f: self.assertEquals(f.read(), file_content) with open(c.get_abs_path('file4.txt')) as f: self.assertEquals(f.read(), file_content_different)
def test_folders(self): """ Similar as test_files, but I manipulate a tree of folders """ import tempfile import os, shutil import random, string a = Node() # Since Node uses the same method of Folder(), # for this test I create a test folder by hand # For any non-test usage, use SandboxFolder()! directory = os.path.realpath(os.path.join('/', 'tmp', 'tmp_try')) while os.path.exists(os.path.join(directory)): # I append a random letter/number until it is unique directory += random.choice(string.ascii_uppercase + string.digits) # create a folder structure to copy around tree_1 = os.path.join(directory, 'tree_1') os.makedirs(tree_1) file_content = 'some text ABCDE' file_content_different = 'other values 12345' with open(os.path.join(tree_1, 'file1.txt'), 'w') as f: f.write(file_content) os.mkdir(os.path.join(tree_1, 'dir1')) os.mkdir(os.path.join(tree_1, 'dir1', 'dir2')) with open(os.path.join(tree_1, 'dir1', 'file2.txt'), 'w') as f: f.write(file_content) os.mkdir(os.path.join(tree_1, 'dir1', 'dir2', 'dir3')) # add the tree to the node a.add_path(tree_1, 'tree_1') # verify if the node has the structure I expect self.assertEquals(set(a.get_folder_list()), set(['tree_1'])) self.assertEquals(set(a.get_folder_list('tree_1')), set(['file1.txt', 'dir1'])) self.assertEquals( set(a.get_folder_list(os.path.join('tree_1', 'dir1'))), set(['dir2', 'file2.txt'])) with open(a.get_abs_path(os.path.join('tree_1', 'file1.txt'))) as f: self.assertEquals(f.read(), file_content) with open(a.get_abs_path(os.path.join('tree_1', 'dir1', 'file2.txt'))) as f: self.assertEquals(f.read(), file_content) # try to exit from the folder with self.assertRaises(ValueError): a.get_folder_list('..') # copy into a new node b = a.copy() self.assertNotEquals(a.uuid, b.uuid) # Check that the content is there self.assertEquals(set(b.get_folder_list('.')), set(['tree_1'])) self.assertEquals(set(b.get_folder_list('tree_1')), set(['file1.txt', 'dir1'])) self.assertEquals( set(b.get_folder_list(os.path.join('tree_1', 'dir1'))), set(['dir2', 'file2.txt'])) with open(b.get_abs_path(os.path.join('tree_1', 'file1.txt'))) as f: self.assertEquals(f.read(), file_content) with open(b.get_abs_path(os.path.join('tree_1', 'dir1', 'file2.txt'))) as f: self.assertEquals(f.read(), file_content) # I overwrite a file and create a new one in the copy only dir3 = os.path.join(directory, 'dir3') os.mkdir(dir3) b.add_path(dir3, os.path.join('tree_1', 'dir3')) # no absolute path here with self.assertRaises(ValueError): b.add_path('dir3', os.path.join('tree_1', 'dir3')) with tempfile.NamedTemporaryFile() as f: f.write(file_content_different) f.flush() b.add_path(f.name, 'file3.txt') # I check the new content, and that the old one has not changed # old self.assertEquals(set(a.get_folder_list('.')), set(['tree_1'])) self.assertEquals(set(a.get_folder_list('tree_1')), set(['file1.txt', 'dir1'])) self.assertEquals( set(a.get_folder_list(os.path.join('tree_1', 'dir1'))), set(['dir2', 'file2.txt'])) with open(a.get_abs_path(os.path.join('tree_1', 'file1.txt'))) as f: self.assertEquals(f.read(), file_content) with open(a.get_abs_path(os.path.join('tree_1', 'dir1', 'file2.txt'))) as f: self.assertEquals(f.read(), file_content) # new self.assertEquals(set(b.get_folder_list('.')), set(['tree_1', 'file3.txt'])) self.assertEquals(set(b.get_folder_list('tree_1')), set(['file1.txt', 'dir1', 'dir3'])) self.assertEquals( set(b.get_folder_list(os.path.join('tree_1', 'dir1'))), set(['dir2', 'file2.txt'])) with open(b.get_abs_path(os.path.join('tree_1', 'file1.txt'))) as f: self.assertEquals(f.read(), file_content) with open(b.get_abs_path(os.path.join('tree_1', 'dir1', 'file2.txt'))) as f: self.assertEquals(f.read(), file_content) # This should in principle change the location of the files, # so I recheck a.store() # I now copy after storing c = a.copy() # I overwrite a file, create a new one and remove a directory # in the copy only with tempfile.NamedTemporaryFile() as f: f.write(file_content_different) f.flush() c.add_path(f.name, os.path.join('tree_1', 'file1.txt')) c.add_path(f.name, os.path.join('tree_1', 'dir1', 'file4.txt')) c.remove_path(os.path.join('tree_1', 'dir1', 'dir2')) # check old self.assertEquals(set(a.get_folder_list('.')), set(['tree_1'])) self.assertEquals(set(a.get_folder_list('tree_1')), set(['file1.txt', 'dir1'])) self.assertEquals( set(a.get_folder_list(os.path.join('tree_1', 'dir1'))), set(['dir2', 'file2.txt'])) with open(a.get_abs_path(os.path.join('tree_1', 'file1.txt'))) as f: self.assertEquals(f.read(), file_content) with open(a.get_abs_path(os.path.join('tree_1', 'dir1', 'file2.txt'))) as f: self.assertEquals(f.read(), file_content) # check new self.assertEquals(set(c.get_folder_list('.')), set(['tree_1'])) self.assertEquals(set(c.get_folder_list('tree_1')), set(['file1.txt', 'dir1'])) self.assertEquals( set(c.get_folder_list(os.path.join('tree_1', 'dir1'))), set(['file2.txt', 'file4.txt'])) with open(c.get_abs_path(os.path.join('tree_1', 'file1.txt'))) as f: self.assertEquals(f.read(), file_content_different) with open(c.get_abs_path(os.path.join('tree_1', 'dir1', 'file2.txt'))) as f: self.assertEquals(f.read(), file_content) # garbage cleaning shutil.rmtree(directory)
def test_attributes_on_copy(self): import copy a = Node() attrs_to_set = { 'none': None, 'bool': self.boolval, 'integer': self.intval, 'float': self.floatval, 'string': self.stringval, 'dict': self.dictval, 'list': self.listval, 'emptydict': {}, 'emptylist': [], } for k, v in attrs_to_set.iteritems(): a._set_attr(k, v) a.store() # I now set extras extras_to_set = { 'bool': 'some non-boolean value', 'some_other_name': 987 } for k, v in extras_to_set.iteritems(): a.set_extra(k, v) # I make a copy b = a.copy() # I modify an attribute and add a new one; I mirror it in the dictionary # for later checking b_expected_attributes = copy.deepcopy(attrs_to_set) b._set_attr('integer', 489) b_expected_attributes['integer'] = 489 b._set_attr('new', 'cvb') b_expected_attributes['new'] = 'cvb' # I check before storing that the attributes are ok self.assertEquals({k: v for k, v in b.iterattrs()}, b_expected_attributes) # Note that during copy, I do not copy the extras! self.assertEquals({k: v for k, v in b.iterextras()}, {}) # I store now b.store() # and I finally add a extras b.set_extra('meta', 'textofext') b_expected_extras = {'meta': 'textofext'} # Now I check for the attributes # First I check that nothing has changed self.assertEquals({k: v for k, v in a.iterattrs()}, attrs_to_set) self.assertEquals({k: v for k, v in a.iterextras()}, extras_to_set) # I check then on the 'b' copy self.assertEquals({k: v for k, v in b.iterattrs()}, b_expected_attributes) self.assertEquals({k: v for k, v in b.iterextras()}, b_expected_extras)