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

from collections import defaultdict 

 

from django.urls import reverse 

from django.test import Client 

from hypothesis.extra.django import TestCase, from_model 

from hypothesis.strategies import integers, characters, dates, sampled_from, composite, just, one_of 

from hypothesis_csv.strategies import csv 

from csv import Dialect, QUOTE_MINIMAL 

from hypothesis import given, settings, assume, note 

 

from members.models import DeliveryNumber, Person 

from members.api_helper import post_countries 

from django.core.files.uploadedfile import SimpleUploadedFile 

 

c = Client() 

 

delivery_num_strategy = from_model(DeliveryNumber, 

recipient=from_model(Person, 

chaos_number=integers(min_value=1, max_value=100000000) 

) 

) 

 

 

class PostCSV(Dialect): 

delimiter = ';' 

quoting = QUOTE_MINIMAL 

quotechar = '"' 

lineterminator = '\r\n' 

doublequote = True 

escapechar = None 

 

 

@composite 

def csv_delivery_num(draw): 

delivery_num = draw(delivery_num_strategy) 

# iso8895-15 is roughly the Unicode subsect from 0 to 0xFF 

# The differences are caught by catching encoding exceptions 

iso8895_15_compat = characters(min_codepoint=0, max_codepoint=0xFF, blacklist_characters='\0') 

sdgs = draw(one_of(integers(), sampled_from([10, 20, 30, 40]))) 

adrmerk = draw( 

one_of(integers(), sampled_from([10, 11, 13, 14, 18, 19, 21, 22, 25, 30, 31, 50, 53, 55, 12, 20, 51]))) 

delivery_num_number = draw(one_of(just(delivery_num.number), iso8895_15_compat)) 

nsa = { 

'NSA_Na1': draw(iso8895_15_compat), 

'NSA_Na2': draw(iso8895_15_compat), 

'NSA_Na3': draw(iso8895_15_compat), 

'NSA_Na4': draw(iso8895_15_compat), 

'NSA_Str': draw(iso8895_15_compat), 

'NSA_HNr': draw(iso8895_15_compat), 

'NSA_PLZ': draw(iso8895_15_compat), 

'NSA_Ort': draw(iso8895_15_compat), 

'NSA_Land': draw(one_of(sampled_from(sorted(post_countries.keys())), iso8895_15_compat)), 

'NSA_Postf': draw(iso8895_15_compat), 

'NSA_PLZPostf': draw(iso8895_15_compat), 

'NSA_OrtPostfach': draw(iso8895_15_compat), 

'NSA_LandPostfach': draw(one_of(sampled_from(sorted(post_countries.keys())), iso8895_15_compat)), 

} 

csv_values = { 

'UebgID': integers(), 

'Fehlercodes': integers(), 

'PreAdrID': integers(), 

'ProdVar': integers(), 

'VersProd': integers(), 

'ProdID': integers(), 

'FrkArt': integers(), 

'FrkDat': dates().map(lambda d: d.strftime('%Y-%m-%d')), 

'EinlAbrNr': integers(), 

'SerienNr': integers(), 

'AuftrNr': integers(), 

'ZKZ': integers(), 

'HeftNr': integers(), 

'AboNr': integers(), 

'SdgNr': integers(), 

'Teiln': integers(), 

'KdInfoDMC': integers(), 

'SdgS': just(sdgs), 

'AdrMerk': just(adrmerk), 

'AbrProdNrAdress': integers(), 

'AbrProdRetoure': integers(), 

'AbrKontrakt': integers(), 

'Kd_Na1': iso8895_15_compat, 

'Kd_Na2': iso8895_15_compat, 

'Kd_Na3': iso8895_15_compat, 

'Kd_Na4': iso8895_15_compat, 

'Kd_Str': iso8895_15_compat, 

'Kd_HNr': iso8895_15_compat, 

'Kd_PLZ': iso8895_15_compat, 

'Kd_Ort': iso8895_15_compat, 

'Kd_Postf': iso8895_15_compat, 

'Kd_Info': just(delivery_num_number), 

'E_Na1': iso8895_15_compat, 

'E_Na2': iso8895_15_compat, 

'E_Na3': iso8895_15_compat, 

'E_Na4': iso8895_15_compat, 

'E_Str': iso8895_15_compat, 

'E_HNr': iso8895_15_compat, 

'E_PLZ': iso8895_15_compat, 

'E_Ort': iso8895_15_compat, 

'E_Postf': iso8895_15_compat, 

'NSA_Na1': just(nsa['NSA_Na1']), 

'NSA_Na2': just(nsa['NSA_Na2']), 

'NSA_Na3': just(nsa['NSA_Na3']), 

'NSA_Na4': just(nsa['NSA_Na4']), 

'NSA_Str': just(nsa['NSA_Str']), 

'NSA_HNr': just(nsa['NSA_HNr']), 

'NSA_PLZ': just(nsa['NSA_PLZ']), 

'NSA_Ort': just(nsa['NSA_Ort']), 

'NSA_Land': just(nsa['NSA_Land']), 

'NSA_Postf': just(nsa['NSA_Postf']), 

'NSA_PLZPostf': just(nsa['NSA_PLZPostf']), 

'NSA_OrtPostfach': just(nsa['NSA_OrtPostfach']), 

'NSA_LandPostfach': just(nsa['NSA_LandPostfach']), 

'KdInfoDMC_Dezimal': integers(), 

'KdInfoDMC_Ascii': iso8895_15_compat, 

} 

csv_draw = draw(csv(header=csv_values.keys(), columns=csv_values.values(), lines=1, dialect=PostCSV)) 

return csv_draw, delivery_num, delivery_num_number, sdgs, adrmerk, nsa 

 

 

def addr_unk(person_orig, person_mod, delivery_number, _): 

"""No failure and the address unknown counter should be counted up""" 

assert person_orig.address_unknown + 1 == person_mod.address_unknown 

assert delivery_number.returned is True 

 

 

def new_addr(person_orig, person_mod, _, nsa): 

"""The address should be changed and address_unknown should be 0""" 

is_postfach = bool(nsa['NSA_Postf']) 

 

country = nsa['NSA_LandPostfach'] if is_postfach else nsa['NSA_Land'] 

if country in post_countries: 

assert (post_countries[country] == person_mod.address_country) 

else: 

failed(person_orig, person_mod, None, nsa) 

return 

 

def rm_whitespace(string): 

return ''.join(string.split()) 

 

address_from_person = rm_whitespace(person_mod.first_name + person_mod.last_name + (person_mod.address_1 or '') + ( 

person_mod.address_2 or '') + (person_mod.address_3 or '')) 

 

143 ↛ 147line 143 didn't jump to line 147, because the condition on line 143 was never false if is_postfach: 

assert (address_from_person == rm_whitespace(nsa['NSA_Na1'] + nsa['NSA_Na2'] + nsa['NSA_Na3'] + nsa['NSA_Na4'] + 

nsa['NSA_Postf'] + nsa['NSA_PLZPostf'].zfill(5) + nsa['NSA_OrtPostfach'])) 

else: 

assert (address_from_person == rm_whitespace(nsa['NSA_Na1'] + nsa['NSA_Na2'] + nsa['NSA_Na3'] + nsa['NSA_Na4'] + 

nsa['NSA_Str'] + nsa['NSA_HNr'] + nsa['NSA_PLZ'].zfill(5) + nsa['NSA_Ort'])) 

assert (person_mod.address_unknown == 0) 

 

 

def death(_1, person_mod, _2, _3): 

"""Person has been removed from the database""" 

assert (person_mod is None) 

 

 

def failed(person_orig, person_mod, _1, _2): 

"""No changes in person object""" 

assert (person_orig == person_mod) 

 

 

def failed_row(): 

return defaultdict(ret_failed) 

 

 

def ret_failed(): 

return failed 

 

 

"""This table represents all possible combinations and their desired outcomes for Sendungsschicksal and Adressvermerk. 

Usage: state_table[AdrMerk][SdgS] -> test_function(person, ...)""" 

state_table = defaultdict(failed_row, { 

10: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

11: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

12: defaultdict(ret_failed, {10: death, 20: failed, 30: failed, 40: death}), 

13: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

14: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

18: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

19: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

20: defaultdict(ret_failed, {10: new_addr, 20: new_addr, 30: failed, 40: new_addr}), 

21: defaultdict(ret_failed, {10: addr_unk, 20: addr_unk, 30: failed, 40: addr_unk}), 

22: defaultdict(ret_failed, {10: addr_unk, 20: addr_unk, 30: failed, 40: addr_unk}), 

25: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

30: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

31: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

50: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

51: defaultdict(ret_failed, {10: new_addr, 20: new_addr, 30: failed, 40: new_addr}), 

53: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

55: defaultdict(ret_failed, {10: addr_unk, 20: failed, 30: failed, 40: addr_unk}), 

}) 

 

 

class ImportPremiumAdressTestCase(TestCase): 

 

@given(csv_delivery_num()) 

@settings(deadline=None, max_examples=1000) 

def test_import_premium_adress(self, tupled_test_data): 

csv_string, delivery_num, delivery_num_number, sdgs, adrmerk, nsa = tupled_test_data 

try: 

encoded = csv_string.encode("iso-8859-15") 

except UnicodeEncodeError: 

assume(False) 

return 

 

# Take note of person state before import 

person_orig = delivery_num.recipient 

 

csv_upload = SimpleUploadedFile("upload.csv", encoded, content_type="text/plain") 

response = c.post(reverse('members:premiumadress_import'), {'docfile': csv_upload}) 

 

# No matter what error happens, the webpage should load properly 

assert response.status_code == 200 

 

person_mod = Person.objects.filter(chaos_number=person_orig.chaos_number).first() 

 

# Special case of wrong delivery number 

if delivery_num_number != delivery_num.number: 

failed(person_orig, person_mod, None, None) 

return 

 

# Check for all different kinds of possible combinations 

action = state_table[adrmerk][sdgs] 

note('Sdgs: {} AdrMerk: {} Action: {}'.format(sdgs, adrmerk, action.__name__)) 

delivery_num_mod = DeliveryNumber.objects.filter(number=delivery_num.number).first() 

action(person_orig, person_mod, delivery_num_mod, nsa)