Exemple #1
0
 def simulateMoveTo(self, pos):
     lines = ['%s:' % self.name]
     hkl = self.parameter_to_hkl(pos)
     if isinstance(hkl[0], (tuple, list)):
         (angles, _) = self._diffcalc.hkl_list_to_angles(hkl)
         hkl, _ = self._diffcalc.angles_to_hkl(angles)
     #fmt_scn_params = '  '.join([' : %9.4f'] + ['%.4f'] * (len(pos) - 1))
     #lines.append('  ' + self.name + fmt_scn_params % tuple(pos))
     width = max(len(k) for k in self.inputNames)
     fmt = '  %' + str(width) + 's : % 9.4f'
     for idx, k in enumerate(self.inputNames):
         lines.append(fmt % (k, pos[idx]))
     lines.append('\n  ' + 'hkl'.rjust(width) + ' : %9.4f  %.4f  %.4f' %
                  (hkl[0], hkl[1], hkl[2]))
     res = Hkl.simulateMoveTo(self, hkl)
     lines.append(res)
     return '\n'.join(lines)
class TestHkl(unittest.TestCase):

    def setUp(self):
        self.mockDiffcalc = mock.Mock(spec=diffcalc_.Diffcalc)
        self.mockSixc = mock.Mock(spec=DiffractometerScannableGroup)
        self.hkl = Hkl('hkl', self.mockSixc, self.mockDiffcalc)

    def testInit(self):
        self.mockDiffcalc.angles_to_hkl.return_value = ([1, 2, 3], PARAM_DICT)
        self.assertEqual(self.hkl.getPosition(), [1, 2, 3])

    def testAsynchronousMoveTo(self):
        self.mockDiffcalc.hkl_to_angles.return_value = ([6, 5, 4, 3, 2, 1],
                                                       None)
        self.hkl.asynchronousMoveTo([1, 0, 1])
        self.mockDiffcalc.hkl_to_angles.assert_called_with(1, 0, 1)
        self.mockSixc.asynchronousMoveTo.assert_called_with([6, 5, 4, 3, 2, 1])

    def testGetPosition(self):
        self.mockSixc.getPosition.return_value = [6, 5, 4, 3, 2, 1]
        self.mockDiffcalc.angles_to_hkl.return_value = ([1, 0, 1], PARAM_DICT)
        self.assertEqual(self.hkl.getPosition(), [1, 0, 1])
        self.mockDiffcalc.angles_to_hkl.assert_called_with([6, 5, 4, 3, 2, 1])

    def testAsynchronousMoveToWithNonesOutsideScan(self):
        self.mockSixc.getPosition.return_value = [6, 5, 4, 3, 2, 1]
        self.mockDiffcalc.angles_to_hkl.return_value = ([1, 0, 1],
                                                       PARAM_DICT)
        self.mockDiffcalc.hkl_to_angles.return_value = ([12, 5, 4, 3, 2, 1],
                                                       PARAM_DICT)  # <- 2,0,1

        self.hkl.asynchronousMoveTo([2, 0, None])

        self.mockDiffcalc.angles_to_hkl.assert_called_with([6, 5, 4, 3, 2, 1])
        self.mockDiffcalc.hkl_to_angles.assert_called_with(2, 0, 1)
        self.mockSixc.asynchronousMoveTo.assert_called_with(
            [12, 5, 4, 3, 2, 1])

    def testAsynchronousMoveToWithNonesInScan(self):

        self.mockSixc.getPosition.return_value = [6, 5, 4, 3, 2, 1]
        self.mockDiffcalc.angles_to_hkl.return_value = ([1, 0, 1], PARAM_DICT)
        self.hkl.atScanStart()
        # should not be used:
        self.mockSixc.getPosition.return_value = [6.1, 5.1, 4.1, 3.1, 2.1, 1.1]
        self.mockDiffcalc.hkl_to_angles.return_value = ([12, 5, 4, 3, 2, 1],
                                                       PARAM_DICT)
        self.hkl.asynchronousMoveTo([2, 0, None])
        # atScanStart:
        self.mockDiffcalc.angles_to_hkl.assert_called_with([6, 5, 4, 3, 2, 1])
        self.mockDiffcalc.hkl_to_angles.assert_called_with(2, 0, 1)
        self.mockSixc.asynchronousMoveTo.assert_called_with(
            [12, 5, 4, 3, 2, 1])

    def testAsynchronousMoveToWithNonesInScanAfterCommandFailure(self):
        # should be forgotten:
        self.mockSixc.getPosition.return_value = [6, 5, 4, 3, 2, 1]
        self.mockDiffcalc.angles_to_hkl.return_value = ([1, 0, 1], PARAM_DICT)
        self.hkl.atScanStart()
        self.hkl.atCommandFailure()

        self.mockSixc.getPosition.return_value = [6.1, 5.1, 4.1, 3.1, 2.1, 1.1]
        self.mockDiffcalc.hkl_to_angles.return_value = (
                [12.1, 5.1, 4.1, 3.1, 2.1, 1.1], PARAM_DICT)
        self.hkl.asynchronousMoveTo([2, 0, None])

        self.mockDiffcalc.angles_to_hkl.assert_called_with(
            [6.1, 5.1, 4.1, 3.1, 2.1, 1.1])
        self.mockDiffcalc.hkl_to_angles.assert_called_with(2, 0, 1)
        self.mockSixc.asynchronousMoveTo.assert_called_with(
            [12.1, 5.1, 4.1, 3.1, 2.1, 1.1])

    def testAsynchronousMoveToWithNonesInScanAfterAtScanEnd(self):
        self.mockSixc.getPosition.return_value = [6, 5, 4, 3, 2, 1]
        self.mockDiffcalc.angles_to_hkl.return_value = ([1, 0, 1], PARAM_DICT)
        self.hkl.atScanStart()
        self.hkl.atScanEnd()

        self.mockSixc.getPosition.return_value = [6.1, 5.1, 4.1, 3.1, 2.1, 1.1]
        self.mockDiffcalc.hkl_to_angles.return_value = (
            [12.1, 5.1, 4.1, 3.1, 2.1, 1.1], PARAM_DICT)
        self.hkl.asynchronousMoveTo([2, 0, None])

        self.mockDiffcalc.angles_to_hkl.assert_called_with(
            [6.1, 5.1, 4.1, 3.1, 2.1, 1.1])
        self.mockDiffcalc.hkl_to_angles.assert_called_with(2, 0, 1)
        self.mockSixc.asynchronousMoveTo.assert_called_with(
            [12.1, 5.1, 4.1, 3.1, 2.1, 1.1])

    def testIsBusy(self):
        self.mockSixc.isBusy.return_value = False
        self.assertFalse(self.hkl.isBusy(), False)
        self.mockSixc.isBusy.assert_called()

    def testWaitWhileBusy(self):
        self.hkl.waitWhileBusy()
        self.mockSixc.waitWhileBusy.assert_called()

    def testWhereMoveTo(self):
        # just check for exceptions
        self.mockDiffcalc.hkl_to_angles.return_value = ([6, 5, 4, 3, 2, 1],
                                                       PARAM_DICT)
        self.mockSixc.getName.return_value = 'sixc'
        self.mockSixc.getInputNames.return_value = ['alpha', 'delta', 'gamma',
                                                    'omega', 'chi', 'phi']
        print self.hkl.simulateMoveTo((1.23, 0, 0))

    def testDisp(self):
        print self.hkl.__repr__()
class TestHkl(object):

    def setup_method(self):
        self.mock_dc_module = mock.Mock()
        self.mockSixc = mock.Mock(spec=DiffractometerScannableGroup)
        self.hkl = Hkl('hkl', self.mockSixc, self.mock_dc_module)

    def testInit(self):
        self.mock_dc_module.angles_to_hkl.return_value = ([1, 2, 3], PARAM_DICT)
        assert self.hkl.getPosition() == [1, 2, 3]

    def testAsynchronousMoveTo(self):
        self.mock_dc_module.hkl_to_angles.return_value = ([6, 5, 4, 3, 2, 1],
                                                       None)
        self.hkl.asynchronousMoveTo([1, 0, 1])
        self.mock_dc_module.hkl_to_angles.assert_called_with(1, 0, 1)
        self.mockSixc.asynchronousMoveTo.assert_called_with([6, 5, 4, 3, 2, 1])

    def testGetPosition(self):
        self.mockSixc.getPosition.return_value = [6, 5, 4, 3, 2, 1]
        self.mock_dc_module.angles_to_hkl.return_value = ([1, 0, 1], PARAM_DICT)
        assert self.hkl.getPosition() == [1, 0, 1]
        self.mock_dc_module.angles_to_hkl.assert_called_with([6, 5, 4, 3, 2, 1])

    def testAsynchronousMoveToWithNonesOutsideScan(self):
        self.mockSixc.getPosition.return_value = [6, 5, 4, 3, 2, 1]
        self.mock_dc_module.angles_to_hkl.return_value = ([1, 0, 1],
                                                       PARAM_DICT)
        self.mock_dc_module.hkl_to_angles.return_value = ([12, 5, 4, 3, 2, 1],
                                                       PARAM_DICT)  # <- 2,0,1

        self.hkl.asynchronousMoveTo([2, 0, None])

        self.mock_dc_module.angles_to_hkl.assert_called_with([6, 5, 4, 3, 2, 1])
        self.mock_dc_module.hkl_to_angles.assert_called_with(2, 0, 1)
        self.mockSixc.asynchronousMoveTo.assert_called_with(
            [12, 5, 4, 3, 2, 1])

    def testAsynchronousMoveToWithNonesInScan(self):

        self.mockSixc.getPosition.return_value = [6, 5, 4, 3, 2, 1]
        self.mock_dc_module.angles_to_hkl.return_value = ([1, 0, 1], PARAM_DICT)
        self.hkl.atScanStart()
        # should not be used:
        self.mockSixc.getPosition.return_value = [6.1, 5.1, 4.1, 3.1, 2.1, 1.1]
        self.mock_dc_module.hkl_to_angles.return_value = ([12, 5, 4, 3, 2, 1],
                                                       PARAM_DICT)
        self.hkl.asynchronousMoveTo([2, 0, None])
        # atScanStart:
        self.mock_dc_module.angles_to_hkl.assert_called_with([6, 5, 4, 3, 2, 1])
        self.mock_dc_module.hkl_to_angles.assert_called_with(2, 0, 1)
        self.mockSixc.asynchronousMoveTo.assert_called_with(
            [12, 5, 4, 3, 2, 1])

    def testAsynchronousMoveToWithNonesInScanAfterCommandFailure(self):
        # should be forgotten:
        self.mockSixc.getPosition.return_value = [6, 5, 4, 3, 2, 1]
        self.mock_dc_module.angles_to_hkl.return_value = ([1, 0, 1], PARAM_DICT)
        self.hkl.atScanStart()
        self.hkl.atCommandFailure()

        self.mockSixc.getPosition.return_value = [6.1, 5.1, 4.1, 3.1, 2.1, 1.1]
        self.mock_dc_module.hkl_to_angles.return_value = (
                [12.1, 5.1, 4.1, 3.1, 2.1, 1.1], PARAM_DICT)
        self.hkl.asynchronousMoveTo([2, 0, None])

        self.mock_dc_module.angles_to_hkl.assert_called_with(
            [6.1, 5.1, 4.1, 3.1, 2.1, 1.1])
        self.mock_dc_module.hkl_to_angles.assert_called_with(2, 0, 1)
        self.mockSixc.asynchronousMoveTo.assert_called_with(
            [12.1, 5.1, 4.1, 3.1, 2.1, 1.1])

    def testAsynchronousMoveToWithNonesInScanAfterAtScanEnd(self):
        self.mockSixc.getPosition.return_value = [6, 5, 4, 3, 2, 1]
        self.mock_dc_module.angles_to_hkl.return_value = ([1, 0, 1], PARAM_DICT)
        self.hkl.atScanStart()
        self.hkl.atScanEnd()

        self.mockSixc.getPosition.return_value = [6.1, 5.1, 4.1, 3.1, 2.1, 1.1]
        self.mock_dc_module.hkl_to_angles.return_value = (
            [12.1, 5.1, 4.1, 3.1, 2.1, 1.1], PARAM_DICT)
        self.hkl.asynchronousMoveTo([2, 0, None])

        self.mock_dc_module.angles_to_hkl.assert_called_with(
            [6.1, 5.1, 4.1, 3.1, 2.1, 1.1])
        self.mock_dc_module.hkl_to_angles.assert_called_with(2, 0, 1)
        self.mockSixc.asynchronousMoveTo.assert_called_with(
            [12.1, 5.1, 4.1, 3.1, 2.1, 1.1])

    def testIsBusy(self):
        self.mockSixc.isBusy.return_value = False
        assert not self.hkl.isBusy()
        self.mockSixc.isBusy.assert_called()

    def testWaitWhileBusy(self):
        self.hkl.waitWhileBusy()
        self.mockSixc.waitWhileBusy.assert_called()

    def testWhereMoveTo(self):
        # just check for exceptions
        self.mock_dc_module.hkl_to_angles.return_value = ([6, 5, 4, 3, 2, 1],
                                                       PARAM_DICT)
        self.mockSixc.getName.return_value = 'sixc'
        self.mockSixc.getInputNames.return_value = ['alpha', 'delta', 'gamma',
                                                    'omega', 'chi', 'phi']
        print self.hkl.simulateMoveTo((1.23, 0, 0))

    def testDisp(self):
        print self.hkl.__repr__()