Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions qcodes/tests/instrument_mocks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import time
import logging
from functools import partial

from qcodes.instrument.base import Instrument
from qcodes.instrument.mock import MockInstrument, MockModel
Expand All @@ -19,7 +17,8 @@ def _reset(self):
self._gates = [0.0, 0.0, 0.0]
self._excitation = 0.1

def fmt(self, value):
@staticmethod
def fmt(value):
return '{:.3f}'.format(value)

def gates_set(self, parameter, value):
Expand Down Expand Up @@ -48,6 +47,7 @@ def source_set(self, parameter, value):
if parameter == 'ampl':
try:
self._excitation = float(value)
# TODO(giulioungaretti) fix bare-except
except:
# "Off" as in the MultiType sweep step test
self._excitation = None
Expand Down Expand Up @@ -91,25 +91,25 @@ def __init__(self, *args, **kwargs):
self.attach_adder()

def attach_adder(self):
'''
"""
this function attaches a closure to the object, so can only be
executed after creating the server because a closure is not
picklable
'''
"""
a = 5

def f(b):
'''
"""
not the same function as the original method
'''
"""
return a + b
self.add5 = f

def add5(self, b):
'''
"""
The class copy of this should not get run, because it should
be overwritten on the server by the closure version.
'''
"""
raise RuntimeError('dont run this one!')


Expand Down Expand Up @@ -186,17 +186,18 @@ def __init__(self, model=None, **kwargs):
class DummyInstrument(Instrument):

def __init__(self, name='dummy', gates=['dac1', 'dac2', 'dac3'], **kwargs):
''' Create a dummy instrument that can be used for testing

"""
Create a dummy instrument that can be used for testing

Args:
name (string): name for the instrument
gates (list): list of names that is used to create parameters for
the instrument
'''
"""
super().__init__(name, **kwargs)

# make gates
for i, g in enumerate(gates):
for _, g in enumerate(gates):
self.add_parameter(g,
parameter_class=ManualParameter,
initial_value=0,
Expand Down
148 changes: 6 additions & 142 deletions qcodes/tests/test_instrument.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from unittest import TestCase
"""
Test suite for instument.*
"""
from datetime import datetime, timedelta
from unittest import TestCase
import time
from collections import namedtuple

from qcodes.instrument.base import Instrument
from qcodes.instrument.mock import MockInstrument
from qcodes.instrument.parameter import (Parameter, ManualParameter,
StandardParameter)
from qcodes.instrument.function import Function
from qcodes.instrument.parameter import ManualParameter
from qcodes.instrument.server import get_instrument_server_manager

from qcodes.utils.validators import Numbers, Ints, Strings, MultiType, Enum
Expand All @@ -20,112 +20,6 @@
from .common import strip_qc


class TestParamConstructor(TestCase):

def test_name_s(self):
p = Parameter('simple')
self.assertEqual(p.name, 'simple')

with self.assertRaises(ValueError):
# you need a name of some sort
Parameter()

# or names
names = ['H1', 'L1']
p = Parameter(names=names)
self.assertEqual(p.names, names)
# if you don't provide a name, it's called 'None'
# TODO: we should probably require an explicit name.
self.assertEqual(p.name, 'None')

# or both, that's OK too.
names = ['Peter', 'Paul', 'Mary']
p = Parameter(name='complex', names=names)
self.assertEqual(p.names, names)
# You can always have a name along with names
self.assertEqual(p.name, 'complex')

shape = (10,)
setpoints = (range(10),)
setpoint_names = ('my_sp',)
setpoint_labels = ('A label!',)
p = Parameter('makes_array', shape=shape, setpoints=setpoints,
setpoint_names=setpoint_names,
setpoint_labels=setpoint_labels)
self.assertEqual(p.shape, shape)
self.assertFalse(hasattr(p, 'shapes'))
self.assertEqual(p.setpoints, setpoints)
self.assertEqual(p.setpoint_names, setpoint_names)
self.assertEqual(p.setpoint_labels, setpoint_labels)

shapes = ((2,), (3,))
setpoints = ((range(2),), (range(3),))
setpoint_names = (('sp1',), ('sp2',))
setpoint_labels = (('first label',), ('second label',))
p = Parameter('makes arrays', shapes=shapes, setpoints=setpoints,
setpoint_names=setpoint_names,
setpoint_labels=setpoint_labels)
self.assertEqual(p.shapes, shapes)
self.assertFalse(hasattr(p, 'shape'))
self.assertEqual(p.setpoints, setpoints)
self.assertEqual(p.setpoint_names, setpoint_names)
self.assertEqual(p.setpoint_labels, setpoint_labels)

def test_repr(self):
for i in [0, "foo", "", "fåil"]:
with self.subTest(i=i):
param = Parameter(name=i)
s = param.__repr__()
st = '<{}.{}: {} at {}>'.format(
param.__module__, param.__class__.__name__,
param.name, id(param))
self.assertEqual(s, st)

blank_instruments = (
None, # no instrument at all
namedtuple('noname', '')(), # no .name
namedtuple('blank', 'name')('') # blank .name
)
named_instrument = namedtuple('yesname', 'name')('astro')

def test_full_name(self):
# three cases where only name gets used for full_name
for instrument in self.blank_instruments:
p = Parameter(name='fred')
p._instrument = instrument
self.assertEqual(p.full_name, 'fred')

p.name = None
self.assertEqual(p.full_name, None)

# and finally an instrument that really has a name
p = Parameter(name='wilma')
p._instrument = self.named_instrument
self.assertEqual(p.full_name, 'astro_wilma')

p.name = None
self.assertEqual(p.full_name, None)

def test_full_names(self):
for instrument in self.blank_instruments:
# no instrument
p = Parameter(name='simple')
p._instrument = instrument
self.assertEqual(p.full_names, None)

p = Parameter(names=['a', 'b'])
p._instrument = instrument
self.assertEqual(p.full_names, ['a', 'b'])

p = Parameter(name='simple')
p._instrument = self.named_instrument
self.assertEqual(p.full_names, None)

p = Parameter(names=['penn', 'teller'])
p._instrument = self.named_instrument
self.assertEqual(p.full_names, ['astro_penn', 'astro_teller'])


class GatesBadDelayType(MockGates):

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -584,37 +478,6 @@ def test_val_mapping_parsers(self):
with self.assertRaises(ValueError):
gates.modecoded.set('0')

def test_param_cmd_with_parsing(self):
def set_p(val):
self._p = val

def get_p():
return self._p

def parse_set_p(val):
return '{:d}'.format(val)

p = StandardParameter('p_int', get_cmd=get_p, get_parser=int,
set_cmd=set_p, set_parser=parse_set_p)

p(5)
self.assertEqual(self._p, '5')
self.assertEqual(p(), 5)

def test_bare_function(self):
# not a use case we want to promote, but it's there...
p = ManualParameter('test')

def doubler(x):
p.set(x * 2)

f = Function('f', call_cmd=doubler, args=[Numbers(-10, 10)])

f(4)
self.assertEqual(p.get(), 8)
with self.assertRaises(ValueError):
f(20)

def test_standard_snapshot(self):
self.maxDiff = None
snap = self.meter.snapshot()
Expand Down Expand Up @@ -997,6 +860,7 @@ def setUp(self):
name='testdummy', gates=['dac1', 'dac2', 'dac3'], server_name=None)

def tearDown(self):
#TODO (giulioungaretti) remove ( does nothing ?)
pass

def test_attr_access(self):
Expand Down
Loading