source: sasview/test/calculatorview/test/utest_gui_sld.py @ 88d2e70

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1249
Last change on this file since 88d2e70 was 88d2e70, checked in by Paul Kienzle <pkienzle@…>, 22 months ago

support for py37 in unit tests

  • Property mode set to 100644
File size: 9.0 KB
Line 
1"""
2test gui for sld calculator
3"""
4
5import unittest
6import wx
7
8H2O_DENSITY = 1.0
9WAVELENGTH = 6.0
10
11class testTextCtrl(unittest.TestCase):
12    """
13    txtctrl should have a pink background when the result are incorrect
14    and white when reset or correct value
15    """
16    def setUp(self):
17        """
18        Create an Sld calculator
19        """
20        self.app = wx.App()
21        from sas.sasgui.perspectives.calculator.sld_panel import SldWindow
22        self.sld_frame = SldWindow()
23       
24    def testCompoundTextCtrl(self):
25        """
26        Test Compund textCtrl
27        """
28        #add  invalid value for compound
29        self.sld_frame.panel.compound_ctl.SetValue("String not in table")
30        self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY))
31        id = self.sld_frame.panel.button_calculate.GetId()
32        clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id)
33        self.sld_frame.panel.ProcessEvent(clickEvent)
34        bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour()
35        self.assertTrue(bkg.GetAsString() == 'pink')
36        #compute invariant without entering a value for compound
37        self.sld_frame.panel.compound_ctl.SetValue("")
38        self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY))
39        self.sld_frame.panel.ProcessEvent(clickEvent)
40        bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour()
41        self.assertTrue(bkg.GetAsString() == 'pink')
42        #compute invariant without entering a value for compound
43        self.sld_frame.panel.compound_ctl.SetValue("H2O")
44        self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY))
45        self.sld_frame.panel.ProcessEvent(clickEvent)
46        bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour()
47        self.assertTrue(bkg.GetAsString() == 'white')
48       
49    def testDensityTextCtrl(self):
50        """
51        Test Density textCtrl
52       
53        """
54        #add  invalid value for density
55        self.sld_frame.panel.compound_ctl.SetValue("H2O")
56        self.sld_frame.panel.density_ctl.SetValue("invalid density")
57        id = self.sld_frame.panel.button_calculate.GetId()
58        clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id)
59        self.sld_frame.panel.ProcessEvent(clickEvent)
60        bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour()
61        self.assertTrue(bkg.GetAsString() == 'pink')
62        #compute invariant without entering a value for density
63        self.sld_frame.panel.compound_ctl.SetValue("H2O")
64        self.sld_frame.panel.density_ctl.SetValue("")
65        self.sld_frame.panel.ProcessEvent(clickEvent)
66        bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour()
67        self.assertTrue(bkg.GetAsString() == 'pink')
68        #compute invariant without entering a value for density
69        self.sld_frame.panel.compound_ctl.SetValue("H2O")
70        self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY))
71        self.sld_frame.panel.ProcessEvent(clickEvent)
72        bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour()
73        self.assertTrue(bkg.GetAsString() == 'white')
74       
75    def testWavelengthTextCtrl(self):
76        """
77        Test wavelength textCtrl
78       
79        """
80        #add  invalid value for wavelength
81        self.sld_frame.panel.compound_ctl.SetValue("H2O")
82        self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY))
83        self.sld_frame.panel.wavelength_ctl.SetValue("invalid wavelength")
84        id = self.sld_frame.panel.button_calculate.GetId()
85        clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id)
86        self.sld_frame.panel.ProcessEvent(clickEvent)
87        bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour()
88        self.assertTrue(bkg.GetAsString() == 'pink')
89        #compute invariant without entering a value for wavelegnth
90        self.sld_frame.panel.compound_ctl.SetValue("H2O")
91        self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY))
92        self.sld_frame.panel.wavelength_ctl.SetValue("")
93        self.sld_frame.panel.ProcessEvent(clickEvent)
94        cp_bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour()
95        self.assertTrue(cp_bkg.GetAsString() == 'white')
96        ds_bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour()
97        self.assertTrue(ds_bkg.GetAsString() == 'white')
98        wv_bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour()
99        value = self.sld_frame.panel.wavelength_ctl.GetValue()
100        self.assertTrue(wv_bkg.GetAsString() == 'white')
101        self.assertTrue(float(value) == WAVELENGTH)
102        sld_real = self.sld_frame.panel.neutron_sld_real_ctl.GetValue()
103        sld_im = self.sld_frame.panel.neutron_sld_im_ctl.GetValue()
104        mo_real = self.sld_frame.panel.mo_ka_sld_real_ctl.GetValue()
105        mo_im = self.sld_frame.panel.mo_ka_sld_im_ctl.GetValue()
106        cu_real = self.sld_frame.panel.cu_ka_sld_real_ctl.GetValue()
107        cu_im = self.sld_frame.panel.cu_ka_sld_im_ctl.GetValue()
108        abs = self.sld_frame.panel.neutron_abs_ctl.GetValue()
109        incoh = self.sld_frame.panel.neutron_inc_ctl.GetValue()
110        length = self.sld_frame.panel.neutron_length_ctl.GetValue()
111       
112        self.assertAlmostEqual(float(sld_real), 1.04e-6, 1)
113        self.assertAlmostEqual(float(sld_im), -1.5e-7, 1)
114        #test absorption value
115        self.assertAlmostEqual(float(abs) , 0.0741, 2)
116        self.assertAlmostEqual(float(incoh), 5.62, 2)
117        #Test length
118        self.assertAlmostEqual(float(length), 0.1755, 2)
119        #test Cu sld
120        self.assertAlmostEqual(float(cu_real), 9.46e-6, 1)
121        self.assertAlmostEqual(float(cu_im), 3.01e-8)
122        # test Mo sld
123        self.assertAlmostEqual(float(mo_real), 9.43e-6)
124        self.assertAlmostEqual(float(mo_im), 5.65e-7, 1)
125        #compute invariant with all correct inputs value
126        self.sld_frame.panel.compound_ctl.SetValue("H2O")
127        self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY))
128        self.sld_frame.panel.wavelength_ctl.SetValue(str(WAVELENGTH/2))
129        self.sld_frame.panel.ProcessEvent(clickEvent)
130        bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour()
131        value = self.sld_frame.panel.wavelength_ctl.GetValue()
132        self.assertTrue(bkg.GetAsString() == 'white')
133        self.assertTrue(float(value) == WAVELENGTH/2)
134       
135    def testSomeCombination(self):
136        """
137        Test other error
138        """
139        #only wavelength is invalid
140        self.sld_frame.panel.compound_ctl.SetValue("H2O")
141        self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY))
142        self.sld_frame.panel.wavelength_ctl.SetValue("invalid wavelength")
143        id = self.sld_frame.panel.button_calculate.GetId()
144        clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id)
145        self.sld_frame.panel.ProcessEvent(clickEvent)
146        cp_bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour()
147        self.assertTrue(cp_bkg.GetAsString() == 'white')
148        ds_bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour()
149        self.assertTrue(ds_bkg.GetAsString() == 'white')
150        wv_bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour()
151        self.assertTrue(wv_bkg.GetAsString() == 'pink')
152        #density, wavelength is invalid
153        self.sld_frame.panel.compound_ctl.SetValue("H2O")
154        self.sld_frame.panel.density_ctl.SetValue("invalid density")
155        self.sld_frame.panel.wavelength_ctl.SetValue("invalid wavelength")
156        id = self.sld_frame.panel.button_calculate.GetId()
157        clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id)
158        self.sld_frame.panel.ProcessEvent(clickEvent)
159        cp_bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour()
160        self.assertTrue(cp_bkg.GetAsString() == 'white')
161        ds_bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour()
162        self.assertTrue(ds_bkg.GetAsString() == 'pink')
163        wv_bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour()
164        self.assertTrue(wv_bkg.GetAsString() == 'pink')
165        #density, wavelength is invalid
166        self.sld_frame.panel.compound_ctl.SetValue("invalid compound")
167        self.sld_frame.panel.density_ctl.SetValue("invalid density")
168        self.sld_frame.panel.wavelength_ctl.SetValue("")
169        id = self.sld_frame.panel.button_calculate.GetId()
170        clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id)
171        self.sld_frame.panel.ProcessEvent(clickEvent)
172        cp_bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour()
173        self.assertTrue(cp_bkg.GetAsString() == 'pink')
174        ds_bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour()
175        self.assertTrue(ds_bkg.GetAsString() == 'pink')
176        wv_bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour()
177        self.assertTrue(wv_bkg.GetAsString() == 'white')
178        value = self.sld_frame.panel.wavelength_ctl.GetValue()
179        self.assertTrue(float(value) == WAVELENGTH)
180
181       
182       
183    def tearDown(self):
184        """
185        Destroy the sld calculator frame
186        """
187        self.sld_frame.Close()
188        self.app.MainLoop()
189       
190if __name__ == "__main__":
191    unittest.main()
Note: See TracBrowser for help on using the repository browser.