def test_set_func(self): self.set_val = None def set_func(val): self.set_val = val p = Parameter('test', set_func=set_func) p.set(-1) assert self.set_val == -1
class TestBuffer(MeasurementTests): @fixture def buffer(self): index = pd.MultiIndex.from_product([range(2), range(2)], names=['x', 'y']) frame = pd.DataFrame({'z': range(4)}, index=index) self.pscale = Parameter('scale', value=1) self.source = Function(lambda scale: frame * scale, args=[self.pscale], coordinates=[Parameter('x'), Parameter('y')], values=[Parameter('z')]) return Buffer(self.source) # run MeasurementTests against uninitialized and initialized buffers @fixture(params=[False, True], ids=['uninitialized', 'initialized']) def measurement(self, request, buffer): if request.param: buffer.writer() return buffer.reader def test_call(self, buffer): with raises(TypeError): buffer() def test_read_write(self, buffer): self.pscale.set(1) ref1 = self.source(output_data=True) buffer.writer() read1 = buffer.reader(output_data=True) assert ref1.equals(read1), 'buffer did not return same value as source' self.pscale.set(2) ref2 = self.source(output_data=True) assert not ref1.equals( ref2), 'source return did not change, test error' read2 = buffer.reader(output_data=True) assert ref1.equals(read2), 'buffer return affected by change of source' def test_multi_read(self, buffer): ''' assert that multiple readers can be inserted into the tree ''' ma = MeasurementArray(buffer.writer, buffer.reader, buffer.reader) ma() def test_multi_write(self, buffer): ''' assert that only one writer can be inserted into the tree ''' ma = MeasurementArray(buffer.writer, buffer.writer, buffer.reader) with raises(ValueError): ma()
class CountingMeasurement(Measurement): def __init__(self, raises=None, raise_count=0, **kwargs): ''' A measurement that returns incrementing integers. Parameters: raises (Exception, optional) - Exception to raise when the counter reaches raise_count. raise_count (int, default 0) - Counter value that triggers the Exception. ''' super(CountingMeasurement, self).__init__(**kwargs) self.counter = Parameter('count', value=-1) self.prepare_count = 0 self.setup_count = 0 self.measure_count = 0 self.teardown_count = 0 self.values.append(self.counter) self.raises = raises self.raise_count = raise_count def _setup(self): self.setup_count += 1 def _prepare(self): self.prepare_count += 1 def _measure(self, **kwargs): self.measure_count += 1 self.counter.set(self.counter.get() + 1) if (self.raises is not None) and (self.counter.get() == self.raise_count): raise self.raises() frame = pd.DataFrame.from_dict( {self.counter.name: [self.counter.get()]}) self.store.append(frame) return frame def _teardown(self): self.teardown_count += 1
def test_parameter_arg(self, parameter): p = Parameter('p') ctx = SetParameter(parameter, p) p.set(10) with ctx: assert parameter.get() == 10
def test_parameter_arg(self, instrument): p = Parameter('p') ctx = SetInstrument(instrument, power=p) p.set(10) with ctx: assert instrument.get('power') == 10