Skip to content

Commit

Permalink
Add some unit tests for babel formats
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 30, 2021
1 parent 0a77401 commit f1434ac
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -31,6 +31,7 @@ ADD_PYTHON_TEST(PyQgsAttributeTableConfig test_qgsattributetableconfig.py)
ADD_PYTHON_TEST(PyQgsAttributeTableModel test_qgsattributetablemodel.py)
ADD_PYTHON_TEST(PyQgsAuthenticationSystem test_qgsauthsystem.py)
ADD_PYTHON_TEST(PyQgsAuthBasicMethod test_qgsauthbasicmethod.py)
ADD_PYTHON_TEST(PyQgsBabelGpsFormat test_qgsbabelgpsformat.py)
ADD_PYTHON_TEST(PyQgsBatchGeocodeAlgorithm test_qgsgeocoderalgorithm.py)
ADD_PYTHON_TEST(PyQgsBearingUtils test_qgsbearingutils.py)
ADD_PYTHON_TEST(PyQgsBinaryWidget test_qgsbinarywidget.py)
Expand Down
155 changes: 155 additions & 0 deletions tests/src/python/test_qgsbabelgpsformat.py
@@ -0,0 +1,155 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsAbstractBabelFormat and subclasses.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '2021-07'
__copyright__ = 'Copyright 2021, The QGIS Project'

import qgis # NOQA

from qgis.core import (
Qgis,
QgsBabelSimpleImportFormat,
QgsBabelGpsDeviceFormat
)
from qgis.testing import start_app, unittest

start_app()


class TestQgsBabelGpsFormat(unittest.TestCase):

def test_simple_format(self):
"""
Test QgsBabelSimpleImportFormat
"""
f = QgsBabelSimpleImportFormat('shapefile', Qgis.BabelFormatCapability.Waypoints)
self.assertEqual(f.capabilities(), Qgis.BabelFormatCapabilities(
Qgis.BabelFormatCapability.Waypoints | Qgis.BabelFormatCapability.Import))
f = QgsBabelSimpleImportFormat('shapefile', Qgis.BabelFormatCapabilities(
Qgis.BabelFormatCapability.Waypoints | Qgis.BabelFormatCapability.Tracks))
self.assertEqual(f.capabilities(), Qgis.BabelFormatCapabilities(
Qgis.BabelFormatCapability.Waypoints | Qgis.BabelFormatCapability.Tracks | Qgis.BabelFormatCapability.Import))

self.assertEqual(
f.importCommand('babel.exe', Qgis.GpsFeatureType.Waypoint, 'c:/test/test.shp', 'c:/test/test.gpx'),
['"babel.exe"',
'-w',
'-i',
'shapefile',
'-o',
'gpx',
'"c:/test/test.shp"',
'"c:/test/test.gpx"'])
self.assertEqual(
f.importCommand('babel.exe', Qgis.GpsFeatureType.Track, 'c:/test/test.shp', 'c:/test/test.gpx'),
['"babel.exe"',
'-t',
'-i',
'shapefile',
'-o',
'gpx',
'"c:/test/test.shp"',
'"c:/test/test.gpx"'])
self.assertEqual(
f.importCommand('babel.exe', Qgis.GpsFeatureType.Route, 'c:/test/test.shp', 'c:/test/test.gpx'),
['"babel.exe"',
'-r',
'-i',
'shapefile',
'-o',
'gpx',
'"c:/test/test.shp"',
'"c:/test/test.gpx"'])
# export not supported
self.assertEqual(
f.exportCommand('babel.exe', Qgis.GpsFeatureType.Waypoint, 'c:/test/test.shp', 'c:/test/test.gpx'), [])

def test_gps_device_format(self):
"""
Test QgsBabelGpsDeviceFormat
"""
f = QgsBabelGpsDeviceFormat(
"%babel -w -i garmin -o gpx %in %out",
"%babel -w -i gpx -o garmin %in %out",
"%babel -r -i garmin -o gpx %in %out",
"%babel -r -i gpx -o garmin %in %out",
"%babel -t -i garmin -o gpx %in %out",
"%babel -t -i gpx -o garmin %in %out"
)
# TODO -- I suspect that the waypoint/track/route capability should be automatically set/removed
# depending on whether the corresponding commands are empty!
#self.assertEqual(f.capabilities(), Qgis.BabelFormatCapabilities(
# Qgis.BabelFormatCapability.Waypoints | Qgis.BabelFormatCapability.Import))

# TODO -- babel command should possibly be quoted (or NOT in QgsBabelSimpleImportFormat)
self.assertEqual(
f.importCommand('babel.exe', Qgis.GpsFeatureType.Waypoint, 'c:/test/test.shp', 'c:/test/test.gpx'),
['babel.exe',
'-w',
'-i',
'garmin',
'-o',
'gpx',
'"c:/test/test.shp"',
'"c:/test/test.gpx"'])
self.assertEqual(
f.importCommand('babel.exe', Qgis.GpsFeatureType.Track, 'c:/test/test.shp', 'c:/test/test.gpx'),
['babel.exe',
'-t',
'-i',
'garmin',
'-o',
'gpx',
'"c:/test/test.shp"',
'"c:/test/test.gpx"'])
self.assertEqual(
f.importCommand('babel.exe', Qgis.GpsFeatureType.Route, 'c:/test/test.shp', 'c:/test/test.gpx'),
['babel.exe',
'-r',
'-i',
'garmin',
'-o',
'gpx',
'"c:/test/test.shp"',
'"c:/test/test.gpx"'])

self.assertEqual(
f.exportCommand('babel.exe', Qgis.GpsFeatureType.Waypoint, 'c:/test/test.shp', 'c:/test/test.gpx'),
['babel.exe',
'-w',
'-i',
'gpx',
'-o',
'garmin',
'"c:/test/test.shp"',
'"c:/test/test.gpx"'])
self.assertEqual(
f.exportCommand('babel.exe', Qgis.GpsFeatureType.Track, 'c:/test/test.shp', 'c:/test/test.gpx'),
['babel.exe',
'-t',
'-i',
'gpx',
'-o',
'garmin',
'"c:/test/test.shp"',
'"c:/test/test.gpx"'])
self.assertEqual(
f.exportCommand('babel.exe', Qgis.GpsFeatureType.Route, 'c:/test/test.shp', 'c:/test/test.gpx'),
['babel.exe',
'-r',
'-i',
'gpx',
'-o',
'garmin',
'"c:/test/test.shp"',
'"c:/test/test.gpx"'])


if __name__ == '__main__':
unittest.main()

0 comments on commit f1434ac

Please sign in to comment.