Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    def test_invalid_split_cases(self):
        """
        Test errors that are generated out of the split function
        """
        work_dir = join(self.tmp_dir, 'NNTPContent_Test.chunk')
        # Now we want to load it into a NNTPContent object
        content = NNTPContent(work_dir=work_dir)

        # Nothing to split gives an error
        assert (content.split() is None)

        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.chunk', '5K.rar')
        assert (isfile(tmp_file) is False)
        assert (self.touch(tmp_file, size='1MB', random=True) is True)
        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)

        # No size to split on gives an error
        assert (content.split(size=0) is None)
        assert (content.split(size=-1) is None)
        assert (content.split(size=None) is None)
        assert (content.split(size='bad_string') is None)

        # Invalid Memory Limit
        assert (content.split(mem_buf=0) is None)
        assert (content.split(mem_buf=-1) is None)
        assert (content.split(mem_buf=None) is None)
        assert (content.split(mem_buf='bad_string') is None)
Ejemplo n.º 4
0
    def test_invalid_split_cases(self):
        """
        Test errors that are generated out of the split function
        """
        work_dir = join(self.tmp_dir, 'NNTPContent_Test.chunk')
        # Now we want to load it into a NNTPContent object
        content = NNTPContent(work_dir=work_dir)

        # Nothing to split gives an error
        assert(content.split() is None)

        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.chunk', '5K.rar')
        assert(isfile(tmp_file) is False)
        assert(self.touch(tmp_file, size='1MB', random=True) is True)
        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)

        # No size to split on gives an error
        assert(content.split(size=0) is None)
        assert(content.split(size=-1) is None)
        assert(content.split(size=None) is None)
        assert(content.split(size='bad_string') is None)

        # Invalid Memory Limit
        assert(content.split(mem_buf=0) is None)
        assert(content.split(mem_buf=-1) is None)
        assert(content.split(mem_buf=None) is None)
        assert(content.split(mem_buf='bad_string') is None)
Ejemplo n.º 5
0
    def test_append(self):
        """
        Test the split() and then append() function
        """
        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.append', '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)

        # Attach our content
        assert (content_a.is_attached() is False)
        content_a.attach()

        # We'll split it in 4
        results = content_a.split(strsize_to_bytes('512K'))

        # Tests that our results are expected
        assert (isinstance(results, sortedset) is True)
        assert (len(results) == 2)

        # Create a new content object
        content_b = NNTPContent(work_dir=self.tmp_dir)

        # Our content should be attached
        assert (content_a.is_attached() is True)
        assert (content_b.is_attached() is True)

        # For each of our disassembled items, re-assemble them
        for content in results:
            assert (content_b.append(content) is True)

        # Test that our content is the same again
        assert (len(content_a) == len(content_b))
        assert (content_a.md5() == content_b.md5())
Ejemplo n.º 6
0
    def test_append(self):
        """
        Test the split() and then append() function
        """
        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.append', '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)

        # Attach our content
        assert(content_a.is_attached() is False)
        content_a.attach()

        # We'll split it in 4
        results = content_a.split(strsize_to_bytes('512K'))

        # Tests that our results are expected
        assert(isinstance(results, sortedset) is True)
        assert(len(results) == 2)

        # Create a new content object
        content_b = NNTPContent(work_dir=self.tmp_dir)

        # Our content should be attached
        assert(content_a.is_attached() is True)
        assert(content_b.is_attached() is True)

        # For each of our disassembled items, re-assemble them
        for content in results:
            assert(content_b.append(content) is True)

        # Test that our content is the same again
        assert(len(content_a) == len(content_b))
        assert(content_a.md5() == content_b.md5())