Skip to content

Commit

Permalink
Add lots more tests to cover expected errors
Browse files Browse the repository at this point in the history
  • Loading branch information
julianneswinoga committed Jan 1, 2024
1 parent 1513145 commit 4f80df4
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/test_ctrls_connection.py
@@ -0,0 +1,25 @@
from flightgear_python.fg_if import CtrlsConnection
from flightgear_python.fg_util import FGConnectionError
from testing_common import supported_ctrls_versions

import pytest


def test_ctrls_wrong_version_on_create():
with pytest.raises(NotImplementedError):
CtrlsConnection(ctrls_version=1)


@pytest.mark.parametrize('ctrls_version', supported_ctrls_versions)
def test_ctrls_bad_port(mocker, ctrls_version):
def mock_bind(addr):
# Binding to port 1 should usually fail with Should fail with `[Errno 13] Permission denied`
# but this is more reliable
raise PermissionError('Mock permission fail')

mocker.patch('socket.socket.bind', mock_bind)

ctrls_c = CtrlsConnection(ctrls_version)
with pytest.raises(FGConnectionError):
# Should fail with [Errno 13] Permission denied
ctrls_c.connect_rx('localhost', 1, lambda data, pipe: data)
20 changes: 20 additions & 0 deletions tests/test_fdm_connection.py
@@ -1,4 +1,5 @@
from flightgear_python.fg_if import FDMConnection
from flightgear_python.fg_util import FGConnectionError
from testing_common import supported_fdm_versions

import pytest
Expand Down Expand Up @@ -90,3 +91,22 @@ def rx_cb(fdm_data, event_pipe):

# Prevent 'ResourceWarning: unclosed' warning
fdm_c.fg_rx_sock.close()


def test_fdm_wrong_version_on_create():
with pytest.raises(NotImplementedError):
FDMConnection(fdm_version=1)


@pytest.mark.parametrize('fdm_version', supported_fdm_versions)
def test_fdm_bad_port(mocker, fdm_version):
def mock_bind(addr):
# Binding to port 1 should usually fail with Should fail with `[Errno 13] Permission denied`
# but this is more reliable
raise PermissionError('Mock permission fail')

mocker.patch('socket.socket.bind', mock_bind)

fdm_c = FDMConnection(fdm_version)
with pytest.raises(FGConnectionError):
fdm_c.connect_rx('localhost', 1, lambda data, pipe: data)
25 changes: 25 additions & 0 deletions tests/test_gui_connection.py
@@ -0,0 +1,25 @@
from flightgear_python.fg_if import GuiConnection
from flightgear_python.fg_util import FGConnectionError
from testing_common import supported_gui_versions

import pytest


def test_gui_wrong_version_on_create():
with pytest.raises(NotImplementedError):
GuiConnection(gui_version=1)


@pytest.mark.parametrize('gui_version', supported_gui_versions)
def test_gui_bad_port(mocker, gui_version):
def mock_bind(addr):
# Binding to port 1 should usually fail with Should fail with `[Errno 13] Permission denied`
# but this is more reliable
raise PermissionError('Mock permission fail')

mocker.patch('socket.socket.bind', mock_bind)

gui_c = GuiConnection(gui_version)
with pytest.raises(FGConnectionError):
# Should fail with [Errno 13] Permission denied
gui_c.connect_rx('localhost', 1, lambda data, pipe: data)
22 changes: 22 additions & 0 deletions tests/test_http_connection.py
Expand Up @@ -63,3 +63,25 @@ def test_http_set_prop(preset_value):
m.post(h_con.url + prop_path)
# Check the code doesn't break (nothing to assert)
h_con.set_prop(prop_path, 'test value')


def test_http_get_prop_must_be_absolute():
h_con = HTTPConnection('localhost', 55555)
with requests_mock.Mocker() as m:
prop_path = 'non_absolute/path'
# Set the mock value
m.get(h_con.url + prop_path, json={})

with pytest.raises(ValueError):
h_con.get_prop(prop_path)


def test_http_set_prop_must_be_absolute():
h_con = HTTPConnection('localhost', 55555)
with requests_mock.Mocker() as m:
prop_path = 'non_absolute/path'
# Set the mock value
m.get(h_con.url + prop_path, json={})
m.post(h_con.url + prop_path)
with pytest.raises(ValueError):
h_con.set_prop(prop_path, 'test value')
16 changes: 16 additions & 0 deletions tests/test_integration_FG_FDM.py
@@ -1,6 +1,9 @@
import pytest

from flightgear_python.fg_if import FDMConnection
from flightgear_python.fg_util import FGCommunicationError

from testing_common import supported_fdm_versions


pytestmark = pytest.mark.fg_integration
Expand Down Expand Up @@ -34,3 +37,16 @@ def rx_cb(fdm_data, event_pipe):
# Prevent 'ResourceWarning: unclosed' warning
fdm_c.fg_rx_sock.close()
fdm_c.fg_tx_sock.close()


def test_fdm_wrong_version_in_stream():
fdm_version_supported_but_not_downloaded = fdm_version + 1
assert fdm_version_supported_but_not_downloaded in supported_fdm_versions

fdm_c = FDMConnection(fdm_version_supported_but_not_downloaded)
fdm_c.connect_rx('localhost', 5501, lambda data, pipe: data)
with pytest.raises(FGCommunicationError):
fdm_c._fg_packet_roundtrip()

# Prevent 'ResourceWarning: unclosed' warning
fdm_c.fg_rx_sock.close()
7 changes: 7 additions & 0 deletions tests/test_integration_FG_telnet.py
Expand Up @@ -3,6 +3,7 @@
import pytest

from flightgear_python.fg_if import PropsConnection
from flightgear_python.fg_util import FGConnectionError


pytestmark = pytest.mark.fg_integration
Expand Down Expand Up @@ -42,3 +43,9 @@ def test_telnet_set_prop():
t_con.set_prop('/controls/flight/rudder', -0.5)
aileron_value = t_con.get_prop('/controls/flight/rudder')
assert aileron_value == -0.5


def test_telnet_wrong_port():
t_con = PropsConnection('localhost', 123)
with pytest.raises(FGConnectionError):
t_con.connect()
18 changes: 18 additions & 0 deletions tests/test_props_connection.py
@@ -1,5 +1,7 @@
from flightgear_python.fg_if import PropsConnection

import pytest


def setup_props_mock(mocker, cmd_str):
# this is mocking what flightgear will send
Expand Down Expand Up @@ -30,3 +32,19 @@ def test_props_round_trip(mocker):
assert ('A' * 512) in resp
# Prevent 'ResourceWarning: unclosed' warning
p_con.sock.close()


def test_props_get_prop_must_be_absolute(mocker):
cmd = 'test123'
setup_props_mock(mocker, cmd)
p_con = PropsConnection('localhost', 55554)
with pytest.raises(ValueError):
p_con.get_prop('non_absolute/path')


def test_props_set_prop_must_be_absolute(mocker):
cmd = 'test123'
setup_props_mock(mocker, cmd)
p_con = PropsConnection('localhost', 55554)
with pytest.raises(ValueError):
p_con.set_prop('non_absolute/path', 'test value')

0 comments on commit 4f80df4

Please sign in to comment.