def setup(self):
     from  job_presenter  import JobPresenter
     from  job_model      import JobModel
     from  job_view       import JobView, NotFoundRequestError
     from  jobmock        import JobMock
     self.model  = JobMock(1)
     self.view   = JobView()
     self.presen = JobPresenter(self.model, self.view)
    def setup(self):
        self.app = wx.App()

        # self.job   = JobMock()
        self.view = JobView()

        # set properties
        self.init_prop_dict = dict(
            id            = 5           , 
            expected_time = '3:00'      , 
            start_time    = '15:00'     , 
            elasped_time  = '1:00'      , 
            finish_time   = ''          , 
            location      = 'hpcs'      , 
            jms           = 'LSF'       , 
            name          = 'test job'  , 
            project       = 'project X' , 
        )

        for prop, val in self.init_prop_dict.items():
            set_prop = 'set_'+prop
            get_prop = 'get_'+prop
            getattr(self.view, set_prop)( val )

        # set state
        self.view.set_state( 'Runnable' )

        # set popup menu
        self.init_request_dict = dict(
            request_submit  = False , 
            request_convert = False , 
            request_send    = False , 
            request_run     = True  , 
            request_stop    = True  , 
            request_cancel  = True  , 
            request_rerun   = False , 
            request_receive = False , 
            request_sync    = False , 
        )
        self.view.set_request_dict( self.init_request_dict )
class TestJobPresenter:

    def setup(self):
        from  job_presenter  import JobPresenter
        from  job_model      import JobModel
        from  job_view       import JobView, NotFoundRequestError
        from  jobmock        import JobMock
        self.model  = JobMock(1)
        self.view   = JobView()
        self.presen = JobPresenter(self.model, self.view)

    def teardown(self):
        self.model = None
        self.view = None
        self.present = None

    def test_init(self):
        # ec = EventCatcher(self.view.delete_event)
        ec = EventCatcher(self.model.state_changed_event)
        ec.trigger('fuga')
        assert_catch_event(ec,'fuga')
        assert_method(self.view, 'get_name', ret='task name' )
        #assert hasattr(self.presen, 'change_state')
        #assert ec.is_event()

    def test_change_state(self):
        mstate = self.model.get_state()

        # event catch and do method
        ec = EventCatcher(self.model.state_changed_event)
        ec.trigger()
        assert_catch_event(ec)

        state, image = self.view.get_state()
        assert_equal(state, mstate)

    def test_update_reqmenu(self):
        request_dict = {}
        ava_req_list = self.model.get_available_request()
        all_req_list = self.model.get_all_request()
        for req in all_req_list:
            request_dict[req] = True if req in ava_req_list else False

        self.presen.update_reqmenu()

        for req, enable in request_dict.items():
            reqname = req.split('_')[-1]
            reqid = 'ID_' + reqname.upper()
            if reqid=='ID_DELETE': continue
            ret_enable = self.view.is_enabled_menuitem(
                getattr(self.view, reqid))
            assert_equal(ret_enable, enable)

    def test_request(self):
        request_list = [
            'request_submit',
            'request_convert',
            'request_send',
            'request_run',
            'request_stop',
            'request_cancel',
            'request_rerun',
            'request_receive',
            'request_sync',
        ]
        for reqname in request_list:
            assert_method(self.presen, reqname)
            assert_method(self.model, reqname)

    def test_update_view(self):
        ec = EventCatcher(self.model.update_event)

        prop_val_dict = dict(
            expected_time = '2:00'       , 
            start_time    = '18:00'      , 
            finish_time   = '20:00'      , 
            elasped_time  = '1:00'       , 
            jms           = 'NQS'        , 
            location      = 'vlsn'       , 
            name          = 'Job name b' , 
        )

        for prop in prop_val_dict.keys():
            ec.trigger(prop)
            assert_catch_event(ec, prop)

            exp_val = getattr(self.model, prop)
            ret_val = getattr(self.view,  prop)

            assert_equal(ret_val, exp_val)

    def test_enable_auto(self):
        enable = True
        self.presen.enable_auto(enable)
        assert_equal(self.model.is_auto(), enable)

        enable = False
        self.presen.enable_auto(enable)
        assert_equal(self.model.is_auto(), enable)
class TestJobViewWithState:
    
    def setup(self):
        self.app = wx.App()

        # self.job   = JobMock()
        self.view = JobView()

        # set properties
        self.init_prop_dict = dict(
            id            = 5           , 
            expected_time = '3:00'      , 
            start_time    = '15:00'     , 
            elasped_time  = '1:00'      , 
            finish_time   = ''          , 
            location      = 'hpcs'      , 
            jms           = 'LSF'       , 
            name          = 'test job'  , 
            project       = 'project X' , 
        )

        for prop, val in self.init_prop_dict.items():
            set_prop = 'set_'+prop
            get_prop = 'get_'+prop
            getattr(self.view, set_prop)( val )

        # set state
        self.view.set_state( 'Runnable' )

        # set popup menu
        self.init_request_dict = dict(
            request_submit  = False , 
            request_convert = False , 
            request_send    = False , 
            request_run     = True  , 
            request_stop    = True  , 
            request_cancel  = True  , 
            request_rerun   = False , 
            request_receive = False , 
            request_sync    = False , 
        )
        self.view.set_request_dict( self.init_request_dict )

    def teardown(self):
        wx.CallAfter(self.app.Exit)
        self.app.MainLoop()
        self.view   = None

    def test_init(self):

        # test properties
        for prop, val in self.init_prop_dict.items():
            exp_val = val
            ret_val = getattr(self.view, prop)
            assert_equal( ret_val, exp_val )

        # test init state
        state, image = self.view.get_state()
        assert_equal(state, 'Runnable')

        # test init request

    def test_request_menu(self):
        menu = self.view.get_menu()
        for menuitem in menu.GetMenuItems():
            itemname = menuitem.GetLabel()
            enable   = menuitem.IsEnabled()
            reqname  = 'request_' + itemname
            ret_enable = self.init_request_dict[reqname]

            assert reqname in self.init_request_dict.keys(), reqname
            assert_equal(ret_enable, enable)

    def test_state_and_image(self):
        import degras.image as im

        for state in im.JOBSTATE_PATH_DICT.keys():
            image = im.get_jobstate_image(state)
            # set state
            self.view.set_state( state )
            ret_state, ret_image = self.view.get_state()
            assert_equal(ret_state, state)
            assert_equal(ret_image, image)

    def test_update_properties(self):
        prop_dict = dict(
            id            = 5           , 
            expected_time = '5:00'      , 
            start_time    = '18:00'     , 
            elasped_time  = '5:00'      , 
            finish_time   = '23:00'     , 
            location      = 'vlsn'      , 
            jms           = 'NQS'       , 
            name          = 'test job 2', 
            project       = 'project Y' , 
        )
        for prop, val in prop_dict.items():
            set_prop = 'set_'+prop
            get_prop = 'get_'+prop
            getattr(self.view, set_prop)( val )

        for prop, val in prop_dict.items():
            exp_val = val
            ret_val = getattr(self.view, prop)
            assert_equal( ret_val, exp_val )

    def test_update_state(self):
        self.view.set_state( 'Running' )
        import degras.image as im
        expected_image = im.get_jobstate_image('Running')

        ret_state, ret_image = self.view.get_state()
        assert_equal(ret_state, 'Running')
        assert_equal(ret_image, expected_image)

    def test_update_requests(self):
        request_dict = dict(
            request_submit  = False , 
            request_convert = False , 
            request_send    = False , 
            request_run     = False , 
            request_stop    = True  , 
            request_cancel  = True  , 
            request_rerun   = False , 
            request_receive = False , 
            request_sync    = False , 
        )
        self.view.set_request_dict( request_dict )

        menu = self.view.get_menu()
        for menuitem in menu.GetMenuItems():
            itemname = menuitem.GetLabel()
            enable   = menuitem.IsEnabled()
            reqname  = 'request_' + itemname
            ret_enable = request_dict[reqname]

            assert reqname in request_dict.keys(), reqname
            assert_equal(ret_enable, enable)

    # Todo
    def test_on_select(self):
        pass

    def test_on_enable_auto(self):
        pass
            col_locs, x+self.__job_listctrl.GetScrollPos(wx.HORIZONTAL)) - 1
        return col, row


if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = wx.Frame(None, -1, 'Job ListCtrl')
    managerview = ManagerView(frame)

    # create JobView
    from job_view import JobView
    from job_model import JobModel
    from tests.jobmock import JobMock
    job = JobMock(1)

    jobview = JobView()
    # set properties
    init_prop_dict = dict(
        id            = 5           , 
        expected_time = '3:00'      , 
        start_time    = '15:00'     , 
        elasped_time  = '1:00'      , 
        finish_time   = ''          , 
        location      = 'hpcs'      , 
        jms           = 'LSF'       , 
        name          = 'test job'  , 
        project       = 'project X' , 
    )

    for prop, val in init_prop_dict.items():
        set_prop = 'set_'+prop
        return col, row


if __name__ == "__main__":
    app = wx.App(redirect=False)
    frame = wx.Frame(None, -1, "Job ListCtrl")
    managerview = ManagerView(frame)

    # create JobView
    from job_view import JobView
    from job_model import JobModel
    from tests.jobmock import JobMock

    job = JobMock(1)

    jobview = JobView()
    # set properties
    init_prop_dict = dict(
        id=5,
        expected_time="3:00",
        start_time="15:00",
        elasped_time="1:00",
        finish_time="",
        location="hpcs",
        jms="LSF",
        name="test job",
        project="project X",
    )

    for prop, val in init_prop_dict.items():
        set_prop = "set_" + prop