Beispiel #1
0
    def test_with(self):
        """
        Test the use of the with clause
        """
        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.with', '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)
        content_a.attach()

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

        with content_a as fp:
            while 1:
                # Read our data
                buf = fp.read(1024)
                if not buf:
                    break

                # Write our content to disk
                content_b.write(buf)

        # At this point we should have a duplicate of our original object
        assert (len(content_a) == len(content_b))
        assert (content_a.md5() == content_b.md5())
Beispiel #2
0
    def test_with(self):
        """
        Test the use of the with clause
        """
        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.with', '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)
        content_a.attach()

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

        with content_a as fp:
            while 1:
                # Read our data
                buf = fp.read(1024)
                if not buf:
                    break

                # Write our content to disk
                content_b.write(buf)

        # At this point we should have a duplicate of our original object
        assert(len(content_a) == len(content_b))
        assert(content_a.md5() == content_b.md5())
Beispiel #3
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)
Beispiel #4
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)
Beispiel #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())
Beispiel #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())
Beispiel #7
0
    def test_directory_support(self):
        """
        NNTPContent objects can wrap directories too

        """
        my_dir = join(self.tmp_dir, 'NNTPContent', 'my_dir')
        assert (isdir(my_dir) is False)
        assert (mkdir(my_dir) is True)
        assert (isdir(my_dir) is True)

        #  Now create our NNTPContent Object against our directory
        obj = NNTPContent(
            filepath=my_dir,
            work_dir=self.tmp_dir,
        )

        # Directories are never attached by default
        assert (obj.is_attached() is False)

        # Deleting the object means the directory remains behind
        del obj
        assert (isdir(my_dir) is True)

        # However
        obj = NNTPContent(
            filepath=my_dir,
            work_dir=self.tmp_dir,
        )
        assert (obj.is_attached() is False)
        obj.attach()
        assert (obj.is_attached() is True)

        # Now we've attached the NNTPContent to the object so deleting the
        # object destroys the directory
        del obj
        assert (exists(my_dir) is False)
        assert (isdir(my_dir) is False)
Beispiel #8
0
    def test_directory_support(self):
        """
        NNTPContent objects can wrap directories too

        """
        my_dir = join(self.tmp_dir, 'NNTPContent', 'my_dir')
        assert(isdir(my_dir) is False)
        assert(mkdir(my_dir) is True)
        assert(isdir(my_dir) is True)

        #  Now create our NNTPContent Object against our directory
        obj = NNTPContent(
            filepath=my_dir,
            work_dir=self.tmp_dir,
        )

        # Directories are never attached by default
        assert(obj.is_attached() is False)

        # Deleting the object means the directory remains behind
        del obj
        assert(isdir(my_dir) is True)

        # However
        obj = NNTPContent(
            filepath=my_dir,
            work_dir=self.tmp_dir,
        )
        assert(obj.is_attached() is False)
        obj.attach()
        assert(obj.is_attached() is True)

        # Now we've attached the NNTPContent to the object so deleting the
        # object destroys the directory
        del obj
        assert(exists(my_dir) is False)
        assert(isdir(my_dir) is False)
Beispiel #9
0
    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())
Beispiel #10
0
    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)
Beispiel #11
0
    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())
Beispiel #12
0
    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)