Debian Codegraph @

test/io_test.py

raw symbols SCIP
1 """Unittests for ais.io"""
2
3 import contextlib
4 import sys
5
6 import ais
7
8 import pytest
9 from six.moves import StringIO
10
11
12 def test_open_right_object(typeexamples_nmea_path):
13 """Make sure ais.open() returns the right object"""
14 with ais.open(typeexamples_nmea_path) as src:
15 assert isinstance(src, ais.NmeaFile)
16 # pytest places a different object in sys.stdin so do an old-shool
17 # type comparison
18 with ais.open('-') as src:
19 assert isinstance(src._fobj, type(sys.stdin))
20
21
22 def test_open_path(typeexamples_nmea_path):
23 """ais.open() with a file path on disk"""
24 with ais.open(typeexamples_nmea_path) as src:
25 for idx, line in enumerate(src):
26 assert isinstance(line, dict)
27 assert idx >= 20, "20 messages was kind of chosen arbitrarily. " \
28 "Adjust if necessary."
29
30
31 def test_open_file_like_object(bare_nmea):
32 """Make sure ais.open() can handle a file-like object."""
33 with contextlib.closing(StringIO(bare_nmea)) as f, ais.open(f) as src:
34 for idx, line in enumerate(src):
35 assert isinstance(line, dict)
36 print(line)
37 assert idx >= 6
38
39
40 def test_open_exceptions():
41 """Trigger the exceptions in ais.open()"""
42
43 # Bad value for mode parameter
44 with pytest.raises(ValueError):
45 ais.open('something', mode='bad-mode')
46
47 # Invalid file-like object
48 with pytest.raises(TypeError):
49 ais.open(None)
50
51
52 def test_NmeaFile_io_states(typeexamples_nmea_path):
53 """Make sure NmeaFile is tearing down properly."""
54 with ais.open(typeexamples_nmea_path) as src:
55 assert not src.closed
56 assert not src._fobj.closed
57 assert src.closed
58 assert src._fobj.closed
59
60
61 def test_NmeaFile_properties(typeexamples_nmea_path):
62 with ais.open(typeexamples_nmea_path) as src:
63 assert src.name == typeexamples_nmea_path