-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
127 lines (93 loc) · 4.15 KB
/
tests.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
''' Unit tests for bkp891.py '''
from __future__ import print_function
import unittest
import bkp891
from serial import SerialException
def dummy_sendcmd(command):
''' Dummy sendcmd method for testing SCPI command generation '''
return bytes('{0}\n'.format(command), 'utf-8')
class TestConnect(unittest.TestCase):
''' Test cases for the connect function '''
def setUp(self):
''' Test setup '''
self.bkp = bkp891
self.goodserial = '/dev/tty.SLAB_USBtoUART'
self.badserial = '/dev/null'
def test_goodconnect(self):
''' Test to ensure our 'happy path' of a valid serial connection.
Assumes self.goodserial is set to a valid serial device '''
try:
scpi_obj = self.bkp.connect(self.goodserial)
except SerialException:
print('\n!!!!!!! IMPORTANT !!!!!!!!\n',
'Skipped test: test_goodconnect because no device found\n'
'!!!!!!! IMPORTANT !!!!!!!!')
return
self.assertTrue(isinstance(scpi_obj, bkp891.ScpiConnection))
def test_badconnect(self):
''' Test to ensure our 'unhappy path' is producing the correct
exception. Assumes self.badserial is set to an invalid
serial device. '''
with self.assertRaises(SerialException):
self.bkp.connect(self.badserial)
class TestParse(unittest.TestCase):
''' Test cases for the parse function '''
def setUp(self):
''' Test setup '''
self.bkp = bkp891
def test_integers(self):
''' Tests integer parsing '''
ints = self.bkp.parse('+800,-900234,0,27'.encode('ascii'))
self.assertTupleEqual(ints, (800, -900234, 0, 27))
def test_floats(self):
''' Tests float parsing '''
floats = self.bkp.parse('+2.345678e+04,-1.34567e-01'.encode('ascii'))
self.assertTupleEqual(floats, (23456.78, -0.134567))
def test_booleans(self):
''' Tests boolean parsing '''
bools = self.bkp.parse('ON,OFF'.encode('ascii'))
self.assertTupleEqual(bools, (True, False))
def test_literals(self):
''' Tests string literal parsing '''
literals = self.bkp.parse('HOLD,TESTING'.encode('ascii'))
self.assertTupleEqual(literals, ('HOLD', 'TESTING'))
def test_blanks(self):
''' Tests blank '----' results '''
blanks = self.bkp.parse('----,N'.encode('ascii'))
self.assertTupleEqual(blanks, (None, None))
def test_single(self):
''' Tests non-tuple single result '''
single_int = self.bkp.parse('123'.encode('ascii'))
single_float = self.bkp.parse('+2.345678e+04'.encode('ascii'))
single_none = self.bkp.parse('N'.encode('ascii'))
single_bool = self.bkp.parse('ON'.encode('ascii'))
single_literal = self.bkp.parse('HOLD'.encode('ascii'))
self.assertTupleEqual(
(single_int, single_float, single_none, single_bool, single_literal),
(123, 23456.78, None, True, 'HOLD')
)
class TestScpiConnection(unittest.TestCase):
''' Tests SCPI Connection object - specifically the command output '''
def setUp(self):
''' Sets up our ScpiConnection object with sendcmd dummy object '''
self.scpi = bkp891.ScpiConnection(None, 100)
self.scpi.sendcmd = dummy_sendcmd
def test_badfrequency(self):
''' Ensures appropriate exception is thrown for set_frequency. '''
with self.assertRaises(bkp891.ScpiException):
self.scpi.set_frequency(90)
def test_badprimary(self):
''' Ensures appropriate exception is thrown for set_primary. '''
with self.assertRaises(bkp891.ScpiException):
self.scpi.set_primary('THETA')
def test_badsecondary(self):
''' Ensures appropriate exception is thrown for set_primary. '''
with self.assertRaises(bkp891.ScpiException):
self.scpi.set_secondary('L')
def test_badtolerance(self):
''' Ensures appropriate exception is thrown for
set_tolerance_range. '''
with self.assertRaises(bkp891.ScpiException):
self.scpi.set_tolerance_range(3)
if __name__ == '__main__':
unittest.main()