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 30, 2017
1 parent 852494a commit c9f6e2b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
28 changes: 24 additions & 4 deletions doc/user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -910,10 +910,30 @@ naturally want different precisions:
... frequencies.append((Temperature(temp, 'C'), Frequency(freq, 'Hz')))
>>> for temp, freq in frequencies:
... print(temp, freq)
-25C 999.988kHz
25C 1.00021MHz
75C 1.00178MHz
... print(f'{temp:>4s} {freq}')
-25C 999.988kHz
25C 1.00021MHz
75C 1.00178MHz
In this example, a subclass is created that is intended to report in
concentrations.

.. code-block:: python
>>> class Concentration(Quantity):
... pass
>>> Concentration.set_prefs(
... map_sf = dict(u=' PPM', n= ' PPB', p=' PPT'),
... show_label = True,
... )
>>> pollutants = dict(CO=5, SO2=20, NO2=0.10)
>>> concentrations = [Concentration(v, scale=1e-6, name=k) for k, v in pollutants.items()]
>>> for each in concentrations:
... print(each)
CO = 5 PPM
SO2 = 20 PPM
NO2 = 100 PPB
When a subclass is created, the preferences active in the main class are copied
into the subclass. Subsequent changes to the preferences in the main class do
Expand Down
2 changes: 1 addition & 1 deletion quantiphy.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def _initialize_recognizers(cls):

# identify desired scale factors {{{3
known_sf = ''.join(MAPPINGS)
if cls.get_pref('input_sf') is None:
if cls.get_pref('input_sf') is None: # pragma: no cover
input_sf = known_sf
else:
input_sf = cls.get_pref('input_sf')
Expand Down
1 change: 1 addition & 0 deletions test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def test_scaled_format():
assert '{:n°F}'.format(q) == 'Tboil'
assert '{:d°F}'.format(q) == 'boiling point of water'
assert '{:X°F}'.format(q) == '100 °C'
assert '{!r}'.format(q) == "Quantity('100 °C')"

def test_number_fmt():
Quantity.set_prefs(spacer=None, show_label=None, label_fmt=None, label_fmt_full=None, show_desc=False)
Expand Down
11 changes: 11 additions & 0 deletions test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ class Foo(Quantity):
assert Quantity('10e-6 m').render() == '10 μm'
assert Quantity('10e-6 m').render(show_si=False) == '10×10⁻⁶ m'
Quantity.set_prefs(map_sf=None)
sf_map = {
'u': ' PPM',
'n': ' PPB',
'p': ' PPT',
'f': ' PPQ',
}
with Quantity.prefs(map_sf=sf_map):
assert Quantity('10e-6').render() == '10 PPM'
assert Quantity('1e-7').render() == '100 PPB'
assert Quantity('1e-12').render() == '1 PPT'
assert Quantity('1e-13').render() == '100 PPQ'

# test set_prefs error handling
with pytest.raises(KeyError):
Expand Down

0 comments on commit c9f6e2b

Please sign in to comment.