Example #1
0
 def test_default(self):
     s = Serializer()
     a = (None, 'foo', 1)
     b = ('', 'foo', '1')
     c = ('', 'foo', '1')
     for x, y, z in zip(a, b, c):
         self.assertEqual(s.encode(x), y)
         self.assertEqual(s.decode(y), z)
 def test_default(self):
     s = Serializer()
     a = (None, 'foo', 1)
     b = (b'', b'foo', b'1')
     c = ('', 'foo', '1')
     for x, y, z in zip(a, b, c):
         self.assertEqual(s.encode(x), y)
         self.assertEqual(s.decode(y), z)
Example #3
0
 def test_uints_string(self):
     s = Serializer.uints(3, string=True)
     a = ((0, 255, "foo"), (256, (1<<64)-1, ""))
     b = ("\x00\x01\xff\x01\x03foo", ("\x02\x01\x00\x08\xff\xff\xff\xff\xff\xff\xff\xff\x00"))
     c = ((0, 255, "foo"), (256, (1<<64)-1, ""))
     for x, y, z in zip(a, b, c):
         self.assertEqual(s.encode(x), y)
         self.assertEqual(s.decode(y), z)
Example #4
0
 def test_uints(self):
     s = Serializer.uints(2)
     a = ((0, 255), (256, (1<<64)-1))
     b = ("\x00\x01\xff", ("\x02\x01\x00\x08\xff\xff\xff\xff\xff\xff\xff\xff"))
     c = ((0, 255), (256, (1<<64)-1))
     for x, y, z in zip(a, b, c):
         self.assertEqual(s.encode(x), y)
         self.assertEqual(s.decode(y), z)
Example #5
0
 def test_uint(self):
     s = Serializer.uint()
     a = (0, 255, 256, (1<<64)-1)
     b = ("\x00", "\x01\xff", "\x02\x01\x00", "\x08\xff\xff\xff\xff\xff\xff\xff\xff")
     c = (0, 255, 256, (1<<64)-1)
     for x, y, z in zip(a, b, c):
         self.assertEqual(s.encode(x), y)
         self.assertEqual(s.decode(y), z)
 def test_uints_string(self):
     s = Serializer.uints(3, string=True)
     a = ((0, 255, "foo"), (256, (1 << 64) - 1, ""))
     b = (b"\x00\x01\xff\x01\x03foo",
          b"\x02\x01\x00\x08\xff\xff\xff\xff\xff\xff\xff\xff\x00")
     c = ((0, 255, "foo"), (256, (1 << 64) - 1, ""))
     for x, y, z in zip(a, b, c):
         self.assertEqual(s.encode(x), y)
         self.assertEqual(s.decode(y), z)
 def test_uints(self):
     s = Serializer.uints(2)
     a = ((0, 255), (256, (1 << 64) - 1))
     b = (b"\x00\x01\xff",
          b"\x02\x01\x00\x08\xff\xff\xff\xff\xff\xff\xff\xff")
     c = ((0, 255), (256, (1 << 64) - 1))
     for x, y, z in zip(a, b, c):
         self.assertEqual(s.encode(x), y)
         self.assertEqual(s.decode(y), z)
Example #8
0
 def test_uint(self):
     s = Serializer.uint()
     a = (0, 255, 256, (1 << 64) - 1)
     b = ("\x00", "\x01\xff",
          "\x02\x01\x00", "\x08\xff\xff\xff\xff\xff\xff\xff\xff")
     c = (0, 255, 256, (1 << 64) - 1)
     for x, y, z in zip(a, b, c):
         self.assertEqual(s.encode(x), y)
         self.assertEqual(s.decode(y), z)
Example #9
0
 def test_fifo(self):
     with self.g.transaction(write=True) as txn:
         f = txn.fifo('foo', serialize_value=Serializer.uint())
         f.push(1, 2, 3)
         out = f.pop(4)
         self.assertEqual(out, (1, 2, 3))
         self.assertTrue(f.empty)
         f.push(4, 5, 6)
         self.assertFalse(f.empty)
         self.assertEqual(len(f), 3)
         out = f.pop(4)
         self.assertEqual(out, (4, 5, 6))
         self.assertEqual(len(f), 0)
Example #10
0
 def test_fifo(self):
     with self.g.transaction(write=True) as txn:
         f = txn.fifo('foo', serialize_value=Serializer.uint())
         f.push(1, 2, 3)
         out = f.pop(4)
         self.assertEqual(out, (1, 2, 3))
         self.assertTrue(f.empty)
         f.push(4, 5, 6)
         self.assertFalse(f.empty)
         self.assertEqual(len(f), 3)
         out = f.pop(4)
         self.assertEqual(out, (4, 5, 6))
         self.assertEqual(len(f), 0)
Example #11
0
 def test_msgpack(self):
     s = Serializer.msgpack()
     a = { 'foo': 'bar', u'foo\u2020': u'bar\u2020' }
     b = s.encode(a)
     c = s.decode(b)
     self.assertEqual(a, c)
Example #12
0
class TestAlgorithms(unittest.TestCase):
    serializer = Serializer.msgpack()
    # each test_foo() method is wrapped w/ setup/teardown around it, so each test has a fresh graph
    def setUp(self):
        fd, path = tempfile.mkstemp()
        os.close(fd)
        self.g = Graph(path, serialize_property_value=self.serializer)

    def tearDown(self):
        self.g.delete()

    def test_sp1(self):
        with self.g.transaction(write=True) as txn:
            load_data(txn)

        with self.g.transaction(write=False) as txn:
            n0 = txn.node(**node(0))
            n1 = txn.node(**node(1))
            n2 = txn.node(**node(2))

            e0 = edge(0)
            e1 = edge(1)

            e0['src'] = txn.node(**e0['src'])
            e0['tgt'] = txn.node(**e0['tgt'])
            e1['src'] = txn.node(**e1['src'])
            e1['tgt'] = txn.node(**e1['tgt'])

            e0 = txn.edge(**e0)
            e1 = txn.edge(**e1)

            expect_path = (n0, e0, n1, e1, n2)

            res_path = n0.shortest_path(n2, directed=False)
            self.assertEqual(
                tuple(x.ID for x in expect_path),
                tuple(x.ID for x in res_path))

            res_path = n0.shortest_path(n2, directed=True)
            self.assertEqual(
                tuple(x.ID for x in expect_path),
                tuple(x.ID for x in res_path))

            fail = n2.shortest_path(n0, directed=True)
            self.assertEqual(fail, None)

    def test_sp2(self):
        with self.g.transaction(write=True) as txn:
            '''
                 n1a
                /   \
              e0a   e1a
              /       \
            n0         n2
              \       /
              e0b   e1b
                \   /
                 n1b
            '''
            n0 = txn.node(type='foo', value='0')
            n1a = txn.node(type='foo', value='1a')
            n1b = txn.node(type='foo', value='1b')
            n2 = txn.node(type='foo', value='2')
            e0a = txn.edge(type='foo', src=n0, tgt=n1a)
            e0b = txn.edge(type='foo', src=n0, tgt=n1b)
            e1a = txn.edge(type='foo', src=n1a, tgt=n2)
            e1b = txn.edge(type='foo', src=n1b, tgt=n2)

            # should transit upper path
            self.assertPathEqual(n0.shortest_path(n2), (n0, e0a, n1a, e1a, n2))

            # use cost to force it through lower path
            e1b['cost'] = 0.5
            self.assertPathEqual(n0.shortest_path(n2, cost_field='cost'), (n0, e0b, n1b, e1b, n2))

            # use default cost to make it find the upper path again
            self.assertPathEqual(n0.shortest_path(n2, cost_field='cost', cost_default=0.5), (n0, e0a, n1a, e1a, n2))


    def assertPathEqual(self, a, b):
        self.assertEqual(type(a), type(b))
        if a is not None:
            self.assertEqual(
                tuple(x.ID for x in a),
                tuple(x.ID for x in b))
Example #13
0
 def test_msgpack(self):
     s = Serializer.msgpack()
     a = {'foo': 'bar', u'foo\u2020': u'bar\u2020'}
     b = s.encode(a)
     c = s.decode(b)
     self.assertEqual(a, c)