Skip to content

Commit

Permalink
ods: test export styling for date, time and datetime values
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalfree committed Jul 10, 2024
1 parent 8189db6 commit 0bbb847
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pathlib import Path
from uuid import uuid4

from odf import opendocument, table
from openpyxl.reader.excel import load_workbook

import tablib
Expand Down Expand Up @@ -1165,6 +1166,16 @@ def test_tsv_export(self):


class ODSTests(BaseTestCase):
FORMAT_CONVERT = {
'yearlong': '%Y',
'monthlong': '%m',
'daylong': '%d',
'hourslong': '%H',
'minuteslong': '%M',
'secondslong': '%S',
'secondslong0': '%S',
}

def test_ods_export_import_set(self):
date = dt.date(2019, 10, 4)
date_time = dt.datetime(2019, 10, 4, 12, 30, 8)
Expand All @@ -1185,6 +1196,39 @@ def test_ods_export_import_set(self):
self.assertEqual(data.dict[0]['date/time'], date_time)
self.assertEqual(data.dict[0]['None'], '')

def test_ods_export_display(self):
"""Test that exported datetime types are displayed correctly in office software"""
date = dt.date(2019, 10, 4)
date_time = dt.datetime(2019, 10, 4, 12, 30, 8)
time = dt.time(14, 30)
data.append((date, time, date_time))
data.headers = ('date', 'time', 'date/time')
_ods = data.ods
ods_book = opendocument.load(BytesIO(_ods))
styles = {style.getAttribute('name'): style for style in ods_book.styles.childNodes}
automatic_styles = {
style.getAttribute('name'): style.getAttribute('datastylename')
for style in ods_book.automaticstyles.childNodes
}

def get_format(cell):
style = styles[automatic_styles[cell.getAttribute('stylename')]]
f = []
for number in style.childNodes:
name = number.qname[1] + ''.join(number.attributes.values())
f.append(self.FORMAT_CONVERT.get(name, str(number)))
return ''.join(f)

cells = ods_book.spreadsheet.getElementsByType(table.TableRow)[1].childNodes
self.assertEqual(str(date), str(cells[0]))
self.assertEqual('%Y-%m-%d', get_format(cells[0]))

self.assertEqual(str(time), str(cells[1]))
self.assertEqual('%H:%M:%S', get_format(cells[1]))

self.assertEqual(str(date_time), str(cells[2]))
self.assertEqual('%Y-%m-%d %H:%M:%S', get_format(cells[2]))

def test_ods_import_book(self):
ods_source = Path(__file__).parent / 'files' / 'book.ods'
with ods_source.open('rb') as fh:
Expand Down

0 comments on commit 0bbb847

Please sign in to comment.