Beispiel #1
0
    def test_different_cmf_args(self):

        # 1) test with valid CMF and LayerProperties objects
        test_cmf = CrashMoveFolder(self.path_to_valid_cmf_des)
        test_lp = LayerProperties(test_cmf, "test", verify_on_creation=False)
        test_mcb = MapCookbook(test_cmf, test_lp, verify_on_creation=False)
        self.assertIsInstance(test_mcb, MapCookbook)

        # 2) test with invalid cmf object (eg and CrashMoveFolder object
        #    where verify_paths() returns False)
        test_cmf = CrashMoveFolder(self.path_to_invalid_cmf_des,
                                   verify_on_creation=False)
        self.assertRaises(ValueError, MapCookbook, test_cmf, test_lp)
Beispiel #2
0
    def test_get_cookbook_steps(self, mock_pf):
        # This asserts that a list of steps is added to the stack when `get_cookbook_steps` if called
        # First do the work:
        test_runner = DummyRunner(Event(self.path_to_event_file))
        test_runner.cmf = CrashMoveFolder(self.path_to_cmf_file)
        initial_step = plugin_controller.get_cookbook_steps(
            test_runner, None, dry_run=True, verify_on_creation=False)
        main_stack.process_stack(initial_step, None)

        # Now get the information out of the mock:
        found_list_of_steps = False
        for call in mock_pf.call_args_list:
            try:
                # print()
                # print('---------------------')
                # print('test_get_cookbook_steps')
                # print(call)
                # print('END:test_get_cookbook_steps')
                # print('---------------------')
                # retrieved_result = call[1]['result']
                retrieved_result = call[1].get('result', None)
                if all([isinstance(stp, Step) for stp in retrieved_result]):
                    # print('found_list_of_steps=True')
                    found_list_of_steps = True
            except TypeError:
                pass

        self.assertTrue(found_list_of_steps)
Beispiel #3
0
    def test_recipe_atlas_constructor(self):
        cmf = CrashMoveFolder(
            os.path.join(self.parent_dir, 'example',
                         'cmf_description_relative_paths_test.json'))
        cmf.layer_properties = os.path.join(
            self.parent_dir, 'tests', 'testfiles', 'cookbooks',
            'fixture_layer_properties_for_atlas.json')

        # 1) atlas matches other sections 'fixture_cookbook_good_with_atlas.json'
        cmf.map_definitions = os.path.join(
            self.parent_dir, 'tests', 'testfiles', 'cookbooks',
            'fixture_cookbook_good_with_atlas.json')
        test_lp = LayerProperties(cmf, ".lyr", verify_on_creation=False)
        MapCookbook(cmf, test_lp, verify_on_creation=True)
        self.assertTrue(True)

        # 2) mismatch map_frame name 'fixture_cookbook_atlas_mismatch_map_frame.json'
        # 3) mismatch layer name (but present in other map frame) 'fixture_cookbook_atlas_mismatch_layer1.json'
        # 4) mismatch layer name, not present elsewhere in the recipe. 'fixture_cookbook_atlas_mismatch_layer2.json'
        # 5) mismatch column_names
        test_cb_names = ('fixture_cookbook_atlas_mismatch_map_frame.json',
                         'fixture_cookbook_atlas_mismatch_layer1.json',
                         'fixture_cookbook_atlas_mismatch_layer2.json',
                         'fixture_cookbook_atlas_mismatch_column_name.json')

        for test_cb in test_cb_names:
            cmf.map_definitions = os.path.join(self.parent_dir, 'tests',
                                               'testfiles', 'cookbooks',
                                               test_cb)
            test_lp = LayerProperties(cmf, ".lyr", verify_on_creation=False)
            with self.assertRaises(ValueError):
                MapCookbook(cmf, test_lp, verify_on_creation=True)
Beispiel #4
0
    def test_check_for_dup_layers_and_mapframs(self):
        cmf = CrashMoveFolder(
            os.path.join(self.parent_dir, 'example',
                         'cmf_description_relative_paths_test.json'))
        cmf.layer_properties = os.path.join(
            self.parent_dir, 'tests', 'testfiles', 'cookbooks',
            'fixture_layer_properties_for_atlas.json')

        # Fail with multiple layer with the same name in the same mapframe.
        test_cookbooks = (('fixture_cookbook_with_dup_layers.json',
                           'mainmap_tran_por_pt_s0_allmaps'),
                          ('fixture_cookbook_with_dup_mapframes.json',
                           'Main map'))

        for cb_filename, fail_msg in test_cookbooks:
            cmf.map_definitions = os.path.join(self.parent_dir, 'tests',
                                               'testfiles', 'cookbooks',
                                               cb_filename)
            test_lp = LayerProperties(cmf, ".lyr", verify_on_creation=False)
            with self.assertRaises(ValueError) as ve:
                MapCookbook(cmf, test_lp, verify_on_creation=True)

            if six.PY2:
                self.assertRegexpMatches(str(ve.exception), fail_msg)
            else:
                self.assertRegex(str(ve.exception), fail_msg)
Beispiel #5
0
 def setUp(self):
     self.parent_dir = os.path.dirname(
         os.path.dirname(os.path.realpath(__file__)))
     self.path_to_valid_cmf_des = os.path.join(
         self.parent_dir, 'tests', 'test_data',
         'fixture_cmf_description_flat_test.json')
     self.cmf = CrashMoveFolder(self.path_to_valid_cmf_des)
def get_defaultcmf_step_list(cmf_config_path):
    """
    Generates a list of Steps, each of which execucutes a suitable naming convention test
    for a static item within the crash move folder.
    """
    cmf = CrashMoveFolder(cmf_config_path)

    ncs_to_check = (
        (cmf.layer_rendering, cmf.layer_nc_definition, '.lyr', 'layer'),
        (cmf.layer_rendering, cmf.layer_nc_definition, '.qml', 'layer'),
        (cmf.layer_rendering, cmf.layer_nc_definition, '.qlr', 'layer'),
        (cmf.map_projects, cmf.map_projects_nc_definition, '.qgs', 'map project'),
        (cmf.map_projects, cmf.map_projects_nc_definition, '.mxd', 'map project'),
        (cmf.map_templates, cmf.map_template_nc_definition, '.qgs', 'map template'),
        (cmf.map_templates, cmf.map_template_nc_definition, '.aprx', 'map template'),
        (cmf.map_templates, cmf.map_template_nc_definition, '.pagx', 'map template'),
        (cmf.map_templates, cmf.map_template_nc_definition, '.mxd', 'map template')
    )

    name_convention_steps = []

    for dir_to_check, nc_desc_file, extn_to_check, convention_name in ncs_to_check:
        nc = name_convention.NamingConvention(nc_desc_file)
        name_convention_steps.extend(
            _step_builer(_get_all_files(dir_to_check, extn_to_check), nc, convention_name, cmf))

    return name_convention_steps
Beispiel #7
0
    def test_check_for_dup_text_elements(self):
        cmf = CrashMoveFolder(
            os.path.join(self.parent_dir, 'example',
                         'cmf_description_relative_paths_test.json'))
        cmf.layer_properties = os.path.join(
            self.parent_dir, 'tests', 'testfiles', 'cookbooks',
            'fixture_layer_properties_for_atlas.json')

        # 1) Pass with "good" text elements in just one map_frame
        cmf.map_definitions = os.path.join(
            self.parent_dir, 'tests', 'testfiles', 'cookbooks',
            'fixture_cookbook_good_with_atlas.json')
        test_lp = LayerProperties(cmf, ".lyr", verify_on_creation=False)
        MapCookbook(cmf, test_lp, verify_on_creation=True)
        self.assertTrue(True)

        # 2) Fail with duplicate text elements in multiple map_frames
        cmf.map_definitions = os.path.join(
            self.parent_dir, 'tests', 'testfiles', 'cookbooks',
            'fixture_cookbook_with_dup_text_elements.json')
        test_lp = LayerProperties(cmf, ".lyr", verify_on_creation=False)
        with self.assertRaises(ValueError) as ve:
            MapCookbook(cmf, test_lp, verify_on_creation=True)

        fail_msg = 'More than one "map_frame" is linked to the Scale text element "scale"'
        if six.PY2:
            self.assertRegexpMatches(str(ve.exception), fail_msg)
        else:
            self.assertRegex(str(ve.exception), fail_msg)
Beispiel #8
0
    def setUp(self):
        self.parent_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
        self.path_to_valid_cmf_des = os.path.join(self.parent_dir, 'example', 'cmf_description_flat_test.json')
        self.path_to_invalid_cmf_des = os.path.join(
            self.parent_dir, 'tests', 'testfiles', 'fixture_cmf_description_one_file_and_one_dir_not_valid.json')

        self.cmf = CrashMoveFolder(self.path_to_valid_cmf_des, verify_on_creation=False)
        self.lyr_props = LayerProperties(self.cmf, '', verify_on_creation=False)
def get_active_data_step_list(humev_config_path):
    """
    Generates a list of Steps, each of which execucutes a suitable naming convention test
    for a GIS dataset with the `2_Active_data` folder within the crash move folder.
    """
    humev = Event(humev_config_path)
    cmf = CrashMoveFolder(humev.cmf_descriptor_path)
    nc = name_convention.NamingConvention(cmf.data_nc_definition)
    return _step_builer(get_all_gisfiles(cmf), nc, 'data', cmf)
    def __init__(self, hum_event, ** kwargs):
        self.hum_event = hum_event
        self.cmf = CrashMoveFolder(self.hum_event.cmf_descriptor_path)

        if not self.cmf.verify_paths():
            raise ValueError("Cannot find paths and directories referenced by cmf {}".format(self.cmf.path))

        if self.__class__ is BaseRunnerPlugin:
            raise NotImplementedError(
                'BaseRunnerPlugin is an abstract class and cannot be instantiated directly')
 def setUp(self):
     self.parent_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
     self.cmf_descriptor_path = os.path.join(self.parent_dir, 'example', 'cmf_description_flat_test.json')
     self.event_descriptor_path = os.path.join(self.parent_dir, 'example', 'event_description.json')
     self.recipe_file = os.path.join(self.parent_dir, 'example', 'example_single_map_recipe.json')
     self.non_existant_file = os.path.join(self.parent_dir, 'example', 'non-existant-file.json')
     self.cmf = CrashMoveFolder(self.cmf_descriptor_path, verify_on_creation=False)
     self.event = Event(self.event_descriptor_path)
     # self.cmf = CrashMoveFolder(self.cmf_descriptor_path)
     self.lyr_props = LayerProperties(self.cmf, '', verify_on_creation=False)
    def test_cmf_path_validation(self):

        cmf_partial_fail = os.path.join(
            self.parent_dir, 'tests', 'testfiles',
            'fixture_cmf_description_one_file_and_one_dir_not_valid.json')

        # test validation on creation (for failing case) using default parameters
        self.assertRaises(ValueError, CrashMoveFolder, cmf_partial_fail)
        # force validation on creation, for a json file we know would otherwise fail:
        self.assertRaises(ValueError,
                          CrashMoveFolder,
                          cmf_partial_fail,
                          verify_on_creation=True)
        # test validation is correctly disabled on creation, for a json file we know would otherwise fail:
        test_cmf = CrashMoveFolder(cmf_partial_fail, verify_on_creation=False)
        self.assertIsInstance(test_cmf, CrashMoveFolder)

        # check message included in the ValueError:
        with self.assertRaises(ValueError) as cm:
            test_cmf = CrashMoveFolder(cmf_partial_fail,
                                       verify_on_creation=True)

        if six.PY2:
            self.assertRegexpMatches(str(cm.exception), "map_templates")
            self.assertNotRegexpMatches(str(cm.exception), "original_data")
        else:
            self.assertRegex(str(cm.exception), "map_templates")
            self.assertNotRegex(str(cm.exception), "original_data")

        # create a valid CMF object and then test paths, after creation
        test_cmf_path = os.path.join(self.parent_dir, 'example',
                                     'cmf_description_flat_test.json')
        test_cmf = CrashMoveFolder(test_cmf_path)
        self.assertTrue(test_cmf.verify_paths())
        test_cmf_path = os.path.join(
            self.parent_dir, 'example',
            'cmf_description_relative_paths_test.json')
        test_cmf = CrashMoveFolder(test_cmf_path)
        self.assertTrue(test_cmf.verify_paths())
        test_cmf.active_data = os.path.join(self.parent_dir, 'DOES-NOT-EXIST')
        self.assertFalse(test_cmf.verify_paths())
    def test_different_cmf_args(self):
        # 1) test with valid cmf object
        test_cmf = CrashMoveFolder(self.path_to_valid_cmf_des)
        test_lp = LayerProperties(test_cmf, "test", verify_on_creation=False)
        self.assertIsInstance(test_lp, LayerProperties)

        # 2) test with valid cmf file
        test_lp = LayerProperties(self.path_to_valid_cmf_des,
                                  "test",
                                  verify_on_creation=False)
        self.assertIsInstance(test_lp, LayerProperties)

        # 3) test with invalid cmf object (eg and CrashMoveFolder object
        #    where verify_paths() returns False)
        test_cmf = CrashMoveFolder(self.path_to_invalid_cmf_des,
                                   verify_on_creation=False)
        self.assertRaises(ValueError, LayerProperties, test_cmf, "test")

        # 4) test with invalid cmd file
        self.assertRaises(ValueError, LayerProperties,
                          self.path_to_invalid_cmf_des, "test")
 def check_lyr_props_vs_map_cookbook(self, **kwargs):
     try:
         cmf = CrashMoveFolder(self.cmf_desc_path)
         lyrs = LayerProperties(cmf, '', verify_on_creation=False)
         MapCookbook(cmf, lyrs, verify_on_creation=True)
         return (
             'No inconsistency detected between:\n'
             ' * the contents of the layer properties json file:\n\t{props}\n'
             ' * and the contents of the MapCookbook json:\n\t{cbook}\n'.
             format(props=cmf.layer_properties, cbook=cmf.map_definitions))
     except ValueError:
         raise
    def setUp(self):
        parent_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

        self.dnc_json_path = os.path.join(
            parent_dir, 'example', 'data_naming_convention.json')

        self.test_files_dir = os.path.join(parent_dir, 'tests', 'testfiles', '.')

        cmf_descriptor_path = os.path.join(
            parent_dir, 'example', 'cmf_description.json')
        self.cmf = CrashMoveFolder(cmf_descriptor_path, verify_on_creation=False)
        self.cmf.dnc_lookup_dir = os.path.join(parent_dir, 'example')
 def setUp(self):
     self.parent_dir = os.path.dirname(
         os.path.dirname(os.path.realpath(__file__)))
     self.dir_to_valid_cmf_des = os.path.join(self.parent_dir, 'example')
     self.path_to_valid_cmf_des = os.path.join(
         self.dir_to_valid_cmf_des, 'cmf_description_flat_test.json')
     self.path_to_event_des = os.path.join(self.dir_to_valid_cmf_des,
                                           'event_description.json')
     self.cmf = CrashMoveFolder(self.path_to_valid_cmf_des)
     self.lyr_props = LayerProperties(self.cmf,
                                      '',
                                      verify_on_creation=False)
     self.dummy_runner = DummyRunner(Event(self.path_to_event_des))
 def check_lyr_props_vs_rendering_dir(self, **kwargs):
     cmf = CrashMoveFolder(self.cmf_desc_path)
     for lyr_exn in self.lyr_file_exn_list:
         try:
             LayerProperties(cmf, lyr_exn, verify_on_creation=True)
             return (
                 'No inconsistency detected between:\n'
                 ' * the contents of the layer properties json file:\n\t{props}\n'
                 ' * and layer rendering dir:\n\t{render}\n'.format(
                     props=cmf.layer_properties,
                     render=cmf.layer_rendering))
         except ValueError:
             raise
Beispiel #18
0
    def test_layer_props_and_cmf_mismatch(self):

        # create and test_cmf and test_lp which refer different layer_prop.json files
        cmf1 = CrashMoveFolder(self.path_to_valid_cmf_des)
        test_lp = LayerProperties(cmf1, "test", verify_on_creation=False)
        # now point the test_cmf to a different layer_props.json
        cmf2 = CrashMoveFolder(self.path_to_valid_cmf_des)
        cmf2.layer_properties = os.path.join(
            self.parent_dir, 'tests', 'testfiles',
            'fixture_layer_properties_four_layers.json')

        # check that a ValueError is raised when `verify_on_creation=True`
        with self.assertRaises(ValueError) as ve:
            MapCookbook(cmf2, test_lp, verify_on_creation=True)

        if six.PY2:
            self.assertRegexpMatches(str(ve.exception), "strange results")
        else:
            self.assertRegex(str(ve.exception), "strange results")

        # check that a ValueError is not raised when `verify_on_creation=False`
        test_mcb = MapCookbook(cmf2, test_lp, verify_on_creation=False)
        self.assertIsInstance(test_mcb, MapCookbook)
 def setUp(self):
     self.parent_dir = os.path.dirname(
         os.path.dirname(os.path.realpath(__file__)))
     self.path_to_valid_cmf_des = os.path.join(
         self.parent_dir, 'tests', 'test_data',
         'fixture_cmf_description_flat_test.json')
     self.cmf = CrashMoveFolder(self.path_to_valid_cmf_des)
     self.event = Event(
         os.path.join(self.parent_dir, 'tests', 'test_data',
                      'event_description.json'))
     self.my_mxd_fname = os.path.join(
         self.parent_dir, 'tests', 'test_data',
         'output_arcgis_10_6_reference_landscape_bottom.mxd')
     self.layer_props = LayerProperties(self.cmf, '.lyr')
     self.cookBook = MapCookbook(self.cmf, self.layer_props)
Beispiel #20
0
    def test_get_single_file_checker(self):
        cmf = CrashMoveFolder(self.cmf_descriptor_path)
        nc_desc_path = os.path.join(self.parent_dir, 'example',
                                    'data_naming_convention.json')
        nc = name_convention.NamingConvention(nc_desc_path)

        passing_path = '/path/to/some/gisdata/206_bldg/ken_bldg_bdg_py_s4_osm_pp.shp'
        func = check_naming_convention.get_single_file_checker(
            passing_path, nc, cmf)
        self.assertIn('parsable and valid', func().get_message)

        failing_path = '/path/to/some/gisdata/202_admn/ken_admn_ad0_ln_s0_IEBC_pp_HDX.shp'
        func = check_naming_convention.get_single_file_checker(
            failing_path, nc, cmf)
        self.assertRaises(ValueError, func)
    def test_zero_length_file_extention(self):
        test_cmf = CrashMoveFolder(self.path_to_valid_cmf_des)

        test_lp1 = LayerProperties(test_cmf, '', verify_on_creation=False)
        self.assertIsInstance(test_lp1, LayerProperties)

        layer_rendering_test_root = os.path.join(self.parent_dir, 'tests',
                                                 'testfiles',
                                                 'test_layer_rendering')
        test_cmf.layer_properties = os.path.join(
            self.parent_dir, 'tests', 'testfiles',
            'fixture_layer_properties_four_layers.json')
        test_cmf.layer_rendering = os.path.join(layer_rendering_test_root,
                                                'four_files_exact_match')
        test_lp2 = LayerProperties(test_cmf, '', verify_on_creation=True)
        self.assertIsInstance(test_lp2, LayerProperties)
Beispiel #22
0
    def test_layer_props_and_cookbook_mismatch(self):
        cmf = CrashMoveFolder(self.path_to_valid_cmf_des)
        cmf.layer_properties = os.path.join(
            self.parent_dir, 'tests', 'testfiles',
            'fixture_layer_properties_four_layers.json')
        cmf.map_definitions = os.path.join(
            self.parent_dir, 'tests', 'testfiles',
            'fixture_cookbook_1map_4layers.json')
        test_lp = LayerProperties(cmf, "test", verify_on_creation=False)

        # 1) map_cb and lp match
        test_mcb = MapCookbook(cmf, test_lp, verify_on_creation=True)
        self.assertIsInstance(test_mcb, MapCookbook)

        # 2) layer in map_cb but not in lp
        # 3) layer in lp but not in map_cb
        # 4) unmatched layers in both lp and map_cb
        testcases = (
            ('fixture_cookbook_1map_5layers.json',
             ("mainmap-tran-rds-ln-s0-allmaps")),
            ('fixture_cookbook_1map_3layers.json',
             ("locationmap_stle_stl_pt_s0_locationmaps")),
            ('fixture_cookbook_1map_mismatch_layers.json',
             ("mainmap-tran-rds-ln-s0-allmaps",
              "locationmap_stle_stl_pt_s0_locationmaps")),
        )

        for cb_file, strings_in_ex_msg in testcases:
            cmf.map_definitions = os.path.join(self.parent_dir, 'tests',
                                               'testfiles', cb_file)

            with self.assertRaises(ValueError) as ve:
                MapCookbook(cmf, test_lp, verify_on_creation=True)

            if six.PY2:
                self.assertRegexpMatches(
                    str(ve.exception),
                    "One or more layer names occur in only one of these files")
                for s in strings_in_ex_msg:
                    self.assertRegexpMatches(str(ve.exception), s)
            else:
                self.assertRegex(
                    str(ve.exception),
                    "One or more layer names occur in only one of these files")
                for s in strings_in_ex_msg:
                    self.assertRegex(str(ve.exception), s)
Beispiel #23
0
    def setUp(self):
        self.parent_dir = os.path.dirname(
            os.path.dirname(os.path.realpath(__file__)))
        self.path_to_valid_cmf_des = os.path.join(
            self.parent_dir, 'tests', 'test_data',
            'fixture_cmf_description_flat_test.json')
        self.cmf = CrashMoveFolder(self.path_to_valid_cmf_des)
        self.event = Event(
            os.path.join(self.cmf.path, 'event_description.json'))
        self.arcmap_runner = arcmap_runner.ArcMapRunner(self.event)

        # 1) insert map
        self.df1 = mock.Mock(name='data_frame1')
        self.df1.name = 'data_frame1'
        self.df1.elementHeight = 19
        self.df1.elementWidth = 17
        # 2) main map
        self.df2 = mock.Mock(name='data_frame2')
        self.df2.name = 'data_frame2'
        self.df2.elementHeight = 100
        self.df2.elementWidth = 200
        # 3) main map (same size different asspect ratio)
        self.df3 = mock.Mock(name='data_frame3')
        self.df3.name = 'data_frame3'
        self.df3.elementHeight = 50
        self.df3.elementWidth = 400
        # 4) main map - identical to df3
        self.df4 = mock.Mock(name='data_frame4')
        self.df4.name = 'data_frame4'
        self.df4.elementHeight = 50
        self.df4.elementWidth = 400
        # 5) main map - widest, but not the largest area
        self.df5 = mock.Mock(name='data_frame5')
        self.df5.name = 'data_frame5'
        self.df5.elementHeight = 10
        self.df5.elementWidth = 500
        # 6) inset map - identical to df1 in every way including the name
        self.df6 = mock.Mock(name='data_frame6')
        self.df6.name = 'data_frame1'
        self.df6.elementHeight = 19
        self.df6.elementWidth = 17
    def setUp(self):
        self.parent_dir = os.path.dirname(
            os.path.dirname(os.path.realpath(__file__)))
        self.cmf_descriptor_path = os.path.join(
            self.parent_dir, 'example', 'cmf_description_flat_test.json')
        self.cmf = CrashMoveFolder(self.cmf_descriptor_path,
                                   verify_on_creation=False)
        self.event_descriptor_path = os.path.join(self.parent_dir, 'example',
                                                  'event_description.json')
        self.event = Event(self.event_descriptor_path)
        # self.cmf = CrashMoveFolder(self.cmf_descriptor_path)
        self.lyr_props = LayerProperties(self.cmf,
                                         '',
                                         verify_on_creation=False)

        self.recipe_descriptor_path = os.path.join(
            self.parent_dir, 'example', 'example_single_map_recipe.json')
        with open(self.recipe_descriptor_path) as json_file:
            self.recipe_def = json.load(json_file)

        self.recipe = MapRecipe(self.recipe_def, self.lyr_props)
Beispiel #25
0
    def test_select_recipes(self):
        # This looks for the MapID in the step.running_msg for the selected maps

        # A list of tuples to test
        # * The param (as entered by a human) to be passed to `get_cookbook_steps`. Includes upper, lower
        #   and mixed case, MapIDs that aren't in the cookbook etc. Can be either a string or a list
        # * A list of the mapIDs that should be created (exactly as in the cookbook file - match case etc)
        # * A list of mapID that should NOT be created (and represent a failure)
        #
        # This is the list of mapID that exist in the relevant cookbook
        #  ['MA001', 'MA002', 'MA003', 'MA004']
        test_cases = [('ma001', ['MA001'], ['MA002', 'MA003', 'MA004']),
                      ('MA004', ['MA004'], ['MA001', 'MA002', 'MA003']),
                      ('Ma004', ['MA004'], ['MA001', 'MA002', 'MA003']),
                      ('MA001', ['MA001'], ['MA002', 'MA003', 'MA004']),
                      (['mA001', 'ma002'], ['MA001',
                                            'MA002'], ['MA003', 'MA004']),
                      ('ma999', [], ['MA001', 'MA002', 'MA003', 'MA004']),
                      (None, ['MA001', 'MA002', 'MA003', 'MA004'], [])]

        cmf = CrashMoveFolder(self.path_to_cmf_file)
        lp = LayerProperties(cmf, 'test', False)
        cb = MapCookbook(cmf, lp, False)

        for mapid_arg, should_create, fail_list in test_cases:
            # print('mapid_arg = {}'.format(mapid_arg))
            # print('should_create = {}'.format(should_create))
            # print('fail_list = {}'.format(fail_list))
            should_create_set = set(should_create)

            selected_recipes = plugin_controller.select_recipes(cb, mapid_arg)
            selected_map_ids = set(
                [recipe.mapnumber.upper() for recipe in selected_recipes])

            # Check that no MapID were selected that shouldn't have been:
            self.assertTrue(selected_map_ids.isdisjoint(fail_list))

            # Check that all of the `selected_ids` match the `should_create` set:
            self.assertEqual(should_create_set, selected_map_ids)
    def __init__(self, cmf, extension, verify_on_creation=True):
        """
        Positional Arguments:
            * cmf: Either a CrashMoveFolder object or a path to a cmf_description.json file. If it
                   is a CrashMoveFolder object and cmf.verify_paths() returns False then an
                   ValueError exception will be rasied.
            * extension: The file extension of the layer_rendering file type. (typically `.lyr` for
                         ESRI Layer files, `'.qml` for QGIS style)

        Optional Named Arguments
            verify_on_creation: True by default. If True then
            `verify_match_with_layer_rendering_dir()` will be called from the constructor.

        """
        try:
            if cmf.verify_paths():
                self.cmf = cmf
            else:
                raise ValueError(
                    'The `cmf` parameter for LayerProperties.__init__() can only accept'
                    'values where the paths verify. eg `cmf.verify_paths() == True`.'
                    'The value passed in this case failed this test')
        except AttributeError:
            self.cmf = CrashMoveFolder(cmf, verify_on_creation=True)

        if len(extension) == 0 or extension.startswith('.'):
            self.extension = extension
        else:
            self.extension = '.{}'.format(extension)

        self.properties = {}  # Dictionary
        self._parse()

        if verify_on_creation:
            msg = self.get_difference_with_other_layer_set(
                self._get_lyr_rendering_names_as_set(),
                self._get_mismatch_with_layer_rendering_message)
            if msg:
                raise ValueError(msg)
Beispiel #27
0
    def test_load_recipe_with_layer_props_inc(self):
        # Test that a MapRecipe read form json with only a layername, combined with the
        # relevant LayerProperties is equal to a MapRecipe with all of the layerdetails embeded.

        # layerpros with
        cmf = CrashMoveFolder(
            os.path.join(self.parent_dir, 'example',
                         'cmf_description_relative_paths_test.json'))
        cmf.layer_properties = os.path.join(
            self.parent_dir, 'tests', 'testfiles', 'cookbooks',
            'fixture_layer_properties_for_atlas.json')
        test_lp = LayerProperties(cmf, ".lyr", verify_on_creation=False)

        # recipe with layer name only
        recipe1 = MapRecipe(fixtures.recipe_with_layer_name_only, test_lp)
        # combined recipe with layer props
        recipe2 = MapRecipe(fixtures.recipe_with_layer_details_embedded,
                            test_lp)

        self.assertEqual(recipe1, recipe2)

        # 2) with non-matching layer props schema
        recipe3 = MapRecipe(fixtures.recipe_with_positive_iso3_code, test_lp)
        self.assertNotEqual(recipe1, recipe3)
Beispiel #28
0
    def test_filter_lyr_for_use_in_frame_extent(self):
        # Have included the digit at the start of the string, so that can be sorted easily.
        cmf = CrashMoveFolder(
            os.path.join(self.parent_dir, 'example',
                         'cmf_description_relative_paths_test.json'))
        cmf.layer_properties = os.path.join(
            self.parent_dir, 'tests', 'testfiles', 'cookbooks',
            'fixture_layer_properties_for_atlas.json')
        test_lp = LayerProperties(cmf, ".lyr", verify_on_creation=False)

        # recipe with layer name only
        with open(
                os.path.join(
                    self.parent_dir, 'tests', 'testfiles',
                    'fixture_cookbook_1map_5layers_1frame.json')) as rf:
            cookbook_def = json.load(rf)
        # get the first (only) recipe in the cookbook
        recipe_def = cookbook_def['recipes'].pop()

        generic_lyr_def = json.loads('''{
            "name": "the_name",
            "reg_exp": "^wrl_admn_ad0_py_(.*?)_(.*?)_([phm][phm])(.+)shp$",
            "schema_definition": "admin1_reference.yml",
            "definition_query": "",
            "display": true,
            "add_to_legend": true,
            "label_classes": []
        }''')

        # Case 1
        # test white list
        test_white_list1 = [('1a', True), ('1b', False), ('1c', None),
                            ('1d', True), ('1e', False)]
        expected_white_result1 = ['1a', '1d']

        test_white_list2 = [('2a', True), ('2b', None), ('2c', None),
                            ('2d', True), ('2e', None)]
        expected_white_result2 = ['2a', '2d']

        # Case 2
        # Black List
        test_black_list = [('3a', None), ('3b', False), ('3c', None),
                           ('3d', None), ('3e', False)]
        expected_black_result = ['3a', '3c', '3d']

        # Case 3
        # Default
        test_default_list = [('4a', None), ('4b', None), ('4c', None),
                             ('4d', None), ('4e', None)]
        expected_default_result = ['4a', '4b', '4c', '4d', '4e']

        all_test_params = [(test_white_list1, expected_white_result1),
                           (test_white_list2, expected_white_result2),
                           (test_black_list, expected_black_result),
                           (test_default_list, expected_default_result)]

        for test_list, expected_result in all_test_params:
            test_recipe = MapRecipe(recipe_def, test_lp)

            # Build up a mock list of layer to test
            replacement_lyrs = []
            for test_lyr_details in test_list:
                new_lyr = RecipeLayer(generic_lyr_def,
                                      test_lp,
                                      verify_on_creation=False)
                new_lyr.name = test_lyr_details[0]
                new_lyr.use_for_frame_extent = test_lyr_details[1]
                # vaugely near Lebanon
                new_lyr.extent = (35, 33, 36, 34)
                new_lyr.crs = 'epsg:4326'

                replacement_lyrs.append(new_lyr)

            test_frame = test_recipe.map_frames.pop()
            test_frame.layers = replacement_lyrs
            result_lyrs = test_frame._filter_lyr_for_use_in_frame_extent()
            actual_result = [lyr.name for lyr in result_lyrs]
            self.assertEqual(actual_result, expected_result)
Beispiel #29
0
    def test_get_map_frame_extents(self):
        cmf = CrashMoveFolder(
            os.path.join(self.parent_dir, 'example',
                         'cmf_description_relative_paths_test.json'))
        cmf.layer_properties = os.path.join(
            self.parent_dir, 'tests', 'testfiles', 'cookbooks',
            'fixture_layer_properties_for_atlas.json')
        test_lp = LayerProperties(cmf, ".lyr", verify_on_creation=False)

        # recipe with layer name only
        with open(
                os.path.join(
                    self.parent_dir, 'tests', 'testfiles',
                    'fixture_cookbook_1map_5layers_1frame.json')) as rf:
            cookbook_def = json.load(rf)
        # get the first (only) recipe in the cookbook
        recipe_def = cookbook_def['recipes'].pop()

        generic_lyr_def = json.loads('''{
            "name": "the_name",
            "reg_exp": "^wrl_admn_ad0_py_(.*?)_(.*?)_([phm][phm])(.+)shp$",
            "schema_definition": "admin1_reference.yml",
            "definition_query": "",
            "display": true,
            "add_to_legend": true,
            "label_classes": []
        }''')

        # Case 1
        # One or more layers does not have it's extent defined
        case1_list = [('case1_lyrA', (35, 33, 36, 34), 'epsg:4326'),
                      ('case1_lyrB', None, None)]
        case1_result = (35, 33, 36, 34)

        # Case 2
        # Simple union with two lyrs of same crs
        case2_list = [('case2_lyrA', (33, 51, 36, 58), 'epsg:4326'),
                      ('case2_lyrB', (15, 52, 35, 55), 'epsg:4326')]
        case2_result = (15, 51, 36, 58)

        # Case 3
        # Union with two lyrs of with different crs
        # 'epsg:4326'== WGS1984, 'epsg:3785' == Web Mercator
        case3_list = [('case3_lyrA', (33, 51, 36, 58), 'epsg:4326'),
                      ('case3_lyrB', (1669792.36, 6800125.45, 3896182.18,
                                      7361866.11), 'epsg:3785')]
        case3_result = (15, 51, 36, 58)

        # Case 4
        # One layer which stradles 180 degree meridian

        all_test_params = [(case1_list, case1_result),
                           (case2_list, case2_result),
                           (case3_list, case3_result)]

        for test_list, expected_result in all_test_params:
            test_recipe = MapRecipe(recipe_def, test_lp)

            # Build up a mock list of layer to test
            replacement_lyrs = []
            for name, extent, crs in test_list:
                new_lyr = RecipeLayer(generic_lyr_def,
                                      test_lp,
                                      verify_on_creation=False)
                new_lyr.name = name
                new_lyr.use_for_frame_extent = bool(extent)
                # vaugely near Lebanon
                new_lyr.extent = extent
                new_lyr.crs = crs

                replacement_lyrs.append(new_lyr)

            # print('test_get_map_frame_extents')
            # print('expected_result = {}'.format(expected_result))
            test_frame = test_recipe.map_frames.pop()
            test_frame.crs = 'epsg:4326'
            test_frame.layers = replacement_lyrs
            test_frame.calc_extent(state=test_recipe)
            actual_result = test_frame.extent
            # print('actual_result = {}'.format(actual_result))
            for actual, expected in zip(actual_result, expected_result):
                self.assertAlmostEqual(actual, expected)
    def test_verify_with_rendering_files(self):
        # self.fail()

        # load a valid CMF
        test_cmf = CrashMoveFolder(self.path_to_valid_cmf_des)
        # Overwright the Layer Properties file path with
        # mapactionpy_controller\tests\testfiles\fixture_layer_properties_four_layers.json
        test_cmf.layer_properties = os.path.join(
            self.parent_dir, 'tests', 'testfiles',
            'fixture_layer_properties_four_layers.json')

        layer_rendering_test_root = os.path.join(self.parent_dir, 'tests',
                                                 'testfiles',
                                                 'test_layer_rendering')
        test_cmf.layer_rendering

        # 1) Exact match of .lyr files and layer properties
        test_cmf.layer_rendering = os.path.join(layer_rendering_test_root,
                                                'four_files_exact_match')
        lyr_lp = LayerProperties(test_cmf, '.lyr', verify_on_creation=True)
        self.assertFalse(lyr_lp.is_difference_with_layer_rendering_dir())

        qml_lp = LayerProperties(test_cmf, '.qml', verify_on_creation=True)
        self.assertFalse(qml_lp.is_difference_with_layer_rendering_dir())

        # 2) .lyr files which don't have layer properties entries
        test_cmf.layer_rendering = os.path.join(layer_rendering_test_root,
                                                'five_files')
        self.assertRaises(ValueError, LayerProperties, test_cmf, '.lyr')
        self.assertRaises(ValueError, LayerProperties, test_cmf, '.qml')

        # 3) layer properties entries which don't have cooresponding .lyr files.
        test_cmf.layer_rendering = os.path.join(layer_rendering_test_root,
                                                'three_files')
        self.assertRaises(ValueError, LayerProperties, test_cmf, '.lyr')
        self.assertRaises(ValueError, LayerProperties, test_cmf, '.qml')

        # 4) Both 2 & 3 combined
        test_cmf.layer_rendering = os.path.join(layer_rendering_test_root,
                                                'four_files_mis_match')
        self.assertRaises(ValueError, LayerProperties, test_cmf, '.lyr')
        self.assertRaises(ValueError, LayerProperties, test_cmf, '.qml')

        # 6) Overrided validation checks in constructor
        test_cmf.layer_rendering = os.path.join(layer_rendering_test_root,
                                                'four_files_mis_match')
        long_lived_lp = LayerProperties(test_cmf,
                                        '.lyr',
                                        verify_on_creation=False)
        self.assertTrue(long_lived_lp.is_difference_with_layer_rendering_dir())
        long_lived_lp.cmf.layer_rendering = os.path.join(
            layer_rendering_test_root, 'four_files_exact_match')
        self.assertFalse(
            long_lived_lp.is_difference_with_layer_rendering_dir())

        # 7) Same results whether or not the '.' character is included in the file extension
        test_cmf.layer_rendering = os.path.join(layer_rendering_test_root,
                                                'four_files_exact_match')
        # passing example with dot
        lyr_lp = LayerProperties(test_cmf, '.lyr', verify_on_creation=True)
        self.assertFalse(lyr_lp.is_difference_with_layer_rendering_dir())
        # passing example without dot
        lyr_lp = LayerProperties(test_cmf, 'lyr', verify_on_creation=True)
        self.assertFalse(lyr_lp.is_difference_with_layer_rendering_dir())
        # failing example with dot
        test_cmf.layer_rendering = os.path.join(layer_rendering_test_root,
                                                'four_files_mis_match')
        self.assertRaises(ValueError, LayerProperties, test_cmf, '.lyr')
        # failing example without dot
        test_cmf.layer_rendering = os.path.join(layer_rendering_test_root,
                                                'four_files_mis_match')
        self.assertRaises(ValueError, LayerProperties, test_cmf, 'lyr')