source: sasview/test/projectsaveandload/test/utest_project.py @ 08f921e

unittest-saveload
Last change on this file since 08f921e was 08f921e, checked in by krzywon, 6 years ago

Thread sasview wx gui so it can be called and run in parallel with tests.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1"""
2    Unit tests for saving and loading .svs files
3"""
4
5import os
6import sys
7import unittest
8import warnings
9import threading
10
11from sas.sascalc.dataloader.loader import Loader
12from sas.sascalc.fit.pagestate import Reader as fit_state_reader
13from sas.sasgui.perspectives.invariant.invariant_state import Reader as invariant_reader
14from sas.sasgui.perspectives.pr.inversion_state import Reader as pr_reader
15from sas.sasgui.perspectives.corfunc.corfunc_state import Reader as corfunc_reader
16
17warnings.simplefilter("ignore")
18
19TEMP_FOLDER = "temp_folder"
20STATE_LOADERS = [fit_state_reader, invariant_reader, pr_reader, corfunc_reader]
21
22
23class projects(unittest.TestCase):
24
25    def setUp(self):
26        """
27        Set up the base unit test class and variables used throughout the tests
28        """
29        self.addCleanup(self.remove_dir)
30        self.loader = Loader()
31        self.data1d = self.loader.load("test_data/data1D.h5")
32        self.data2d = self.loader.load("test_data/data2D.h5")
33        self.sasviewThread = sasviewThread()
34        self.sasviewThread.start_local()
35        if not (os.path.isdir(TEMP_FOLDER)):
36            os.makedirs(TEMP_FOLDER)
37
38    def tearDown(self):
39        self.remove_dir()
40        if hasattr(self.sasviewThread, "isAlive"):
41            if self.sasviewThread.isAlive():
42                print("TODO: Close app directly")
43                self.app.gui.Close()
44                pass
45
46    def remove_dir(self):
47        if(os.path.isdir(TEMP_FOLDER)):
48            os.removedirs(TEMP_FOLDER)
49
50    def test_saveload_data1d_fitting_only(self):
51        """
52        Test saving and loading a project with a single data set sent to fitting
53        """
54        self.sasviewThread.join(5)
55        self.assertTrue(1 == 1)
56
57        # TODO: Send 1D to fitting, select model, save project, load project
58        # TODO: Send both to fitting, select model on both, save/load
59        # TODO: Send both to fitting, select model on one, save/load
60        # TODO: Send 1D to each other persepective, save/load
61        # TODO: Send 1D to every perspective, save/load
62        # TODO: Save/load simultaneous/constrained fit project
63
64
65class sasviewThread(threading.Thread):
66    """Run the MainLoop as a thread. Access the frame with self.frame."""
67
68    def __init__(self, autoStart=True):
69        threading.Thread.__init__(self)
70        self.setDaemon(1)
71        self.start_orig = self.start
72        self.start = self.start_local
73        self.frame = None  # to be defined in self.run
74        self.lock = threading.Lock()
75        self.lock.acquire()  # lock until variables are set
76        if autoStart:
77            self.start()  # automatically start thread on init
78
79    def run(self):
80        import sas.sasview.sasview as sasview
81        app = sasview.run_gui()
82        self.frame = app.frame
83
84        # define frame and release lock
85        # The lock is used to make sure that SetData is defined.
86        self.lock.release()
87
88        app.MainLoop()
89
90    def start_local(self):
91        self.start_orig()
92        # After thread has started, wait until the lock is released
93        # before returning so that functions get defined.
94        self.lock.acquire()
Note: See TracBrowser for help on using the repository browser.