示例#1
0
    def testUnknownStreamSetEntries(self):
        class Subset(StreamSet):
            ignoreUnknown = PRESERVE_UNKNOWN
            streamDict = {1: (SMALL, StringStream, "name")}

        class SubParent(StreamSet):
            ignoreUnknown = PRESERVE_UNKNOWN
            streamDict = {1: (DYNAMIC, Subset, "nested")}

        class Superset(StreamSet):
            streamDict = {
                1: (SMALL, StringStream, "name"),
                2: (SMALL, IntStream, "number"),
                3: (LARGE, IntStream, "number2")
            }

        class SuperParent(StreamSet):
            streamDict = {1: (DYNAMIC, Superset, "nested")}

        super = Superset()
        super.name.set("test")
        super.number.set(12)
        super.number2.set(34)

        sub = Subset(super.freeze())
        assert (sub.freeze() == super.freeze())
        assert (sub.freeze(freezeUnknown=False) == '\x01\x00\x04test')
        known = sub.freeze(freezeUnknown=False)
        unknown = sub.freeze(freezeKnown=False)
        assert (sub.freeze() == known + unknown)
        sub = Subset()
        sub.thaw(known)
        sub.thaw(unknown)
        assert (sub.freeze() == super.freeze())

        super = SuperParent()
        super.nested.name.set("test")
        super.nested.number.set(12)
        super.nested.number2.set(34)

        sub = SubParent(super.freeze())
        assert (sub.freeze() == super.freeze())
        otherSub = SubParent()
        otherSub.nested.name.set("test")
        assert (otherSub.freeze() == sub.freeze(freezeUnknown=False))

        sub2 = SubParent()
        self.assertRaises(ValueError, sub.diff, sub2)
        self.assertRaises(ValueError, sub2.diff, sub)

        old = Superset()
        old.name.set('hello')
        new = Superset()
        new.name.set('world')
        new.number.set(10)
        new.number2.set(20)

        diff = new.diff(old)
        sub = Subset(old.freeze())
        sub.twm(diff, sub)
        assert (sub.freeze() == new.freeze())

        # this sets number2, which means number1 has to get inserted into the
        # unknown list rather than just appended
        old.number2.set(20)
        diff = new.diff(old)
        sub = Subset(old.freeze())
        sub.twm(diff, sub)
        assert (sub.freeze() == new.freeze())

        frz = new.freeze()
        split = splitFrozenStreamSet(frz)

        self.assertEqual(split, [(1, 'world'), (2, '\x00\x00\x00\n'),
                                 (3, '\x00\x00\x00\x14')])
示例#2
0
    def testUnknownStreamSetEntries(self):
        class Subset(StreamSet):
            ignoreUnknown = PRESERVE_UNKNOWN
            streamDict = { 1 : ( SMALL, StringStream, "name" ) }

        class SubParent(StreamSet):
            ignoreUnknown = PRESERVE_UNKNOWN
            streamDict = { 1 : ( DYNAMIC, Subset, "nested" ) }

        class Superset(StreamSet):
            streamDict = { 1 : ( SMALL, StringStream, "name" ),
                           2 : ( SMALL, IntStream, "number" ),
                           3 : ( LARGE, IntStream, "number2" ) }

        class SuperParent(StreamSet):
            streamDict = { 1 : ( DYNAMIC, Superset, "nested" ) }

        super = Superset()
        super.name.set("test")
        super.number.set(12)
        super.number2.set(34)

        sub = Subset(super.freeze())
        assert(sub.freeze() == super.freeze())
        assert(sub.freeze(freezeUnknown = False) == '\x01\x00\x04test')
        known = sub.freeze(freezeUnknown = False)
        unknown = sub.freeze(freezeKnown = False)
        assert(sub.freeze() == known + unknown)
        sub = Subset()
        sub.thaw(known)
        sub.thaw(unknown)
        assert(sub.freeze() == super.freeze())

        super = SuperParent()
        super.nested.name.set("test")
        super.nested.number.set(12)
        super.nested.number2.set(34)

        sub = SubParent(super.freeze())
        assert(sub.freeze() == super.freeze())
        otherSub = SubParent()
        otherSub.nested.name.set("test")
        assert(otherSub.freeze() == sub.freeze(freezeUnknown = False))


        sub2 = SubParent()
        self.assertRaises(ValueError, sub.diff, sub2)
        self.assertRaises(ValueError, sub2.diff, sub)

        old = Superset()
        old.name.set('hello')
        new = Superset()
        new.name.set('world')
        new.number.set(10)
        new.number2.set(20)

        diff = new.diff(old)
        sub = Subset(old.freeze())
        sub.twm(diff, sub)
        assert(sub.freeze() == new.freeze())

        # this sets number2, which means number1 has to get inserted into the
        # unknown list rather than just appended
        old.number2.set(20)
        diff = new.diff(old)
        sub = Subset(old.freeze())
        sub.twm(diff, sub)
        assert(sub.freeze() == new.freeze())

        frz = new.freeze()
        split = splitFrozenStreamSet(frz)

        self.assertEqual(split, [(1, 'world'),
                                     (2, '\x00\x00\x00\n'),
                                     (3, '\x00\x00\x00\x14')])
示例#3
0
    def testStreamSet(self):
        frozen = '\x01\x00\x07thename\x02\x00\x04\x00\x00z\xe3'

        class Rigid(StreamSet):
            streamDict = {1: (SMALL, StringStream, "name")}

        class Flexible(StreamSet):
            streamDict = {1: (SMALL, StringStream, "name")}
            ignoreUnknown = SKIP_UNKNOWN

        class Superset(StreamSet):
            streamDict = {
                1: (SMALL, StringStream, "name"),
                2: (SMALL, IntStream, "number"),
                3: (SMALL, StringStream, "unused")
            }

        # "True" used to mean SKIP_UNKNOWN; make sure it still does
        assert (Flexible.ignoreUnknown == True)

        stream = Superset()
        stream.name.set('thename')
        stream.number.set(31459)
        frz = stream.freeze()
        expected = '\x01\x00\x07thename\x02\x00\x04\x00\x00z\xe3'
        assert (frz == expected)
        assert (stream.find(1, frz)() == "thename")
        assert (stream.find(2, frz)() == 31459)
        assert (stream.find(3, frz) == None)

        # test comparison and hash functions
        stream2 = Superset()
        stream2.name.set('thename')
        stream2.number.set(31459)
        assert (stream == stream2)
        assert (hash(stream) == hash(stream2))
        stream2.number.set(31460)
        assert (stream != stream2)
        assert (hash(stream) != hash(stream2))

        frz = stream2.freeze()
        split = splitFrozenStreamSet(frz)
        self.assertEqual(split, [(1, 'thename'), (2, '\x00\x00z\xe4')])

        # test ignoreUnknown for thawing
        self.assertRaises(ValueError, Rigid, frz)
        stream2 = Flexible(frz)
        assert (stream2.freeze() == '\x01\x00\x07thename')

        # test ignoreUnknown for twm
        first = Superset()
        first.name.set('name')
        first.number.set(4)
        second = Superset()
        second.name.set('another')
        second.number.set(7)
        diff = second.diff(first)

        flex = Flexible()
        flex.name.set('name')
        flex.twm(diff, flex)
        assert (flex.name() == 'another')
        assert (flex.freeze() == '\x01\x00\x07another')

        rigid = Rigid()
        rigid.name.set('name')
        self.assertRaises(ValueError, rigid.twm, diff, rigid)

        # test to make sure that empty streams are excluded properly
        stream.name.set('')
        frz = stream.freeze()
        expected = '\x02\x00\x04\x00\x00z\xe3'
        assert (frz == expected)

        # test the error handling when a class is missing _streamDict
        class Broken(StreamSet):
            streamDicty = {
                1: (SMALL, StringStream, "name"),
                2: (SMALL, IntStream, "number")
            }

        try:
            broken = Broken()
        except ValueError, e:
            assert (str(e) ==
                    'Broken class is missing the streamDict class variable')
示例#4
0
    def testStreamSet(self):
        frozen = '\x01\x00\x07thename\x02\x00\x04\x00\x00z\xe3'

        class Rigid(StreamSet):
            streamDict = { 1 : ( SMALL, StringStream, "name" ) }

        class Flexible(StreamSet):
            streamDict = { 1 : ( SMALL, StringStream, "name" ) }
            ignoreUnknown = SKIP_UNKNOWN

        class Superset(StreamSet):
            streamDict = { 1 : ( SMALL, StringStream, "name" ),
                           2 : ( SMALL, IntStream, "number" ),
                           3 : ( SMALL, StringStream, "unused" ) }

        # "True" used to mean SKIP_UNKNOWN; make sure it still does
        assert(Flexible.ignoreUnknown == True)

        stream = Superset()
        stream.name.set('thename')
        stream.number.set(31459)
        frz = stream.freeze()
        expected = '\x01\x00\x07thename\x02\x00\x04\x00\x00z\xe3'
        assert(frz == expected)
        assert(stream.find(1, frz)() == "thename")
        assert(stream.find(2, frz)() == 31459)
        assert(stream.find(3, frz) == None)

        # test comparison and hash functions
        stream2 = Superset()
        stream2.name.set('thename')
        stream2.number.set(31459)
        assert(stream == stream2)
        assert(hash(stream) == hash(stream2))
        stream2.number.set(31460)
        assert(stream != stream2)
        assert(hash(stream) != hash(stream2))

        frz = stream2.freeze()
        split = splitFrozenStreamSet(frz)
        self.assertEqual(split, [(1, 'thename'), (2, '\x00\x00z\xe4')])

        # test ignoreUnknown for thawing
        self.assertRaises(ValueError, Rigid, frz)
        stream2 = Flexible(frz)
        assert(stream2.freeze() == '\x01\x00\x07thename')

        # test ignoreUnknown for twm
        first = Superset()
        first.name.set('name')
        first.number.set(4)
        second = Superset()
        second.name.set('another')
        second.number.set(7)
        diff = second.diff(first)

        flex = Flexible()
        flex.name.set('name')
        flex.twm(diff, flex)
        assert(flex.name() == 'another')
        assert(flex.freeze() == '\x01\x00\x07another')

        rigid = Rigid()
        rigid.name.set('name')
        self.assertRaises(ValueError, rigid.twm, diff, rigid)

        # test to make sure that empty streams are excluded properly
        stream.name.set('')
        frz = stream.freeze()
        expected = '\x02\x00\x04\x00\x00z\xe3'
        assert(frz == expected)

        # test the error handling when a class is missing _streamDict
        class Broken(StreamSet):
            streamDicty = { 1 : ( SMALL, StringStream, "name" ),
                           2 : ( SMALL, IntStream, "number" ) }
        try:
            broken = Broken()
        except ValueError, e:
            assert(str(e) == 'Broken class is missing the streamDict class variable')