Example #1
0
    def test_use_short_keys(self):

        r=ObjectRegistry(min_objid_len=8,use_short_keys=True)
        HASH_LEN =len(r.hash_obj(object())) # should be 40 for sha1
        self.assertEqual(len(r.register(object())), 8)

        r=ObjectRegistry(min_objid_len=7,use_short_keys=True)
        self.assertEqual(len(r.register(object())), 7)

        r=ObjectRegistry(min_objid_len=7,use_short_keys=False)
        self.assertEqual(len(r.register(object())), HASH_LEN)

        # must use full key when short_keys=False
        self.assertEqual(r.get(r.register(object())[:-1]), None)
Example #2
0
    def test_return_min_len_key_more(self):
        def full_id(partial):
            return [x for x in d if x.startswith(partial)][0]
        # start returning keys with len >=8 (MIN_OBJID_LEN)
        # force collisions by building keys and shoving them
        # into the registry
        # then reregister same object and make sure
        # we get back an objid which is longer then
        # previous iteration, and that we can get back the object
        # # with it.

        r=ObjectRegistry(min_objid_len=8)
        d=r._registry

        HASH_LEN =len(r.hash_obj(object())) # should be 40 for sha1

        objs = [object()]
        partial = r.register(objs[-1])
        oid = full_id(partial)
        for i in range(8,HASH_LEN):
        # now we've yanked a reference out, let's inject
        # back in with the same (i+1)-prefix
            print(d.keys())
            partial += '_'
            new_oid = partial + oid[i+1:]

            # use the registry to store a reference,
            # then yank it out and reinsert with new_oid
            objs.append(object())
            oid = full_id(r.register(objs[-1]))
            d[new_oid] = d.pop(oid)

        # check that the prefix gets us the original object
        # and that the prefix+_ gets us the new object
        # and that registering the same object, gets us the
        # new id
        #     self.assertEqual(r.get(partial[:-1]), objs[-2])

            self.assertEqual(r.get(partial[:-1]), objs[-2])
            self.assertEqual(r.get(partial), objs[-1])
Example #3
0
    def test_return_min_len_key_more2(self):
        def full_id(partial):
            return [x for x in d if x.startswith(partial)][0]
            # start returning keys with len >=8 (MIN_OBJID_LEN)
            # force collisions by building keys and shoving them
            # into the registry
            # then reregister same object and make sure
            # we get back an objid which is longer then
            # previous iteration, and that we can get back the object
            # # with it.

        MIN_OBJID_LEN=8

        r=ObjectRegistry(min_objid_len=MIN_OBJID_LEN)
        d=r._registry

        HASH_LEN =len(r.hash_obj(object())) # should be 40 for sha1

        ##### new test
        for i in range(MIN_OBJID_LEN,HASH_LEN-1):
            o = object()
            oid = r.register(o)
            foid = full_id(oid)
            new_oid = foid[i:] + '_' * (HASH_LEN-i)
            d[new_oid] = d.pop(foid)
            self.assertTrue(len(r.register(o)) >= len(oid))

        for j in range(100):
            import random
            o = object()
            oid = r.register(o)
            foid = full_id(oid)
            i=random.randint(MIN_OBJID_LEN,HASH_LEN-1)
            new_oid = foid[i:] + '_' * (HASH_LEN-i)
            d[new_oid] = d.pop(foid)
            self.assertTrue(len(r.register(o)) >= len(oid))