2
2
"""Test the Quantity class Array API compatibility."""
3
3
4
4
import astropy .units as u
5
+ import numpy as np
6
+ import pytest
5
7
6
8
from quantity import Quantity , api
7
9
@@ -14,42 +16,43 @@ def test_issubclass_api():
14
16
assert issubclass (Quantity , api .QuantityArray )
15
17
16
18
19
+ def test_ndarray ():
20
+ """Test that ndarray does not satisfy the Quantity API."""
21
+ assert not issubclass (np .ndarray , api .Quantity )
22
+ assert not isinstance (np .array ([1 , 2 , 3 ]), api .Quantity )
23
+
24
+
17
25
def test_astropy_quantity ():
26
+ """Test that astropy.units.Quantity works with the Quantity API."""
18
27
assert issubclass (u .Quantity , api .Quantity )
19
- assert issubclass (u .Quantity , api .QuantityArray )
20
- aq = u .Quantity (1.0 , u .m )
21
- assert isinstance (aq , api .Quantity )
22
- assert isinstance (aq , api .QuantityArray )
28
+ assert isinstance (u .Quantity (np .array ([1 , 2 , 3 ]), u .m ), api .Quantity )
29
+
30
+
31
+ # ------------------------------
32
+
33
+
34
+ @pytest .fixture
35
+ def array_and_quantity (request ):
36
+ xp = request .param .xp
37
+ value = xp .asarray ([1.0 , 2.0 , 3.0 ])
38
+ q = Quantity (value , u .m )
39
+ return value , q
23
40
24
41
25
- class IsinstanceAPITests :
42
+ @pytest .mark .parametrize ("array_and_quantity" , ARRAY_NAMESPACES , indirect = True )
43
+ class TestIsinstanceAPI :
26
44
"""Check Quantities are properly recognized independent of the array type."""
27
45
28
- # Note: the actual test classes are created at the end
29
-
30
- @classmethod
31
- def setup_class (cls ):
32
- super ().setup_class ()
33
- cls .a = cls .xp .asarray ([1.0 , 2.0 , 3.0 ])
34
- cls .q = Quantity (cls .a , u .m )
35
-
36
- def test_issubclass_api (self ):
37
- assert not issubclass (type (self .a ), api .Quantity )
38
- assert not issubclass (type (self .a ), api .QuantityArray )
39
- # The two below Duplicate test_issubclass_api above, but OK to have
40
- # it more and less explicit.
41
- assert issubclass (type (self .q ), api .Quantity )
42
- assert issubclass (type (self .q ), api .QuantityArray )
43
-
44
- def test_isinstance_api (self ):
45
- assert not isinstance (self .a , api .Quantity )
46
- assert not isinstance (self .a , api .QuantityArray )
47
- assert isinstance (self .q , api .Quantity )
48
- assert isinstance (self .q , api .QuantityArray )
49
-
50
-
51
- # Create the test classes for the different array types.
52
- for base_setup in ARRAY_NAMESPACES :
53
- for tests in (IsinstanceAPITests ,):
54
- name = f"Test{ tests .__name__ } { base_setup .__name__ } "
55
- globals ()[name ] = type (name , (tests , base_setup ), {})
46
+ def test_issubclass_api (self , array_and_quantity ):
47
+ v , q = array_and_quantity
48
+ assert not issubclass (type (v ), api .Quantity )
49
+ assert not issubclass (type (v ), api .QuantityArray )
50
+ assert issubclass (type (q ), api .Quantity )
51
+ assert issubclass (type (q ), api .QuantityArray )
52
+
53
+ def test_isinstance_api (self , array_and_quantity ):
54
+ v , q = array_and_quantity
55
+ assert not isinstance (v , api .Quantity )
56
+ assert not isinstance (v , api .QuantityArray )
57
+ assert isinstance (q , api .Quantity )
58
+ assert isinstance (q , api .QuantityArray )
0 commit comments