3
3
datetime ,
4
4
)
5
5
import re
6
+ import uuid
6
7
7
8
import pytest
8
9
9
- from pandas .compat import is_platform_windows
10
-
11
10
import pandas as pd
12
- import pandas ._testing as tm
13
11
14
12
from pandas .io .excel import ExcelWriter
15
13
16
14
odf = pytest .importorskip ("odf" )
17
15
18
- if is_platform_windows ():
19
- pytestmark = pytest .mark .single_cpu
20
-
21
16
22
17
@pytest .fixture
23
18
def ext ():
24
19
return ".ods"
25
20
26
21
27
- def test_write_append_mode_raises (ext ):
22
+ @pytest .fixture
23
+ def tmp_excel (ext , tmp_path ):
24
+ tmp = tmp_path / f"{ uuid .uuid4 ()} { ext } "
25
+ tmp .touch ()
26
+ return str (tmp )
27
+
28
+
29
+ def test_write_append_mode_raises (tmp_excel ):
28
30
msg = "Append mode is not supported with odf!"
29
31
30
- with tm .ensure_clean (ext ) as f :
31
- with pytest .raises (ValueError , match = msg ):
32
- ExcelWriter (f , engine = "odf" , mode = "a" )
32
+ with pytest .raises (ValueError , match = msg ):
33
+ ExcelWriter (tmp_excel , engine = "odf" , mode = "a" )
33
34
34
35
35
36
@pytest .mark .parametrize ("engine_kwargs" , [None , {"kwarg" : 1 }])
36
- def test_engine_kwargs (ext , engine_kwargs ):
37
+ def test_engine_kwargs (tmp_excel , engine_kwargs ):
37
38
# GH 42286
38
39
# GH 43445
39
40
# test for error: OpenDocumentSpreadsheet does not accept any arguments
40
- with tm .ensure_clean (ext ) as f :
41
- if engine_kwargs is not None :
42
- error = re .escape (
43
- "OpenDocumentSpreadsheet() got an unexpected keyword argument 'kwarg'"
44
- )
45
- with pytest .raises (
46
- TypeError ,
47
- match = error ,
48
- ):
49
- ExcelWriter (f , engine = "odf" , engine_kwargs = engine_kwargs )
50
- else :
51
- with ExcelWriter (f , engine = "odf" , engine_kwargs = engine_kwargs ) as _ :
52
- pass
53
-
54
-
55
- def test_book_and_sheets_consistent (ext ):
41
+ if engine_kwargs is not None :
42
+ error = re .escape (
43
+ "OpenDocumentSpreadsheet() got an unexpected keyword argument 'kwarg'"
44
+ )
45
+ with pytest .raises (
46
+ TypeError ,
47
+ match = error ,
48
+ ):
49
+ ExcelWriter (tmp_excel , engine = "odf" , engine_kwargs = engine_kwargs )
50
+ else :
51
+ with ExcelWriter (tmp_excel , engine = "odf" , engine_kwargs = engine_kwargs ) as _ :
52
+ pass
53
+
54
+
55
+ def test_book_and_sheets_consistent (tmp_excel ):
56
56
# GH#45687 - Ensure sheets is updated if user modifies book
57
- with tm .ensure_clean (ext ) as f :
58
- with ExcelWriter (f ) as writer :
59
- assert writer .sheets == {}
60
- table = odf .table .Table (name = "test_name" )
61
- writer .book .spreadsheet .addElement (table )
62
- assert writer .sheets == {"test_name" : table }
57
+ with ExcelWriter (tmp_excel ) as writer :
58
+ assert writer .sheets == {}
59
+ table = odf .table .Table (name = "test_name" )
60
+ writer .book .spreadsheet .addElement (table )
61
+ assert writer .sheets == {"test_name" : table }
63
62
64
63
65
64
@pytest .mark .parametrize (
@@ -78,7 +77,9 @@ def test_book_and_sheets_consistent(ext):
78
77
(date (2010 , 10 , 10 ), "date" , "date-value" , "2010-10-10" ),
79
78
],
80
79
)
81
- def test_cell_value_type (ext , value , cell_value_type , cell_value_attribute , cell_value ):
80
+ def test_cell_value_type (
81
+ tmp_excel , value , cell_value_type , cell_value_attribute , cell_value
82
+ ):
82
83
# GH#54994 ODS: cell attributes should follow specification
83
84
# http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#refTable13
84
85
from odf .namespaces import OFFICENS
@@ -89,18 +90,17 @@ def test_cell_value_type(ext, value, cell_value_type, cell_value_attribute, cell
89
90
90
91
table_cell_name = TableCell ().qname
91
92
92
- with tm .ensure_clean (ext ) as f :
93
- pd .DataFrame ([[value ]]).to_excel (f , header = False , index = False )
94
-
95
- with pd .ExcelFile (f ) as wb :
96
- sheet = wb ._reader .get_sheet_by_index (0 )
97
- sheet_rows = sheet .getElementsByType (TableRow )
98
- sheet_cells = [
99
- x
100
- for x in sheet_rows [0 ].childNodes
101
- if hasattr (x , "qname" ) and x .qname == table_cell_name
102
- ]
103
-
104
- cell = sheet_cells [0 ]
105
- assert cell .attributes .get ((OFFICENS , "value-type" )) == cell_value_type
106
- assert cell .attributes .get ((OFFICENS , cell_value_attribute )) == cell_value
93
+ pd .DataFrame ([[value ]]).to_excel (tmp_excel , header = False , index = False )
94
+
95
+ with pd .ExcelFile (tmp_excel ) as wb :
96
+ sheet = wb ._reader .get_sheet_by_index (0 )
97
+ sheet_rows = sheet .getElementsByType (TableRow )
98
+ sheet_cells = [
99
+ x
100
+ for x in sheet_rows [0 ].childNodes
101
+ if hasattr (x , "qname" ) and x .qname == table_cell_name
102
+ ]
103
+
104
+ cell = sheet_cells [0 ]
105
+ assert cell .attributes .get ((OFFICENS , "value-type" )) == cell_value_type
106
+ assert cell .attributes .get ((OFFICENS , cell_value_attribute )) == cell_value
0 commit comments