class SurveyModelTest(unittest.TestCase):

    def setUp(self):
        self.model = SurveyModel()
        self.num_proposals = 4

    def test_basic_information_after_creation(self):
        self.assertIsInstance(self.model.config_obj, Survey)
        self.assertEqual(len(self.model.params), 5)
        self.assertEqual(len(self.model.proposals), 1)
        self.assertEqual(len(self.model.proposals["GEN"]), self.num_proposals)

    def test_is_proposal_active(self):
        self.assertTrue(self.model.is_proposal_active("GalacticPlane"))
        self.assertFalse(self.model.is_proposal_active("TestTheProp"))

    def test_get_parameters(self):
        self.assertEqual(len(self.model.get_parameter("survey/general_proposals")), self.num_proposals)
class SurveyModelTest(unittest.TestCase):

    def setUp(self):
        self.model = SurveyModel()
        self.num_general_proposals = 4
        self.num_sequence_proposals = 1

    def test_basic_information_after_creation(self):
        self.assertIsInstance(self.model.config_obj, Survey)
        self.assertEqual(len(self.model.params), 6)
        self.assertEqual(len(self.model.proposals), 2)
        self.assertEqual(len(self.model.proposals["GEN"]), self.num_general_proposals)
        self.assertEqual(len(self.model.proposals["SEQ"]), self.num_sequence_proposals)

    def test_is_proposal_active(self):
        self.assertTrue(self.model.is_proposal_active("GalacticPlane"))
        self.assertTrue(self.model.is_proposal_active("DeepDrillingCosmology1"))
        self.assertFalse(self.model.is_proposal_active("TestTheProp"))

    def test_get_parameters(self):
        self.assertEqual(len(self.model.get_parameter("survey/general_proposals")),
                         self.num_general_proposals)
        self.assertEqual(len(self.model.get_parameter("survey/sequence_proposals")),
                         self.num_sequence_proposals)
class SurveyController(BaseController):
    """The controller for the survey configuration.
    """
    def __init__(self, name):
        """Initialize the class.

        Parameters
        ----------
        name : str
            The tab name for the configuration view.
        """
        BaseController.__init__(self, name)
        self.model = SurveyModel()
        self.widget = SurveyWidget(name, self.model.proposals)

        self.widget.set_information(self.model.params)

        self.widget.checkProperty.connect(self.check_property)
        self.widget.getProperty.connect(self.get_property)
        self.widget.saveConfiguration.connect(self.save_configuration)

    def apply_overrides(self, config_files, extra_props=None):
        """Apply configuration overrides.

        Parameters
        ----------
        config_files : list
            The list of configuration file paths.
        extra_props : str, optional
            A path for extra proposals.
        """
        params = self.model.apply_overrides(config_files)
        self.widget.set_information(params, full_check=True)
        self.widget.full_check = False

    def check_property(self, param_name, param_value, position):
        """Check the stored value of the parameter name against input.

        Parameters
        ----------
        param_name : str
            The parameter name to retrieve the stored value of.
        param_value : any
            The value of the parameter to check against the stored one.
        position : list(int)
            The widget position that requested this check.
        """
        pname = str(param_name)
        pvalue = str(param_value)
        if "general_proposals" in pname or "sequence_proposals" in pname:
            prop_name = pname.split('/')[-1]
            if pvalue == "True":
                is_changed = False
            else:
                is_changed = self.model.is_proposal_active(prop_name)
            self.widget.is_changed(position, is_changed)
        else:
            BaseController.check_property(self, param_name, param_value,
                                          position)

    def get_property(self, param_name, position):
        """Get the property value for the requested name.

        Parameters
        ----------
        param_name : str
            The parameter name to retrieve the stored value of.
        position : list(int)
            The widget position that requested this check.
        """
        pname = str(param_name)
        if "general_proposals" in pname or "sequence_proposals" in pname:
            prop_name = pname.split('/')[-1]
            is_active = self.model.is_proposal_active(prop_name)
            self.widget.reset_field(position, str(is_active))
        else:
            BaseController.get_property(self, param_name, position)
class SurveyController(BaseController):
    """The controller for the survey configuration.
    """

    def __init__(self, name):
        """Initialize the class.

        Parameters
        ----------
        name : str
            The tab name for the configuration view.
        """
        BaseController.__init__(self, name)
        self.model = SurveyModel()
        self.widget = SurveyWidget(name, self.model.proposals)

        self.widget.set_information(self.model.params)

        self.widget.checkProperty.connect(self.check_property)
        self.widget.getProperty.connect(self.get_property)
        self.widget.saveConfiguration.connect(self.save_configuration)

    def check_property(self, param_name, param_value, position):
        """Check the stored value of the parameter name against input.

        Parameters
        ----------
        param_name : str
            The parameter name to retrieve the stored value of.
        param_value : any
            The value of the parameter to check against the stored one.
        position : list(int)
            The widget position that requested this check.
        """
        pname = str(param_name)
        pvalue = str(param_value)
        if "general_proposals" in pname:
            prop_name = pname.split('/')[-1]
            if pvalue == "True":
                is_changed = False
            else:
                is_changed = self.model.is_proposal_active(prop_name)
            self.widget.is_changed(position, is_changed)
        else:
            BaseController.check_property(self, param_name, param_value, position)

    def get_property(self, param_name, position):
        """Get the property value for the requested name.

        Parameters
        ----------
        param_name : str
            The parameter name to retrieve the stored value of.
        position : list(int)
            The widget position that requested this check.
        """
        pname = str(param_name)
        if "general_proposals" in pname:
            prop_name = pname.split('/')[-1]
            is_active = self.model.is_proposal_active(prop_name)
            self.widget.reset_field(position, str(is_active))
        else:
            BaseController.get_property(self, param_name, position)