unit testing xsd

janmc

New Member
I have been tasked with finding out why a program passes an XML unit test when the attributes in the xsd portion do not match the values in the unit test. The problem is I know next to nothing about XML or xsd. I have read up on it in various places but nohing has helped me understand why the test is failing.It seems that before I can figure out why it's not working, I need to understand what is happening in the method doing the unit testing. I think I grasp most of it, but there is a portion that I'm not sure I understand. This portion also deals with the xsd.Here is my xsd:\[code\]public static class Constants{ public const string Xsd = @"<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema""> <xs:element name=""Datas""> <xs:complexType> <xs:sequence> <xs:element name=""Data""> <xs:attributeGroup ref=""allocData""/> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:attributeGroup id=""allocData""> <xs:attribute name=""Site"" type=""string""/> <xs:attribute name=""MonthNum"" type=""string""/> <xs:attribute name=""Department"" type=""string""/> <xs:attribute name=""Numerator"" type=""float""/> <xs:attribute name=""Data_Indicator"" type=""string""/> <xs:attribute name=""Industry_Benchmark"" type=""int""/> <xs:attribute name=""OSHA_Benchmark"" type=""int""/> <!--xs:attribute name=""Comments"" type=""string""/--> <xs:attribute name=""Executive_Comments"" type=""string""/> <xs:attribute name=""Fleet_Executive_Comments"" type=""string""/> </xs:attributeGroup> </xs:schema>";\[/code\]and here is the method for my unit test:\[code\]public void XMLTest(){ List<DataResults> ListOfResults = new List<DataResults>(); DataResults r1 = new DataResults(); DataResults r2 = new DataResults(); HarvesterHandle testData = http://stackoverflow.com/questions/11248131/new HarvesterHandle(); r1.Site ="Springfield Site"; r1.Department = "Freak"; r1.MonthNum = "8"; r1.Numerator = 65807; r1.Data_Indicator = "A12"; // r1.Comments = "Hello, World"; r1.Industry_Benchmark = 23; r1.OSHA_Benchmark = 2; r1.ExecutiveComments = "It costs HOW MUCH?"; r1.FleetExecComments = "No you can't have a new car"; r2.Site = "Joplin Site"; r2.Department = "Rock"; r2.MonthNum = "12"; r2.Numerator = 65625; r2.Data_Indicator = "Ou812"; r2.Industry_Benchmark = 523; r2.OSHA_Benchmark = 2456; // r2.Comments = "GoodBye, World!"; r2.ExecutiveComments = "Cut it!"; r2.FleetExecComments = "It only has 250,000 miles"; ListOfResults.Add(r1); ListOfResults.Add(r2); var test = testData.XMLTest(ListOfResults); var xmlDoc = XDocument.Parse(test.ToString()); XmlSchemaSet set = new XmlSchemaSet(); set.Add(XmlSchema.Read(XElement.Parse(Constants.Xsd).CreateReader(), (o, e) => { })); bool valid = true; xmlDoc.Validate(set, (o, e) => { valid = false; }); Assert.IsTrue(valid); var element = test.Descendants("Data").FirstOrDefault(x => x.Attribute("Site").Value.Equals(r1.Site)); Assert.IsNotNull(element); Assert.AreEqual(r1.Site, element.Attribute("Site").Value); Assert.AreEqual(r1.Department, element.Attribute("Department").Value); Assert.AreEqual(r1.MonthNum.ToString(), element.Attribute("MonthNum").Value); Assert.AreEqual(r1.Numerator.ToString(), element.Attribute("Numerator").Value); Assert.AreEqual(r1.Data_Indicator, element.Attribute("Data_Indicator").Value); Assert.AreEqual(r1.Industry_Benchmark.ToString(), element.Attribute("Industry_Benchmark").Value); Assert.AreEqual(r1.OSHA_Benchmark.ToString(), element.Attribute("OSHA_Benchmark").Value); // Assert.AreEqual(r1.Comments.ToString(), element.Attribute( // "Comments").Value); Assert.AreEqual(r1.ExecutiveComments.ToString(), element.Attribute("Executive_Comments").Value); Assert.AreEqual(r1.FleetExecComments.ToString(), element.Attribute("Fleet_Executive_Comments").Value); element = test.Descendants("Data").FirstOrDefault(x => x.Attribute("Site").Value.Equals(r2.Site)); Assert.IsNotNull(element); Assert.AreEqual(r2.Site, element.Attribute("Site").Value); Assert.AreEqual(r2.Department, element.Attribute("Department").Value); Assert.AreEqual(r2.MonthNum.ToString(), element.Attribute("MonthNum").Value); Assert.AreEqual(r2.Numerator.ToString(), element.Attribute("Numerator").Value); Assert.AreEqual(r2.Data_Indicator, element.Attribute("Data_Indicator").Value); Assert.AreEqual(r2.Industry_Benchmark.ToString(), element.Attribute("Industry_Benchmark").Value); Assert.AreEqual(r2.OSHA_Benchmark.ToString(), element.Attribute("OSHA_Benchmark").Value); // Assert.AreEqual(r2.Comments.ToString(), element.Attribute( // "Comments").Value); Assert.AreEqual(r2.ExecutiveComments.ToString(), element.Attribute(Executive_Comments").Value); Assert.AreEqual(r2.FleetExecComments.ToString(), element.Attribute("Fleet_Executive_Comments").Value); }\[/code\]the part that appear to be dealing with the xsd is this:\[code\]XmlSchemaSet set = new XmlSchemaSet();set.Add(XmlSchema.Read(XElement.Parse(Constants.Xsd).CreateReader(), (o, e) => { })); bool valid = true;xmlDoc.Validate(set, (o, e) =>{ valid = false;}); Assert.IsTrue(valid);\[/code\]The problem is if the attribute data types in the xsd do not match the actual data, the test still passes. For instance, if any of the string attributes (MonthNum, Site, etc) are changed to float, the test still passes. Also, if the .xsd element names Datas and Data are changed to something like Fred and Ethyl, the test still passes.I am assuming that whatever the xsd portion of the code in the xml unit test method is doing, it is not checking for correct datatypes or element namesper request, here is my xml:\[code\] public const string XSLT = @"<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""> <xsl:output method=""text"" encoding=""us-ascii"" /> <xsl:template match=""/"">Site, MonthNum, Department, Numerator, Denominator, Data_Indicator, Industry_Benchmark, OSHA_Benchmark, <!--Comments--> Executive_Comments, Fleet_Executive_Comments, <xsl:for-each select=""Datas/Data""> <xsl:text>'</xsl:text> <xsl:value-of select=""@site""/> <xsl:text>','</xsl:text> <xsl:value-of select=""@monthNum""/> <xsl:text>','</xsl:text> <xsl:value-of select=""@Department""/> <xsl:text>','</xsl:text> <xsl:value-of select=""@numerator""/> <xsl:text>','</xsl:text>, <xsl:value-of select=""Industry_Benchmark""/> <xsl:text>'</xsl:text>, <xsl:value-of select=""OSHA_Benchmark""/> <xsl:text>'</xsl:text>, <!--xsl:value-of select=""Comments""/--> <!--xsl:text>'</xsl:text-->, <xsl:value-of select=""Executive_Comments""/> <xsl:text>'</xsl:text>, <xsl:value-of select=""Fleet_Executive_Comments""/> <xsl:text>'</xsl:text>, </xsl:for-each> </xsl:template></xsl:stylesheet>";}\[/code\]}edit: This is the XML generated when running the test:\[code\]<Data> <Data Site="Springfield Site" MonthNum="8" Department="Freak" Numerator="65807" Data_Indicator="A12" Industry_Benchmark="23" OSHA_Benchmark="2" Executive_Comments="It costs HOW MUCH?" Fleet_Executive_Comments="No you can't have a new car" /> <Data Site="Joplin Site" MonthNum="12" Department="Rock" Numerator="65625" Data_Indicator="Ou812" Industry_Benchmark="523" OSHA_Benchmark="2456" Executive_Comments="Cut it!" Fleet_Executive_Comments="It only has 250,000 miles" /></Data>\[/code\]
 
Back
Top