Пример #1
0
    def test_validation_schema_xml(self):
        test_schema = self.make_simple_schema()

        x = StoredConfig(self.good_data_filename, schema_file=test_schema.name)
        is_valid = x.validate()

        os.remove(test_schema.name)

        self.assertTrue(is_valid, 'Configuration schema should be valid.')
Пример #2
0
    def test_validation(self):
        x = StoredConfig(
            self.good_data_filename, schema_file=self.good_schema_filename)

        is_valid = x.validate()

        self.assertTrue(
            is_valid,
            'Configuration schema and data should be valid and should validate'
            + ' successfully.')
Пример #3
0
    def test_validation_schema_xml_wellformed(self):
        test_schema = tempfile.NamedTemporaryFile(delete=False)
        test_schema.write('<?xml version="1.0" encoding="utf-8"?>\n')
        test_schema.write('<DistrictBuilder><junk/></DistrictBuilder>\n')
        test_schema.close()

        x = StoredConfig(self.good_data_filename, schema_file=test_schema.name)
        is_valid = x.validate()

        os.remove(test_schema.name)

        self.assertFalse(is_valid, 'Configuration schema was not valid.')
Пример #4
0
    def test_validation_data_xml_parser(self):
        test_schema = self.make_simple_schema()

        test_data = tempfile.NamedTemporaryFile(delete=False)
        test_data.write('<?xml version="1.0" encoding="utf-8"?>\n')
        test_data.write('<DistrictBuilder><junk></DistrictBuilder>\n')
        test_data.close()

        x = StoredConfig(test_data.name, schema_file=test_schema.name)
        is_valid = x.validate()

        os.remove(test_data.name)
        os.remove(test_schema.name)

        self.assertFalse(is_valid, 'Configuration data was not valid.')
Пример #5
0
    def test_construct_okay(self):
        x = StoredConfig(
            self.good_data_filename, schema_file=self.good_schema_filename)

        self.assertEqual(x.datafile, self.good_data_filename,
                         'Configuration data is not correct.')
        self.assertEqual(x.schema_file, self.good_schema_filename,
                         'Configuration schema is not correct.')
Пример #6
0
 def test_construct_missing_data(self):
     try:
         x = StoredConfig(
             self.bad_data_filename, schema_file=self.good_schema_filename)
         self.fail('Expected failure when passing nonexistant filenames to '
                   + 'constructor.')
     except:
         pass
 def setUp(self):
     self.store = StoredConfig('./config/config.dist.xml')
     self.store.validate()
     self.cmd = Command()
class RegionConfigTest(BaseTestCase):
    """
    Test the configuration options
    """

    fixtures = []

    def setUp(self):
        self.store = StoredConfig('./config/config.dist.xml')
        self.store.validate()
        self.cmd = Command()

    def test_get_largest_geolevel(self):
        region1 = self.store.filter_regions()[0]
        region2 = self.store.filter_regions()[1]

        def get_largest_geolevel(region_node):
            def get_youngest_child(node):
                if len(node) == 0:
                    return node
                else:
                    return get_youngest_child(node[0])

            geolevels = self.store.get_top_regional_geolevel(region_node)
            if len(geolevels) > 0:
                return get_youngest_child(geolevels[0]).get('ref')

        bg1 = get_largest_geolevel(region1)
        bg2 = get_largest_geolevel(region2)

        self.assertEqual('county', bg1, "Didn't get correct geolevel")
        self.assertEqual('vtd', bg2, "Didn't get correct geolevel")

    def test_get_or_create_regional_geolevels(self):
        self.cmd.import_prereq(ConfigImporter(self.store), False)

        expected_geolevels = [
            'county', 'vtd', 'block', 'va_county', 'va_vtd', 'va_block',
            'dc_vtd', 'dc_block'
        ]
        all_geolevels = Geolevel.objects.all()

        self.assertEqual(len(expected_geolevels), len(all_geolevels),
                         "Didn't return correct number of geolevels")
        for geolevel in expected_geolevels:
            self.assertTrue(
                reduce(lambda x, y: x or y.name == geolevel, all_geolevels,
                       False), "Regional geolevel %s not created" % geolevel)

    def test_filter_functions(self):
        function_dict = self.cmd.create_filter_functions(self.store)
        self.assertEqual(2, len(function_dict), 'No filter functions found')

        shapefile = 'redistricting/testdata/test_data.shp'
        test_layer = DataSource(shapefile)[0]

        va = dc = 0
        for feat in test_layer:
            geolevels = []
            for key, value in function_dict.iteritems():
                for filter_function in value:
                    if filter_function(feat):
                        geolevels.append(key)
                        break

            if len(geolevels) == 2:
                self.assertTrue(
                    'dc' in geolevels,
                    "Regional feature not found in unfiltered geolevel")
                self.assertTrue(
                    'va' in geolevels,
                    "Unfiltered feature not found in unfiltered geolevel")
                va += 1
                dc += 1
            elif len(geolevels) == 1:
                self.assertTrue(
                    'va' in geolevels,
                    "Regional feature not found in unfiltered geolevel")
                va += 1
            else:
                self.fail(
                    "Incorrect number of geolevels returned - should never get"
                    + " here")
        self.assertEqual(1, dc,
                         "Incorrect number of geolevels found for region")
        self.assertEqual(
            3, va, "Incorrect number of geolevels found for unfiltered region")

    def tearDown(self):
        self.region_tree = None
Пример #9
0
    def handle(self, *args, **options):
        """
        Perform the command.
        """
        self.setup_logging(int(options.get('verbosity')))
        force = options.get('force')

        translation.activate('en')

        if options.get('config') is None:
            logger.warning("""
ERROR:

    This management command requires the -c or --config option. This option
    specifies the main configuration file.
""")
            sys.exit(1)

        try:
            store = StoredConfig(options.get('config'))
        except Exception:
            logger.info("""
ERROR:

The configuration file specified could not be parsed. Please check the
contents of the file and try again.
""")
            # Indicate that an error has occurred
            sys.exit(1)

        if not store.validate():
            logger.info("""
ERROR:

The configuration file was not valid. Please check the contents of the
file and try again.
""")
            # Indicate that an error has occurred
            sys.exit(1)

        # Create an importer for use in importing the config objects
        config = ConfigImporter(store)

        # When the setup script is run, it re-computes the secret key
        # used to secure session data. Blow away any old sessions that
        # were in the DB.
        success = Utils.purge_sessions()

        if not success:
            sys.exit(1)

        # When configuring, we want to keep track of any failures so we can
        # return the correct exit code.  Since False evaluates to a 0, we can
        # multiply values by all_ok so that a single False value means all_ok
        # will remain false
        all_ok = True

        success = config.import_superuser(force)

        all_ok = success
        try:
            all_ok = all_ok * self.import_prereq(config, force)
        except:
            all_ok = False
            logger.info('Error importing configuration.')
            logger.info(traceback.format_exc())

        # Create the utilities for spatial operations (renesting and geoserver)
        geoutil = SpatialUtils(store)

        try:
            updatefield = None
            if options.get("updatefield") is not None:
                updatefield = options.get("updatefield")[0]
            optlevels = options.get("geolevels")
            nestlevels = options.get("nesting")

            if (not optlevels is None) or (not nestlevels is None):
                # Begin the import process
                geolevels = store.filter_geolevels()

                for i, geolevel in enumerate(geolevels):
                    if optlevels is not None:
                        importme = len(optlevels) == 0 or (i in optlevels)
                        if importme:
                            self.import_geolevel(store, geolevel, updatefield)

                    if nestlevels is not None:
                        nestme = len(nestlevels) == 0
                        nestme = nestme or (i in nestlevels)
                        if nestme:
                            geoutil.renest_geolevel(geolevel)
        except:
            all_ok = False
            logger.info('ERROR importing geolevels.')
            logger.info(traceback.format_exc())

        # Do this once after processing the geolevels
        config.import_contiguity_overrides()

        # Save any changes to the config locale files
        config.save()

        if options.get("views"):
            # Create views based on the subjects and geolevels
            try:
                configure_views()
            except:
                logger.info(traceback.format_exc())
                all_ok = False

        if options.get("geoserver"):
            try:
                all_ok = all_ok * geoutil.purge_geoserver()
                if all_ok:
                    logger.info("Geoserver configuration cleaned.")
                    all_ok = all_ok * geoutil.configure_geoserver()
                else:
                    logger.info(
                        "Geoserver configuration could not be cleaned.")
            except:
                logger.info('ERROR configuring geoserver.')
                logger.info(traceback.format_exc())
                all_ok = False

        if options.get("templates"):
            try:
                self.create_template(store.data)
            except:
                logger.info('ERROR creating templates.')
                logger.info(traceback.format_exc())
                all_ok = False

        if options.get("static"):
            call_command('collectstatic',
                         interactive=False,
                         verbosity=options.get('verbosity'))
            call_command('compress',
                         interactive=False,
                         verbosity=options.get('verbosity'),
                         force=True)

        if options.get("languages"):
            call_command('makelanguagefiles',
                         interactive=False,
                         verbosity=options.get('verbosity'))

        if options.get("adjacency"):
            self.import_adjacency(store.data)

        # For our return value, a 0 (False) means OK, any nonzero (i.e., True or 1)
        # means that  an error occurred - the opposite of the meaning of all_ok's bool
        sys.exit(not all_ok)