Пример #1
0
    def test_setstate_with_changed_files(self):
        # refresh the sfs
        self.sfs.__enter__()

        # get the original state
        state_before = self.sfs.__getstate__()
        self.assertEqual(len(state_before['things']), self.file_count)

        # delete one of the files
        file_to_remove = self.files.pop()
        localfile_md5 = hashlib.md5(open(file_to_remove,
                                         'rb').read()).hexdigest()
        localfilename = "sf_%s" % localfile_md5

        # remove it from origin
        os.remove(file_to_remove)
        self.assertFalse(file_to_remove in self.files)
        self.assertFalse(os.path.exists(file_to_remove))
        #        print "removed %s" % file_to_remove

        #        # remove it from localdir
        localfile_to_remove = os.path.join(self.localdir, localfilename)
        os.remove(localfile_to_remove)
        self.assertFalse(os.path.exists(localfile_to_remove))

        # create a new sfs
        new_sfs = SeedfileSet()
        new_sfs.__setstate__(state_before)

        self.assertEqual(len(new_sfs.things), (self.file_count - 1))

        #        print "Newthings: %s" % new_sfs.things.keys()
        for k, thing in state_before['things'].iteritems():
            #            print "k: %s" % k
            if k == localfile_md5:
                self.assertFalse(k in new_sfs.things)
                continue
            else:
                # is there a corresponding thing in sfs?
                self.assertTrue(k in new_sfs.things)

            for x, y in thing.iteritems():
                # was it set correctly?
                sfsthing = new_sfs.things[k].__dict__[x]
                if hasattr(sfsthing, '__dict__'):
                    # some things are complex objects themselves
                    # so we have to compare their __dict__ versions
                    self._same_dict(y, sfsthing.__dict__)
                else:
                    # others are just simple objects and we can
                    # compare them directly
                    self.assertEqual(y, sfsthing)

        self.assertEqual(self.file_count - 1, new_sfs.sfcount)
Пример #2
0
    def test_setstate_with_changed_files(self):
        # refresh the sfs
        self.sfs.__enter__()

        # get the original state
        state_before = self.sfs.__getstate__()
        self.assertEqual(len(state_before['things']), self.file_count)

        # delete one of the files
        file_to_remove = self.files.pop()
        localfile_md5 = hashlib.md5(open(file_to_remove, 'rb').read()).hexdigest()
        localfilename = "sf_%s" % localfile_md5

        # remove it from origin
        os.remove(file_to_remove)
        self.assertFalse(file_to_remove in self.files)
        self.assertFalse(os.path.exists(file_to_remove))
#        print "removed %s" % file_to_remove

#        # remove it from localdir
        localfile_to_remove = os.path.join(self.localdir, localfilename)
        os.remove(localfile_to_remove)
        self.assertFalse(os.path.exists(localfile_to_remove))

        # create a new sfs
        new_sfs = SeedfileSet()
        new_sfs.__setstate__(state_before)

        self.assertEqual(len(new_sfs.things), (self.file_count - 1))

#        print "Newthings: %s" % new_sfs.things.keys()
        for k, thing in state_before['things'].iteritems():
#            print "k: %s" % k
            if k == localfile_md5:
                self.assertFalse(k in new_sfs.things)
                continue
            else:
                # is there a corresponding thing in sfs?
                self.assertTrue(k in new_sfs.things)

            for x, y in thing.iteritems():
                # was it set correctly?
                sfsthing = new_sfs.things[k].__dict__[x]
                if hasattr(sfsthing, '__dict__'):
                    # some things are complex objects themselves
                    # so we have to compare their __dict__ versions
                    self._same_dict(y, sfsthing.__dict__)
                else:
                    # others are just simple objects and we can
                    # compare them directly
                    self.assertEqual(y, sfsthing)

        self.assertEqual(self.file_count - 1, new_sfs.sfcount)