-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathtest_units.py
More file actions
40 lines (30 loc) · 1.45 KB
/
test_units.py
File metadata and controls
40 lines (30 loc) · 1.45 KB
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
33
34
35
36
37
38
39
40
import pytest
from .. import units as pq
from .common import TestCase
class TestUnits(TestCase):
def test_compound_units(self):
pc_per_cc = pq.CompoundUnit("pc/cm**3")
self.assertEqual(str(pc_per_cc.dimensionality), "(pc/cm**3)")
self.assertEqual(str(pc_per_cc), "1 (pc/cm**3)")
temp = pc_per_cc * pq.CompoundUnit('m/m**3')
self.assertEqual(str(temp.dimensionality), "(pc/cm**3)*(m/m**3)")
self.assertEqual(str(temp), "1.0 (pc/cm**3)*(m/m**3)")
def test_units_protected(self):
def setunits(u, v):
u.units = v
def inplace(op, u, val):
getattr(u, '__i%s__'%op)(val)
self.assertRaises(AttributeError, setunits, pq.m, pq.ft)
self.assertRaises(TypeError, inplace, 'add', pq.m, pq.m)
self.assertRaises(TypeError, inplace, 'sub', pq.m, pq.m)
self.assertRaises(TypeError, inplace, 'mul', pq.m, pq.m)
self.assertRaises(TypeError, inplace, 'truediv', pq.m, pq.m)
self.assertRaises(TypeError, inplace, 'pow', pq.m, 2)
def test_units_copy(self):
self.assertQuantityEqual(pq.m.copy(), pq.m)
pc_per_cc = pq.CompoundUnit("pc/cm**3")
self.assertQuantityEqual(pc_per_cc.copy(), pc_per_cc)
def test_code_injection(self):
with pytest.raises(LookupError) as exc_info:
pq.CompoundUnit("exec(\"print('Hello there.')\\nprint('General Wasabi!')\")")
assert "Wasabi" in str(exc_info.value)