def test_redis(self): destination_and_settings_classes = DestinationTap.of(TapKind.REDIS) settings = destination_and_settings_classes.settings_class() settings.parse_args("--out-namespace out".split()) destination = destination_and_settings_classes.destination_class(settings) destination.redis = mock.MagicMock() message_written = Message(content={"key": "written"}) destination.write(message_written) destination.close() source_and_settings_classes = SourceTap.of(TapKind.REDIS) settings = source_and_settings_classes.settings_class() settings.parse_args("--in-namespace in".split()) source = source_and_settings_classes.source_class(settings) source.redis = mock.MagicMock()
def test_parse_deep(self): """ Note: This testcase only succeeds within 24 hours from initialization of DB. (Because of 'alives' function) """ self.assertEqual( Pipeline.parse('Alive Device Ratio: #divide(#alives(), #devices())'), Message([ PlainText(name='Alive Device Ratio: '), Function(name='divide', messages=[ Message([ Function(name='alives', messages=[ Message([]) ]), ]), Message([ Function(name='devices', messages=[ Message([]) ]), ]), ]) ]) )
def test_csv_file(self): destination_and_settings_classes = DestinationTap.of(TapKind.CSV) with tempfile.NamedTemporaryFile() as tmpfile: out_filename = tmpfile.name settings = destination_and_settings_classes.settings_class() settings.parse_args(f"--out-filename {out_filename}".split()) destination = destination_and_settings_classes.destination_class(settings) message_written = Message(content={"key": "written"}) destination.write(message_written) destination.close() source_and_settings_classes = SourceTap.of(TapKind.CSV) settings = source_and_settings_classes.settings_class() settings.parse_args(f"--in-filename {out_filename}".split()) source = source_and_settings_classes.source_class(settings) message_read = next(source.read()) assert message_written == message_read
def test_file_gz(self): destination_and_settings_classes = DestinationTap.of(TapKind.FILE) settings = destination_and_settings_classes.settings_class() with tempfile.NamedTemporaryFile(suffix=".gz") as tmpfile: out_filename = tmpfile.name settings.parse_args(f"--out-filename {out_filename} --overwrite".split()) destination = destination_and_settings_classes.destination_class(settings) message_written = Message(content={"key": "written"}) destination.write(message_written) destination.close() source_and_settings_classes = SourceTap.of(TapKind.FILE) settings = source_and_settings_classes.settings_class() settings.parse_args(f"--in-filename {out_filename}".split()) source = source_and_settings_classes.source_class(settings) message_read = next(source.read()) self.assertEqual( message_written.content.get("key"), message_read.content.get("key") )
def test_pipeline(self): pipeline = Pipeline(in_kind=TapKind.MEM, out_kind=TapKind.MEM) pipeline.add_destination_topic("test") destination = pipeline.destination_of("test") destination.write(Message(content={"key": "dummy", "test": "value"})) self.assertEqual(len(destination.results), 1)
def test_parsing_exception(self): with pytest.raises(PipelineMessageError): Message.deserialize(b"HFHGKDJFHG")
def test_key(self): message = Message(id="m", content={"value": "*" * 2048}) assert message.id == "m"
def test_serialization_compression_unicode(self): message = Message(content={"key": u"message \u6D88\u606F"}) result = Message.deserialize(message.serialize(compress=True)) self.assertDictEqual(message.content, result.content)
def test_serialization_compression(self): message = Message(content={"key": "message"}) result = deserialize_message(serialize_message(message, compress=True)) self.assertDictEqual(message.content, result.content)
def test_dict(self): m = Message(content={"key": "value"}) assert m.content.get("key") == "value"
def test_default(self): assert Message(content={}) is not None