-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_module.py
executable file
·193 lines (164 loc) · 7.15 KB
/
test_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
"""Tests for dwdatareader python module.
Execute with:
python test_module.py
"""
import os
import platform
import sys
import unittest
import xml.etree.ElementTree as et
import pandas as pd
import pytest
import dwdatareader as dw
class TestDW(unittest.TestCase):
def setUp(self):
""" Identify path to the d7d test data file """
import os
self.d7dname = os.path.join(os.path.dirname(__file__),
"Example_Drive01.d7d")
def test_context(self):
"""Check that the d7d is open and closed according to context."""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
ch = d7d['ENG_RPM']
# The d7d should be closed outside of the above context
self.assertTrue(d7d.closed, 'd7d did not close')
with self.assertRaises(IndexError,
msg="accessing channel data"):
ch.series() # ScaledSamplesCount returns -1
with self.assertRaises(ValueError,
msg="accessing channel metadata"):
d7d['ENG_RPM'] # I/O operation on closed file
def test_info(self):
"""Check that the file info was read correctly."""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
self.assertEqual(100, d7d.info.sample_rate)
def test_filelike(self):
"""Test module access and usage of filelike objects.
In this test the dw module must create a temporary file copy
of the d7d filelike object so that the dll will have something to
open. The temporary d7d file should be removed when the DWFile is
closed."""
import os.path
with open(self.d7dname, 'rb') as f:
self.assertFalse(f.closed, 'file did not open')
with dw.open(f) as d7d:
self.assertFalse(d7d.closed, 'temporary d7d did not open')
self.assertFalse(os.path.exists(d7d.name),
'temporary d7d not removed')
def test_metadata(self):
"""Make sure channel metadata is correctly loaded."""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
self.assertEqual(20, len(d7d))
self.assertEqual('rpm', d7d['ENG_RPM'].unit)
def test_header(self):
"""Make sure file headers are available."""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
# Unfortunately, no headers in sample file
self.assertDictEqual({}, d7d.header)
def test_export_header(self):
"""Make sure header is exported to file local.xml"""
file_name = "local.xml"
with dw.open(self.d7dname) as dwf:
dwf.export_header(file_name)
assert os.path.isfile("local.xml")
def test_events(self):
"""Make sure events are readable."""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
self.assertEqual(2, len(d7d.events()))
def test_series(self):
"""Read a series and make sure its value matches expectation."""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
s = d7d['GPSvel'].series()
s5 = s[5.0:5.5] # time-based slice!
self.assertEqual(76.46, round(s5.mean(), 2))
def test_series_generator(self):
"""Read a series and make sure its value matches expectation."""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
channel = d7d['GPSvel']
expected = channel.series()
actual = pd.concat(list(channel.series_generator(500)))
self.assertEqual(len(expected), len(actual))
self.assertTrue(abs(actual.sum() - expected.sum()) < 1)
def test_channel_type(self):
"""Channel type"""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
channel = d7d['ENG_RPM']
actual = channel.channel_type
expected = 1
self.assertEqual(actual, expected)
@unittest.expectedFailure
def test_CAN_channel(self):
"""Read channel data with CAN in its channel_index"""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
channel = d7d['ENG_RPM']
# Following did not fail prior to DWDataReader v4.2.0.31
# Now all channels whose channel_index begins with CAN will fail with "CAN note supported"
scaled = channel.scaled()
def test_channel_index(self):
"""Channel type"""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
channel = d7d['GPSvel']
actual = channel.channel_index
expected = 'AI;4'
self.assertEqual(actual, expected)
def test_channel_xml(self):
"""Channel type"""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
channel = d7d['ENG_RPM']
xml = channel.channel_xml
root = et.fromstring(xml)
actual = root.find('ForceSinglePrecision').text
expected = 'True'
self.assertEqual(actual, expected)
def test_channel_longname(self):
"""Channel long name"""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
channel = d7d['ENG_RPM']
actual = channel.long_name
expected = 'ENG_RPM'
self.assertEqual(actual, expected)
def test_reduced(self):
"""Read reduced channel data and check value."""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
r = d7d['GPSvel'].reduced()
r5 = r.ave.asof(5.0) # index into reduced list near time=5.0
self.assertEqual(76.46, round(r5, 2))
def test_channel_dataframe(self):
"""Read one channel as a DataFrame"""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
df = d7d['GPSvel'].dataframe()
self.assertEqual(len(df.GPSvel), 9580)
def test_dataframe(self):
"""Read all channel data as a single DataFrame."""
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
channels = [channel for channel in d7d
if not d7d[channel].channel_index.startswith('CAN')]
self.assertEqual((11385, 8), d7d.dataframe(channels).shape)
def test_encoding_uft8(self):
""" Check that encoding is set correcly """
dw.encoding = 'utf-8'
with dw.open(self.d7dname) as d7d:
self.assertFalse(d7d.closed, 'd7d did not open')
def test_encoding_uft32(self):
""" Check that wrong encoding raises an error """
dw.encoding = 'utf-32'
with self.assertRaises(dw.DWError):
dw.open(self.d7dname)
if __name__ == '__main__':
unittest.main()