def setUp(self, mock_run_script):
     self.portal = self.layer['portal']
     self.experiments = self.portal[defaults.EXPERIMENTS_FOLDER_ID]
     # create and run sdm experiment
     formhelper = SDMExperimentHelper(self.portal)
     sdmform = formhelper.get_form()
     sdmform.request.form.update({
         'form.buttons.save': 'Create and start',
     })
     # update form with updated request
     sdmform.update()
     # setup mock_run_script
     mock_run_script.side_effect = formhelper.mock_run_script
     # We should have only one SDM
     sdmexp = self.experiments.values()[0]
     transaction.commit()
     # setup som threshold values our projection
     sdmproj = sdmexp.values()[0]['proj_test.tif']
     md = IBCCVLMetadata(sdmproj)
     # there is only one layer
     layermd = md['layers'].values()[0]
     layermd['min'] = 0.0
     layermd['max'] = 1.0
     transaction.commit()
     self.form = BiodiverseExperimentHelper(self.portal, sdmexp)
 def setUp(self, mock_run_script):
     self.portal = self.layer['portal']
     self.experiments = self.portal[defaults.EXPERIMENTS_FOLDER_ID]
     # create and run sdm experiment
     formhelper = SDMExperimentHelper(self.portal)
     sdmform = formhelper.get_form()
     sdmform.request.form.update({
         'form.buttons.save': 'Create and start',
     })
     # update form with updated request
     sdmform.update()
     # setup mock_run_script
     mock_run_script.side_effect = formhelper.mock_run_script
     # We should have only one SDM
     sdmexp = self.experiments.values()[0]
     transaction.commit()
     # setup som threshold values our projection
     sdmproj = sdmexp.values()[0]['proj_test.tif']
     md = IBCCVLMetadata(sdmproj)
     # there is only one layer
     layermd = md['layers'].values()[0]
     layermd['min'] = 0.0
     layermd['max'] = 1.0
     transaction.commit()
     self.form = BiodiverseExperimentHelper(self.portal, sdmexp)
 def setUp(self, mock_run_script):
     self.portal = self.layer['portal']
     self.experiments = self.portal[defaults.EXPERIMENTS_FOLDER_ID]
     # create and run sdm experiment
     formhelper = SDMExperimentHelper(self.portal)
     sdmform = formhelper.get_form()
     sdmform.request.form.update({
         'form.buttons.save': 'Create and start',
     })
     # update form with updated request
     sdmform.update()
     # setup mock_run_script
     mock_run_script.side_effect = formhelper.mock_run_script
     transaction.commit()
     # We should have only one SDM
     sdmexp = self.experiments.values()[0]
     self.form = EnsembleExperimentHelper(self.portal, sdmexp)
 def setUp(self, mock_run_script):
     self.portal = self.layer['portal']
     self.experiments = self.portal[defaults.EXPERIMENTS_FOLDER_ID]
     # create and run sdm experiment
     formhelper = SDMExperimentHelper(self.portal)
     sdmform = formhelper.get_form()
     sdmform.request.form.update({
         'form.buttons.save': 'Create and start',
     })
     # update form with updated request
     sdmform.update()
     # setup mock_run_script
     mock_run_script.side_effect = formhelper.mock_run_script
     transaction.commit()
     # We should have only one SDM
     sdmexp = self.experiments.values()[0]
     self.form = EnsembleExperimentHelper(self.portal, sdmexp)
class ExperimentSDMAddTest(unittest.TestCase):

    # Use functional layer for now to get a new demostorage layer for each test
    layer = BCCVL_FUNCTIONAL_TESTING
    # integration testing gives only a new transaction for each test
    # (rolled back at end)
    # layer = BCCVL_INTEGRATION_TESTING

    def setUp(self):
        self.portal = self.layer['portal']
        self.experiments = self.portal[defaults.EXPERIMENTS_FOLDER_ID]
        self.datasets = self.portal[defaults.DATASETS_FOLDER_ID]
        self.form = SDMExperimentHelper(self.portal)

    def test_add_experiment_missing_input(self):
        form = self.form.get_form()
        # remove required field
        del form.request.form['form.widgets.IDublinCore.title']
        form.request.form.update({
            'form.buttons.create': 'Create',
        })
        # submit form
        form.update()
        errors = [e for e in form.widgets.errors]
        self.assertEqual(len(errors), 1)
        # check self.experiments still empty
        # IStatusMessage

    @mock.patch('org.bccvl.tasks.compute.run_script')
    def test_add_experiment(self, mock_run_script):
        form = self.form.get_form()
        form.request.form.update({
            'form.buttons.save': 'Create and start',
        })
        # update form with updated request
        form.update()
        self.assertEqual(form.status, u'')
        self.assertEqual(len(form.widgets.errors), 0)
        self.assertIn('my-experiment', self.experiments)
        exp = self.experiments['my-experiment']
        self.assertEqual(exp.environmental_datasets.keys(),
                         [unicode(self.form.current.UID())])
        self.assertEqual(exp.environmental_datasets.values(),
                         [set([u'B01', u'B02'])])
        # get result container: (there is only one)
        self.assertEqual(len(exp.objectIds()), 1)
        result = exp.objectValues()[0]
        # FIXME: test result.job_params
        self.assertEqual(result.job_params['function'], 'bioclim')
        self.assertEqual(result.job_params[
                         'environmental_datasets'], exp.environmental_datasets)
        # no result files yet
        self.assertEqual(len(result.keys()), 0)
        # test job state
        jt = IExperimentJobTracker(exp)
        self.assertEqual(jt.state, u'QUEUED')
        # setup mock_run_script
        mock_run_script.side_effect = self.form.mock_run_script
        # after transaction commit the job sholud finish
        transaction.commit()
        self.assertEqual(jt.state, u'COMPLETED')
        # and we should have a result as well
        self.assertGreaterEqual(len(result.keys()), 1)
        # TODO: assrt two result folders,....
        # TODO: check mix of jt.state (in multistate scenario with queued,
        # running etc. mixed)

    @mock.patch('org.bccvl.tasks.compute.run_script')
    def test_run_experiment_twice(self, mock_run_script):
        # create experiment
        form = self.form.get_form()
        form.request.form.update({
            'form.buttons.save': 'Create and start',
        })
        # update form with updated request
        form.update()
        # start experiment
        jt = IExperimentJobTracker(self.experiments['my-experiment'])
        self.assertEqual(jt.state, u'QUEUED')
        # error
        state = jt.start_job(form.request)
        self.assertEqual(state[0], 'error')
        # setup mock_run_script
        mock_run_script.side_effect = self.form.mock_run_script
        # finish current job
        transaction.commit()
        self.assertEqual(jt.state, u'COMPLETED')
        # TODO: after commit tasks cause site to disappear and the
        # following code will fail, bceause without site we can't find
        # a catalog without whchi we can't finde the toolkit by uuid
        jt.start_job(form.request)
        self.assertEqual(jt.state, u'PARTIAL')
        transaction.commit()
        self.assertEqual(jt.state, u'COMPLETED')

    def test_mixed_resolution_highest(self):
        current_1k_uuid = unicode(
            self.datasets[defaults.DATASETS_ENVIRONMENTAL_FOLDER_ID]['current_1k'].UID())
        form = self.form.get_form()
        form.request.form.update({
            'form.buttons.create': 'Create',
            # select 1k dataset as well
            'form.widgets.scale_down': 'true',
            'form.widgets.scale_down-empty-marker': 1,
            'form.widgets.environmental_datasets.item.2': current_1k_uuid,
            'form.widgets.environmental_datasets.item.2.item': [u'B01'],
            'form.widgets.environmental_datasets.count': '3',
        })
        form.update()
        # resolution should be set to the lowest of selected datasets
        expmd = IBCCVLMetadata(self.experiments['my-experiment'])
        self.assertEqual(expmd['resolution'], 'Resolution30s')

    def test_mixed_resolution_lowest(self):
        current_1k_uuid = unicode(
            self.datasets[defaults.DATASETS_ENVIRONMENTAL_FOLDER_ID]['current_1k'].UID())
        form = self.form.get_form()
        form.request.form.update({
            'form.buttons.create': 'Create',
            # select 1k dataset as well
            'form.widgets.scale_down': 'false',
            'form.widgets.scale_down-empty-marker': 1,
            'form.widgets.environmental_datasets.item.2': current_1k_uuid,
            'form.widgets.environmental_datasets.item.2.item': [u'B01'],
            'form.widgets.environmental_datasets.count': '3',
        })
        form.update()
        # resolution should be set to the lowest of selected datasets
        expmd = IBCCVLMetadata(self.experiments['my-experiment'])
        self.assertEqual(expmd['resolution'], 'Resolution2_5m')
class ExperimentSDMAddTest(unittest.TestCase):

    # Use functional layer for now to get a new demostorage layer for each test
    layer = BCCVL_FUNCTIONAL_TESTING

    # integration testing gives only a new transaction for each test
    # (rolled back at end)
    # layer = BCCVL_INTEGRATION_TESTING

    def setUp(self):
        self.portal = self.layer['portal']
        self.experiments = self.portal[defaults.EXPERIMENTS_FOLDER_ID]
        self.datasets = self.portal[defaults.DATASETS_FOLDER_ID]
        self.form = SDMExperimentHelper(self.portal)

    def test_add_experiment_missing_input(self):
        form = self.form.get_form()
        # remove required field
        del form.request.form['form.widgets.IDublinCore.title']
        form.request.form.update({
            'form.buttons.create': 'Create',
        })
        # submit form
        form.update()
        errors = [e for e in form.widgets.errors]
        self.assertEqual(len(errors), 1)
        # check self.experiments still empty
        # IStatusMessage

    @mock.patch('org.bccvl.tasks.compute.run_script')
    def test_add_experiment(self, mock_run_script):
        form = self.form.get_form()
        form.request.form.update({
            'form.buttons.save': 'Create and start',
        })
        # update form with updated request
        form.update()
        self.assertEqual(form.status, u'')
        self.assertEqual(len(form.widgets.errors), 0)
        self.assertIn('my-experiment', self.experiments)
        exp = self.experiments['my-experiment']
        self.assertEqual(exp.environmental_datasets.keys(),
                         [unicode(self.form.current.UID())])
        self.assertEqual(exp.environmental_datasets.values(),
                         [set([u'B01', u'B02'])])
        # get result container: (there is only one)
        self.assertEqual(len(exp.objectIds()), 1)
        result = exp.objectValues()[0]
        # FIXME: test result.job_params
        self.assertEqual(result.job_params['function'], 'bioclim')
        self.assertEqual(result.job_params['environmental_datasets'],
                         exp.environmental_datasets)
        # no result files yet
        self.assertEqual(len(result.keys()), 0)
        # test job state
        jt = IExperimentJobTracker(exp)
        self.assertEqual(jt.state, u'QUEUED')
        # setup mock_run_script
        mock_run_script.side_effect = self.form.mock_run_script
        # after transaction commit the job sholud finish
        transaction.commit()
        self.assertEqual(jt.state, u'COMPLETED')
        # and we should have a result as well
        self.assertGreaterEqual(len(result.keys()), 1)
        # TODO: assrt two result folders,....
        # TODO: check mix of jt.state (in multistate scenario with queued,
        # running etc. mixed)

    @mock.patch('org.bccvl.tasks.compute.run_script')
    def test_run_experiment_twice(self, mock_run_script):
        # create experiment
        form = self.form.get_form()
        form.request.form.update({
            'form.buttons.save': 'Create and start',
        })
        # update form with updated request
        form.update()
        # start experiment
        jt = IExperimentJobTracker(self.experiments['my-experiment'])
        self.assertEqual(jt.state, u'QUEUED')
        # error
        state = jt.start_job(form.request)
        self.assertEqual(state[0], 'error')
        # setup mock_run_script
        mock_run_script.side_effect = self.form.mock_run_script
        # finish current job
        transaction.commit()
        self.assertEqual(jt.state, u'COMPLETED')
        # TODO: after commit tasks cause site to disappear and the
        # following code will fail, bceause without site we can't find
        # a catalog without whchi we can't finde the toolkit by uuid
        jt.start_job(form.request)
        self.assertEqual(jt.state, u'PARTIAL')
        transaction.commit()
        self.assertEqual(jt.state, u'COMPLETED')

    def test_mixed_resolution_highest(self):
        current_1k_uuid = unicode(self.datasets[
            defaults.DATASETS_ENVIRONMENTAL_FOLDER_ID]['current_1k'].UID())
        form = self.form.get_form()
        form.request.form.update({
            'form.buttons.create':
            'Create',
            # select 1k dataset as well
            'form.widgets.scale_down':
            'true',
            'form.widgets.scale_down-empty-marker':
            1,
            'form.widgets.environmental_datasets.item.2':
            current_1k_uuid,
            'form.widgets.environmental_datasets.item.2.item': [u'B01'],
            'form.widgets.environmental_datasets.count':
            '3',
        })
        form.update()
        # resolution should be set to the lowest of selected datasets
        expmd = IBCCVLMetadata(self.experiments['my-experiment'])
        self.assertEqual(expmd['resolution'], 'Resolution30s')

    def test_mixed_resolution_lowest(self):
        current_1k_uuid = unicode(self.datasets[
            defaults.DATASETS_ENVIRONMENTAL_FOLDER_ID]['current_1k'].UID())
        form = self.form.get_form()
        form.request.form.update({
            'form.buttons.create':
            'Create',
            # select 1k dataset as well
            'form.widgets.scale_down':
            'false',
            'form.widgets.scale_down-empty-marker':
            1,
            'form.widgets.environmental_datasets.item.2':
            current_1k_uuid,
            'form.widgets.environmental_datasets.item.2.item': [u'B01'],
            'form.widgets.environmental_datasets.count':
            '3',
        })
        form.update()
        # resolution should be set to the lowest of selected datasets
        expmd = IBCCVLMetadata(self.experiments['my-experiment'])
        self.assertEqual(expmd['resolution'], 'Resolution2_5m')