def test_open_right_object(typeexamples_nmea_path): """Make sure ais.open() returns the right object""" with ais.open(typeexamples_nmea_path) as src: assert isinstance(src, ais.NmeaFile) # pytest places a different object in sys.stdin so do an old-shool # type comparison with ais.open('-') as src: assert isinstance(src._fobj, type(sys.stdin))
def test_open_exceptions(): """Trigger the exceptions in ais.open()""" # Bad value for mode parameter with pytest.raises(ValueError): ais.open('something', mode='bad-mode') # Invalid file-like object with pytest.raises(TypeError): ais.open(None)
def test_NmeaFile_io_states(typeexamples_nmea_path): """Make sure NmeaFile is tearing down properly.""" with ais.open(typeexamples_nmea_path) as src: assert not src.closed assert not src._fobj.closed assert src.closed assert src._fobj.closed
def test_open_file_like_object(bare_nmea): """Make sure ais.open() can handle a file-like object.""" with contextlib.closing(StringIO(bare_nmea)) as f, ais.open(f) as src: for idx, line in enumerate(src): assert isinstance(line, dict) print(line) assert idx >= 6
def test_open_path(typeexamples_nmea_path): """ais.open() with a file path on disk""" with ais.open(typeexamples_nmea_path) as src: for idx, line in enumerate(src): assert isinstance(line, dict) assert idx >= 20, "20 messages was kind of chosen arbitrarily. " \ "Adjust if necessary."
def test_NmeaFile_against_queue(nmea): queue = nmea_queue.NmeaQueue() for line in nmea.splitlines(): queue.put(line) expected = [] msg = queue.GetOrNone() while msg: expected.append(msg) msg = queue.GetOrNone() with contextlib.closing(StringIO(nmea)) as f, ais.open(f) as src: actual = list(src) for e, a in zip(expected, actual): assert e == a
def test_NmeaFile_properties(typeexamples_nmea_path): with ais.open(typeexamples_nmea_path) as src: assert src.name == typeexamples_nmea_path
pushIfHas ("dim_b", "to_stern") pushIfHas ("dim_c", "to_port") pushIfHas ("dim_d", "to_starboard") pushIfHas ("id", "type") pushIfHas ("vendor_id", "vendorid") return res ## -------------------------------------------------------------------------- ## main() if __name__ == '__main__': count = 0 with ais.open (sys.stdin) as msgStream: for msg in msgStream: count += 1 if count % 1000 == 0: sys.stderr.write ("-- Done msgs count: %d\n" % (count,)) ## Pass if message failed to decode if "decoded" not in msg: continue if msgHasDesiredType (msg): genDict = genMsgOutputDict (msg) sys.stdout.write (json.dumps (genDict)) sys.stdout.write ("\n");