예제 #1
0
def assert_json_bindings(context: XmlContext, obj: Any, save_path: Optional[Path]):
    __tracebackhide__ = True

    serializer = JsonSerializer(context=context, config=config)
    parser = JsonParser(context=context)
    obj_json = serializer.render(obj)
    obj_b = parser.from_string(obj_json, obj.__class__)

    if save_path:
        save_path.with_suffix(".json").write_text(obj_json)

        with contextlib.suppress(FileNotFoundError):
            save_path.with_suffix(".diff").unlink()

    if obj != obj_b:
        obj_b_json = serializer.render(obj_b)

        if obj_json == obj_b_json:
            return

        diff = "\n".join(difflib.ndiff(obj_json.splitlines(), obj_b_json.splitlines()))

        if save_path:
            save_path.with_suffix(".diff").write_text(diff)

        raise AssertionError(f"JSON Round trip failed\n{diff}")
예제 #2
0
def validate_bindings(schema: Path, clazz: Type, output_format: str):
    __tracebackhide__ = True

    chapter = schema.stem.replace("chapter", "")

    sample = here.joinpath(f"samples/chapter{chapter}.xml")
    output_xml = here.joinpath(f"output/chapter{chapter}.xsdata.xml")
    output_json = here.joinpath(f"output/chapter{chapter}.xsdata.json")

    context = XmlContext(class_type=output_format)
    obj = XmlParser(context=context).from_path(sample, clazz)
    config = SerializerConfig(pretty_print=True)

    actual_json = JsonSerializer(context=context, config=config).render(obj)
    actual_xml = XmlSerializer(context=context, config=config).render(obj)

    if output_json.exists() and chapter != "13":
        assert output_json.read_text() == actual_json
        assert obj == JsonParser(context=context).from_string(
            actual_json, clazz)
    else:
        output_json.write_text(actual_json, encoding="utf-8")

    if output_xml.exists():
        assert output_xml.read_text() == actual_xml
    else:
        output_xml.write_text(actual_xml, encoding="utf-8")

    validator = etree.XMLSchema(etree.parse(str(schema)))
    validator.assertValid(etree.fromstring(actual_xml.encode()))
예제 #3
0
def test_json_documents():
    filepath = fixtures_dir.joinpath("series")
    package = "tests.fixtures.series"
    runner = CliRunner()
    result = runner.invoke(cli, [str(filepath), "--package", package])

    if result.exception:
        raise result.exception

    clazz = load_class(result.output, "Series")

    parser = JsonParser()
    serializer = JsonSerializer(indent=4)

    for i in range(1, 3):
        ori = filepath.joinpath(f"show{i}.json").read_text()
        obj = parser.from_string(ori, clazz)
        actual = serializer.render(obj)

        assert filter_none(json.loads(ori)) == filter_none(json.loads(actual))
예제 #4
0
 def from_json(json_obj: Dict, model_cls: Type[T]) -> T:
     parser = JsonParser(context=XmlContext())
     return parser.from_string(json.dumps(json_obj), model_cls)
예제 #5
0
def parse_json(source):
    parser = JsonParser(context=context)
    parser.from_bytes(source, Books)
예제 #6
0
here = Path(__file__).parent
fixtures = here.joinpath("fixtures")

is_travis = "TRAVIS" in os.environ


@dataclass
class Documentation:
    title: str
    skip_message: str
    source: str
    target: str


xml_parser = XmlParser()
json_parser = JsonParser()
xml_serializer = XmlSerializer(pretty_print=True)
json_serializer = JsonSerializer(indent=4)

xmls = sorted([
    xsd for xsd in fixtures.glob("defxmlschema/*/chapter*.xml")
    if not str(xsd).endswith("xsdata.xml")
])

total = 0
skipped = 0


@pytest.mark.parametrize("fixture", xmls, ids=lambda x: x.name)
def test_binding(fixture: Path):
    global total, skipped
예제 #7
0
def json_parser(xml_context):
    return JsonParser(context=xml_context)