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.stability: two saves that differ only in the cached value of a date or time field compare equal, and any other 21difference does not.""" 22import zipfile 23 24import pytest 25 26from aootest import stability 27 28pytestmark = pytest.mark.selftest 29 30BODY = '<office:text><text:p>Hello <text:time text:time-value="2026-11-02T07:47:54" text:fixed="false">07:47:54</text:time></text:p></office:text>' 31 32 33def package(path, content, styles="<office:document-styles/>"): 34 with zipfile.ZipFile(path, "w") as z: 35 z.writestr("content.xml", content) 36 if styles is not None: 37 z.writestr("styles.xml", styles) 38 return str(path) 39 40 41def test_normalize_drops_the_cached_time_and_the_value_attribute(): 42 other = BODY.replace("07:47:54", "07:48:00") 43 assert stability.normalize(BODY) == stability.normalize(other) 44 assert "07:47:54" not in stability.normalize(BODY) 45 46 47def test_two_packages_differing_only_in_a_time_field_compare_equal(tmp_path): 48 a = package(tmp_path / "a.zip", BODY) 49 b = package(tmp_path / "b.zip", BODY.replace("07:47:54", "09:00:00")) 50 assert stability.differing_parts(a, b) == [] 51 52 53@pytest.mark.parametrize("change", [lambda t: t.replace("Hello", "Hullo"), lambda t: t.replace('text:fixed="false"', 'text:fixed="true"'), 54 lambda t: t.replace("<text:p>", "<text:p text:style-name=\"P1\">")]) 55def test_any_other_difference_is_reported(tmp_path, change): 56 a = package(tmp_path / "a.zip", BODY) 57 b = package(tmp_path / "b.zip", change(BODY)) 58 assert stability.differing_parts(a, b) == ["content.xml"] 59 60 61def test_a_difference_in_styles_xml_is_reported(tmp_path): 62 a = package(tmp_path / "a.zip", BODY) 63 b = package(tmp_path / "b.zip", BODY, "<office:document-styles>x</office:document-styles>") 64 assert stability.differing_parts(a, b) == ["styles.xml"] 65 66 67def test_a_part_neither_package_has_is_not_compared(tmp_path): 68 a = package(tmp_path / "a.zip", BODY, None) 69 b = package(tmp_path / "b.zip", BODY, None) 70 assert stability.differing_parts(a, b) == [] 71 assert stability.differing_parts(a, package(tmp_path / "c.zip", BODY.replace("Hello", "Hullo"), None)) == ["content.xml"] 72 73 74def test_a_part_only_one_package_has_is_reported(tmp_path): 75 a = package(tmp_path / "a.zip", BODY, None) 76 b = package(tmp_path / "b.zip", BODY) 77 assert stability.differing_parts(a, b) == ["styles.xml"] 78 assert stability.differing_parts(b, a) == ["styles.xml"] 79