Changeset fffb074 for src


Ignore:
Timestamp:
May 16, 2020, 12:32:19 PM (7 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
debian/bullseye, master
Children:
817f446
Parents:
0c8a692
Message:

Added tests for

  • n2kdpoller
  • gpsdpoller
  • Daemon
Location:
src/gnuboat/device/tests
Files:
4 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/gnuboat/device/tests/__init__.py

    r0c8a692 rfffb074  
    1515    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1616'''
     17
     18import time
     19import sys
     20
     21
     22def gpsd_fakehook(linenumber, fakegps):
     23    """
     24        Used as pred for gps fake device,
     25        and indicates, when the device is ready
     26        to generate data
     27    """
     28    if not fakegps.testload.sentences:
     29        sys.stderr.write("fakegps: no sentences in test load.\n")
     30        raise SystemExit(1)
     31    if linenumber % len(fakegps.testload.sentences) == 0:
     32        if linenumber > 0:
     33            return False
     34    time.sleep(1)
     35    return True
  • src/gnuboat/device/tests/conftest.py

    r0c8a692 rfffb074  
    1717
    1818import pytest
    19 from werkzeug.utils import import_string
    2019import time
    2120
    22 from gnuboat.common.webservice.Config import (
    23     config_get
    24 )
    25 
    26 
    27 @pytest.fixture(scope='session')
    28 def configuration():
    29     return import_string('gnuboat.device.Config.TestingConfig')
     21from . import n2kdfake
    3022
    3123
    3224@pytest.fixture(scope='module')
    33 def gps_fake(configuration):
     25def gps_fake():
    3426    """
    3527        Supplies a GPS Fake instance and cleans it up after the test ends.
    … …  
    4335    # is ended shortly after the fakelog has been at the end
    4436    testsession = TestSession(
    45         port=int(config_get(configuration, 'GPSD_PORT', 2947)),
     37        port=2947,
    4638        options="", verbose=0, timeout=1)
    4739    testsession.spawn()
    … …  
    5042        testsession.cleanup()
    5143    except gps.fake.TestSessionError as e:
    52         if e.message == 'daemon died':
     44        if e.message == 'Daemon died':
    5345            pass
    5446        else:
    5547            raise e
     48
     49
     50@pytest.fixture(scope='module')
     51def n2kd_fake():
     52    """
     53        Supplies a N2KD Fake instance and cleans it up after the test ends.
     54        To use it, you need to
     55        1. add a n2kd fake request handler
     56        2. call testsession.start()
     57    """
     58    n2kdfakesession = n2kdfake.N2kdFakeSession(host='127.0.0.1', port=2597)
     59    yield n2kdfakesession
     60    n2kdfakesession.shutdown()
  • src/gnuboat/device/tests/test_gpsd_poller.py

    r0c8a692 rfffb074  
    2525
    2626from gnuboat.device.daemon import gpsdpoller
    27 from gnuboat.common.webservice.Config import (
    28     config_get
    29 )
    3027
    31 
    32 def fakehook(linenumber, fakegps):
    33     """
    34         Used as pred for gps fake device,
    35         and indicates, when the device is ready
    36         to generate data
    37     """
    38     if not fakegps.testload.sentences:
    39         sys.stderr.write("fakegps: no sentences in test load.\n")
    40         raise SystemExit(1)
    41     if linenumber % len(fakegps.testload.sentences) == 0:
    42         if linenumber > 0:
    43             return False
    44     time.sleep(1)
    45     return True
     28from . import gpsd_fakehook
    4629
    4730
    … …  
    5134
    5235
    53 def assert_custom(result, error_text, gps_client):
    54     if not result:
    55         gps_client.stop()
    56         gps_client.join()
    57     assert result, error_text
     36@pytest.fixture
     37def gpsd_poller():
     38    ready_event = threading.Event()
     39    gps_client = gpsdpoller.GpsdPoller(
     40        port=2947,
     41        ready_event=ready_event)
     42    yield (gps_client, ready_event)
     43    gps_client.stop()
     44    gps_client.join(5)
    5845
    5946
    … …  
    10895    ]
    10996)
    110 def test_gpsdpoller(configuration, gps_fake, gps_logfile, results):
     97def test_gpsdpoller(gps_fake, gpsd_poller, gps_logfile, results):
    11198    gps_fake.gps_add(
    112         gps_logfile, pred=fakehook,
     99        gps_logfile, pred=gpsd_fakehook,
    113100        speed=4800, oneshot=True)
    114     ready_event = threading.Event()
    115     gps_client = gpsdpoller.GpsdPoller(
    116         port=int(config_get(configuration, 'GPSD_PORT', 2947)),
    117         ready_event=ready_event)
     101    gps_client = gpsd_poller[0]
     102    ready_event = gpsd_poller[1]
    118103    gps_client.start()
    119104    ready_event.wait(10)
    … …  
    130115            key = gps_time.strftime('%d%m%y%H%M%S')
    131116            if key in results:
    132                 assert_custom(
    133                     math.isclose(
     117                assert math.isclose(
    134118                        longitude, results[key]['longitude'], rel_tol=1e-6
    135                     ),
    136                     'Wrong longitude {} vs. {}'.format(
     119                    ), 'Wrong longitude {} vs. {}'.format(
    137120                        longitude, results[key]['longitude']
    138                     ), gps_client
    139                 )
    140                 assert_custom(
    141                     math.isclose(
     121                    )
     122                assert math.isclose(
    142123                        latitude, results[key]['latitude'], rel_tol=1e-6
    143                     ),
    144                     'Wrong latitude {} vs. {}'.format(
     124                    ), 'Wrong latitude {} vs. {}'.format(
    145125                        latitude, results[key]['latitude']
    146                     ), gps_client
    147                 )
    148                 assert_custom(fix >= 2, 'No fix, where expected', gps_client)
    149                 assert_custom(
    150                     gps_time == results[key]['datetime'],
     126                    )
     127                assert fix >= 2, 'No fix, where expected'
     128                assert gps_time == results[key]['datetime'], \
    151129                    'Wrong time {} vs {}'.format(
    152130                        gps_client.time_utc().isoformat('T'),
    153131                        results[key]['datetime'].isoformat('T')
    154                     ), gps_client
    155                 )
     132                    )
    156133                if key not in found:
    157134                    found.append(key)
    158135        time.sleep(0.25)
    159136    missed = abs(len(results) - len(found))
    160     assert_custom(
    161         missed < 2,
    162         'Missed {} out of {} messages'.format(missed, len(results)),
    163         gps_client
    164     )
    165 
    166     gps_fake.cleanup()
    167     gps_client.stop()
    168     gps_client.join()
     137    assert missed < 2, \
     138        'Missed {} out of {} messages'.format(missed, len(results))
Note: See TracChangeset for help on using the changeset viewer.