Debian Codegraph @

tests/unit/build_tests/mapper_tests/test_build_datetime.py

raw
1 from hdmf.utils import docval, getargs
2 from hdmf import Container
3 from hdmf.spec import GroupSpec, DatasetSpec
4 from hdmf.testing import TestCase
5 from datetime import datetime, date
6
7 from tests.unit.helpers.utils import create_test_type_map
8
9
10 class Bar(Container):
11
12 @docval({'name': 'name', 'type': str, 'doc': 'the name of this Bar'},
13 {'name': 'data', 'type': ('data', 'array_data', datetime, date), 'doc': 'some data'})
14 def __init__(self, **kwargs):
15 name, data = getargs('name', 'data', kwargs)
16 super().__init__(name=name)
17 self.__data = data
18
19 @property
20 def data_type(self):
21 return 'Bar'
22
23 @property
24 def data(self):
25 return self.__data
26
27
28 class TestBuildDatasetDateTime(TestCase):
29 """Test that building a dataset with dtype isodatetime works with datetime and date objects."""
30
31 def test_datetime_scalar(self):
32 bar_spec = GroupSpec(
33 doc='A test group specification with a data type',
34 data_type_def='Bar',
35 datasets=[DatasetSpec(doc='an example dataset', name='data', dtype='isodatetime')],
36 )
37 type_map = create_test_type_map([bar_spec], {'Bar': Bar})
38
39 bar_inst = Bar(name='my_bar', data=datetime(2023, 7, 9))
40 builder = type_map.build(bar_inst)
41 ret = builder.get('data')
42 assert ret.data == b'2023-07-09T00:00:00'
43 assert ret.dtype == 'ascii'
44
45 def test_date_scalar(self):
46 bar_spec = GroupSpec(
47 doc='A test group specification with a data type',
48 data_type_def='Bar',
49 datasets=[DatasetSpec(doc='an example dataset', name='data', dtype='isodatetime')],
50 )
51 type_map = create_test_type_map([bar_spec], {'Bar': Bar})
52
53 bar_inst = Bar(name='my_bar', data=date(2023, 7, 9))
54 builder = type_map.build(bar_inst)
55 ret = builder.get('data')
56 assert ret.data == b'2023-07-09'
57 assert ret.dtype == 'ascii'
58
59 def test_datetime_array(self):
60 bar_spec = GroupSpec(
61 doc='A test group specification with a data type',
62 data_type_def='Bar',
63 datasets=[DatasetSpec(doc='an example dataset', name='data', dtype='isodatetime', dims=(None,))],
64 )
65 type_map = create_test_type_map([bar_spec], {'Bar': Bar})
66
67 bar_inst = Bar(name='my_bar', data=[datetime(2023, 7, 9), datetime(2023, 7, 10)])
68 builder = type_map.build(bar_inst)
69 ret = builder.get('data')
70 assert ret.data == [b'2023-07-09T00:00:00', b'2023-07-10T00:00:00']
71 assert ret.dtype == 'ascii'
72
73 def test_date_array(self):
74 bar_spec = GroupSpec(
75 doc='A test group specification with a data type',
76 data_type_def='Bar',
77 datasets=[DatasetSpec(doc='an example dataset', name='data', dtype='isodatetime', dims=(None,))],
78 )
79 type_map = create_test_type_map([bar_spec], {'Bar': Bar})
80
81 bar_inst = Bar(name='my_bar', data=[date(2023, 7, 9), date(2023, 7, 10)])
82 builder = type_map.build(bar_inst)
83 ret = builder.get('data')
84 assert ret.data == [b'2023-07-09', b'2023-07-10']
85 assert ret.dtype == 'ascii'