Beispiel #1
0
    def testAbsoluteStreamCollection(self):
        class Collection(AbsoluteStreamCollection):
            streamDict = {1: StringStream, 2: IntStream}

        c = Collection()
        c.addStream(1, StringStream("first"))
        c.addStream(1, StringStream("second"))
        c.addStream(1, StringStream("third"))
        c.addStream(2, IntStream(1))
        c.addStream(2, IntStream(2))
        c.addStream(2, IntStream(3))

        c2 = Collection(c.freeze())
        assert (c == c2)

        c2.addStream(2, IntStream(4))
        c2.delStream(2, IntStream(3))
        d = c2.diff(c)
        assert (
            d ==
            '\x01\x00\x05first\x01\x00\x06second\x01\x00\x05third\x02\x00\x04\x00\x00\x00\x01\x02\x00\x04\x00\x00\x00\x02\x02\x00\x04\x00\x00\x00\x04'
        )

        c.twm(d, c)
        assert (c == c2)
        assert (c2.diff(c) == d)

        c3 = Collection(d)
        assert (c2 == c3)
Beispiel #2
0
    def testOrderedStreamCollection(self):
        class Collection(OrderedStreamCollection):
            streamDict = {1: StringStream}

        c = Collection()
        # make sure we can store Great Big Stuff
        s = ' ' * (1 << 17)
        c.addStream(1, StringStream(s))
        frz = c.freeze()
        self.assertEqual(frz, '\x01\x80\x02\x00\x00' + s)

        c = Collection()
        # make sure we can store smaller stuff
        s = ' ' * 63
        c.addStream(1, StringStream(s))
        frz = c.freeze()
        self.assertEqual(frz, '\x01?' + s)

        s2 = ' ' * 64
        c.addStream(1, StringStream(s2))
        frz = c.freeze()
        self.assertEqual(frz, '\x01?' + s + '\x01@@' + s2)

        # test add via diff
        c1 = Collection()
        c1.addStream(1, StringStream('one'))
        c1.addStream(1, StringStream('two'))
        c2 = Collection(c1.freeze())
        c2.addStream(1, StringStream('three'))
        c2.addStream(1, StringStream('four'))
        diff = c2.diff(c1)
        c1.twm(diff, c1)
        self.assertEqual(c1, c2)

        # test del via diff
        c1 = Collection()
        c1.addStream(1, StringStream('one'))
        c1.addStream(1, StringStream('two'))
        c2 = Collection(c1.freeze())
        c1.addStream(1, StringStream('three'))
        diff = c2.diff(c1)
        c1.twm(diff, c1)
        self.assertEqual(c1, c2)
        self.assertEqual([x() for x in c1.getStreams(1)], ['one', 'two'])
Beispiel #3
0
    def testStreamCollection(self):
        class Collection(StreamCollection):
            streamDict = {1: StringStream, 2: IntStream}

        c = Collection()
        c.addStream(1, StringStream("first"))
        c.addStream(1, StringStream("second"))
        c.addStream(1, StringStream("third"))
        assert (sorted([x() for x in c.getStreams(1)
                        ]) == ["first", "second", "third"])
        assert (sorted([x[1]()
                        for x in c.iterAll()]) == ["first", "second", "third"])

        c.addStream(2, IntStream(1))
        c.addStream(2, IntStream(2))
        c.addStream(2, IntStream(3))
        assert (sorted([x() for x in c.getStreams(2)]) == [1, 2, 3])
        assert (sorted([x[1]() for x in c.iterAll()
                        ]) == [1, 2, 3, 'first', 'second', 'third'])

        c2 = Collection(c.freeze())
        assert (c == c2)

        c2.addStream(2, IntStream(4))
        c2.delStream(2, IntStream(3))
        d = c2.diff(c)
        assert (
            d ==
            '\x00\x01\x00\x01\x02\x00\x04\x00\x00\x00\x03\x02\x00\x04\x00\x00\x00\x04'
        )
        c.twm(d, c)
        assert (c == c2)
        assert (c2.diff(c) is None)

        # test overflow
        c2 = Collection()
        c2.addStream(1, StringStream(' ' * 65536))
        self.assertRaises(OverflowError, c2.freeze)
Beispiel #4
0
    def testUnicodeStringStreamSet(self):
        ss = StringStream()
        ns = 'Foo bar baz quux'
        us = u'Iñtërnâtiônàlizætiøn'

        # sanity check on regular non-unicode string
        ss.set(ns)
        assert (ss.freeze() == ns)

        # now for the unicode string
        ss.set(us)
        assert (ss.freeze() == us.encode('utf-8'))
        nss = StringStream()
        nss.thaw(ss.freeze())
        assert (ss == nss)
        assert (nss() == us.encode('utf-8'))
Beispiel #5
0
    def testString(self):
        s = StringStream("test")
        s2 = StringStream(s.freeze())
        assert (s == s2)

        s2 = StringStream("another")
        assert (s != s2)
        diff = s.diff(s2)
        assert (diff == "test")
        diff2 = s2.diff(s)
        assert (diff2 == "another")
        # after the merge, s2 will be "test"
        assert (not s2.twm(diff, s2))
        assert (s == s2)

        s3 = StringStream("yet one more")
        # check conflict behavior, this should return True
        assert (s2.twm(diff2, s3))
        # doing a merge to the final value should work
        diff = s3.diff(s)
        assert (not s3.twm(diff, s2))

        # check string streams that whole None as the value
        s4 = StringStream(None)
        assert (s3 != s4)
        assert (s3 > s4)
        assert (s4 < s3)
        # attempt to twm into a string that has a value of None
        # and a conflicting "other"
        assert (s4.twm(diff, s2))

        # ensure that two None string streams compare equal
        s5 = StringStream(None)
        assert (s4 == s5)

        # ensure that invalid types are handled properly
        try:
            StringStream(1)
        except TypeError, e:
            assert (str(e) == 'frozen value must be None or a string')
Beispiel #6
0
    def testUnicodeStringStreamSet(self):
        ss = StringStream()
        ns = 'Foo bar baz quux'
        us = u'Iñtërnâtiônàlizætiøn'

        # sanity check on regular non-unicode string
        ss.set(ns)
        assert(ss.freeze() == ns)

        # now for the unicode string
        ss.set(us)
        assert(ss.freeze() == us.encode('utf-8'))
        nss = StringStream()
        nss.thaw(ss.freeze())
        assert(ss == nss)
        assert(nss() == us.encode('utf-8'))
Beispiel #7
0
    def testString(self):
        s = StringStream("test")
        s2 = StringStream(s.freeze())
        assert(s == s2)

        s2 = StringStream("another")
        assert(s != s2)
        diff = s.diff(s2)
        assert(diff == "test")
        diff2 = s2.diff(s)
        assert(diff2 == "another")
        # after the merge, s2 will be "test"
        assert(not s2.twm(diff, s2))
        assert(s == s2)

        s3 = StringStream("yet one more")
        # check conflict behavior, this should return True
        assert(s2.twm(diff2, s3))
        # doing a merge to the final value should work
        diff = s3.diff(s)
        assert(not s3.twm(diff, s2))

        # check string streams that whole None as the value
        s4 = StringStream(None)
        assert(s3 != s4)
        assert(s3 > s4)
        assert(s4 < s3)
        # attempt to twm into a string that has a value of None
        # and a conflicting "other"
        assert(s4.twm(diff, s2))

        # ensure that two None string streams compare equal
        s5 = StringStream(None)
        assert(s4 == s5)

        # ensure that invalid types are handled properly
        try:
            StringStream(1)
        except TypeError, e:
            assert (str(e) == 'frozen value must be None or a string')