From 227e5273f55a26a83a38f895cb36007a2bcfe823 Mon Sep 17 00:00:00 2001
From: Duc Le <duc.le@stfc.ac.uk>
Date: Thu, 1 Sep 2016 17:20:20 +0100
Subject: [PATCH] Re #10147 - added unit test.

---
 scripts/CMakeLists.txt     |  1 +
 scripts/test/PyChopTest.py | 54 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
 create mode 100644 scripts/test/PyChopTest.py

diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt
index 0650d0cf18d..02f817baed1 100644
--- a/scripts/CMakeLists.txt
+++ b/scripts/CMakeLists.txt
@@ -34,6 +34,7 @@ set ( TEST_PY_FILES
       test/DirectEnergyConversionTest.py
       test/ISISDirecInelasticConfigTest.py
       test/IndirectCommonTests.py
+      test/PyChopTest.py
       test/ReductionWrapperTest.py
       test/ReflectometryQuickAuxiliaryTest.py
       test/RunDescriptorTest.py
diff --git a/scripts/test/PyChopTest.py b/scripts/test/PyChopTest.py
new file mode 100644
index 00000000000..a845f9f596e
--- /dev/null
+++ b/scripts/test/PyChopTest.py
@@ -0,0 +1,54 @@
+"""Test suite for the PyChop package
+"""
+import unittest
+import numpy as np
+
+# Import mantid to setup the python paths to the bundled scripts
+import mantid
+from PyChop import PyChop2
+
+class PyChop2Tests(unittest.TestCase):
+
+    def test_pychop2(self):
+        instnames = ['let', 'maps', 'mari', 'merlin']
+        modulenames = ['ISISDisk', 'ISISFermi', 'ISISFermi', 'ISISFermi']
+        res = []
+        flux = []
+        for inc, instname in enumerate(instnames):
+            chopobj = PyChop2(instname)
+            # Checks that initialisations instanciates the correct submodule
+            # which does the actual calculations. PyChop2 is just a wrapper.
+            self.assertIn(modulenames[inc], chopobj.getObject().__module__)
+            # Code should give an error if the chopper settings and Ei have
+            # not been set.
+            with self.assertRaises(ValueError):
+                chopobj.getResolution()
+            if 'Fermi' in modulenames[inc]:
+                chopobj.setChopper('s', 200)
+            else:
+                chopobj.setFrequency(200)
+            chopobj.setEi(18)
+            rr, ff = chopobj.getResFlux(np.linspace(0,17,10))
+            res.append(rr)
+            flux.append(ff)
+        # Checks that the flux should be highest for LET, then MERLIN, MARI
+        # and MAPS in that order
+        self.assertGreater(flux[0], flux[3])
+        self.assertGreater(flux[3], flux[2])
+        self.assertGreater(flux[2], flux[1])
+        # Checks that the resolution should be best for LET, then MARI, MAPS
+        # and MERLIN in that order
+        self.assertLess(res[0][0], res[2][0])
+        self.assertLess(res[2][0], res[1][0])
+        self.assertLess(res[1][0], res[3][0]) 
+        # Now tests the standalone function
+        for inc, instname in enumerate(instnames):
+            if 'Fermi' in modulenames[inc]:
+                rr, ff = PyChop2.calculate(instname, 's', 200, 18, 0)
+            else:
+                rr, ff = PyChop2.calculate(instname, 200, 18, 0)
+            self.assertAlmostEqual(rr[0], res[inc][0], places=7)
+            self.assertAlmostEqual(ff, flux[inc], places=7)
+
+if __name__ == "__main__":
+    unittest.main()
-- 
GitLab