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

1""" 

2crate_anon/common/tests/spreadsheet_tests.py 

3 

4=============================================================================== 

5 

6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry. 

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

8 

9 This file is part of CRATE. 

10 

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. 

15 

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. 

20 

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/>. 

23 

24=============================================================================== 

25 

26Unit testing. 

27 

28""" 

29 

30# ============================================================================= 

31# Imports 

32# ============================================================================= 

33 

34import datetime 

35import tempfile 

36import os 

37from unittest import TestCase 

38 

39from crate_anon.common.spreadsheet import write_spreadsheet 

40 

41 

42# ============================================================================= 

43# Unit tests 

44# ============================================================================= 

45 

46 

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) 

69 

70 tsv_filename = os.path.join(dirname, "test.tsv") 

71 write_spreadsheet(tsv_filename, data) 

72 

73 xlsx_filename = os.path.join(dirname, "test.tsv") 

74 write_spreadsheet(xlsx_filename, data) 

75 

76 # ... the test is that nothing crashes.