Commit c46cabd6 authored by Mark Canterbury's avatar Mark Canterbury
Browse files

Updating tests to include validation of instance documents

parent 9ea2ce54
Loading
Loading
Loading
Loading
+27 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://FooServiceSchema.example.com/schema/v1.1.1/"
    elementFormDefault="qualified"
    xmlns="http://FooServiceSchema.example.com/schema/v1.1.1/"
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>

  <xs:element name="FooItem" type="FooItem"></xs:element>

  <xs:complexType name="FooItem">
    <xs:sequence>
      <xs:element name="item1" type="ItemType"></xs:element>
      <xs:element name="item2" type="ItemType"></xs:element>
      <xs:element name="item3" type="ItemType"></xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:simpleType name="ItemType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Foo"></xs:enumeration>
      <xs:enumeration value="Bar"></xs:enumeration>
      <xs:enumeration value="Baz"></xs:enumeration>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>
+20 −5
Original line number Original line Diff line number Diff line
@@ -92,6 +92,7 @@ if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser = argparse.ArgumentParser()
    parser.add_argument("-v", "--verbosity", help="verbosity level", action="count", default=0)
    parser.add_argument("-v", "--verbosity", help="verbosity level", action="count", default=0)
    parser.add_argument("input", help="include a directory or file", action="append", nargs="+")
    parser.add_argument("input", help="include a directory or file", action="append", nargs="+")
    parser.add_argument("-p", "--primaryNamespace", help="Primary schema namespace for instance doc validation")
    args = parser.parse_args()
    args = parser.parse_args()


    logging.getLogger().setLevel(logging.WARNING)
    logging.getLogger().setLevel(logging.WARNING)
@@ -101,6 +102,7 @@ if __name__ == '__main__':
        logging.getLogger().setLevel(logging.DEBUG)
        logging.getLogger().setLevel(logging.DEBUG)
        logging.debug("Very verbose selected")
        logging.debug("Very verbose selected")



    logging.debug(f"Path: {args.input}")
    logging.debug(f"Path: {args.input}")
    includeFileList = []
    includeFileList = []
    includeInstanceDocList = []
    includeInstanceDocList = []
@@ -118,14 +120,18 @@ if __name__ == '__main__':
                logging.info(f">Including instance doc {g}")
                logging.info(f">Including instance doc {g}")
                includeInstanceDocList.append(g)
                includeInstanceDocList.append(g)
        else:
        else:
            logging.info(f"Including {p.absolute()}")
            logging.info(f">Including {p.absolute()}")
            if str(p.absolute()).endswith('.xml'):
            if str(p.absolute()).endswith('.xml'):
                includeInstanceDocList.append(p.absolute())
                includeInstanceDocList.append(str(p.absolute()))
            elif str(p.absolute()).endswith('.xml'):
            elif str(p.absolute()).endswith('.xsd'):
                includeFileList.append(p.absolute())
                includeFileList.append(str(p.absolute()))
            else:
            else:
                logging.warning(f'Ignoring file {p.absolute()}')
                logging.warning(f'Ignoring file {p.absolute()}')
    
    
    if len(includeInstanceDocList) and (args.primaryNamespace is None):
        print("Cannot validate instance documents without specifying a primary namespace (use -h for usage guidelines)")
        exit(-1)

    syntaxErrors = 0
    syntaxErrors = 0
    
    
    print ("XSD syntax checks:")
    print ("XSD syntax checks:")
@@ -169,6 +175,15 @@ if __name__ == '__main__':
    print ("-----------------------------")
    print ("-----------------------------")
    errorCount = 0
    errorCount = 0


    primarySchema = schemaDict[args.primaryNamespace]
    for instanceDoc in includeInstanceDocList:
    for instanceDoc in includeInstanceDocList:
        print (f"  {instanceDoc:}")
        try:
            results = primarySchema.validate(instanceDoc)
            print (f"  {instanceDoc} : OK")
        except Exception as ex:
            errorCount += 1
            print (f"  {instanceDoc} : {str(ex)}")
    print (f"{errorCount} instance doc errors detected")
    exit(errorCount)