def test_other_exception(close_before_and_after): """Test an unexpected exception occurred during finding instrument""" with pytest.raises(TypeError, match="unhashable type: 'dict'"): # in order to raise an unexpected exception, and make sure it is # passed through the call stack, let's pass an empty dict instead # of a string with instrument name _ = find_or_create_instrument(DummyInstrument, {})
def test_create(close_before_and_after, request): """Test creating an instrument that does not yet exist""" instr = find_or_create_instrument(DummyInstrument, name='instr', gates=['dac1', 'dac2', 'dac3']) request.addfinalizer(instr.close) assert 'instr' == instr.name
def test_other_exception(self): """Test an unexpected exception occurred during finding instrument""" with self.assertRaises(TypeError) as cm: # in order to raise an unexpected exception, and make sure it is # passed through the call stack, let's pass an empty dict instead # of a string with instrument name _ = find_or_create_instrument(DummyInstrument, {}) self.assertEqual(str(cm.exception), "unhashable type: 'dict'")
def test_create(self): """Test creating an instrument that does not yet exist""" instr = find_or_create_instrument( DummyInstrument, name='instr', gates=['dac1', 'dac2', 'dac3']) self.assertEqual('instr', instr.name) instr.close()
def test_find(testdummy): """Test finding an existing instrument""" instr_2 = find_or_create_instrument( DummyInstrument, name='testdummy', gates=['dac1', 'dac2', 'dac3']) assert instr_2 is testdummy assert instr_2.name == testdummy.name
def test_find(self): """Test finding an existing instrument""" instr = DummyInstrument( name='instr', gates=['dac1', 'dac2', 'dac3']) instr_2 = find_or_create_instrument( DummyInstrument, name='instr', gates=['dac1', 'dac2', 'dac3']) self.assertEqual(instr_2, instr) self.assertEqual(instr_2.name, instr.name) instr.close()
def test_recreate(close_before_and_after, request): """Test the case when instrument needs to be recreated""" instr = DummyInstrument(name='instr', gates=['dac1', 'dac2', 'dac3']) request.addfinalizer(instr.close) assert ['instr'] == list(Instrument._all_instruments.keys()) instr_2 = find_or_create_instrument(DummyInstrument, name='instr', gates=['dac1', 'dac2'], recreate=True) request.addfinalizer(instr_2.close) assert ['instr'] == list(Instrument._all_instruments.keys()) assert instr_2 in Instrument._all_instruments.values() assert instr not in Instrument._all_instruments.values()
def test_find_same_name_but_different_class(close_before_and_after, request): """Test finding an existing instrument with different class""" instr = DummyInstrument(name='instr', gates=['dac1', 'dac2', 'dac3']) request.addfinalizer(instr.close) class GammyInstrument(Instrument): some_other_attr = 25 # Find an instrument with the same name but of different class error_msg = ("Instrument instr is <class " "'qcodes.tests.instrument_mocks.DummyInstrument'> but " "<class 'qcodes.tests.test_instrument" ".test_find_same_name_but_different_class.<locals>" ".GammyInstrument'> was requested") with pytest.raises(TypeError, match=error_msg): _ = find_or_create_instrument(GammyInstrument, name='instr', gates=['dac1', 'dac2', 'dac3'])
def test_find_same_name_but_different_class(self): """Test finding an existing instrument with different class""" instr = DummyInstrument( name='instr', gates=['dac1', 'dac2', 'dac3']) class GammyInstrument(Instrument): some_other_attr = 25 # Find an instrument with the same name but of different class with self.assertRaises(TypeError) as cm: _ = find_or_create_instrument( GammyInstrument, name='instr', gates=['dac1', 'dac2', 'dac3']) self.assertEqual("Instrument instr is <class " "'qcodes.tests.instrument_mocks.DummyInstrument'> but " "<class 'qcodes.tests.test_instrument" ".TestFindOrCreateInstrument" ".test_find_same_name_but_different_class.<locals>" ".GammyInstrument'> was requested", str(cm.exception)) instr.close()
def test_recreate(self): """Test the case when instrument needs to be recreated""" instr = DummyInstrument(name='instr', gates=['dac1', 'dac2', 'dac3']) instr_ref = weakref.ref(instr) self.assertListEqual(['instr'], list(Instrument._all_instruments.keys())) instr_2 = find_or_create_instrument(DummyInstrument, name='instr', gates=['dac1', 'dac2'], recreate=True) instr_2_ref = weakref.ref(instr_2) self.assertListEqual(['instr'], list(Instrument._all_instruments.keys())) self.assertIn(instr_2_ref, Instrument._all_instruments.values()) self.assertNotIn(instr_ref, Instrument._all_instruments.values()) instr_2.close()