Skip to content

Commit

Permalink
Improve data types (#58)
Browse files Browse the repository at this point in the history
* Change `PageCount` element type to nonNegativeInteger
* Change Series `Volume` sub-element type to nonNegativeInteger
  • Loading branch information
bpepple authored Nov 14, 2024
1 parent 01150be commit 5f624ed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
4 changes: 2 additions & 2 deletions drafts/v1.0/MetronInfo.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<xs:element name="Prices" type="pricesType" minOccurs="0" />
<xs:element name="CoverDate" type="xs:date" minOccurs="0" />
<xs:element name="StoreDate" type="xs:date" minOccurs="0" />
<xs:element name="PageCount" type="xs:int" minOccurs="0" default="0" />
<xs:element name="PageCount" type="xs:nonNegativeInteger" minOccurs="0" default="0" />
<xs:element name="Notes" type="xs:string" minOccurs="0" />
<xs:element name="Genres" type="genresType" minOccurs="0" />
<xs:element name="Tags" type="tagsType" minOccurs="0" />
Expand Down Expand Up @@ -92,7 +92,7 @@
<xs:all>
<xs:element name="Name" type="xs:string" />
<xs:element name="SortName" type="xs:string" minOccurs="0" />
<xs:element name="Volume" type="xs:int" minOccurs="0" />
<xs:element name="Volume" type="xs:nonNegativeInteger" minOccurs="0" />
<xs:element name="Format" type="formatType" minOccurs="0" />
<xs:element name="StartYear" type="xs:gYear" minOccurs="0" />
<xs:element name="IssueCount" type="xs:positiveInteger" minOccurs="0" />
Expand Down
36 changes: 31 additions & 5 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,45 @@


@pytest.mark.parametrize(
("xsd", "xml"), [(TEST_XSD, TEST_FILES_PATH / "valid.xml")], ids=["valid_xml"]
("xsd", "xml"),
[
(TEST_XSD, TEST_FILES_PATH / "valid.xml"),
(
TEST_XSD,
'<?xml version="1.0" encoding="UTF-8"?><MetronInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
"<Series><Name>Foo</Name></Series><Number /><PageCount>0</PageCount></MetronInfo>",
),
(
TEST_XSD,
'<?xml version="1.0" encoding="UTF-8"?><MetronInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
"<Series><Name>Foo</Name><Volume>0</Volume></Series><Number /></MetronInfo>",
),
],
ids=["valid_xml", "zero_page_count", "volume_zero"],
)
def test_valid(xsd, xml) -> None:
def test_valid(xsd: Path, xml: Path | str) -> None:
schema = XMLSchema11(xsd)
schema.validate(xml)


@pytest.mark.parametrize(
("xsd", "xml"),
[(TEST_XSD, TEST_FILES_PATH / "dup_primary_attr.xml")],
ids=["dup_primary_attr_xml"],
[
(TEST_XSD, TEST_FILES_PATH / "dup_primary_attr.xml"),
(
TEST_XSD,
'<?xml version="1.0" encoding="UTF-8"?><MetronInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
"<Series><Name>Foo</Name></Series><Number /><PageCount>-1</PageCount></MetronInfo>",
),
(
TEST_XSD,
'<?xml version="1.0" encoding="UTF-8"?><MetronInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
"<Series><Name>Foo</Name><Volume>-1</Volume></Series><Number /></MetronInfo>",
),
],
ids=["dup_primary_attr_xml", "negative_page_count", "negative_volume"],
)
def test_invalid(xsd, xml) -> None:
def test_invalid(xsd: Path, xml: Path | str) -> None:
schema = XMLSchema11(xsd)
with pytest.raises(XMLSchemaValidationError):
schema.validate(xml)

0 comments on commit 5f624ed

Please sign in to comment.