Coverage for common/tests/spreadsheet_tests.py: 100%
15 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
1"""
2crate_anon/common/tests/spreadsheet_tests.py
4===============================================================================
6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CRATE.
11 CRATE is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 CRATE is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with CRATE. If not, see <https://www.gnu.org/licenses/>.
24===============================================================================
26Unit testing.
28"""
30# =============================================================================
31# Imports
32# =============================================================================
34import datetime
35import tempfile
36import os
37from unittest import TestCase
39from crate_anon.common.spreadsheet import write_spreadsheet
42# =============================================================================
43# Unit tests
44# =============================================================================
47class SpreadsheetTests(TestCase):
48 # noinspection PyMethodMayBeStatic
49 def test_write_values(self) -> None:
50 """
51 Test that all kinds of types of values can be written to all our
52 spreadsheet formats.
53 """
54 data = {
55 "sheet_P": [
56 ["P_heading1", "P_heading2", "P_heading3"],
57 ["a", 5, None],
58 [3.2, datetime.datetime.now(), "b"],
59 ],
60 "sheet_Q": [
61 ["Q_heading1", "Q_heading2", "Q_heading3"],
62 ["a", 5, None],
63 [3.2, datetime.datetime.now(), "b"],
64 ],
65 }
66 with tempfile.TemporaryDirectory() as dirname:
67 ods_filename = os.path.join(dirname, "test.ods")
68 write_spreadsheet(ods_filename, data)
70 tsv_filename = os.path.join(dirname, "test.tsv")
71 write_spreadsheet(tsv_filename, data)
73 xlsx_filename = os.path.join(dirname, "test.tsv")
74 write_spreadsheet(xlsx_filename, data)
76 # ... the test is that nothing crashes.