import epics

from helpers.mgnt_utilities import almost_equal
from mgnt_auto_checkout_test import AutoCheckoutTest


class ZPositionTest(AutoCheckoutTest):
    test_id = 'z_position'
    description = 'Z position'
    read_only = True
    deferred = False

    def __init__(self):
        super(ZPositionTest, self).__init__(
            pre_checkout_routines=[]
        )

    def execute(self, magnet):
        z_pos_pv = magnet['device'] + ':Z'
        z_pos = epics.caget(z_pos_pv)
        if z_pos is None:
            self.data = '{} not found'.format(z_pos_pv)
            self.status = False
            print 'FAIL'

        elif not almost_equal(z_pos, magnet['z_pos']):
            self.data = '{} is {} instead of {}'.format(z_pos_pv, z_pos, magnet['z_pos'])
            self.status = False
            print 'FAIL'

        else:
            self.data = str(z_pos)
            self.status = True
            print 'PASS'
