Skip to content

Commit

Permalink
add a couple of tests to be sure that distance and area strings are
Browse files Browse the repository at this point in the history
localized correctly
  • Loading branch information
alexbruy committed Feb 3, 2022
1 parent 3ac6704 commit db9e483
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions tests/src/python/test_qgsunittypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
from qgis.core import QgsUnitTypes
from qgis.PyQt.QtCore import QLocale

# enforce C locale because the tests expect it
# (decimal separators / thousand separators)
QLocale.setDefault(QLocale.c())


class TestQgsUnitTypes(unittest.TestCase):

def setUp(self):
super().setUp()
# enforce C locale because the tests expect it
# (decimal separators / thousand separators)
QLocale.setDefault(QLocale.c())

def testEncodeDecodeUnitType(self):
"""Test encoding and decoding unit type"""
units = [QgsUnitTypes.TypeDistance,
Expand Down Expand Up @@ -1221,6 +1223,11 @@ def testFormatDistance(self):
self.assertEqual(QgsUnitTypes.formatDistance(0.00000168478, 2, QgsUnitTypes.DistanceMeters, False), '1.68e-06 m')
self.assertEqual(QgsUnitTypes.formatDistance(0.00168478, 2, QgsUnitTypes.DistanceMeters, True), '1.68e-03 m')

# test different locales
QLocale.setDefault(QLocale(QLocale.Italian))
self.assertEqual(QgsUnitTypes.formatDistance(10, 3, QgsUnitTypes.DistanceMeters, False), '10,000 m')
self.assertEqual(QgsUnitTypes.formatDistance(0.5, 2, QgsUnitTypes.DistanceMiles, False), '2.640,00 ft')

def testFormatArea(self):
"""Test formatting areas"""
# keep base unit
Expand Down Expand Up @@ -1266,6 +1273,11 @@ def testFormatArea(self):
self.assertEqual(QgsUnitTypes.formatArea(0.00168478, 2, QgsUnitTypes.AreaSquareMeters, False), '1.68e-03 m²')
self.assertEqual(QgsUnitTypes.formatArea(0.00168478, 2, QgsUnitTypes.AreaSquareMeters, True), '1.68e-03 m²')

# test different locales
QLocale.setDefault(QLocale(QLocale.Italian))
self.assertEqual(QgsUnitTypes.formatArea(100, 2, QgsUnitTypes.AreaSquareKilometers, False), '100,00 km²')
self.assertEqual(QgsUnitTypes.formatArea(2787, 2, QgsUnitTypes.AreaSquareFeet, False), '2.787,00 ft²')

def testEncodeDecodeLayoutUnits(self):
"""Test encoding and decoding layout units"""
units = [QgsUnitTypes.LayoutMillimeters,
Expand Down

0 comments on commit db9e483

Please sign in to comment.