|
1 | | -import unittest |
2 | 1 | import unittest.mock as mock |
| 2 | +import pytest |
3 | 3 |
|
4 | 4 | from ogn.parser import parse |
5 | 5 | from ogn.client.client import create_aprs_login, AprsClient |
6 | 6 | from ogn.client.settings import APRS_APP_NAME, APRS_APP_VER, APRS_KEEPALIVE_TIME |
7 | 7 |
|
8 | 8 |
|
9 | | -class AprsClientTest(unittest.TestCase): |
10 | | - def test_create_aprs_login(self): |
11 | | - basic_login = create_aprs_login('klaus', -1, 'myApp', '0.1') |
12 | | - assert 'user klaus pass -1 vers myApp 0.1\n' == basic_login |
13 | | - |
14 | | - login_with_filter = create_aprs_login('klaus', -1, 'myApp', '0.1', 'r/48.0/11.0/100') |
15 | | - assert 'user klaus pass -1 vers myApp 0.1 filter r/48.0/11.0/100\n' == login_with_filter |
16 | | - |
17 | | - def test_initialisation(self): |
18 | | - client = AprsClient(aprs_user='testuser', aprs_filter='') |
19 | | - assert client.aprs_user == 'testuser' |
20 | | - assert client.aprs_filter == '' |
21 | | - |
22 | | - @mock.patch('ogn.client.client.socket') |
23 | | - def test_connect_full_feed(self, mock_socket): |
24 | | - client = AprsClient(aprs_user='testuser', aprs_filter='') |
25 | | - client.connect() |
26 | | - client.sock.send.assert_called_once_with('user testuser pass -1 vers {} {}\n'.format( |
27 | | - APRS_APP_NAME, APRS_APP_VER).encode('ascii')) |
28 | | - client.sock.makefile.assert_called_once_with('rb') |
29 | | - |
30 | | - @mock.patch('ogn.client.client.socket') |
31 | | - def test_connect_client_defined_filter(self, mock_socket): |
32 | | - client = AprsClient(aprs_user='testuser', aprs_filter='r/50.4976/9.9495/100') |
33 | | - client.connect() |
34 | | - client.sock.send.assert_called_once_with('user testuser pass -1 vers {} {} filter r/50.4976/9.9495/100\n'.format( |
35 | | - APRS_APP_NAME, APRS_APP_VER).encode('ascii')) |
36 | | - client.sock.makefile.assert_called_once_with('rb') |
37 | | - |
38 | | - @mock.patch('ogn.client.client.socket') |
39 | | - def test_disconnect(self, mock_socket): |
40 | | - client = AprsClient(aprs_user='testuser', aprs_filter='') |
41 | | - client.connect() |
| 9 | +def test_create_aprs_login(): |
| 10 | + basic_login = create_aprs_login('klaus', -1, 'myApp', '0.1') |
| 11 | + assert 'user klaus pass -1 vers myApp 0.1\n' == basic_login |
| 12 | + |
| 13 | + login_with_filter = create_aprs_login('klaus', -1, 'myApp', '0.1', 'r/48.0/11.0/100') |
| 14 | + assert 'user klaus pass -1 vers myApp 0.1 filter r/48.0/11.0/100\n' == login_with_filter |
| 15 | + |
| 16 | + |
| 17 | +def test_initialisation(): |
| 18 | + client = AprsClient(aprs_user='testuser', aprs_filter='') |
| 19 | + assert client.aprs_user == 'testuser' |
| 20 | + assert client.aprs_filter == '' |
| 21 | + |
| 22 | + |
| 23 | +@mock.patch('ogn.client.client.socket') |
| 24 | +def test_connect_full_feed(mock_socket): |
| 25 | + client = AprsClient(aprs_user='testuser', aprs_filter='') |
| 26 | + client.connect() |
| 27 | + client.sock.send.assert_called_once_with(f'user testuser pass -1 vers {APRS_APP_NAME} {APRS_APP_VER}\n'.encode('ascii')) |
| 28 | + client.sock.makefile.assert_called_once_with('rb') |
| 29 | + |
| 30 | + |
| 31 | +@mock.patch('ogn.client.client.socket') |
| 32 | +def test_connect_client_defined_filter(mock_socket): |
| 33 | + client = AprsClient(aprs_user='testuser', aprs_filter='r/50.4976/9.9495/100') |
| 34 | + client.connect() |
| 35 | + client.sock.send.assert_called_once_with(f'user testuser pass -1 vers {APRS_APP_NAME} {APRS_APP_VER} filter r/50.4976/9.9495/100\n'.encode('ascii')) |
| 36 | + client.sock.makefile.assert_called_once_with('rb') |
| 37 | + |
| 38 | + |
| 39 | +@mock.patch('ogn.client.client.socket') |
| 40 | +def test_disconnect(mock_socket): |
| 41 | + client = AprsClient(aprs_user='testuser', aprs_filter='') |
| 42 | + client.connect() |
| 43 | + client.disconnect() |
| 44 | + client.sock.shutdown.assert_called_once_with(0) |
| 45 | + client.sock.close.assert_called_once_with() |
| 46 | + assert client._kill is True |
| 47 | + |
| 48 | + |
| 49 | +@mock.patch('ogn.client.client.socket') |
| 50 | +def test_run(mock_socket): |
| 51 | + import socket |
| 52 | + mock_socket.error = socket.error |
| 53 | + |
| 54 | + client = AprsClient(aprs_user='testuser', aprs_filter='') |
| 55 | + client.connect() |
| 56 | + |
| 57 | + client.sock_file.readline = mock.MagicMock() |
| 58 | + client.sock_file.readline.side_effect = [b'Normal text blabla', |
| 59 | + b'my weird character \xc2\xa5', |
| 60 | + UnicodeDecodeError('funnycodec', b'\x00\x00', 1, 2, 'This is just a fake reason!'), |
| 61 | + b'... show must go on', |
| 62 | + BrokenPipeError(), |
| 63 | + b'... and on', |
| 64 | + ConnectionResetError(), |
| 65 | + b'... and on', |
| 66 | + socket.error(), |
| 67 | + b'... and on', |
| 68 | + b'', |
| 69 | + b'... and on', |
| 70 | + KeyboardInterrupt()] |
| 71 | + |
| 72 | + try: |
| 73 | + client.run(callback=lambda msg: print("got: {}".format(msg)), autoreconnect=True) |
| 74 | + except KeyboardInterrupt: |
| 75 | + pass |
| 76 | + finally: |
42 | 77 | client.disconnect() |
43 | | - client.sock.shutdown.assert_called_once_with(0) |
44 | | - client.sock.close.assert_called_once_with() |
45 | | - assert client._kill is True |
46 | | - |
47 | | - @mock.patch('ogn.client.client.socket') |
48 | | - def test_run(self, mock_socket): |
49 | | - import socket |
50 | | - mock_socket.error = socket.error |
51 | | - |
52 | | - client = AprsClient(aprs_user='testuser', aprs_filter='') |
53 | | - client.connect() |
54 | | - |
55 | | - client.sock_file.readline = mock.MagicMock() |
56 | | - client.sock_file.readline.side_effect = [b'Normal text blabla', |
57 | | - b'my weird character \xc2\xa5', |
58 | | - UnicodeDecodeError('funnycodec', b'\x00\x00', 1, 2, 'This is just a fake reason!'), |
59 | | - b'... show must go on', |
60 | | - BrokenPipeError(), |
61 | | - b'... and on', |
62 | | - ConnectionResetError(), |
63 | | - b'... and on', |
64 | | - socket.error(), |
65 | | - b'... and on', |
66 | | - b'', |
67 | | - b'... and on', |
68 | | - KeyboardInterrupt()] |
69 | 78 |
|
70 | | - try: |
71 | | - client.run(callback=lambda msg: print("got: {}".format(msg)), autoreconnect=True) |
72 | | - except KeyboardInterrupt: |
73 | | - pass |
74 | | - finally: |
75 | | - client.disconnect() |
76 | 79 |
|
77 | | - @mock.patch('ogn.client.client.time') |
78 | | - @mock.patch('ogn.client.client.socket') |
79 | | - def test_run_keepalive(self, mock_socket, mock_time): |
80 | | - import socket |
81 | | - mock_socket.error = socket.error |
| 80 | +@mock.patch('ogn.client.client.time') |
| 81 | +@mock.patch('ogn.client.client.socket') |
| 82 | +def test_run_keepalive(mock_socket, mock_time): |
| 83 | + import socket |
| 84 | + mock_socket.error = socket.error |
82 | 85 |
|
83 | | - client = AprsClient(aprs_user='testuser', aprs_filter='') |
84 | | - client.connect() |
| 86 | + client = AprsClient(aprs_user='testuser', aprs_filter='') |
| 87 | + client.connect() |
85 | 88 |
|
86 | | - client.sock_file.readline = mock.MagicMock() |
87 | | - client.sock_file.readline.side_effect = [b'Normal text blabla', |
88 | | - KeyboardInterrupt()] |
| 89 | + client.sock_file.readline = mock.MagicMock() |
| 90 | + client.sock_file.readline.side_effect = [b'Normal text blabla', |
| 91 | + KeyboardInterrupt()] |
89 | 92 |
|
90 | | - mock_time.side_effect = [0, 0, APRS_KEEPALIVE_TIME + 1, APRS_KEEPALIVE_TIME + 1] |
| 93 | + mock_time.side_effect = [0, 0, APRS_KEEPALIVE_TIME + 1, APRS_KEEPALIVE_TIME + 1] |
91 | 94 |
|
92 | | - timed_callback = mock.MagicMock() |
| 95 | + timed_callback = mock.MagicMock() |
93 | 96 |
|
| 97 | + try: |
| 98 | + client.run(callback=lambda msg: print("got: {}".format(msg)), timed_callback=timed_callback) |
| 99 | + except KeyboardInterrupt: |
| 100 | + pass |
| 101 | + finally: |
| 102 | + client.disconnect() |
| 103 | + |
| 104 | + timed_callback.assert_called_with(client) |
| 105 | + |
| 106 | + |
| 107 | +def test_reset_kill_reconnect(): |
| 108 | + client = AprsClient(aprs_user='testuser', aprs_filter='') |
| 109 | + client.connect() |
| 110 | + |
| 111 | + # .run() should be allowed to execute after .connect() |
| 112 | + mock_callback = mock.MagicMock( |
| 113 | + side_effect=lambda raw_msg: client.disconnect()) |
| 114 | + |
| 115 | + assert client._kill is False |
| 116 | + client.run(callback=mock_callback, autoreconnect=True) |
| 117 | + |
| 118 | + # After .disconnect(), client._kill should be True |
| 119 | + assert client._kill is True |
| 120 | + assert mock_callback.call_count == 1 |
| 121 | + |
| 122 | + # After we reconnect, .run() should be able to run again |
| 123 | + mock_callback.reset_mock() |
| 124 | + client.connect() |
| 125 | + client.run(callback=mock_callback, autoreconnect=True) |
| 126 | + assert mock_callback.call_count == 1 |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.skip("Too much invalid APRS data on the live feed") |
| 130 | +def test_50_live_messages(): |
| 131 | + print("Enter") |
| 132 | + remaining_messages = 50 |
| 133 | + |
| 134 | + def process_message(raw_message): |
| 135 | + global remaining_messages |
| 136 | + if raw_message[0] == '#': |
| 137 | + return |
94 | 138 | try: |
95 | | - client.run(callback=lambda msg: print("got: {}".format(msg)), timed_callback=timed_callback) |
96 | | - except KeyboardInterrupt: |
97 | | - pass |
98 | | - finally: |
99 | | - client.disconnect() |
100 | | - |
101 | | - timed_callback.assert_called_with(client) |
102 | | - |
103 | | - def test_reset_kill_reconnect(self): |
104 | | - client = AprsClient(aprs_user='testuser', aprs_filter='') |
105 | | - client.connect() |
106 | | - |
107 | | - # .run() should be allowed to execute after .connect() |
108 | | - mock_callback = mock.MagicMock( |
109 | | - side_effect=lambda raw_msg: client.disconnect()) |
110 | | - |
111 | | - self.assertFalse(client._kill) |
112 | | - client.run(callback=mock_callback, autoreconnect=True) |
113 | | - |
114 | | - # After .disconnect(), client._kill should be True |
115 | | - assert client._kill is True |
116 | | - assert mock_callback.call_count == 1 |
117 | | - |
118 | | - # After we reconnect, .run() should be able to run again |
119 | | - mock_callback.reset_mock() |
120 | | - client.connect() |
121 | | - client.run(callback=mock_callback, autoreconnect=True) |
122 | | - assert mock_callback.call_count == 1 |
123 | | - |
124 | | - @unittest.skip("Too much invalid APRS data on the live feed") |
125 | | - def test_50_live_messages(self): |
126 | | - print("Enter") |
127 | | - self.remaining_messages = 50 |
128 | | - |
129 | | - def process_message(raw_message): |
130 | | - if raw_message[0] == '#': |
131 | | - return |
132 | | - try: |
133 | | - message = parse(raw_message) |
134 | | - print("{}: {}".format(message['aprs_type'], raw_message)) |
135 | | - except NotImplementedError as e: |
136 | | - print("{}: {}".format(e, raw_message)) |
137 | | - return |
138 | | - if self.remaining_messages > 0: |
139 | | - self.remaining_messages -= 1 |
140 | | - else: |
141 | | - raise KeyboardInterrupt |
142 | | - |
143 | | - client = AprsClient(aprs_user='testuser', aprs_filter='') |
144 | | - client.connect() |
145 | | - try: |
146 | | - client.run(callback=process_message, autoreconnect=True) |
147 | | - except KeyboardInterrupt: |
148 | | - pass |
149 | | - finally: |
150 | | - client.disconnect() |
| 139 | + message = parse(raw_message) |
| 140 | + print("{}: {}".format(message['aprs_type'], raw_message)) |
| 141 | + except NotImplementedError as e: |
| 142 | + print("{}: {}".format(e, raw_message)) |
| 143 | + return |
| 144 | + if remaining_messages > 0: |
| 145 | + remaining_messages -= 1 |
| 146 | + else: |
| 147 | + raise KeyboardInterrupt |
| 148 | + |
| 149 | + client = AprsClient(aprs_user='testuser', aprs_filter='') |
| 150 | + client.connect() |
| 151 | + try: |
| 152 | + client.run(callback=process_message, autoreconnect=True) |
| 153 | + except KeyboardInterrupt: |
| 154 | + pass |
| 155 | + finally: |
| 156 | + client.disconnect() |
0 commit comments