def _create_watch_regions(watch_driving_dataset, regions_csv_file): categories = [] f = open(regions_csv_file, "r") for line in f: stripped_line = line.strip() if not stripped_line.startswith('#') and len(stripped_line) > 0: values = line.split(',') if len(values) != 3: log.error("Regions csv file has incorrect number of elements on line reading %s" % stripped_line) exit(-1) category_name = values[0].strip() cat = None for category in categories: if category.name == category_name: cat = category if cat is None: log.info(" adding category {}".format(category_name)) cat = LandCoverRegionCategory(name=category_name) cat.driving_dataset = watch_driving_dataset categories.append(cat) region = LandCoverRegion() region.name = values[1].strip() region.mask_file = "data/WATCH_2D/masks/{}".format(values[2].strip()) region.category = cat log.info(" adding region {} to {} with file {}".format(region.name, category_name, region.mask_file)) f.close()
def test_GIVEN_land_cover_action_saved_WHEN_view_submit_page_THEN_land_cover_action_shown(self): self.create_model_run_ready_for_submit() model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) with session_scope() as session: land_cover_cat = LandCoverRegionCategory() land_cover_cat.driving_dataset_id = model_run.driving_dataset_id land_cover_cat.name = "Rivers" land_cover_region = LandCoverRegion() land_cover_region.name = "Thames" land_cover_region.category = land_cover_cat land_cover_action = LandCoverAction() land_cover_action.model_run = model_run land_cover_action.value_id = 9 land_cover_action.region = land_cover_region land_cover_action.order = 1 session.add(land_cover_action) response = self.app.get( url(controller='model_run', action='submit')) assert_that(response.normal_body, contains_string("Change Thames (Rivers) to Ice"))
def add_land_cover_region(self, model_run): with session_scope() as session: land_cover_cat = LandCoverRegionCategory() land_cover_cat.name = "Countries" land_cover_cat.driving_dataset_id = model_run.driving_dataset_id land_cover_region = LandCoverRegion() land_cover_region.mask_file = "filepath" land_cover_region.name = "Wales" land_cover_region.category = land_cover_cat session.add(land_cover_region) return land_cover_region
def test_GIVEN_land_cover_categories_WHEN_get_land_cover_region_THEN_returned_region_has_category_loaded(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) land_cover_region = self.add_land_cover_region(model_run) id = land_cover_region.id with session_scope() as session: land_cover_cat = LandCoverRegionCategory() land_cover_cat.name = "River Catchments" land_cover_cat.driving_dataset_id = model_run.driving_dataset_id session.add(land_cover_cat) returned_region = self.land_cover_service.get_land_cover_region_by_id(id) assert_that(returned_region.category.name, is_("Countries"))
def _mock_lcs_get_land_cover_region_wrong_dataset(id): dds = DrivingDataset() dds.id = 7 land_cover_type = LandCoverRegionCategory() land_cover_type.id = 1 land_cover_type.driving_dataset_id = 7 land_cover_type.driving_dataset = dds land_cover_region = LandCoverRegion() land_cover_region.id = id land_cover_region.name = "Sand" land_cover_region.category = land_cover_type return land_cover_region
def _mock_lcs_get_actions_for_model_run_wrong_dataset(model_run): cat = LandCoverRegionCategory() cat.id = 1 cat.name = "Countries" cat.driving_dataset_id = 2 region = LandCoverRegion() region.id = 1 region.name = "Wales" region.category = cat action = LandCoverAction() action.id = 1 action.region = region action.value_id = 1 return [action]
def _get_or_create_category(self, categoies, driving_dataset, category_name): """ Gets a category with the given name. Either by finding it or creating it :param categoies: current list of categories (updated if new one is added) :param driving_dataset: the driving dataset to set for the category :param category_name: the name of the category :return: the category """ found_category = None for category in categoies: if category.name == category_name: found_category = category break if found_category is None: found_category = LandCoverRegionCategory(name=category_name) found_category.driving_dataset = driving_dataset categoies.append(found_category) return found_category
def generate_categories_with_regions(self, driving_dataset): # Add categories cat1 = LandCoverRegionCategory() cat1.driving_dataset_id = driving_dataset.id cat1.name = "Rivers" cat1.id = 1 region1 = LandCoverRegion() region1.id = 1 region1.name = "Thames" region1.category_id = 1 region1.category = cat1 region2 = LandCoverRegion() region2.id = 2 region2.name = "Itchen" region2.category_id = 1 region2.category = cat1 cat1.regions = [region1, region2] cat2 = LandCoverRegionCategory() cat2.driving_dataset_id = driving_dataset.id cat2.name = "Counties" cat2.id = 2 region3 = LandCoverRegion() region3.id = 3 region3.name = "Hampshire" region3.category_id = 2 region3.category = cat2 region4 = LandCoverRegion() region4.id = 4 region4.name = "Oxfordshire" region4.category_id = 2 region4.category = cat2 cat2.regions = [region3, region4] return [cat1, cat2]
def test_GIVEN_categories_have_land_cover_regions_WHEN_get_categories_THEN_category_has_regions_loaded(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) with session_scope() as session: cat1 = LandCoverRegionCategory() cat1.name = "Countries" cat1.driving_dataset_id = model_run.driving_dataset_id region1 = LandCoverRegion() region1.mask_file = "filepath" region1.name = "Wales" region1.category = cat1 cat2 = LandCoverRegionCategory() cat2.name = "Rivers" cat2.driving_dataset_id = model_run.driving_dataset_id region2 = LandCoverRegion() region2.mask_file = "filepath2" region2.name = "Thames" region2.category = cat2 session.add_all([region1, region2]) categories = self.land_cover_service.get_land_cover_categories(model_run.driving_dataset_id) assert_that(len(categories[0].regions), is_(1)) assert_that(categories[0].regions[0].name, is_("Wales"))
def _mock_get_land_cover_categories(driving_data_id): cat1 = LandCoverRegionCategory() cat1.driving_dataset_id = driving_data_id cat1.name = "Rivers" cat1.id = 1 region1 = LandCoverRegion() region1.id = 1 region1.name = "Thames" region1.category_id = 1 region1.category = cat1 region2 = LandCoverRegion() region2.id = 2 region2.name = "Itchen" region2.category_id = 1 region2.category = cat1 cat1.regions = [region1, region2] cat2 = LandCoverRegionCategory() cat2.driving_dataset_id = driving_data_id cat2.name = "Counties" cat2.id = 2 region3 = LandCoverRegion() region3.id = 3 region3.name = "Hampshire" region3.category_id = 2 region3.category = cat2 region4 = LandCoverRegion() region4.id = 4 region4.name = "Oxfordshire" region4.category_id = 2 region4.category = cat2 cat2.regions = [region3, region4] return [cat1, cat2]
def test_GIVEN_data_set_with_a_region_WHEN_list_THEN_returns_mask(self): with session_scope() as session: category = LandCoverRegionCategory() category.driving_dataset = self.driving_dataset category.name = "mycategory" region = LandCoverRegion() region.name = "myregion" region.category = category region.mask_file = "data/file_path" session.add(category) self.login(access_level=constants.USER_ACCESS_LEVEL_ADMIN) response = self.app.get( url=url(controller='driving_data', action='edit', id=self.driving_dataset.id), expect_errors=True ) assert_that(response.normal_body, contains_string(category.name)) assert_that(response.normal_body, contains_string(region.name)) assert_that(response.normal_body, contains_string(region.mask_file))
def test_GIVEN_invalid_land_cover_actions_WHEN_post_THEN_error_shown_and_stay_on_page(self): # Add some regions to another driving dataset dds = DatasetService().get_driving_datasets(self.user)[1] with session_scope() as session: land_cat = LandCoverRegionCategory() land_cat.driving_dataset = dds land_cat.name = "Category2" land_region = LandCoverRegion() land_region.name = "Region1" land_region.category = land_cat session.add(land_region) response = self.app.post( url(controller='model_run', action='land_cover'), params={ 'submit': u'Next', 'action_1_region': str(land_region.id), 'action_1_value': u'8', 'action_1_order': u'1' }) assert_that(response.normal_body, contains_string("Land Cover")) # Check still on page assert_that(response.normal_body, contains_string("Land Cover Region not valid for the chosen driving data"))