Ejemplo n.º 1
0
def feed_validation(options):
    """Validate the input feed depending on the user parameters."""
    rule_options = {}
    gpunit_rules.GpUnitOcdIdValidator.init_ocd_id_list(options.c,
                                                       options.ocdid_file,
                                                       not options.g)
    if options.required_languages:
        rule_options.setdefault("AllLanguages", []).append(
            base.RuleOption("required_languages",
                            str.split(options.required_languages, ",")))
    rule_classes_to_check = filter_all_rules_using_user_arg(options)

    errors = 0
    for election_file in options.election_files:
        print("\n--------- Results after validating file: {0} ".format(
            election_file.name))

        print_metadata(election_file)
        registry = base.RulesRegistry(
            election_file=election_file,
            schema_file=options.xsd,
            rule_classes_to_check=rule_classes_to_check,
            rule_options=rule_options)
        registry.check_rules()
        registry.print_exceptions(options.severity, options.verbose)
        if options.verbose:
            registry.count_stats()
        errors = max(errors,
                     compute_max_found_severity(registry.exceptions_wrapper))
    return errors
Ejemplo n.º 2
0
    def _TestFile(self,
                  filename,
                  rules_to_check,
                  expected_errors=0,
                  expected_warnings=0):
        sample_file = os.path.join(
            FLAGS.test_srcdir, 'google3/third_party/py/civics_cdf_validator/'
            'samples/' + filename)
        schema_file = os.path.join(
            FLAGS.test_srcdir, 'google3/third_party/py/civics_cdf_validator/'
            'civics_cdf_spec.xsd')
        registry = base.RulesRegistry(election_file=sample_file,
                                      schema_file=schema_file,
                                      rule_options={},
                                      rule_classes_to_check=rules_to_check)

        registry.check_rules()
        registry.print_exceptions(0, True)
        self.assertEqual(
            expected_errors,
            registry.exceptions_wrapper.count_logs_with_exception_type(
                loggers.ElectionError))
        self.assertEqual(
            expected_warnings,
            registry.exceptions_wrapper.count_logs_with_exception_type(
                loggers.ElectionWarning))
def feed_validation(options):
    """Validate the input feed depending on the user parameters."""
    rule_options = {}
    if options.g:
        rule_options.setdefault("ElectoralDistrictOcdId", []).append(
            base.RuleOption("check_github", False))
        rule_options.setdefault("GpUnitOcdId", []).append(
            base.RuleOption("check_github", False))
    if options.c:
        rule_options.setdefault("ElectoralDistrictOcdId", []).append(
            base.RuleOption("country_code", options.c))
        rule_options.setdefault("GpUnitOcdId", []).append(
            base.RuleOption("country_code", options.c))
    if options.ocdid_file:
        rule_options.setdefault("ElectoralDistrictOcdId", []).append(
            base.RuleOption("local_file", options.ocdid_file))
        rule_options.setdefault("GpUnitOcdId", []).append(
            base.RuleOption("local_file", options.ocdid_file))
    if options.required_languages:
        rule_options.setdefault("AllLanguages", []).append(
            base.RuleOption("required_languages",
                            str.split(options.required_languages, ",")))
    rule_classes_to_check = filter_all_rules_using_user_arg(options)

    errors = []
    for election_file in options.election_files:
        print("\n--------- Results after validating file: {0} ".format(
            election_file.name))

        print_metadata(election_file)
        registry = base.RulesRegistry(
            election_file=election_file,
            schema_file=options.xsd,
            rule_classes_to_check=rule_classes_to_check,
            rule_options=rule_options)
        registry.check_rules()
        registry.print_exceptions(options.severity, options.verbose)
        if options.verbose:
            registry.count_stats()
        if registry.exception_counts[loggers.ElectionError]:
            errors.append(3)
        elif registry.exception_counts[loggers.ElectionWarning]:
            errors.append(2)
        elif registry.exception_counts[loggers.ElectionInfo]:
            errors.append(1)
        else:
            errors.append(0)
    return max(errors)
Ejemplo n.º 4
0
 def setUp(self):
   super(RulesRegistryTest, self).setUp()
   self.registry = base.RulesRegistry("test.xml", "schema.xsd", [], [])
   root_string = """
     <ElectionReport>
       <PartyCollection>
         <Party objectId="par0001">
           <Name>
             <Text language="en">Republican</Text>
           </Name>
           <Color>e30413</Color>
         </Party>
         <Party objectId="par0002">
           <Name>
             <Text language="en">Democratic</Text>
           </Name>
         </Party>
       </PartyCollection>
       <PersonCollection>
         <Person objectId="p1">
           <PartyId>par0001</PartyId>
         </Person>
         <Person objectId="p2" />
         <Person objectId="p3" />
       </PersonCollection>
       <CandidateCollection>
         <Candidate>
           <PartyId>par0003</PartyId>
         </Candidate>
       </CandidateCollection>
       <OfficeCollection>
         <Office><OfficeHolderPersonIds>p1 p2</OfficeHolderPersonIds></Office>
         <Office><OfficeHolderPersonIds>p3</OfficeHolderPersonIds></Office>
       </OfficeCollection>
       <GpUnitCollection>
         <GpUnit objectId="ru0002">
           <ComposingGpUnitIds>abc123</ComposingGpUnitIds>
           <Name>Virginia</Name>
           <Type>state</Type>
         </GpUnit>
         <GpUnit objectId="ru0003">
           <ComposingGpUnitIds></ComposingGpUnitIds>
           <Name>Massachusetts</Name>
           <Type>state</Type>
         </GpUnit>
         <GpUnit>
           <ComposingGpUnitIds>xyz987</ComposingGpUnitIds>
           <Name>New York</Name>
           <Type>state</Type>
         </GpUnit>
       </GpUnitCollection>
       <ContestCollection>
         <Contest objectId="cc11111">
          <Name>Test</Name>
         </Contest>
         <Contest objectId="cc22222">
           <Name>Test1</Name>
         </Contest>
         <Contest objectId="cc33333">
           <Name>Test2</Name>
           <BallotSelection objectId="cs-br1-alckmin">
             <VoteCountsCollection>
               <VoteCounts><Type>total</Type><Count>0</Count></VoteCounts>
             </VoteCountsCollection>
           </BallotSelection>
         </Contest>
       </ContestCollection>
     </ElectionReport>
   """
   self.registry.election_tree = etree.fromstring(root_string)