xref: /trunk/test/aoo-test/tests/odf/test_save_modes.py (revision 2c760c133df3253aa7075d23c106a30339fca6f2)
1################################################################
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19
20"""Every source document (see aootest.documents: the templates and documents the office ships, a new document of each
21application, and any --documents directory) saved in each ODF save mode, then checked: it saved, it declares the right
22version, the package and document-type rules hold, ids and formulas parse, and it validates against the schema its mode
23promises.
24
25Strict ODF 1.2 (mode 4) must validate against 1.2 and 1.3; 1.3 (mode 5) against 1.3; 1.4 (mode 7) against 1.4. The extended
26modes (3, 6, 8 and the product default) carry AOO extensions on purpose, so their schema errors are not asserted here."""
27import pytest
28
29from aootest import documents
30from aootest.odfversion import SAVE_MODES, is_formula, odf_versions
31
32pytestmark = pytest.mark.slow
33
34MODE_NAMES = list(SAVE_MODES)
35DECLARED = {"strict": "1.2", "ext": "1.2", "v13": "1.3", "x13": "1.3", "v14": "1.4", "x14": "1.4"}
36SCHEMAS = [("strict", "12"), ("strict", "13"), ("strict", "14"), ("v13", "13"), ("v14", "14")]
37
38
39def saved_copy(odf_runs, need_mode, mode, source):
40    """The ModeRun and the saved result of `source`; skips when the build lacks the mode."""
41    need_mode(mode)
42    run = odf_runs(mode)
43    return run, run.saved()[source.id]
44
45
46def shall(entries, package):
47    return [e[3] for e in entries if e[0].split("/", 1)[0] == package and e[3].startswith("shall:")]
48
49
50def own(entries, package):
51    return [e for e in entries if e[0].split("/", 1)[0] == package]
52
53
54@pytest.mark.parametrize("mode", MODE_NAMES)
55class TestSavedCopy:
56    def test_saves(self, odf_runs, need_mode, mode, source):
57        _, r = saved_copy(odf_runs, need_mode, mode, source)
58        assert r["status"] == "ok", "%s: %s" % (r["status"], r["detail"])
59
60    def test_declares_the_mode_version(self, odf_runs, need_mode, mode, source):
61        if mode not in DECLARED:
62            pytest.skip("the product default is not pinned to one ODF version")
63        _, r = saved_copy(odf_runs, need_mode, mode, source)
64        assert r["out"], "not saved: %s" % r["detail"]
65        assert odf_versions(r["out"]) == (None if is_formula(r["out"]) else DECLARED[mode], DECLARED[mode])
66
67    def test_package_rules(self, odf_runs, need_mode, mode, source):
68        run, r = saved_copy(odf_runs, need_mode, mode, source)
69        assert r["out"], "not saved: %s" % r["detail"]
70        res = run.validation()["pkg"]
71        if res is None:
72            pytest.skip("needs java, jing and the ODF schemas (python -m aootest.fetch)")
73        assert shall(res["errors"], documents.out_name(source)) == []
74
75    def test_document_type(self, odf_runs, need_mode, mode, source):
76        run, r = saved_copy(odf_runs, need_mode, mode, source)
77        assert r["out"], "not saved: %s" % r["detail"]
78        assert shall(run.validation()["doctype"]["errors"], documents.out_name(source)) == []
79
80    def test_xml_ids(self, odf_runs, need_mode, mode, source):
81        run, r = saved_copy(odf_runs, need_mode, mode, source)
82        assert r["out"], "not saved: %s" % r["detail"]
83        assert own(run.validation()["ids"]["errors"], documents.out_name(source)) == []
84
85    def test_formula_syntax(self, odf_runs, need_mode, mode, source):
86        run, r = saved_copy(odf_runs, need_mode, mode, source)
87        assert r["out"], "not saved: %s" % r["detail"]
88        assert shall(run.validation()["formulas"]["errors"], documents.out_name(source)) == []
89
90
91@pytest.mark.parametrize("mode,version", SCHEMAS)
92def test_valid_against_odf_schema(odf_runs, need_mode, mode, version, source):
93    run, r = saved_copy(odf_runs, need_mode, mode, source)
94    assert r["out"], "not saved: %s" % r["detail"]
95    val = run.validation()["val"]
96    if val is None:
97        pytest.skip("needs java, jing and the ODF schemas (python -m aootest.fetch)")
98    errors = own(val["errors"][version], documents.out_name(source))
99    assert not errors, "%d errors against ODF 1.%s, first: %s" % (len(errors), version[1], errors[0][:4])
100