Skip to content

Commit

Permalink
filter deprecation warnings in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshnielsen committed Feb 26, 2023
1 parent 9545bf9 commit 5b7faca
Showing 1 changed file with 128 additions and 124 deletions.
252 changes: 128 additions & 124 deletions qcodes/tests/test_sweep_values.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import warnings

import pytest

from qcodes.parameters import Parameter
from qcodes.parameters.sweep_values import SweepValues
from qcodes.utils import QCoDeSDeprecationWarning
from qcodes.validators import Numbers


Expand All @@ -25,92 +28,98 @@ def _make_c2():

def test_errors(c0, c1, c2):

# only complete 3-part slices are valid
with pytest.raises(TypeError):
c0[1:2] # For Int params this could be defined as step=1
with pytest.raises(TypeError):
c0[:2:3]
with pytest.raises(TypeError):
c0[1::3]
with pytest.raises(TypeError):
c0[:] # For Enum params we *could* define this one too...

# fails if the parameter has no setter
with pytest.raises(TypeError):
c2[0:0.1:0.01]

# validates every step value against the parameter's Validator
with pytest.raises(ValueError):
c0[5:15:1]
with pytest.raises(ValueError):
c0[5.0:15.0:1.0]
with pytest.raises(ValueError):
c0[-12]
with pytest.raises(ValueError):
c0[-5, 12, 5]
with pytest.raises(ValueError):
c0[-5, 12:8:1, 5]

# cannot combine SweepValues for different parameters
with pytest.raises(TypeError):
_ = c0[0.1] + c1[0.2]

# improper use of extend
with pytest.raises(TypeError):
c0[0.1].extend(5)

# SweepValue object has no getter, even if the parameter does
with pytest.raises(AttributeError):
c0[0.1].get
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=QCoDeSDeprecationWarning)

# only complete 3-part slices are valid
with pytest.raises(TypeError):
c0[1:2] # For Int params this could be defined as step=1
with pytest.raises(TypeError):
c0[:2:3]
with pytest.raises(TypeError):
c0[1::3]
with pytest.raises(TypeError):
c0[:] # For Enum params we *could* define this one too...

# fails if the parameter has no setter
with pytest.raises(TypeError):
c2[0:0.1:0.01]

# validates every step value against the parameter's Validator
with pytest.raises(ValueError):
c0[5:15:1]
with pytest.raises(ValueError):
c0[5.0:15.0:1.0]
with pytest.raises(ValueError):
c0[-12]
with pytest.raises(ValueError):
c0[-5, 12, 5]
with pytest.raises(ValueError):
c0[-5, 12:8:1, 5]

# cannot combine SweepValues for different parameters
with pytest.raises(TypeError):
_ = c0[0.1] + c1[0.2]

# improper use of extend
with pytest.raises(TypeError):
c0[0.1].extend(5)

# SweepValue object has no getter, even if the parameter does
with pytest.raises(AttributeError):
c0[0.1].get


def test_valid(c0):

c0_sv = c0[1]
# setter gets mapped
assert c0_sv.set == c0.set
# normal sequence operations access values
assert list(c0_sv) == [1]
assert c0_sv[0] == 1
assert 1 in c0_sv
assert 2 not in c0_sv

# in-place and copying addition
c0_sv += c0[1.5:1.8:0.1]
c0_sv2 = c0_sv + c0[2]
assert list(c0_sv) == [1, 1.5, 1.6, 1.7]
assert list(c0_sv2) == [1, 1.5, 1.6, 1.7, 2]

# append and extend
c0_sv3 = c0[2]
# append only works with straight values
c0_sv3.append(2.1)
# extend can use another SweepValue, (even if it only has one value)
c0_sv3.extend(c0[2.2])
# extend can also take a sequence
c0_sv3.extend([2.3])
# as can addition
c0_sv3 += [2.4]
c0_sv4 = c0_sv3 + [2.5, 2.6]
assert list(c0_sv3) == [2, 2.1, 2.2, 2.3, 2.4]
assert list(c0_sv4) == [2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6]

# len
assert len(c0_sv3) == 5

# in-place and copying reverse
c0_sv.reverse()
c0_sv5 = reversed(c0_sv)
assert list(c0_sv) == [1.7, 1.6, 1.5, 1]
assert list(c0_sv5) == [1, 1.5, 1.6, 1.7]

# multi-key init, where first key is itself a list
c0_sv6 = c0[[1, 3], 4]
# copying
c0_sv7 = c0_sv6.copy()
assert list(c0_sv6) == [1, 3, 4]
assert list(c0_sv7) == [1, 3, 4]
assert c0_sv6 is not c0_sv7
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=QCoDeSDeprecationWarning)

c0_sv = c0[1]
# setter gets mapped
assert c0_sv.set == c0.set
# normal sequence operations access values
assert list(c0_sv) == [1]
assert c0_sv[0] == 1
assert 1 in c0_sv
assert 2 not in c0_sv

# in-place and copying addition
c0_sv += c0[1.5:1.8:0.1]
c0_sv2 = c0_sv + c0[2]
assert list(c0_sv) == [1, 1.5, 1.6, 1.7]
assert list(c0_sv2) == [1, 1.5, 1.6, 1.7, 2]

# append and extend
c0_sv3 = c0[2]
# append only works with straight values
c0_sv3.append(2.1)
# extend can use another SweepValue, (even if it only has one value)
c0_sv3.extend(c0[2.2])
# extend can also take a sequence
c0_sv3.extend([2.3])
# as can addition
c0_sv3 += [2.4]
c0_sv4 = c0_sv3 + [2.5, 2.6]
assert list(c0_sv3) == [2, 2.1, 2.2, 2.3, 2.4]
assert list(c0_sv4) == [2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6]

# len
assert len(c0_sv3) == 5

# in-place and copying reverse
c0_sv.reverse()
c0_sv5 = reversed(c0_sv)
assert list(c0_sv) == [1.7, 1.6, 1.5, 1]
assert list(c0_sv5) == [1, 1.5, 1.6, 1.7]

# multi-key init, where first key is itself a list
c0_sv6 = c0[[1, 3], 4]
# copying
c0_sv7 = c0_sv6.copy()
assert list(c0_sv6) == [1, 3, 4]
assert list(c0_sv7) == [1, 3, 4]
assert c0_sv6 is not c0_sv7


def test_base():
Expand All @@ -121,51 +130,46 @@ def test_base():

def test_snapshot(c0):

assert c0[0].snapshot() == {
'parameter': c0.snapshot(),
'values': [{'item': 0}]
}

assert c0[0:5:0.3].snapshot()['values'] == [{
'first': 0,
'last': 4.8,
'num': 17,
'type': 'linear'
}]

sv = c0.sweep(start=2, stop=4, num=5)
assert sv.snapshot()['values'] == [{
'first': 2,
'last': 4,
'num': 5,
'type': 'linear'
}]

# mixture of bare items, nested lists, and slices
sv = c0[1, 7, 3.2, [1, 2, 3], 6:9:1, -4.5, 5.3]
assert sv.snapshot()['values'] == [{
'first': 1,
'last': 5.3,
'min': -4.5,
'max': 8,
'num': 11,
'type': 'sequence'
}]

assert (c0[0] + c0[1]).snapshot()['values'] == [
{'item': 0},
{'item': 1}
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=QCoDeSDeprecationWarning)

assert c0[0].snapshot() == {"parameter": c0.snapshot(), "values": [{"item": 0}]}

assert c0[0:5:0.3].snapshot()["values"] == [
{"first": 0, "last": 4.8, "num": 17, "type": "linear"}
]

sv = c0.sweep(start=2, stop=4, num=5)
assert sv.snapshot()["values"] == [
{"first": 2, "last": 4, "num": 5, "type": "linear"}
]

assert (c0[0:3:1] + c0[4, 6, 9]).snapshot()['values'] == [
{'first': 0, 'last': 2, 'num': 3, 'type': 'linear'},
{'first': 4, 'last': 9, 'min': 4, 'max': 9, 'num': 3,
'type': 'sequence'}
# mixture of bare items, nested lists, and slices
sv = c0[1, 7, 3.2, [1, 2, 3], 6:9:1, -4.5, 5.3]
assert sv.snapshot()["values"] == [
{
"first": 1,
"last": 5.3,
"min": -4.5,
"max": 8,
"num": 11,
"type": "sequence",
}
]

assert (c0[0] + c0[1]).snapshot()["values"] == [{"item": 0}, {"item": 1}]

assert (c0[0:3:1] + c0[4, 6, 9]).snapshot()["values"] == [
{"first": 0, "last": 2, "num": 3, "type": "linear"},
{"first": 4, "last": 9, "min": 4, "max": 9, "num": 3, "type": "sequence"},
]


def test_repr(c0):
sv = c0[0]
assert repr(sv) == (
f"<qcodes.parameters.sweep_values.SweepFixedValues: c0 at {id(sv)}>"
)
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=QCoDeSDeprecationWarning)

sv = c0[0]
assert repr(sv) == (
f"<qcodes.parameters.sweep_values.SweepFixedValues: c0 at {id(sv)}>"
)

0 comments on commit 5b7faca

Please sign in to comment.