Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

"""This module holds the automated tests for InterfaceData.""" 

 

from pydgilib_extra import (InterfaceData, valid_interface_data) 

 

 

def test_new_interface_data(): 

"""Test instantiations.""" 

# Simple instantiaton 

data = InterfaceData() 

assert tuple(data) == () 

 

# Instantiation from tuple 

data = InterfaceData(([], [])) 

assert tuple(data) == () 

 

# Instantiation from list 

data = InterfaceData([[1], [2]]) 

assert tuple(data) == ((1, 2),) 

 

# Instantiation from tuple 

data = InterfaceData(([1], [2])) 

assert tuple(data) == ((1, 2),) 

 

# Instantiation from tuple 

data = InterfaceData(([1, 2], [3, 4])) 

assert tuple(data) == ((1, 3), (2, 4),) 

 

# Instantiation from two lists 

data = InterfaceData([1, 2], [3, 4]) 

assert tuple(data) == ((1, 3), (2, 4),) 

 

# Instantiation from two ints 

data = InterfaceData(1, 2) 

assert tuple(data) == ((1, 2),) 

 

# try catch assert error? 

# assertRaises() 

# https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises 

 

 

def test__getattr__(): 

"""Tests for timestamps and values functionality.""" 

# Simple instantiaton 

data = InterfaceData() 

assert data.timestamps == [] 

assert data.values == [] 

 

# Instantiation from tuple 

data = InterfaceData(([], [])) 

assert data.timestamps == [] 

assert data.values == [] 

 

# Instantiation from list 

data = InterfaceData([[1], [2]]) 

assert data.timestamps == [1] 

assert data.values == [2] 

 

# Instantiation from tuple 

data = InterfaceData(([1], [2])) 

assert data.timestamps == [1] 

assert data.values == [2] 

 

# Instantiation from tuple 

data = InterfaceData(([1, 2], [3, 4])) 

assert data.timestamps == [1, 2] 

assert data.values == [3, 4] 

assert data[0] == (1, 3) 

assert data[1] == (2, 4) 

 

# Getting as tuple 

assert tuple(data) == ((1, 3), (2, 4),) 

 

 

def test__setattr__(): 

"""Tests for timestamps and values functionality.""" 

data = InterfaceData([[1], [2]]) 

 

# Setting as tuple (not recommended) 

data.timestamps[0] = 3 

assert tuple(data) == ((3, 2),) 

 

# Setting as attribute (not recommended) 

data.values[0] = 4 

assert tuple(data) == ((3, 4),) 

 

 

def test__iadd__(): 

"""Tests for __iadd__ function.""" 

# Add tuple for existing interface 

data = InterfaceData([[1], [2]]) 

data += ([2], [3]) 

assert tuple(data) == ((1, 2), (2, 3)) 

# Add list for existing interface 

data += [[3], [4]] 

assert tuple(data) == ((1, 2), (2, 3), (3, 4)) 

# Add InterfaceData for existing interface 

data = InterfaceData([[1], [2]]) 

data += InterfaceData([[2, 3], [3, 4]]) 

assert tuple(data) == ((1, 2), (2, 3), (3, 4)) 

 

 

def test__add__(): 

"""Tests for __add__ function.""" 

# Simple addition of objects 

data1 = InterfaceData([[1], [2]]) 

data2 = InterfaceData([[2], [3]]) 

data = data1 + data2 

assert tuple(data) == ((1, 2), (2, 3)) 

# Check that data has been deep copied 

data1 = InterfaceData(([4], [5])) 

assert tuple(data) == ((1, 2), (2, 3)) 

# Delete original copies (decrease reference count to them) 

del data1 

del data2 

assert tuple(data) == ((1, 2), (2, 3)) 

# Check that data has been shallow copied 

data = InterfaceData([[1], [2]]) 

data1 = data 

data += InterfaceData([[3], [4]]) 

assert tuple(data1) == ((1, 2), (3, 4)) 

# Check that data has been deep copied 

data = InterfaceData([[1], [2]]) 

data1 = data + InterfaceData() 

data += InterfaceData([[3], [4]]) 

assert tuple(data1) == ((1, 2),) 

 

 

def test_extend(): 

"""Tests for extend function.""" 

# Simple extention of objects 

data = InterfaceData([[1], [2]]) 

data1 = InterfaceData([[2], [3]]) 

data.extend(data1) 

assert tuple(data) == ((1, 2), (2, 3)) 

# Extention with empty lists and tuples 

data = InterfaceData([[1], [2]]) 

data.extend(InterfaceData()) 

assert tuple(data) == ((1, 2),) 

data.extend(InterfaceData(([], []))) 

assert tuple(data) == ((1, 2),) 

data.extend(InterfaceData([[], []])) 

assert tuple(data) == ((1, 2),) 

data.extend(([], [])) 

assert tuple(data) == ((1, 2),) 

data.extend([[], []]) 

assert tuple(data) == ((1, 2),) 

# Extention with non-empty lists and tuples 

data = InterfaceData([[1], [2]]) 

data.extend(InterfaceData()) 

assert tuple(data) == ((1, 2),) 

data = InterfaceData([[1], [2]]) 

data.extend(InterfaceData(([2], [3]))) 

assert tuple(data) == ((1, 2), (2, 3)) 

data = InterfaceData([[1], [2]]) 

data.extend(InterfaceData([[2], [3]])) 

assert tuple(data) == ((1, 2), (2, 3)) 

data = InterfaceData([[1], [2]]) 

data.extend(([2], [3])) 

assert tuple(data) == ((1, 2), (2, 3)) 

data = InterfaceData([[1], [2]]) 

data.extend([[2], [3]]) 

assert tuple(data) == ((1, 2), (2, 3)) 

 

 

def test__len__(): 

"""Tests for __len__ function.""" 

# Simple instantiaton 

data = InterfaceData() 

assert len(data) == 0 

 

# Instantiation from tuple 

data = InterfaceData(([], [])) 

assert len(data) == 0 

 

# Instantiation from list 

data = InterfaceData([[1], [2]]) 

assert len(data) == 1 

 

# Instantiation from tuple 

data = InterfaceData(([1], [2])) 

assert len(data) == 1 

 

# Instantiation from tuple 

data = InterfaceData(([1, 2], [3, 4])) 

assert len(data) == 2 

 

 

def test__getitem__(): 

"""Tests for __getitem__ function.""" 

data = InterfaceData(([1, 2], [3, 4])) 

assert data[0] == (1, 3) 

assert data[1] == (2, 4) 

assert data[::-1] == ([2, 1], [4, 3]) 

data.append([[9, 8, 7, 6], [34, 45, 56, 67]]) 

assert data[4:6] == ([7, 6], [56, 67]) 

 

# Loops 

for timestamp, value in data: 

assert timestamp == 1 

assert value == 3 

break 

for timestamp, value in reversed(data): 

assert timestamp == 6 

assert value == 67 

break 

for sample in data: 

assert InterfaceData(*sample) in data 

 

 

def test__contains__(): 

"""Tests for __contains__ function.""" 

data = InterfaceData(([1, 2], [3, 4])) 

assert ([2], [4]) in data 

assert ([9], [34]) not in data 

assert ([], []) in data 

assert data in data + ([10], [34]) 

assert data + ([10], [34]) not in data 

assert data + ([10], [34]) in data + ([10], [34]) 

 

 

def test_valid_interface_data(): 

"""Tests for valid_interface_data function.""" 

assert valid_interface_data(([], [])) 

assert valid_interface_data(([1], [2])) 

assert valid_interface_data(([1, 2], [2, 3])) 

 

assert not valid_interface_data([]) 

assert not valid_interface_data(([], [], [])) 

assert not valid_interface_data(([])) 

assert not valid_interface_data(([1], [])) 

assert not valid_interface_data(([], [1]))