Esempio n. 1
0
 def testSerializer(self):
     io = BytesIO()
     writer = createWriter(io)
     reader = createReader(io)
     SERIALIZER.write(createObjects(), writer)
     io.seek(0)
     b = io.read(1000)
     self.assertEqual(
         b,
         fromJava(
             [2, 15, 0, 3, 0, 3, 1, 7, -128, -119, 15, 7, -117, -56, 120, 4, 84, 79, 126, -85, -80, 59, 20, -52, 5,
              5, 72, 101, 108, 108, 111, 5, 20, 62, 1, 18, 127, -62, -128, -56, -76, -33, -65, -32, -96, -128, -28,
              -116, -95, -17, -65, -65, 60, 6, 5, 0, 127, -1, 10, -45, 8, -62, 31, 22, 58, 9, 1, 9, 0, 11, 1, 1, 2,
              -10, 1, 3, 4, 89, 65, 83, 83, 0, 14, 1, 3, 7, 2, 7, 4, 7, 6, 0, 17, 1, 63, -16, 0, 0, 0, 0, 0, 0, 2, 2,
              1, 0, 17, 1, 64, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0])
     )
     io.seek(0)
     result = SERIALIZER.read(reader)
     self.assertEqual(io.read(1), b'')
     self.assertTrue(len(result) == 15)
     self.assertTrue(result[0] is None)
     self.assertEqual(result[1], False)
     self.assertEqual(result[2], True)
     self.assertEqual(result[3].value, 123456)
     self.assertEqual(result[4].value, -987654)
     self.assertEqual(result[5], 1.34545e98)
     self.assertEqual(result[6], "Hello")
     self.assertEqual(result[7], u">\u0001\u0012\u007F\u0080\u0234\u07FF\u0800\u4321\uFFFF<")
     self.assertEqual(result[8], b'\x00\x7f\xff\x0a\xd3')
     expiration = result[9]  # type: Expiration
     self.assertEqual(expiration.year, 2017)
     self.assertEqual(expiration.month, 11)
     self.assertEqual(expiration.day, 29)
     self.assertEqual(result[10], PriceKind.ASK)
     self.assertEqual(result[11], PriceKind.BID)
     stock = result[12]  # type: Stock
     self.assertEqual(stock.id.value, 123)
     self.assertEqual(stock.name, u"YASS")
     self.assertEqual(stock.paysDividend, True)
     unknownInstrumentsException = result[13]  # type: UnknownInstrumentsException
     instrumentIds = unknownInstrumentsException.instrumentIds
     self.assertTrue(len(instrumentIds) == 3)
     self.assertEqual(instrumentIds[0].value, 1)
     self.assertEqual(instrumentIds[1].value, 2)
     self.assertEqual(instrumentIds[2].value, 3)
     self.assertTrue(unknownInstrumentsException.onlyNeededForTests1 is None)
     self.assertTrue(unknownInstrumentsException.onlyNeededForTests2 is None)
     self.assertTrue(unknownInstrumentsException.onlyNeededForTests3 is None)
     node1 = result[14]  # type: Node
     self.assertEqual(node1.id, 1.0)
     self.assertTrue(len(node1.links) == 2)
     self.assertTrue(node1.links[0] is node1)
     node2 = node1.links[1]
     self.assertEqual(node2.id, 2.0)
     self.assertTrue(len(node2.links) == 0)
     io.truncate(0)
     io.seek(0)
     unknownInstrumentsException.onlyNeededForTests1 = 3.14
     SERIALIZER.write(unknownInstrumentsException, writer)
     io.seek(0)
     unknownInstrumentsException = SERIALIZER.read(reader)
     self.assertEqual(io.read(1), b'')
     self.assertEqual(unknownInstrumentsException.onlyNeededForTests1, 3.14)
     try:
         SERIALIZER.write(123, writer)
         self.fail()
     except RuntimeError as e:
         self.assertEqual(
             str(e),
             "missing type <type 'int'> in serializer"
         )
Esempio n. 2
0
 def testWithoutCyclesCompact(self):
     self.assertEqual(
         MyDumper(True, False,
                  set()).toString(createObjects(withCycles=False)),
         RESULT_WITHOUT_CYCLES_COMPACT)
Esempio n. 3
0
 def testWithCyclesExtended(self):
     self.assertEqual(
         MyDumper(False, True,
                  {Integer, Expiration}).toString(createObjects()),
         RESULT_WITH_CYCLES_EXTENDED)
Esempio n. 4
0
 def testWithoutCyclesExtended(self):
     self.assertEqual(
         MyDumper(False, False,
                  set()).toString(createObjects(withCycles=False)),
         RESULT_WITHOUT_CYCLES_EXTENDED)
Esempio n. 5
0
 def testWithCyclesCompact(self):
     self.assertEqual(
         MyDumper(True, True,
                  {Integer, Expiration}).toString(createObjects()),
         RESULT_WITH_CYCLES_COMPACT)
Esempio n. 6
0
 def testWithCyclesCompact(self):
     self.assertEqual(MyDumper(True, True, {Integer, Expiration}).toString(createObjects()), RESULT_WITH_CYCLES_COMPACT)
Esempio n. 7
0
 def testWithoutCyclesExtended(self):
     self.assertEqual(MyDumper(False, False, set()).toString(createObjects(withCycles=False)), RESULT_WITHOUT_CYCLES_EXTENDED)
Esempio n. 8
0
 def testWithCyclesExtended(self):
     self.assertEqual(MyDumper(False, True, {Integer, Expiration}).toString(createObjects()), RESULT_WITH_CYCLES_EXTENDED)
Esempio n. 9
0
 def testWithoutCyclesCompact(self):
     self.assertEqual(MyDumper(True, False, set()).toString(createObjects(withCycles=False)), RESULT_WITHOUT_CYCLES_COMPACT)
Esempio n. 10
0
 def testSerializer(self):
     io = BytesIO()
     writer = createWriter(io)
     reader = createReader(io)
     SERIALIZER.write(createObjects(), writer)
     io.seek(0)
     b = io.read(1000)
     self.assertEqual(
         b,
         fromJava([
             2, 15, 0, 3, 0, 3, 1, 7, -128, -119, 15, 7, -117, -56, 120, 4,
             84, 79, 126, -85, -80, 59, 20, -52, 5, 5, 72, 101, 108, 108,
             111, 5, 20, 62, 1, 18, 127, -62, -128, -56, -76, -33, -65, -32,
             -96, -128, -28, -116, -95, -17, -65, -65, 60, 6, 5, 0, 127, -1,
             10, -45, 8, -62, 31, 22, 58, 9, 1, 9, 0, 11, 1, 1, 2, -10, 1,
             3, 4, 89, 65, 83, 83, 0, 14, 1, 3, 7, 2, 7, 4, 7, 6, 0, 15, 1,
             63, -16, 0, 0, 0, 0, 0, 0, 2, 2, 1, 0, 15, 1, 64, 0, 0, 0, 0,
             0, 0, 0, 2, 0, 0, 0
         ]))
     io.seek(0)
     result = cast(List, SERIALIZER.read(reader))
     self.assertEqual(io.read(1), b'')
     self.assertTrue(len(result) == 15)
     self.assertTrue(result[0] is None)
     self.assertEqual(result[1], False)
     self.assertEqual(result[2], True)
     self.assertEqual(result[3].value, 123456)
     self.assertEqual(result[4].value, -987654)
     self.assertEqual(result[5], 1.34545e98)
     self.assertEqual(result[6], "Hello")
     self.assertEqual(
         result[7],
         u">\u0001\u0012\u007F\u0080\u0234\u07FF\u0800\u4321\uFFFF<")
     self.assertEqual(result[8], b'\x00\x7f\xff\x0a\xd3')
     expiration = result[9]  # type: Expiration
     self.assertEqual(expiration.year, 2017)
     self.assertEqual(expiration.month, 11)
     self.assertEqual(expiration.day, 29)
     self.assertEqual(result[10], PriceKind.ASK)
     self.assertEqual(result[11], PriceKind.BID)
     stock = result[12]  # type: Stock
     self.assertEqual(stock.id.value, 123)
     self.assertEqual(stock.name, u"YASS")
     self.assertEqual(stock.paysDividend, True)
     unknownInstrumentsException = result[
         13]  # type: UnknownInstrumentsException
     instrumentIds = unknownInstrumentsException.instrumentIds
     self.assertTrue(len(instrumentIds) == 3)
     self.assertEqual(instrumentIds[0].value, 1)
     self.assertEqual(instrumentIds[1].value, 2)
     self.assertEqual(instrumentIds[2].value, 3)
     self.assertTrue(
         unknownInstrumentsException.onlyNeededForTests1 is None)
     self.assertTrue(
         unknownInstrumentsException.onlyNeededForTests2 is None)
     self.assertTrue(
         unknownInstrumentsException.onlyNeededForTests3 is None)
     node1 = result[14]  # type: Node
     self.assertEqual(node1.id, 1.0)
     self.assertTrue(len(node1.links) == 2)
     self.assertTrue(node1.links[0] is node1)
     node2 = node1.links[1]
     self.assertEqual(node2.id, 2.0)
     self.assertTrue(len(node2.links) == 0)
     io.truncate(0)
     io.seek(0)
     unknownInstrumentsException.onlyNeededForTests1 = 3.14
     SERIALIZER.write(unknownInstrumentsException, writer)
     io.seek(0)
     unknownInstrumentsException = cast(UnknownInstrumentsException,
                                        SERIALIZER.read(reader))
     self.assertEqual(io.read(1), b'')
     self.assertEqual(unknownInstrumentsException.onlyNeededForTests1, 3.14)
     try:
         SERIALIZER.write(123, writer)
         self.fail()
     except RuntimeError as e:
         self.assertEqual(str(e), "missing type <type 'int'> in serializer")