xref: /trunk/test/aoo-test/tests/selftest/test_kinds.py (revision 85d6e4e58e50c1ed01601e2737a66d9715456246)
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"""aootest.kinds: grouping of schema errors into kinds, and what counts as worse than a baseline."""
21import pytest
22
23from aootest import kinds
24
25pytestmark = pytest.mark.selftest
26
27
28def entry(package, message, part="content.xml"):
29    return ["%s/%s" % (package, part), 2, 10, message]
30
31
32VAL = {"errors": {"12": [entry("a.odt", 'element "text:bogus" not allowed here; expected element "text:p"'),
33                         entry("b.odt", 'element "text:bogus" not allowed here; expected element "text:h"'),
34                         entry("b.odt", 'attribute "x:y" not allowed here'),
35                         entry("other.odt", 'attribute "x:y" not allowed here')]}}
36
37
38def test_errors_are_grouped_by_message_without_the_expected_tail_and_limited_to_the_packages():
39    got = kinds.kinds_of(VAL, "12", {"a.odt", "b.odt"})
40    assert got == {'element "text:bogus" not allowed here': {"errors": 2, "packages": ["a.odt", "b.odt"]},
41                   'attribute "x:y" not allowed here': {"errors": 1, "packages": ["b.odt"]}}
42
43
44def test_jing_failures_that_name_the_work_directory_are_one_kind_whatever_the_directory():
45    a = {"errors": {"12": [["?", 0, 0, "file not found: /tmp/one/w-validate/12/a.odt/META-INF/Manifest.dtd"]]}}
46    b = {"errors": {"12": [["?", 0, 0, "file not found: /var/x/two/w-validate/12/a.odt/META-INF/Manifest.dtd"]]}}
47    # the package of such an entry is "?", so name it
48    assert kinds.kinds_of(a, "12", {"?"}) == kinds.kinds_of(b, "12", {"?"})
49    assert list(kinds.kinds_of(a, "12", {"?"})) == ["UNPARSED: file not found: <workdir>/12/a.odt/META-INF/Manifest.dtd"]
50
51
52BASE = {"documents": ["a.odt", "b.odt"], "kinds": {"k1": {"errors": 3, "packages": ["a.odt", "b.odt"]}, "k2": {"errors": 1, "packages": ["b.odt"]}}}
53
54
55def test_the_same_kinds_are_not_worse():
56    assert kinds.judge(BASE, ["a.odt", "b.odt"], BASE["kinds"]) == ([], False)
57
58
59@pytest.mark.parametrize("change", [
60    lambda k: {**k, "k3": {"errors": 1, "packages": ["a.odt"]}},
61    lambda k: {**k, "k1": {"errors": 4, "packages": ["a.odt", "b.odt"]}},
62    lambda k: {**k, "k2": {"errors": 1, "packages": ["a.odt", "b.odt"]}},
63])
64def test_a_new_kind_or_a_higher_count_or_another_package_is_worse(change):
65    lines, bad = kinds.judge(BASE, ["a.odt", "b.odt"], change(BASE["kinds"]))
66    assert bad and any(l.startswith("WORSE") for l in lines)
67
68
69def test_fewer_errors_are_reported_but_not_worse():
70    lines, bad = kinds.judge(BASE, ["a.odt", "b.odt"], {"k1": {"errors": 2, "packages": ["a.odt"]}})
71    assert not bad and lines and all(l.startswith("better") for l in lines)
72
73
74def test_a_baseline_document_that_is_no_longer_covered_is_worse():
75    lines, bad = kinds.judge(BASE, ["a.odt"], BASE["kinds"])
76    assert bad and lines == ["NOT COVERED  b.odt"]
77
78
79def test_a_written_baseline_reads_back(tmp_path, monkeypatch):
80    monkeypatch.setattr(kinds, "BASELINE", tmp_path / "baseline")
81    kinds.write("ext", "12", {"b.odt", "a.odt"}, BASE["kinds"])
82    assert kinds.load("ext", "12") == BASE
83    assert kinds.load("ext", "13") is None
84