From 12a6c1ab8c1952585677b525c59284e86785aafd Mon Sep 17 00:00:00 2001 From: Ken Kundert Date: Sun, 25 Jun 2017 13:53:45 -0700 Subject: [PATCH] Improve test coverage. --- doc/user.rst | 2 +- quantiphy.py | 8 ++++---- test_constants.py | 9 +++++++++ test_format.py | 40 ++++++++++++++++++++-------------------- test_misc.py | 22 ++++++++++++++++++++++ test_quantity.py | 6 +++--- test_unit_conversion.py | 3 +++ 7 files changed, 62 insertions(+), 28 deletions(-) diff --git a/doc/user.rst b/doc/user.rst index d033724..bb03cc1 100644 --- a/doc/user.rst +++ b/doc/user.rst @@ -1214,4 +1214,4 @@ set or get a preference that is not supported. >>> q = add_constant(Quantity('1ns')) Traceback (most recent call last): ... - NameError: no name specified. + NameError: No name specified. diff --git a/quantiphy.py b/quantiphy.py index 64d7dc4..40b2bab 100644 --- a/quantiphy.py +++ b/quantiphy.py @@ -21,9 +21,9 @@ import math try: from collections import ChainMap -except ImportError: +except ImportError: # pragma: no cover from chainmap import ChainMap -from six import string_types, u, python_2_unicode_compatible +from six import string_types, python_2_unicode_compatible # Utilities {{{1 # is_str {{{2 @@ -211,7 +211,7 @@ def add_constant(value, alias=None, unit_systems=None): specified. """ if not alias and not value.name: - raise NameError('no name specified.') + raise NameError('No name specified.') if is_str(unit_systems): unit_systems = unit_systems.split() @@ -1341,7 +1341,7 @@ def map_sf_to_sci_notation(sf): # The explicit references to unicode here and in _SCI_NOTATION_MAPPER are # for backward compatibility with python2. They can be removed when # python2 support is dropped. - return u(sf).translate(Quantity._SCI_NOTATION_MAPPER) + return sf.translate(Quantity._SCI_NOTATION_MAPPER) # map_sf_to_greek() {{{2 @staticmethod diff --git a/test_constants.py b/test_constants.py index 0900460..1ca9b62 100644 --- a/test_constants.py +++ b/test_constants.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from quantiphy import Quantity, add_constant, set_unit_system +import pytest def test_constants(): Quantity.set_preferences(spacer=' ', label_fmt='{n} = {v} -- {d}') @@ -55,6 +56,9 @@ def test_constants(): assert str(Quantity('c')) == '299.79 Mm/s' assert str(Quantity('0C')) == '273.15 K' + with pytest.raises(ValueError, message='fuzz: not a valid number.'): + str(Quantity('fuzz')) + assert '{:S}'.format(Quantity('h')) == "h = 6.6261e-27 erg-s -- Plank's constant" assert '{:S}'.format(Quantity('hbar')) == "ħ = 1.0546e-27 erg-s -- reduced Plank's constant" assert '{:S}'.format(Quantity('ħ')) == "ħ = 1.0546e-27 erg-s -- reduced Plank's constant" @@ -97,6 +101,8 @@ def test_constants(): assert '{:S}'.format(Quantity('mu0')) == 'μ₀ = 1.2566 uH/m -- permeability of free space' assert '{:S}'.format(Quantity('Z0')) == 'Z₀ = 376.73 Ohms -- characteristic impedance of free space' + add_constant(Quantity('1420.405751786 MHz'), 'hline') + assert str(Quantity('hline')) == '1.4204 GHz' add_constant(Quantity(4.80320427e-10, 'Fr'), 'q', 'esu gaussian') add_constant(Quantity(1.602176487e-20, 'abC'), alias='q', unit_systems='emu') assert str(Quantity('q')) == '160.22e-21 C' @@ -109,3 +115,6 @@ def test_constants(): set_unit_system('emu') assert str(Quantity('q')) == '16.022e-21 abC' set_unit_system('mks') + + with pytest.raises(NameError, message='No name specified.'): + add_constant(Quantity(4.80320427e-10, 'Fr'), unit_systems='esu gaussian') diff --git a/test_format.py b/test_format.py index 33164e4..3ecff61 100644 --- a/test_format.py +++ b/test_format.py @@ -1,5 +1,6 @@ # encoding: utf8 +from __future__ import unicode_literals from quantiphy import Quantity import sys @@ -55,23 +56,22 @@ def test_full_format(): def test_scaled_format(): Quantity.set_preferences(spacer=' ', label_fmt=None, prec=None) - if sys.version_info.major == 3: - q=Quantity('Tboil = 100 °C -- boiling point of water') - assert '{}'.format(q) == '100 °C' - assert '{:.8}'.format(q) == '100 °C' - assert '{:.8s°F}'.format(q) == '212 °F' - assert '{:.8S°F}'.format(q) == 'Tboil = 212 °F' - assert '{:.8q°F}'.format(q) == '212 °F' - assert '{:.8Q°F}'.format(q) == 'Tboil = 212 °F' - assert '{:r°F}'.format(q) == '212' - assert '{:R°F}'.format(q) == 'Tboil = 212' - assert '{:u°F}'.format(q) == '°F' - assert '{:f°F}'.format(q) == '212.0000' - assert '{:F°F}'.format(q) == 'Tboil = 212.0000' - assert '{:e°F}'.format(q) == '2.1200e+02' - assert '{:E°F}'.format(q) == 'Tboil = 2.1200e+02' - assert '{:g°F}'.format(q) == '212' - assert '{:G°F}'.format(q) == 'Tboil = 212' - assert '{:n°F}'.format(q) == 'Tboil' - assert '{:d°F}'.format(q) == 'boiling point of water' - assert '{:X°F}'.format(q) == '100 °C' + q=Quantity('Tboil = 100 °C -- boiling point of water') + assert '{}'.format(q) == '100 °C' + assert '{:.8}'.format(q) == '100 °C' + assert '{:.8s°F}'.format(q) == '212 °F' + assert '{:.8S°F}'.format(q) == 'Tboil = 212 °F' + assert '{:.8q°F}'.format(q) == '212 °F' + assert '{:.8Q°F}'.format(q) == 'Tboil = 212 °F' + assert '{:r°F}'.format(q) == '212' + assert '{:R°F}'.format(q) == 'Tboil = 212' + assert '{:u°F}'.format(q) == '°F' + assert '{:f°F}'.format(q) == '212.0000' + assert '{:F°F}'.format(q) == 'Tboil = 212.0000' + assert '{:e°F}'.format(q) == '2.1200e+02' + assert '{:E°F}'.format(q) == 'Tboil = 2.1200e+02' + assert '{:g°F}'.format(q) == '212' + assert '{:G°F}'.format(q) == 'Tboil = 212' + assert '{:n°F}'.format(q) == 'Tboil' + assert '{:d°F}'.format(q) == 'boiling point of water' + assert '{:X°F}'.format(q) == '100 °C' diff --git a/test_misc.py b/test_misc.py index c1c8077..e4b4853 100644 --- a/test_misc.py +++ b/test_misc.py @@ -1,5 +1,6 @@ # encoding: utf8 +from __future__ import unicode_literals from quantiphy import Quantity, add_constant import pytest import sys @@ -201,6 +202,27 @@ class Foo(Quantity): processed = Quantity.all_from_si_fmt('0s', show_si=True) assert processed == '0 s' + # test input_sf + Quantity.set_preferences(input_sf='GMk', unity_sf='_', spacer='') + assert Quantity('10m').render(show_si=False) == '10_m' + Quantity.set_preferences(input_sf=None, unity_sf='_') + assert Quantity('10m').render(show_si=False) == '10e-3' + Quantity.set_preferences(unity_sf=None, spacer=None) + + # test map_sf + Quantity.set_preferences(map_sf=Quantity.map_sf_to_greek) + assert Quantity('10e-6 m').render() == '10 μm' + Quantity.set_preferences(map_sf=Quantity.map_sf_to_sci_notation) + assert Quantity('10e-6 m').render() == '10 μm' + assert Quantity('10e-6 m').render(show_si=False) == '10×10⁻⁶ m' + Quantity.set_preferences(map_sf=None) + + # test set_preferences error handling + with pytest.raises(NameError, message='fuzz: unknown.'): + Quantity.set_preferences(fuzz=True) + with pytest.raises(NameError, message='fuzz: unknown.'): + fuzz = Quantity.get_preference('fuzz') + mvi_raw_conv = ''' Status @ 0.00000000e+00s: Tests started for mylib.sh:MiM. Assertion successfully detects expected fault @ 1.00013334e-04s in sh_tb.REF (sh): 'V(cm)' out of range. diff --git a/test_quantity.py b/test_quantity.py index 3861087..ecf9ef7 100644 --- a/test_quantity.py +++ b/test_quantity.py @@ -1,5 +1,6 @@ # encoding: utf8 +from __future__ import unicode_literals from quantiphy import Quantity import sys @@ -34,8 +35,7 @@ def __init__(self, name, text, raw, formatted, prefs=None): Case('banker', '1ps', ('1e-12', 's'), '1ps'), Case('conquer', '1ns', ('1e-9', 's'), '1ns'), Case('share', '1us', ('1e-6', 's'), '1us'), - Case('resurface', '1μs', ('1e-6', 's'), '1us') if py3 else None, - # fails on python2, so skip it. + Case('resurface', '1μs', ('1e-6', 's'), '1us'), Case('witch', '1ms', ('1e-3', 's'), '1ms'), Case('engrave', '1cs', ('10e-3', 's'), '10ms'), Case('finance', '1_s', ('1', 's'), '1s'), @@ -313,7 +313,7 @@ def test_number_recognition(): Quantity(str(q)) except AssertionError: raise - except ValueError: + except (ValueError, KeyError): assert None is case.raw, case.name except Exception: print('%s: unexpected exception occurred.' % case.name) diff --git a/test_unit_conversion.py b/test_unit_conversion.py index 813362b..57096a1 100644 --- a/test_unit_conversion.py +++ b/test_unit_conversion.py @@ -4,12 +4,15 @@ from quantiphy import Quantity import math import sys +import pytest def test_simple_scaling(): Quantity.set_preferences(spacer=' ', label_fmt=None) q=Quantity('1kg') assert q.render() == '1 kg' assert q.render(scale=0.001, show_units=False) == '1' + with pytest.raises(KeyError, message="Unable to convert between 'fuzz' and 'g'."): + q.render(scale='fuzz') q=Quantity('1', units='g', scale=1000) assert q.render() == '1 kg'