-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
149 lines (119 loc) · 5.16 KB
/
Copy pathtests.py
File metadata and controls
149 lines (119 loc) · 5.16 KB
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
"""
Contains Unit Tests for LSI Correlator
"""
import unittest
from tempfile import NamedTemporaryFile
import numpy as np # pylint: disable=import-error
from correlator_driver_functions import LSiCorrelatorVendorInterface
from pvdb import Records
from test_utils import test_data
# pylint: disable=line-too-long, invalid-name
macros = {"SIMULATE": "1", "ADDR": "127.0.0.1", "FIRMWARE_REVISION": "4.0.0.3"}
class LSICorrelatorTests(unittest.TestCase):
"""
Unit tests for the LSi Correlator
"""
def setUp(self):
"""
Set up the test case
"""
self.driver = LSiCorrelatorVendorInterface(macros, simulated=True)
self.mocked_api = self.driver.mocked_api
self.device = self.driver.device
self.mocked_api.disconnected = False
def test_GIVEN_device_disconnected_WHEN_data_taken_THEN_device_reads_no_data_and_disconnected(
self,
):
"""
Test that the device reads no data when disconnected
"""
self.assertTrue(self.driver.is_connected)
self.device.disconnected = True
self.driver.take_data(0)
self.assertFalse(self.driver.has_data)
self.assertFalse(self.driver.is_connected)
def test_GIVEN_device_connected_WHEN_data_taken_THEN_device_reads_has_data_and_connected(self):
"""
Test that the device reads data when connected
"""
self.driver.take_data(0)
self.assertTrue(self.driver.has_data)
self.assertTrue(self.driver.is_connected)
def test_GIVEN_device_connected_WHEN_data_taken_THEN_driver_updated_with_correlation_and_time_lags(
self,
):
"""
Test that the driver updates with the correlation and time lags
"""
self.mocked_api.corr = test_data.corr
self.mocked_api.lags = test_data.lags
self.driver.take_data(0)
self.assertTrue(np.allclose(self.driver.corr, test_data.corr_without_nans))
self.assertTrue(np.allclose(self.driver.lags, test_data.lags_without_nans))
def test_GIVEN_device_has_data_WHEN_data_retrieved_from_device_THEN_time_trace_made_and_no_nans_in_correlation(
self,
):
"""
Test that the time trace is made and no nans in correlation
"""
self.device.Correlation = test_data.corr
self.device.Lags = test_data.lags
self.device.TraceChA = test_data.trace_a
self.device.TraceChB = test_data.trace_b
corr, lags, trace_a, trace_b, trace_time = self.driver.get_data_as_arrays(0)
self.assertTrue(np.allclose(corr, test_data.corr_without_nans))
self.assertTrue(np.allclose(lags, test_data.lags_without_nans))
self.assertTrue(np.allclose(trace_a, test_data.trace_a))
self.assertTrue(np.allclose(trace_b, test_data.trace_b))
self.assertTrue(np.allclose(trace_time, test_data.trace_time))
def test_WHEN_data_taken_THEN_start_called(self):
"""
Test that the start method is called when data is taken
"""
self.driver.take_data(0)
self.device.start.assert_called_once()
def test_WHEN_data_taken_AND_measurement_on_THEN_update_called_WHEN_measurement_off_THEN_update_not_called(
self,
):
"""
Test that the update method is called when data is taken and measurement is on
"""
starting_update_count = self.mocked_api.update_count
self.driver.take_data(0)
self.assertGreater(self.mocked_api.update_count, starting_update_count)
self.assertFalse(self.mocked_api.update_called_when_measurement_not_on)
def test_WHEN_save_data_THEN_metadata_written_AND_correlation_data_written_AND_traces_written(
self,
):
"""
Test that the metadata and correlation data are written and the traces are written
"""
# Gather some dummy data that matches the test_data.dat file
self.device.Correlation = test_data.corr
self.device.Lags = test_data.lags
self.device.TraceChA = test_data.trace_a
self.device.TraceChB = test_data.trace_b
metadata = {
Records.SCATTERING_ANGLE.name: 110,
Records.MEASUREMENTDURATION.name: 10,
Records.LASER_WAVELENGTH.name: 642,
Records.SOLVENT_REFRACTIVE_INDEX.name: 1.33,
Records.SOLVENT_VISCOSITY.name: 1,
Records.SAMPLE_TEMP.name: 298,
}
# Save data to two temporary files that are discarded
with NamedTemporaryFile(mode="w+") as user_file, NamedTemporaryFile(
mode="w+"
) as archive_file:
self.driver.save_data(0, user_file, archive_file, metadata)
# Read test_data.dat
with open(test_data.test_data_file, mode="r", encoding="utf-8") as test_data_file:
test_actual_data = test_data_file.read()
# Go back to start of files after write and ignore firstline that has timestamp on it
for file in [user_file, archive_file]:
file.seek(0)
file.readline()
# Assert content is equal
self.assertEqual(test_actual_data, file.read())
if __name__ == "__main__":
unittest.main()