from pml.exceptions import PvException
import pml.element
import pml.device
from pml.physics import Physics
from pml.units import UcPoly
import pytest
import mock


@pytest.fixture
def test_element(length=0.0, uc=UcPoly([1, 0])):

    mock_cs = mock.MagicMock()
    mock_cs.get.return_value = 40.0

    element = pml.element.Element(1, 'Quad', Physics(6))
    rb_pv = 'SR22C-DI-EBPM-04:SA:X'
    sp_pv = 'SR22C-DI-EBPM-04:SA:Y'
    device1 = pml.device.Device(mock_cs, rb_pv, sp_pv)
    device2 = pml.device.Device(mock_cs, sp_pv, rb_pv)

    element.add_device('x', device1, uc)
    element.add_device('y', device2, uc)

    return element


def test_create_element():
    physics = Physics(length=6.0)
    e = pml.element.Element('bpm1', 6.0, 'bpm', physics)
    e.add_to_family('BPM')
    assert 'BPM' in e.families
    assert e.get_length() == 6.0


def test_add_element_to_family():
    physics = Physics(length=6.0)
    e = pml.element.Element('dummy', 'Quad', physics)
    e.add_to_family('fam')
    assert 'fam' in e.families


@pytest.mark.parametrize('pv_type', ['readback', 'setpoint'])
def test_readback_pvs(pv_type, test_element):
    # Tests to get/set pv names and/or values
    # The default unit conversion is identity
    assert test_element.get_pv_value('x', pv_type, unit='physics') == 40.0
    assert test_element.get_pv_value('x', pv_type, unit='hardware') == 40.0
    assert test_element.get_pv_value('y', pv_type, unit='physics') == 40.0
    assert test_element.get_pv_value('y', pv_type, unit='hardware') == 40.0
    assert isinstance(test_element.get_pv_name('x'), list)
    assert isinstance(test_element.get_pv_name('y'), list)
    assert isinstance(test_element.get_pv_name('x', pv_type), str)
    assert isinstance(test_element.get_pv_name('y', pv_type), str)


def test_get_pv_exceptions(test_element):
    with pytest.raises(PvException):
        test_element.get_pv_value('setpoint', 'unknown_field')
    with pytest.raises(PvException):
        test_element.get_pv_value('unknown_handle', 'y')
    with pytest.raises(PvException):
        test_element.get_pv_name('unknown_handle')


def test_identity_conversion():
    uc_id = UcPoly([1, 0])
    element = test_element(uc=uc_id)
    value_physics = element.get_pv_value('x', 'setpoint', 'physics')
    value_machine = element.get_pv_value('x', 'setpoint', 'machine')
    assert value_machine == 40.0
    assert value_physics == 40.0


def test_get_fields(test_element):
    assert set(test_element.get_fields()) == set(['y', 'x'])


def test_is_enabled(test_element):
    assert test_element.is_enabled() == True


def test_set_enabled(test_element):
    test_element.set_enabled(False)
    assert test_element.is_enabled() == False

@@ -1104,20 +1104,20 @@
 est_
-readback_pvs
+get_pv_value
 (pv_
@@ -1522,32 +1522,140 @@
 rdware') == 40.0
+%0A%0A@pytest.mark.parametrize('pv_type', %5B'readback', 'setpoint'%5D)%0Adef test_get_pv_name(pv_type, test_element):
 %0A    assert isin
@@ -1824,16 +1824,16 @@
 ), str)%0A
-
     asse
@@ -1889,24 +1889,100 @@
 pe), str)%0A%0A%0A
+#def test_put_pv_value(test_element):%0A#    test_element.put_pv_value('x')%0A%0A%0A
 def test_get

Change method in separate test cases