def test_writes(self): """ More overhead then a normal write() but none the less, using the write() in this class keeps things simple since the file is automatically opened if it was otherwise closed """ # First we create a 1MB file tmp_file = join(self.tmp_dir, 'NNTPContent_Test.write', 'tmp.file') # File should not already exist assert (isfile(tmp_file) is False) # Now we want to create our NNTPContent() object surrouding this # file that does not exist. content = NNTPContent(filepath=tmp_file, work_dir=dirname(tmp_file)) # It's worth noting that this file will 'still' not exist assert (isfile(tmp_file) is False) # we'll write data data = 'hello\r\n' content.write(data) # It's worth noting that this file will ''STILL'' not exist assert (isfile(tmp_file) is False) # Save content assert (content.save() is True) # Now the file 'will' exist assert (isfile(tmp_file) is True) # Open our file and verify it is the data we saved. with open(tmp_file) as f: data_read = f.read() assert (data == data_read)
def test_writes(self): """ More overhead then a normal write() but none the less, using the write() in this class keeps things simple since the file is automatically opened if it was otherwise closed """ # First we create a 1MB file tmp_file = join(self.tmp_dir, 'NNTPContent_Test.write', 'tmp.file') # File should not already exist assert(isfile(tmp_file) is False) # Now we want to create our NNTPContent() object surrouding this # file that does not exist. content = NNTPContent(filepath=tmp_file, work_dir=dirname(tmp_file)) # It's worth noting that this file will 'still' not exist assert(isfile(tmp_file) is False) # we'll write data data = 'hello\r\n' content.write(data) # It's worth noting that this file will ''STILL'' not exist assert(isfile(tmp_file) is False) # Save content assert(content.save() is True) # Now the file 'will' exist assert(isfile(tmp_file) is True) # Open our file and verify it is the data we saved. with open(tmp_file) as f: data_read = f.read() assert(data == data_read)
def test_split(self): """ Test the split() function """ # First we create a 1MB file tmp_file = join(self.tmp_dir, 'NNTPContent_Test.chunk', '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 = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir) # Loaded files are always detached; The following is loaded # because the path exists. assert (content.is_attached() is False) # We'll split it in 2 results = content.split(strsize_to_bytes('512K')) # Tests that our results are expected assert (isinstance(results, sortedset) is True) assert (len(results) == 2) # We support passing the string format directly in too results = content.split('512K') # Tests that our results are expected assert (isinstance(results, sortedset) is True) assert (len(results) == 2) # Now lets merge them into one again content = NNTPContent(work_dir=self.tmp_dir) assert (content.load(results) is True) # NNTPContent() sets as well as individual objects passed into # load are always attached by default assert (content.is_attached() is True) # Our combined file should be the correct filesize assert (len(content) == strsize_to_bytes('1M')) # Once we save our object, it is no longer attached assert (content.save(filepath=tmp_file) is True) # Now our content is no longer attached assert (content.is_attached() is False) # we'll re-attach it content.attach() # Our file will be gone now if we try to delete it assert (content.is_attached() is True) assert (isfile(tmp_file) is True) del content assert (isfile(tmp_file) is False)
def test_split(self): """ Test the split() function """ # First we create a 1MB file tmp_file = join(self.tmp_dir, 'NNTPContent_Test.chunk', '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 = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir) # Loaded files are always detached; The following is loaded # because the path exists. assert(content.is_attached() is False) # We'll split it in 2 results = content.split(strsize_to_bytes('512K')) # Tests that our results are expected assert(isinstance(results, sortedset) is True) assert(len(results) == 2) # We support passing the string format directly in too results = content.split('512K') # Tests that our results are expected assert(isinstance(results, sortedset) is True) assert(len(results) == 2) # Now lets merge them into one again content = NNTPContent(work_dir=self.tmp_dir) assert(content.load(results) is True) # NNTPContent() sets as well as individual objects passed into # load are always attached by default assert(content.is_attached() is True) # Our combined file should be the correct filesize assert(len(content) == strsize_to_bytes('1M')) # Once we save our object, it is no longer attached assert(content.save(filepath=tmp_file) is True) # Now our content is no longer attached assert(content.is_attached() is False) # we'll re-attach it content.attach() # Our file will be gone now if we try to delete it assert(content.is_attached() is True) assert(isfile(tmp_file) is True) del content assert(isfile(tmp_file) is False)
def test_checksum(self): """Test the assorted checksums supported """ # First we create a 1MB file tmp_file = join( self.tmp_dir, 'NNTPContent_Test.checksum', 'tmpa.tmp') # File should not already exist assert(isfile(tmp_file) is False) # Create a random 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 = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir) md5 = content.md5() sha1 = content.sha1() sha256 = content.sha256() assert(md5 is not None) assert(sha1 is not None) assert(sha256 is not None) tmp_file_2 = join( self.tmp_dir, 'NNTPContent_Test.checksum', 'tmp2.rar') # File should not already exist assert(isfile(tmp_file_2) is False) # We'll create a copy of our file assert(content.save(filepath=tmp_file_2, copy=True) is True) # Now it should assert(isfile(tmp_file_2) is True) # Now we'll open the new file we created content_2 = NNTPContent(filepath=tmp_file_2, work_dir=self.tmp_dir) md5_2 = content_2.md5() sha1_2 = content_2.sha1() sha256_2 = content_2.sha256() assert(md5_2 is not None) assert(sha1_2 is not None) assert(sha256_2 is not None) # files should be the same assert(md5 == md5_2) assert(sha1 == sha1_2) assert(sha256 == sha256_2)
def test_checksum(self): """Test the assorted checksums supported """ # First we create a 1MB file tmp_file = join(self.tmp_dir, 'NNTPContent_Test.checksum', 'tmpa.tmp') # File should not already exist assert (isfile(tmp_file) is False) # Create a random 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 = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir) md5 = content.md5() sha1 = content.sha1() sha256 = content.sha256() assert (md5 is not None) assert (sha1 is not None) assert (sha256 is not None) tmp_file_2 = join(self.tmp_dir, 'NNTPContent_Test.checksum', 'tmp2.rar') # File should not already exist assert (isfile(tmp_file_2) is False) # We'll create a copy of our file assert (content.save(filepath=tmp_file_2, copy=True) is True) # Now it should assert (isfile(tmp_file_2) is True) # Now we'll open the new file we created content_2 = NNTPContent(filepath=tmp_file_2, work_dir=self.tmp_dir) md5_2 = content_2.md5() sha1_2 = content_2.sha1() sha256_2 = content_2.sha256() assert (md5_2 is not None) assert (sha1_2 is not None) assert (sha256_2 is not None) # files should be the same assert (md5 == md5_2) assert (sha1 == sha1_2) assert (sha256 == sha256_2)
def test_saves(self): """ Saving allows for a variety of inputs, test that they all check out okay """ # First we create a 1MB file tmp_file = join(self.tmp_dir, 'NNTPContent_Test.save', 'testfile.tmp') # File should not already exist assert (isfile(tmp_file) is False) # Create a random file assert (self.touch(tmp_file, size='5MB', 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 = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir) # Test our file exists assert (len(content) == strsize_to_bytes('5M')) # By default our load makes it so our file is NOT attached assert (content.is_attached() is False) # as we can't make a copy of our current file on top of the old _filepath = content.path() assert (content.save(copy=True) is True) assert (content.path() == _filepath) assert (content.path() == tmp_file) # File should still be detached assert (content.is_attached() is False) # Filepath shoul still not have changed # Let's attach it content.attach() assert (content.is_attached() is True) # If we call save() a copy parameter, there should be no change assert (content.save(copy=True) is True) # Still no change assert (content.is_attached() is True) # Now lets actually copy it to a new location tmp_file_copy = join(self.tmp_dir, 'NNTPContent_Test.save', 'testfile.copy.tmp') # File should not already exist assert (isfile(tmp_file_copy) is False) # call save using our copy variable and new filename assert (content.save(tmp_file_copy, copy=True) is True) # File should exist now assert (isfile(tmp_file_copy) is True) # Old File should still exist too assert (isfile(tmp_file) is True) # Path should still be the old path and not the new assert (content.path() == tmp_file) # Still no change in attachment assert (content.is_attached() is True) # Create a new file now tmp_file_copy2 = join(self.tmp_dir, 'NNTPContent_Test.save', 'testfile.copy2.tmp') assert (isfile(tmp_file_copy2) is False) # call save with copy set to false; This performs an official # move (adjusting our internal records. assert (content.save(tmp_file_copy2, copy=False) is True) # Old File should no longer exist assert (isfile(tmp_file) is False) # Content should be detached assert (content.is_attached() is False) # New file should exist assert (isfile(tmp_file_copy2) is True) assert (content.path() != _filepath) assert (content.path() == tmp_file_copy2)
def test_saves(self): """ Saving allows for a variety of inputs, test that they all check out okay """ # First we create a 1MB file tmp_file = join( self.tmp_dir, 'NNTPContent_Test.save', 'testfile.tmp') # File should not already exist assert(isfile(tmp_file) is False) # Create a random file assert(self.touch(tmp_file, size='5MB', 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 = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir) # Test our file exists assert(len(content) == strsize_to_bytes('5M')) # By default our load makes it so our file is NOT attached assert(content.is_attached() is False) # as we can't make a copy of our current file on top of the old _filepath = content.path() assert(content.save(copy=True) is True) assert(content.path() == _filepath) assert(content.path() == tmp_file) # File should still be detached assert(content.is_attached() is False) # Filepath shoul still not have changed # Let's attach it content.attach() assert(content.is_attached() is True) # If we call save() a copy parameter, there should be no change assert(content.save(copy=True) is True) # Still no change assert(content.is_attached() is True) # Now lets actually copy it to a new location tmp_file_copy = join( self.tmp_dir, 'NNTPContent_Test.save', 'testfile.copy.tmp') # File should not already exist assert(isfile(tmp_file_copy) is False) # call save using our copy variable and new filename assert(content.save(tmp_file_copy, copy=True) is True) # File should exist now assert(isfile(tmp_file_copy) is True) # Old File should still exist too assert(isfile(tmp_file) is True) # Path should still be the old path and not the new assert(content.path() == tmp_file) # Still no change in attachment assert(content.is_attached() is True) # Create a new file now tmp_file_copy2 = join( self.tmp_dir, 'NNTPContent_Test.save', 'testfile.copy2.tmp') assert(isfile(tmp_file_copy2) is False) # call save with copy set to false; This performs an official # move (adjusting our internal records. assert(content.save(tmp_file_copy2, copy=False) is True) # Old File should no longer exist assert(isfile(tmp_file) is False) # Content should be detached assert(content.is_attached() is False) # New file should exist assert(isfile(tmp_file_copy2) is True) assert(content.path() != _filepath) assert(content.path() == tmp_file_copy2)