def test_copy02(self): """ Test the copy() function some more """ # First we create a 1MB file tmp_file = join(self.tmp_dir, 'NNTPContent_Test.copy', '1MB.rar') # File should not already exist assert (isfile(tmp_file) is False) # Create our file assert (self.touch(tmp_file, size='1MB', random=True) is True) # File should exist now assert (isfile(tmp_file) is True) # Now we want to load it into a NNTPContent object content_a = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir) # We want to make a copy of it content_b = content_a.copy() # Our content should be the same assert (len(content_a) == len(content_b)) assert (content_a.md5() == content_b.md5()) # Because we made a copy, we're working with a different file however # we share the exact same content and filename identifier. assert (content_a == content_b) # Our paths are not the same though; this is what makes us a official # copy assert (content_a.path() != content_b.path())
def test_copy02(self): """ Test the copy() function some more """ # First we create a 1MB file tmp_file = join(self.tmp_dir, 'NNTPContent_Test.copy', '1MB.rar') # File should not already exist assert(isfile(tmp_file) is False) # Create our file assert(self.touch(tmp_file, size='1MB', random=True) is True) # File should exist now assert(isfile(tmp_file) is True) # Now we want to load it into a NNTPContent object content_a = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir) # We want to make a copy of it content_b = content_a.copy() # Our content should be the same assert(len(content_a) == len(content_b)) assert(content_a.md5() == content_b.md5()) # Because we made a copy, we're working with a different file however # we share the exact same content and filename identifier. assert(content_a == content_b) # Our paths are not the same though; this is what makes us a official # copy assert(content_a.path() != content_b.path())
def test_from_bestguess(self): """ test from_bestguess() bestguess() does the best of both worlds: from_file() and from_filename(). It never returns None unless you give it bad data. """ # Initialize our mime object m = Mime() # Empty content just gives us an empty response assert(m.from_bestguess(None) is None) assert(m.from_bestguess("") is None) assert(m.from_bestguess(u"") is None) # First we take a binary file image = join(self.var_dir, 'joystick.jpg') c = NNTPContent(image, work_dir=self.tmp_dir) copy = c.copy() # since we have a filename, we can pick it up from that assert(m.from_bestguess(copy.filename).type() == 'image/jpeg') # We can also get it from_file() because even though our temporary # file does not have an extension at all, we can still assert(m.from_bestguess(copy.path()).type() == 'image/jpeg')
def test_copy01(self): """ Test our copy function The copy function allows us to duplicate an existing NNTPContent object without obstructing the original. Copied content is always attached; so if the object falls out of scope; so does the file. """ my_dir = join(self.tmp_dir, 'NNTPContent', 'test_copy') assert (isdir(my_dir) is False) assert (mkdir(my_dir) is True) assert (isdir(my_dir) is True) # Now create our NNTPContent object witin our directory obj = NNTPContent( filepath='myfile', work_dir=my_dir, ) # Content is attached by default assert (obj.is_attached() is True) obj.detach() assert (obj.is_attached() is False) new_dir = join(my_dir, 'copy') assert (isdir(new_dir) is False) # Create a copy of our object obj_copy = obj.copy() # Successfully loaded files are never attached reguardless # of the original copy assert (obj_copy.is_attached() is True) # Reattach the original so it dies when this test is over obj.attach() assert (obj.is_attached() is True) # Create a copy of our copy obj_copy2 = obj_copy.copy() assert (obj_copy2.is_attached() is True) assert (isfile(obj_copy2.path())) _path = obj_copy2.path() del obj_copy2 assert (isfile(_path) is False) assert (isfile(obj_copy.path()) is True) _path = obj_copy.path() del obj_copy assert (isfile(_path) is False) assert (isfile(obj.path()) is True) _path = obj.path() del obj assert (isfile(_path) is False) # now lets do a few more tests but with actual files this time tmp_file = join(my_dir, '2MB.zip') assert (self.touch(tmp_file, size='2MB', random=True) is True) # Now create our NNTPContent object witin our directory obj = NNTPContent( filepath=tmp_file, work_dir=my_dir, ) assert (isfile(obj.path()) is True) obj_copy = obj.copy() assert (isfile(obj_copy.path()) is True) stats = stat(obj_copy.path()) assert (bytes_to_strsize(stats['size']) == "2.00MB") # Compare that our content is the same assert (compare(obj_copy.path(), obj.path()) is True) # note that the filenames are NOT the same so we are dealing with 2 # distinct copies here assert (isfile(obj_copy.path()) is True) assert (isfile(obj.path()) is True) assert (obj.path() != obj_copy.path())
def test_copy01(self): """ Test our copy function The copy function allows us to duplicate an existing NNTPContent object without obstructing the original. Copied content is always attached; so if the object falls out of scope; so does the file. """ my_dir = join(self.tmp_dir, 'NNTPContent', 'test_copy') assert(isdir(my_dir) is False) assert(mkdir(my_dir) is True) assert(isdir(my_dir) is True) # Now create our NNTPContent object witin our directory obj = NNTPContent( filepath='myfile', work_dir=my_dir, ) # Content is attached by default assert(obj.is_attached() is True) obj.detach() assert(obj.is_attached() is False) new_dir = join(my_dir, 'copy') assert(isdir(new_dir) is False) # Create a copy of our object obj_copy = obj.copy() # Successfully loaded files are never attached reguardless # of the original copy assert(obj_copy.is_attached() is True) # Reattach the original so it dies when this test is over obj.attach() assert(obj.is_attached() is True) # Create a copy of our copy obj_copy2 = obj_copy.copy() assert(obj_copy2.is_attached() is True) assert(isfile(obj_copy2.path())) _path = obj_copy2.path() del obj_copy2 assert(isfile(_path) is False) assert(isfile(obj_copy.path()) is True) _path = obj_copy.path() del obj_copy assert(isfile(_path) is False) assert(isfile(obj.path()) is True) _path = obj.path() del obj assert(isfile(_path) is False) # now lets do a few more tests but with actual files this time tmp_file = join(my_dir, '2MB.zip') assert(self.touch(tmp_file, size='2MB', random=True) is True) # Now create our NNTPContent object witin our directory obj = NNTPContent( filepath=tmp_file, work_dir=my_dir, ) assert(isfile(obj.path()) is True) obj_copy = obj.copy() assert(isfile(obj_copy.path()) is True) stats = stat(obj_copy.path()) assert(bytes_to_strsize(stats['size']) == "2.00MB") # Compare that our content is the same assert(compare(obj_copy.path(), obj.path()) is True) # note that the filenames are NOT the same so we are dealing with 2 # distinct copies here assert(isfile(obj_copy.path()) is True) assert(isfile(obj.path()) is True) assert(obj.path() != obj_copy.path())