Esempio n. 1
0
    def test_add_lifestage_model(self,get_g,m_ls):
        self.make_grass_mock(get_g.return_value)
        # Assume no appropriate files in tmp
        c = config.get_config()
        m = ModelRepository(self.temp_dir)
        m2 = ModelRepository()
        a_file = m2.get_models()['lifestage_test']

        self.create_mock_location(self.temp_dir)
        self.assertEqual(len(m.get_models()), 0)
        # add location to model, save as new
        dm = DispersalModel(a_file)
        dm.set_location('grass_location')
        temp_model_fn = os.path.join(os.path.dirname(a_file),"with_location_model.xml")
        dm.save_model(filename=temp_model_fn)

        get_g.return_value.get_range.return_value = [ 'xxxxxx10' ] * 10
        get_g.return_value.raster_value_freq.return_value = [ [1],[2],[3] ]
        m_ls.return_value = {}

        # and then try to add
        m.add_model(temp_model_fn) 
        self.assertEqual(len(m.get_models()), 1)
        # more tests about lifestage resources?
        self.remove_mock_location(self.temp_dir)
Esempio n. 2
0
    def test_add_lifestage_model(self, get_g, m_ls):
        self.make_grass_mock(get_g.return_value)
        # Assume no appropriate files in tmp
        c = config.get_config()
        m = ModelRepository(self.temp_dir)
        m2 = ModelRepository()
        a_file = m2.get_models()['lifestage_test']

        self.create_mock_location(self.temp_dir)
        self.assertEqual(len(m.get_models()), 0)
        # add location to model, save as new
        dm = DispersalModel(a_file)
        dm.set_location('grass_location')
        temp_model_fn = os.path.join(os.path.dirname(a_file),
                                     "with_location_model.xml")
        dm.save_model(filename=temp_model_fn)

        get_g.return_value.get_range.return_value = ['xxxxxx10'] * 10
        get_g.return_value.raster_value_freq.return_value = [[1], [2], [3]]
        m_ls.return_value = {}

        # and then try to add
        m.add_model(temp_model_fn)
        self.assertEqual(len(m.get_models()), 1)
        # more tests about lifestage resources?
        self.remove_mock_location(self.temp_dir)
Esempio n. 3
0
    def empty_repository_test(self,get_g):
        self.make_grass_mock(get_g.return_value)
        # Assume no appropriate files in tmp
        c = config.get_config()
        
        m = ModelRepository(self.temp_dir)
        self.assertEqual(len(m.get_models()), 0)

        # Test that Repository gets db from shell when it exists
        g = get_g.return_value
        g.grass_vars["GISDBASE"] = self.temp_dir
        g.in_grass_shell = True
        m_in_grass = ModelRepository(self.temp_dir)
        self.assertEqual(m_in_grass.db, self.temp_dir)
        # Test with no specified dir
        m_in_grass = ModelRepository()
        self.assertEqual(m_in_grass.db, self.temp_dir)
        g.in_grass_shell = False
        # Test with dir missing
        self.assertRaises(OSError,ModelRepository,'invalid/dir')

        # Try to add a model from one repository to the empty one
        try: self.remove_mock_location(self.temp_dir)
        except OSError, e:
            stre = str(e)
            if 'No such file' not in stre and \
                'system cannot find the path specified' not in stre:
                raise e
Esempio n. 4
0
    def empty_repository_test(self, get_g):
        self.make_grass_mock(get_g.return_value)
        # Assume no appropriate files in tmp
        c = config.get_config()

        m = ModelRepository(self.temp_dir)
        self.assertEqual(len(m.get_models()), 0)

        # Test that Repository gets db from shell when it exists
        g = get_g.return_value
        g.grass_vars["GISDBASE"] = self.temp_dir
        g.in_grass_shell = True
        m_in_grass = ModelRepository(self.temp_dir)
        self.assertEqual(m_in_grass.db, self.temp_dir)
        # Test with no specified dir
        m_in_grass = ModelRepository()
        self.assertEqual(m_in_grass.db, self.temp_dir)
        g.in_grass_shell = False
        # Test with dir missing
        self.assertRaises(OSError, ModelRepository, 'invalid/dir')

        # Try to add a model from one repository to the empty one
        try:
            self.remove_mock_location(self.temp_dir)
        except OSError, e:
            stre = str(e)
            if 'No such file' not in stre and \
                'system cannot find the path specified' not in stre:
                raise e
Esempio n. 5
0
 def normal_repository_test(self):
     m = ModelRepository()
     models = m.get_models()
     self.assertEqual(len(models),13)
     self.assertTrue("lifestage_test" in models)
     self.assertTrue("variables" in models)
     self.assertTrue("management_area_combine" in models)
     self.assertTrue("management_delay" in models)
     self.assertTrue("management_event" in models)
     self.assertTrue("management_alter_variable" in models)
Esempio n. 6
0
 def normal_repository_test(self):
     m = ModelRepository()
     models = m.get_models()
     self.assertEqual(len(models), 13)
     self.assertTrue("lifestage_test" in models)
     self.assertTrue("variables" in models)
     self.assertTrue("management_area_combine" in models)
     self.assertTrue("management_delay" in models)
     self.assertTrue("management_event" in models)
     self.assertTrue("management_alter_variable" in models)
Esempio n. 7
0
    def test_lifestage_model_missing_files(self,get_g):
        self.make_grass_mock(get_g.return_value)
        # Assume no appropriate files in tmp
        c = config.get_config()
        m = ModelRepository(self.temp_dir)
        m2 = ModelRepository()
        a_file = m2.get_models()['lifestage_test']

        self.create_mock_location(self.temp_dir)
        self.assertEqual(len(m.get_models()), 0)
        # add location to model, save as new
        dm = DispersalModel(a_file)
        dm.set_location('grass_location')
        temp_model_fn = os.path.join(self.temp_dir,"with_location_model.xml")
        dm.save_model(filename=temp_model_fn)

        # and then try to add
        self.assertRaises(mdig.modelrepository.RepositoryException,m.add_model,temp_model_fn) 
        self.assertEqual(len(m.get_models()), 0)
        # more tests about lifestage resources?
        self.remove_mock_location(self.temp_dir)
Esempio n. 8
0
    def test_lifestage_model_missing_files(self, get_g):
        self.make_grass_mock(get_g.return_value)
        # Assume no appropriate files in tmp
        c = config.get_config()
        m = ModelRepository(self.temp_dir)
        m2 = ModelRepository()
        a_file = m2.get_models()['lifestage_test']

        self.create_mock_location(self.temp_dir)
        self.assertEqual(len(m.get_models()), 0)
        # add location to model, save as new
        dm = DispersalModel(a_file)
        dm.set_location('grass_location')
        temp_model_fn = os.path.join(self.temp_dir, "with_location_model.xml")
        dm.save_model(filename=temp_model_fn)

        # and then try to add
        self.assertRaises(mdig.modelrepository.RepositoryException,
                          m.add_model, temp_model_fn)
        self.assertEqual(len(m.get_models()), 0)
        # more tests about lifestage resources?
        self.remove_mock_location(self.temp_dir)
Esempio n. 9
0
    def remove_model_test(self,get_g):
        self.make_grass_mock(get_g.return_value)
        # Assume no appropriate files in tmp
        c = config.get_config()
        m = ModelRepository(self.temp_dir)
        self.assertEqual(len(m.get_models()), 0)

        # Try to add a model from one repository to the empty one
        try:
            self.remove_mock_location(self.temp_dir)
        except OSError, e:
            stre = str(e)
            if 'No such file' not in stre and \
                'system cannot find the path specified' not in stre:
                raise e
Esempio n. 10
0
    def remove_model_test(self, get_g):
        self.make_grass_mock(get_g.return_value)
        # Assume no appropriate files in tmp
        c = config.get_config()
        m = ModelRepository(self.temp_dir)
        self.assertEqual(len(m.get_models()), 0)

        # Try to add a model from one repository to the empty one
        try:
            self.remove_mock_location(self.temp_dir)
        except OSError, e:
            stre = str(e)
            if 'No such file' not in stre and \
                'system cannot find the path specified' not in stre:
                raise e
Esempio n. 11
0
def migrate_repository(grassdb_dir):
    mr = ModelRepository(grassdb_dir)
    model_fns = mr.get_models()
    for model in model_fns:
        model_fn = model_fns[model]
        # check for unseparated instances from main mapset
        dm = DispersalModel(model_fn)
        try:
            instances = dm.get_instances()
            for i in instances:
                mapset = i.get_mapset()
            if not check_instances_have_info_file(dm.get_instances()):
                print "Model %s ok" % model
        except DispersalInstanceException, e:
            if not "no longer supports instances sharing one mapset" in str(e):
                continue
            # if so, fix them
            print dm.get_mapset() + " not ok.. fixing"
            split_instances_into_own_mapsets(dm)
Esempio n. 12
0
def migrate_repository(grassdb_dir):
    mr = ModelRepository(grassdb_dir)
    model_fns = mr.get_models()
    for model in model_fns:
        model_fn = model_fns[model]
        # check for unseparated instances from main mapset
        dm = DispersalModel(model_fn)
        try:
            instances = dm.get_instances()
            for i in instances:
                mapset = i.get_mapset()
            if not check_instances_have_info_file(dm.get_instances()):
                print "Model %s ok" % model
        except DispersalInstanceException, e:
            if not "no longer supports instances sharing one mapset" in str(e):
                continue
            # if so, fix them
            print dm.get_mapset() + " not ok.. fixing"
            split_instances_into_own_mapsets(dm)
Esempio n. 13
0
        self.make_grass_mock(get_g.return_value)
        # Assume no appropriate files in tmp
        c = config.get_config()
        m = ModelRepository(self.temp_dir)
        self.assertEqual(len(m.get_models()), 0)

        # Try to add a model from one repository to the empty one
        try:
            self.remove_mock_location(self.temp_dir)
        except OSError, e:
            stre = str(e)
            if 'No such file' not in stre and \
                'system cannot find the path specified' not in stre:
                raise e
        m2 = ModelRepository()
        a_file = m2.get_models()['variables']
        self.create_mock_location(self.temp_dir)

        # Add location to model
        dm = DispersalModel(a_file)
        dm.set_location('grass_location')
        temp_model_fn = "with_location_model.xml"
        dm.save_model(filename=temp_model_fn)

        m.add_model(temp_model_fn)
        self.assertEqual(len(m.get_models()), 1)
        m.remove_model('variables',force=True)
        self.assertEqual(get_g.return_value.remove_mapset.call_args[0][0], 'variables')
        os.remove(temp_model_fn)

        self.assertRaises(mdig.modelrepository.RepositoryException,m.remove_model,'non_existant')
Esempio n. 14
0
        self.make_grass_mock(get_g.return_value)
        # Assume no appropriate files in tmp
        c = config.get_config()
        m = ModelRepository(self.temp_dir)
        self.assertEqual(len(m.get_models()), 0)

        # Try to add a model from one repository to the empty one
        try:
            self.remove_mock_location(self.temp_dir)
        except OSError, e:
            stre = str(e)
            if 'No such file' not in stre and \
                'system cannot find the path specified' not in stre:
                raise e
        m2 = ModelRepository()
        a_file = m2.get_models()['variables']
        self.create_mock_location(self.temp_dir)

        # Add location to model
        dm = DispersalModel(a_file)
        dm.set_location('grass_location')
        temp_model_fn = "with_location_model.xml"
        dm.save_model(filename=temp_model_fn)

        m.add_model(temp_model_fn)
        self.assertEqual(len(m.get_models()), 1)
        m.remove_model('variables', force=True)
        self.assertEqual(get_g.return_value.remove_mapset.call_args[0][0],
                         'variables')
        os.remove(temp_model_fn)