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

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

 

from pydgilib_extra import ( 

InterfaceData, LoggerData, INTERFACE_POWER, INTERFACE_SPI, INTERFACE_GPIO) 

 

 

def test_init_logger_data(): 

"""Test instantiations.""" 

# Simple instantiaton 

data = LoggerData() 

assert tuple(data[INTERFACE_POWER]) == () 

assert tuple(data[INTERFACE_GPIO]) == () 

 

# Instantiaton from list of interfaces 

data = LoggerData([INTERFACE_GPIO, INTERFACE_POWER]) 

assert tuple(data[INTERFACE_POWER]) == () 

assert tuple(data[INTERFACE_GPIO]) == () 

 

# Instantiation from dictionary with empty values 

data = LoggerData( 

{INTERFACE_POWER: InterfaceData(), INTERFACE_GPIO: InterfaceData()} 

) 

assert tuple(data[INTERFACE_POWER]) == () 

assert tuple(data[INTERFACE_GPIO]) == () 

 

# Instantiation from dictionary 

data = LoggerData( 

{INTERFACE_POWER: ([], []), INTERFACE_GPIO: ([], [])}) 

assert tuple(data[INTERFACE_POWER]) == () 

assert tuple(data[INTERFACE_GPIO]) == () 

 

# Instantiation from dictionary with data 

data = LoggerData( 

{INTERFACE_POWER: ([1], [2]), INTERFACE_GPIO: ([3], [4])}) 

assert tuple(data[INTERFACE_POWER]) == ((1, 2),) 

assert tuple(data[INTERFACE_GPIO]) == ((3, 4),) 

 

# Instantiation from dictionary with InterfaceData 

data = LoggerData({ 

INTERFACE_POWER: InterfaceData(([1], [2])), 

INTERFACE_GPIO: InterfaceData(([3], [4]))}) 

assert tuple(data[INTERFACE_POWER]) == ((1, 2),) 

assert tuple(data[INTERFACE_GPIO]) == ((3, 4),) 

 

 

def test__getattr__(): 

"""Tests for __getattr__ function.""" 

data = LoggerData({ 

INTERFACE_POWER: ([1], [2]), 

INTERFACE_GPIO: ([], []), 

4: ([3, 4], [5, 6])}) 

 

# Getting via dict 

assert tuple(data[INTERFACE_POWER]) == ((1, 2),) 

# Getting via attribute 

assert tuple(data.gpio) == () 

# assert data["gpio"] == ([3], [4]) # Not in syntax 

 

 

def test__setattr__(): 

"""Tests for __setattr__ function.""" 

data = LoggerData({INTERFACE_POWER: ([1], [2])}) 

 

# Setting as dict 

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

assert tuple(data[INTERFACE_POWER]) == ((1, 2),) 

assert tuple(data[INTERFACE_GPIO]) == ((3, 4),) 

 

# Setting as attribute 

data.spi = InterfaceData(([5], [6])) 

assert tuple(data[INTERFACE_SPI]) == ((5, 6),) 

 

 

def test__iadd__(): 

"""Tests for __iadd__ function.""" 

# Add dict for existing interface 

data = LoggerData({INTERFACE_POWER: ([1], [2])}) 

data += {INTERFACE_POWER: ([2], [3])} 

assert tuple(data[INTERFACE_POWER]) == ((1, 2), (2, 3)) 

# Add LoggerData for existing interface 

data = LoggerData({INTERFACE_POWER: ([1], [2])}) 

data += LoggerData({INTERFACE_POWER: ([2], [3])}) 

assert tuple(data[INTERFACE_POWER]) == ((1, 2), (2, 3)) 

 

# Add dict and LoggerData with new interfaces 

data = LoggerData({INTERFACE_POWER: ([1], [2])}) 

data += {INTERFACE_GPIO: ([2], [3])} 

data += LoggerData({4: ([3], [4])}) 

assert tuple(data[INTERFACE_POWER]) == ((1, 2),) 

assert tuple(data[INTERFACE_GPIO]) == ((2, 3),) 

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

 

# Add dict and LoggerData for new and existing interfaces 

data = LoggerData({ 

INTERFACE_POWER: ([1], [2]), 

4: ([3, 4], [5, 6])}) 

data += { 

INTERFACE_POWER: ([2], [3]), INTERFACE_GPIO: ([2], [3])} 

data += LoggerData({INTERFACE_POWER: ([3], [4]), 

INTERFACE_GPIO: ([1], [2])}) 

assert tuple(data[INTERFACE_POWER]) == ((1, 2), (2, 3), (3, 4)) 

assert tuple(data[INTERFACE_GPIO]) == ((2, 3), (1, 2)) 

assert tuple(data[4]) == ((3, 5), (4, 6)) 

 

 

def test__add__(): 

"""Tests for __add__ function.""" 

# Simple addition of objects 

data1 = LoggerData({INTERFACE_POWER: ([1], [2])}) 

data2 = LoggerData({INTERFACE_POWER: ([2], [3])}) 

data = data1 + data2 

assert tuple(data[INTERFACE_POWER]) == ((1, 2), (2, 3)) 

# Check that data has been deep copied 

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

assert tuple(data[INTERFACE_POWER]) == ((1, 2), (2, 3)) 

# Delete original copies (decrease reference count to them) 

del data1 

del data2 

assert tuple(data[INTERFACE_POWER]) == ((1, 2), (2, 3)) 

# # Check that data has been shallow copied 

# data = LoggerData({INTERFACE_POWER: ([1], [2])}), "Incorrect value" 

# data1 = data 

# del data 

# assert data1[INTERFACE_POWER] == ((1, 2),), "Incorrect value" 

# Check that data has been deep copied 

data = LoggerData({INTERFACE_POWER: ([1], [2])}) 

data1 = data + {} 

del data 

assert tuple(data1[INTERFACE_POWER]) == ((1, 2),) 

 

 

def test_extend(): 

"""Tests for extend function.""" 

# Simple extention of objects 

data = LoggerData({INTERFACE_POWER: ([1], [2])}) 

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

data.extend(INTERFACE_POWER, data1) 

assert tuple(data[INTERFACE_POWER]) == ((1, 2), (2, 3)) 

 

 

def test_length(): 

"""Tests for length function.""" 

data = LoggerData({ 

INTERFACE_POWER: ([1], [2]), 

INTERFACE_GPIO: ([], []), 

4: ([3, 4], [5, 6])}) 

# Length of individual interfaces 

assert data.length(INTERFACE_POWER) == 1 

assert data.length(INTERFACE_GPIO) == 0 

assert data.length(4) == 2 

 

# Lengths in dict of interfaces 

len_dict = data.length() 

assert len_dict[INTERFACE_POWER] == 1 

assert len_dict[INTERFACE_GPIO] == 0 

assert len_dict[4] == 2