Skip to content

Commit

Permalink
availability schema
Browse files Browse the repository at this point in the history
  • Loading branch information
jwfraustro committed Dec 7, 2023
1 parent 7b1a8d6 commit 3eae8e2
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/vosi/VOSIAvailability-v1.0.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.ivoa.net/xml/VOSIAvailability/v1.0"
xmlns:tns="http://www.ivoa.net/xml/VOSIAvailability/v1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
version="1.0">

<xsd:annotation>
<xsd:documentation>
A schema for formatting availability metadata as returned by an
availability resource defined in the IVOA Support Interfaces
specification (VOSI).
See http://www.ivoa.net/Documents/latest/VOSI.html.
</xsd:documentation>
</xsd:annotation>

<!--
- the root element for a VOSI availability (section 2.3)
-->
<xsd:element name="availability" type="tns:Availability"/>

<xsd:complexType name="Availability">
<xsd:sequence>

<xsd:element name="available" type="xsd:boolean">
<xsd:annotation>
<xsd:documentation>
Indicates whether the service is currently available.
</xsd:documentation>
</xsd:annotation>
</xsd:element>

<xsd:element name="upSince" type="xsd:dateTime" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
The instant at which the service last became available.
</xsd:documentation>
</xsd:annotation>
</xsd:element>

<xsd:element name="downAt" type="xsd:dateTime" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
The instant at which the service is next scheduled to become
unavailable.
</xsd:documentation>
</xsd:annotation>
</xsd:element>

<xsd:element name="backAt" type="xsd:dateTime" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
The instant at which the service is scheduled to become available
again after a period of unavailability.
</xsd:documentation>
</xsd:annotation>
</xsd:element>

<xsd:element name="note" type="xsd:string"
minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
A textual note concerning availability.
</xsd:documentation>
</xsd:annotation>
</xsd:element>

</xsd:sequence>
</xsd:complexType>

</xsd:schema>
Empty file added tests/vosi/__init__.py
Empty file.
59 changes: 59 additions & 0 deletions tests/vosi/availability_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Tests for VOSI Availability models."""

from datetime import timezone as tz
from unittest import TestCase
from xml.etree.ElementTree import canonicalize

from lxml import etree

with open("tests/vosi/VOSIAvailability-v1.0.xsd") as f:
availability_schema = etree.parse(f)

from vo_models.xml.vosi.availability import Availability
from vo_models.xml.generics import VODateTime

VOSI_AVAILABILITY_HEADER = """xmlns="http://www.ivoa.net/xml/VOSIAvailability/v1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
"""

class TestVOSIAvailabilityType(TestCase):
"""Tests the VOSI Availability complex type."""

test_avaiability_xml = f"""<availability {VOSI_AVAILABILITY_HEADER}>
<available>true</available>
<upSince>2021-01-01T00:00:00.000Z</upSince>
<downAt>2021-01-01T00:00:00.000Z</downAt>
<backAt>2021-01-01T00:00:00.000Z</backAt>
<note>Test note</note>
</availability>"""

def test_read_from_xml(self):
"""Test reading Availability from XML."""
availability = Availability.from_xml(self.test_avaiability_xml)
self.assertTrue(availability.available)
self.assertEqual(availability.up_since, VODateTime(2021, 1, 1, tzinfo=tz.utc))
self.assertEqual(availability.down_at, VODateTime(2021, 1, 1, tzinfo=tz.utc))
self.assertEqual(availability.back_at, VODateTime(2021, 1, 1, tzinfo=tz.utc))
self.assertEqual(availability.note, ["Test note"])

def test_write_to_xml(self):
"""Test writing Availability to XML."""
availability = Availability(
available=True,
up_since="2021-01-01T00:00:00.000Z",
down_at="2021-01-01T00:00:00.000Z",
back_at="2021-01-01T00:00:00.000Z",
note=["Test note"],
)

with open("debug.xml", "w") as f:
f.write(canonicalize(availability.to_xml()))
f.write("\n")
f.write(canonicalize(self.test_avaiability_xml))

availability_xml = availability.to_xml()
self.assertEqual(
canonicalize(availability_xml, strip_text=True),
canonicalize(self.test_avaiability_xml, strip_text=True),
)
1 change: 1 addition & 0 deletions vo_models/xml/vosi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Module containing VOSI (VO Service Interface) classes."""
32 changes: 32 additions & 0 deletions vo_models/xml/vosi/availability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""VOSIAvailability pydantic-xml models."""

from typing import Optional

from pydantic_xml import BaseXmlModel, element

from vo_models.xml.generics import VODateTime

NSMAP = {
"": "http://www.ivoa.net/xml/VOSIAvailability/v1.0",
"xsd": "http://www.w3.org/2001/XMLSchema",
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
}


class Availability(BaseXmlModel, tag="availability", nsmap=NSMAP):
"""VOSI Availability complex type.
Elements:
available (bool): Whether the service is currently available.
upSince (datetime): The instant at which the service last became available.
downAt (datetime): The instant at which the service is next scheduled to become unavailable.
backAt (datetime): The instant at which the service is scheduled to become available again after a period
of unavailability.
note (str): A textual note concerning availability.
"""

available: bool = element(tag="available")
up_since: Optional[VODateTime] = element(tag="upSince")
down_at: Optional[VODateTime] = element(tag="downAt")
back_at: Optional[VODateTime] = element(tag="backAt")
note: Optional[list[str]] = element(tag="note")

0 comments on commit 3eae8e2

Please sign in to comment.