def test_flat_map(self): topo = Topology() s = topo.source(s_none) s.flat_map(fm_none) s.flat_map(fm_int) sr = s.flat_map(fm_str) self.assertEqual(_normalize(SensorReading), sr.oport.schema) sr.flat_map(fm_sensor) s.flat_map(fm_any) s.flat_map(fm_sensor) s = topo.source(s_int) s.flat_map(fm_none) s.flat_map(fm_int) self.assertRaises(TypeError, s.flat_map, fm_str) s.flat_map(fm_any) self.assertRaises(TypeError, s.flat_map, fm_sensor) s = topo.source(s_str) s.flat_map(fm_none) self.assertRaises(TypeError, s.flat_map, fm_int) sr = s.flat_map(fm_str) self.assertEqual(_normalize(SensorReading), sr.oport.schema) sr.flat_map(fm_sensor) s.flat_map(fm_any) self.assertRaises(TypeError, s.flat_map, fm_sensor) s = topo.source(s_any) s.flat_map(fm_none) s.flat_map(fm_int) sr = s.flat_map(fm_str) self.assertEqual(_normalize(SensorReading), sr.oport.schema) sr.flat_map(fm_sensor) s.flat_map(fm_any) s.flat_map(fm_sensor) s = topo.source(s_sensor) s.flat_map(fm_none) self.assertRaises(TypeError, s.flat_map, fm_int) self.assertRaises(TypeError, s.flat_map, fm_str) s.flat_map(fm_any) s.flat_map(fm_sensor) s = topo.source(s_p) s.flat_map(fm_none) self.assertRaises(TypeError, s.flat_map, fm_int) self.assertRaises(TypeError, s.flat_map, fm_str) s.flat_map(fm_any) self.assertRaises(TypeError, s.flat_map, fm_sensor) s.flat_map(fm_p) self.assertRaises(TypeError, s.flat_map, fm_s) s = topo.source(s_s) s.flat_map(fm_p) s.flat_map(fm_s)
def test_source(self): topo = Topology() s = topo.source(s_none) self.assertEqual(CommonSchema.Python, s.oport.schema) s = topo.source(s_int) self.assertEqual(CommonSchema.Python, s.oport.schema) s = topo.source(s_str) self.assertEqual(CommonSchema.String, s.oport.schema) s = topo.source(s_any) self.assertEqual(CommonSchema.Python, s.oport.schema) s = topo.source(s_sensor) self.assertEqual(_normalize(SensorReading), s.oport.schema) s = topo.source(s_str_it) self.assertEqual(CommonSchema.String, s.oport.schema) s = topo.source(s_p) self.assertEqual(CommonSchema.Python, s.oport.schema) s = topo.source(s_s) self.assertEqual(CommonSchema.Python, s.oport.schema)
def test_map(self): topo = Topology() s = topo.source(s_none) s.map(m_none) sr = s.map(m_int) self.assertEqual(CommonSchema.String, sr.oport.schema) sr = s.map(m_str) self.assertEqual(_normalize(SensorReading), sr.oport.schema) s.map(m_any) s.map(m_sensor) s = topo.source(s_int) s.map(m_none) s.map(m_int) self.assertRaises(TypeError, s.map, m_str) s.map(m_any) self.assertRaises(TypeError, s.map, m_sensor) s = topo.source(s_str) s.map(m_none) self.assertRaises(TypeError, s.map, m_int) s.map(m_str) s.map(m_any) self.assertRaises(TypeError, s.map, m_sensor) s = topo.source(s_any) s.map(m_none) s.map(m_int) s.map(m_str) s.map(m_any) s.map(m_sensor) s = topo.source(s_sensor) s.map(m_none) self.assertRaises(TypeError, s.map, m_int) self.assertRaises(TypeError, s.map, m_str) s.map(m_any) s.map(m_sensor) s = topo.source(s_p) s.map(m_none) self.assertRaises(TypeError, s.map, m_int) self.assertRaises(TypeError, s.map, m_str) s.map(m_any) self.assertRaises(TypeError, s.map, m_sensor) sr = s.map(m_p) self.assertEqual(CommonSchema.Python, sr.oport.schema) self.assertRaises(TypeError, s.map, m_s) # Ensure we maintain the hint as well as the schema sr.map(m_p) self.assertRaises(TypeError, sr.map, m_s) sr.map(m_p2s).map(m_s) s = topo.source(s_s) s.map(m_p) s.map(m_s)
def __init__(self, name, operator, index, schema, width=None, partitioned_keys=None, routing=None): self.name = name self.operator = operator self.schema = _normalize(schema) self.index = index self.width = width self.partitioned = partitioned_keys is not None self.partitioned_keys = partitioned_keys self.routing = routing self.inputPorts = []
def test_aggregate(self): topo = Topology() w = topo.source(s_none).last() w.aggregate(agg_none) sr = w.aggregate(agg_int) self.assertEqual(CommonSchema.String, sr.oport.schema) sr = w.aggregate(agg_str) self.assertEqual(_normalize(SensorReading), sr.oport.schema) w.aggregate(agg_any) w.aggregate(agg_sensor) w = topo.source(s_int).last() w.aggregate(agg_none) w.aggregate(agg_int) #self.assertRaises(TypeError, w.aggregate, agg_str) w.aggregate(agg_any) #self.assertRaises(TypeError, w.aggregate, agg_sensor) w = topo.source(s_str).last() w.aggregate(agg_none) #self.assertRaises(TypeError, w.aggregate, agg_int) w.aggregate(agg_str) w.aggregate(agg_any) #self.assertRaises(TypeError, w.aggregate, agg_sensor) w = topo.source(s_any).last() w.aggregate(agg_none) w.aggregate(agg_int) w.aggregate(agg_str) w.aggregate(agg_any) w.aggregate(agg_sensor) w = topo.source(s_sensor).last() w.aggregate(agg_none) #self.assertRaises(TypeError, w.aggregate, agg_int) #self.assertRaises(TypeError, w.aggregate, agg_str) w.aggregate(agg_any) w.aggregate(agg_sensor) w = topo.source(s_p).last() w.aggregate(agg_none) #self.assertRaises(TypeError, w.aggregate, agg_int) #self.assertRaises(TypeError, w.aggregate, agg_str) w.aggregate(agg_any) #self.assertRaises(TypeError, w.aggregate, agg_sensor) sr = w.aggregate(agg_p) self.assertEqual(CommonSchema.Python, sr.oport.schema) #self.assertRaises(TypeError, w.aggregate, agg_s) w = topo.source(s_s).last() w.aggregate(agg_p) w.aggregate(agg_s)
def test_normalize(self): self.assertIsNone(_sch._normalize(None)) for cs in _sch.CommonSchema: self.assertEqual(cs, _sch._normalize(cs)) s = _sch.StreamSchema('tuple<int32 a>') self.assertEqual(s, _sch._normalize(s)) s = _sch.StreamSchema('MyCoolSchema') self.assertEqual(s, _sch._normalize(s)) self.assertEqual(_sch.CommonSchema.Python, _sch._normalize(object)) _u = str if sys.version_info.major == 3 else unicode self.assertEqual(_sch.CommonSchema.String, _sch._normalize(_u)) import json self.assertEqual(_sch.CommonSchema.Json, _sch._normalize(json)) self.assertIsInstance(_sch._normalize('tuple<int32 b>'), _sch.StreamSchema) self.assertIsInstance(_sch._normalize('MyCoolSchema'), _sch.StreamSchema) self.assertRaises(ValueError, _sch._normalize, False) if sys.version_info.major == 3: import typing Employee = typing.NamedTuple('Employee', [('name', str), ('id', int)]) nts = _sch._normalize(Employee) self.assertIsInstance(nts, _sch.StreamSchema) self.assertEqual('tuple<rstring name, int64 id>', nts._schema) AllSPLTypes = typing.NamedTuple('AllSPLTypes', [ ('b', bool), ('i64', int), ('f64', float), ('c64', complex), ('d128', decimal.Decimal), ('s', _u), ('li64', typing.List[int]), ('lf64', typing.List[float]), ('mi64b', typing.Mapping[int, bool]), ('llf64', typing.List[typing.List[float]]), ('mi64li64', typing.Mapping[int, typing.List[int]]), ('sc64', typing.Set[complex]), ('sli64', typing.Set[typing.List[int]]), ('ts_spl', streamsx.spl.types.Timestamp), ('binary', bytes), ('oi64', typing.Optional[int]), ('of64', typing.Union[float, None]), ('ob', typing.Union[None, bool]), ]) nts = _sch._normalize(AllSPLTypes) self.assertIsInstance(nts, _sch.StreamSchema) self.assertEqual( 'tuple<boolean b, int64 i64, float64 f64, complex64 c64, decimal128 d128, rstring s, list<int64> li64, list<float64> lf64, map<int64, boolean> mi64b, list<list<float64>> llf64, map<int64, list<int64>> mi64li64, set<complex64> sc64, set<list<int64>> sli64, timestamp ts_spl, blob binary, optional<int64> oi64, optional<float64> of64, optional<boolean> ob>', nts._schema) self.assertEqual('AllSPLTypes', nts.style.__name__) ont = nts.style self.assertEqual(ont._fields, AllSPLTypes._fields) if sys.version_info.major == 3: self.assertEqual(ont._field_types, AllSPLTypes._field_types)
def test_normalize(self): self.assertIsNone(_sch._normalize(None)) for cs in _sch.CommonSchema: self.assertEqual(cs, _sch._normalize(cs)) s = _sch.StreamSchema('tuple<int32 a>') self.assertEqual(s, _sch._normalize(s)) s = _sch.StreamSchema('MyCoolSchema') self.assertEqual(s, _sch._normalize(s)) self.assertEqual(_sch.CommonSchema.Python, _sch._normalize(object)) _u = str if sys.version_info.major == 3 else unicode self.assertEqual(_sch.CommonSchema.String, _sch._normalize(_u)) import json self.assertEqual(_sch.CommonSchema.Json, _sch._normalize(json)) self.assertIsInstance(_sch._normalize('tuple<int32 b>'), _sch.StreamSchema) self.assertIsInstance(_sch._normalize('MyCoolSchema'), _sch.StreamSchema) self.assertRaises(ValueError, _sch._normalize, False) if sys.version_info.major == 3: import typing Employee = typing.NamedTuple('Employee', [('name', str), ('id', int)]) nts = _sch._normalize(Employee) self.assertIsInstance(nts, _sch.StreamSchema) self.assertEqual('tuple<rstring name, int64 id>', nts._schema) AllSPLTypes = typing.NamedTuple('AllSPLTypes', [ ('b', bool), ('i64', int), ('f64', float), ('c64', complex), ('d128', decimal.Decimal), ('s', _u), ('li64', typing.List[int]), ('lf64', typing.List[float]), ('mi64b', typing.Mapping[int,bool]), ('llf64', typing.List[typing.List[float]]), ('mi64li64', typing.Mapping[int,typing.List[int]]), ('sc64', typing.Set[complex]), ('sli64', typing.Set[typing.List[int]]), ('ts_spl', streamsx.spl.types.Timestamp), ('binary', bytes), ('oi64', typing.Optional[int]), ('of64', typing.Union[float, None]), ('ob', typing.Union[None, bool]), ]) nts = _sch._normalize(AllSPLTypes) self.assertIsInstance(nts, _sch.StreamSchema) self.assertEqual('tuple<boolean b, int64 i64, float64 f64, complex64 c64, decimal128 d128, rstring s, list<int64> li64, list<float64> lf64, map<int64, boolean> mi64b, list<list<float64>> llf64, map<int64, list<int64>> mi64li64, set<complex64> sc64, set<list<int64>> sli64, timestamp ts_spl, blob binary, optional<int64> oi64, optional<float64> of64, optional<boolean> ob>', nts._schema) self.assertEqual('AllSPLTypes', nts.style.__name__) ont = nts.style self.assertEqual(ont._fields, AllSPLTypes._fields) if sys.version_info.major == 3: self.assertEqual(ont._field_types, AllSPLTypes._field_types)