def test_reader(self): messages = [msg for msg in FileReaderStream(self.FILENAME)] assert len(messages) == 7 for msg in messages: assert type(msg) == NMEAMessage assert msg.is_valid assert msg.decode().content is not None
def test_marine_traffic_sample(self): """Test some messages from https://help.marinetraffic.com/hc/en-us /articles/215626187-I-am-an-AIS-data-contributor-Can-you-share-more-data-with-me-""" par_dir = pathlib.Path(__file__).parent.absolute() nmea_file = par_dir.joinpath("nmea_data_sample.txt") with FileReaderStream(nmea_file) as stream: for msg in stream: assert msg.decode()
def test_large_file(self): # The ais sample data is downloaded from https://www.aishub.net/ais-dispatcher par_dir = pathlib.Path(__file__).parent.absolute() large_file = par_dir.joinpath("nmea-sample") for msg in FileReaderStream(large_file): msg.decode()
def test_invalid_filename(self): with self.assertRaises(FileNotFoundError): FileReaderStream("doesnotexist")
def test_reader_with_open(self): with FileReaderStream(self.FILENAME) as stream: msg = next(stream) assert type(msg) == NMEAMessage assert msg.is_valid assert msg.decode().content is not None
def test_mixed_content(self): """Test that the file reader handles mixed content. That means, that is is able to handle text files, that contain both AIS messages and non AIS messages.""" par_dir = pathlib.Path(__file__).parent.absolute() mixed_content_file = par_dir.joinpath("messages.ais") self.assertEqual(len(list(iter(FileReaderStream(mixed_content_file)))), 6)
from pyais.stream import FileReaderStream filename = "sample.ais" for msg in FileReaderStream(filename): decoded_message = msg.decode() ais_content = decoded_message.content # Do something with the ais message