xref: /trunk/test/odf-validate/isoweeknum_rt.py (revision 195598447694ec8542914a9b51ae9942e8ebacd9)
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#**************************************************************
21
22# usage: runpy.sh isoweeknum_rt.py <port> <outdir>
23# ISOWEEKNUM(d) and WEEKNUM(d;mode) share the ODF name ISOWEEKNUM. Saves both to ODS,
24# checks the table:formula strings, reloads and checks that each formula comes back as
25# the same function with the same value. ISOWEEKNUM is checked against Python's ISO
26# calendar; WEEKNUM values are only compared before and after the round trip.
27import sys, os, re, zipfile, datetime as dt
28import uno
29from unohelp import connect, desktop, load, pv
30
31port, outdir = int(sys.argv[1]), os.path.abspath(sys.argv[2])
32os.makedirs(outdir, exist_ok=True)
33DATES = [dt.date(2015, 12, 31), dt.date(2021, 1, 3), dt.date(2018, 12, 31),
34         dt.date(2024, 2, 29), dt.date(2026, 1, 1), dt.date(2020, 12, 28)]
35
36
37# per row: (column, API formula, ODF formula)
38def formulas(row):
39    r = row + 1
40    return [("B", "=ISOWEEKNUM(A%d)" % r, "of:=ISOWEEKNUM([.A%d])" % r),
41            ("C", "=WEEKNUM(A%d;1)" % r, "of:=ISOWEEKNUM([.A%d];1)" % r),
42            ("D", "=WEEKNUM(A%d;2)" % r, "of:=ISOWEEKNUM([.A%d];2)" % r)]
43
44
45total = fails = 0
46
47
48def check(what, ok, detail=""):
49    global total, fails
50    total += 1
51    if not ok:
52        fails += 1
53        print("MISMATCH %s %s" % (what, detail))
54
55
56def check_sheet(doc, tag, ref):
57    sh = doc.getSheets().getByIndex(0)
58    for row, d in enumerate(DATES):
59        for col, api, odf in formulas(row):
60            c = sh.getCellRangeByName("%s%d" % (col, row + 1))
61            check("%s %s%d formula" % (tag, col, row + 1), c.getFormula() == api, "expected %s got %s" % (api, c.getFormula()))
62            key = (col, row)
63            if col == "B":
64                ref[key] = d.isocalendar()[1]
65            elif key not in ref:
66                ref[key] = c.getValue()
67            check("%s %s%d value" % (tag, col, row + 1), c.getError() == 0 and c.getValue() == ref[key],
68                  "%s expected %s got %s err %d" % (d, ref[key], c.getValue(), c.getError()))
69
70
71desk = desktop(connect(port))
72doc = desk.loadComponentFromURL("private:factory/scalc", "_blank", 0, (pv("Hidden", True),))
73sh = doc.getSheets().getByIndex(0)
74for row, d in enumerate(DATES):
75    sh.getCellRangeByName("A%d" % (row + 1)).setValue((d - dt.date(1899, 12, 30)).days)
76    for col, api, odf in formulas(row):
77        sh.getCellRangeByName("%s%d" % (col, row + 1)).setFormula(api)
78doc.calculateAll()
79ref = {}
80check_sheet(doc, "new", ref)
81path = os.path.join(outdir, "isoweeknum.ods")
82doc.storeToURL(uno.systemPathToFileUrl(path), (pv("FilterName", "calc8"),))
83doc.close(True)
84
85xml = zipfile.ZipFile(path).read("content.xml").decode("utf-8")
86saved = re.findall(r'table:formula="([^"]*)"', xml)
87want = [odf for row in range(len(DATES)) for _, _, odf in formulas(row)]
88check("content.xml formulas", saved == want, "expected %s got %s" % (want, saved))
89
90doc = load(desk, path)
91check_sheet(doc, "reload cached", ref)
92doc.calculateAll()
93check_sheet(doc, "reload recalculated", ref)
94doc.close(True)
95
96check("ran at least one check", total > 0)
97print("isoweeknum_rt: %d/%d match" % (total - fails, total))
98sys.exit(1 if fails else 0)
99