Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Jun 25, 2017
1 parent 1ad6629 commit 12a6c1a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 28 deletions.
2 changes: 1 addition & 1 deletion doc/user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 4 additions & 4 deletions quantiphy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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'
Expand All @@ -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')
40 changes: 20 additions & 20 deletions test_format.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: utf8

from __future__ import unicode_literals
from quantiphy import Quantity
import sys

Expand Down Expand Up @@ -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'
22 changes: 22 additions & 0 deletions test_misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: utf8

from __future__ import unicode_literals
from quantiphy import Quantity, add_constant
import pytest
import sys
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions test_quantity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: utf8

from __future__ import unicode_literals
from quantiphy import Quantity
import sys

Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions test_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 12a6c1a

Please sign in to comment.