File: test_infer_base_unit.py

package info (click to toggle)
python-pint 0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,068 kB
  • sloc: python: 9,481; makefile: 138
file content (32 lines) | stat: -rw-r--r-- 1,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from pint import UnitRegistry, set_application_registry
from pint.testsuite import QuantityTestCase
from pint.util import infer_base_unit

ureg = UnitRegistry()
set_application_registry(ureg)
Q = ureg.Quantity


class TestInferBaseUnit(QuantityTestCase):
    def test_infer_base_unit(self):
        from pint.util import infer_base_unit
        self.assertEqual(infer_base_unit(Q(1, 'millimeter * nanometer')), Q(1, 'meter**2').units)

    def test_units_adding_to_zero(self):
        self.assertEqual(infer_base_unit(Q(1, 'm * mm / m / um * s')), Q(1, 's').units)

    def test_to_compact(self):
        r = Q(1000000000, 'm') * Q(1, 'mm') / Q(1, 's') / Q(1, 'ms')
        compact_r = r.to_compact()
        expected = Q(1000., 'kilometer**2 / second**2')
        self.assertQuantityAlmostEqual(compact_r, expected)

        r = (Q(1, 'm') * Q(1, 'mm') / Q(1, 'm') / Q(2, 'um') * Q(2, 's')).to_compact()
        self.assertQuantityAlmostEqual(r, Q(1000, 's'))

    def test_volts(self):
        from pint.util import infer_base_unit
        r = Q(1, 'V') * Q(1, 'mV') / Q(1, 'kV')
        b = infer_base_unit(r)
        self.assertEqual(b, Q(1, 'V').units)
        self.assertQuantityAlmostEqual(r, Q(1, 'uV'))