-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
224 lines (201 loc) · 9.48 KB
/
test.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
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
import unittest
import pyvcard
from traceback import print_exc
import os
from fuzzywuzzy import fuzz
vcard_dir = "./vcards/"
test_path = "./tests/"
bundle = pyvcard.vCardSet()
indexer = pyvcard.vCardIndexer(index_params=True)
for i in os.listdir(vcard_dir):
if i.endswith(".vcf"):
pth = os.path.join(vcard_dir, i)
try:
vcard = pyvcard.openfile(pth, encoding="utf-8", indexer=indexer).vcards()
bundle.update(vcard)
except pyvcard.vCardValidationError as e:
print(pth, str(e))
except Exception as e:
# print_exc()
print(pth, str(e))
bundle.setindex(indexer)
def wratio(str1, str2):
return fuzz.WRatio(str1, str2)
class vcardtest(unittest.TestCase):
def test_parsing_errors(self):
c = 0
for i in os.listdir(os.path.join(vcard_dir, "errors")):
try:
if i.endswith(".vcf"):
pth = os.path.join(vcard_dir, i)
pyvcard.openfile(pth, encoding="utf-8").vcards()
c += 1
except Exception:
pass
self.assertEqual(c, 0)
def test_building_vcf(self):
factory = pyvcard.builder()
factory.set_version("4.0")
factory.set_phone(12345678910)
factory.set_name("Test App")
txt = factory.build().repr_vcard()
f = open(os.path.join(test_path, "factory.xml"), "w", encoding="utf-8")
f.write(txt)
f.close()
def test_converting_vcf(self):
txt = bundle.repr_vcard()
f = open(os.path.join(test_path, "test.vcf"), "w", encoding="utf-8")
f.write(txt)
f.close()
vcardfd = pyvcard.openfile(os.path.join(test_path, "test.vcf"), "r", encoding="utf-8")
txt = vcardfd.vcards().repr_vcard()
f = open(os.path.join(test_path, "test2.vcf"), "w", encoding="utf-8")
f.write(txt)
f.close()
def test_csv(self):
txt = pyvcard.convert(bundle).csv().result()
f = open(os.path.join(test_path, "test.csv"), "w", encoding="utf-8")
f.write(txt)
f.close()
pyvcard.parse_from(txt, "csv").vcards()
def test_csv_permanent(self):
txt = pyvcard.convert(bundle).csv().permanent_result()
f = open(os.path.join(test_path, "test2.csv"), "w", encoding="utf-8")
f.write(txt)
f.close()
def test_json(self):
txt = pyvcard.convert(bundle).json().result()
f = open(os.path.join(test_path, "test.json"), "w", encoding="utf-8")
f.write(txt)
f.close()
pyvcard.parse_from(txt, "json").vcards()
def test_jsoncsv_examples(self):
f = open(os.path.join(test_path, "xmltest.xml"), "r", encoding="utf-8")
txt = f.read()
f.close()
pyvcard.parse_from(txt, "xml").vcards()
f = open(os.path.join(test_path, "jtest.json"), "r", encoding="utf-8")
txt = f.read()
f.close()
pyvcard.parse_from(txt, "json").vcards()
def test_xml(self):
txt = pyvcard.convert(bundle).xml().result()
f = open(os.path.join(test_path, "test.xml"), "w", encoding="utf-8")
f.write(txt)
f.close()
pyvcard.parse_from(txt, "xml").vcards()
with open(os.path.join(vcard_dir, "rfc6351-example.xml")) as f:
pyvcard.parse_from(f.read(), "xml").vcards()
def test_search(self):
self.file = open(os.path.join(test_path, "log.txt"), "w", encoding="utf-8")
r = [None for i in range(17)]
r[0] = bundle.find_by_name("Андрей", fullmatch=False, case=False, indexsearch=False)
r[1] = bundle.find_by_name("Андрей", fullmatch=True, case=True, indexsearch=False)
r[2] = bundle.find_by_name("Дима", fullmatch=True, case=True, indexsearch=False)
r[3] = bundle.find_by_phone_endswith("185", indexsearch=False)
r[4] = bundle.find_by_phone_endswith(185, indexsearch=False)
r[5] = bundle.find_by_phone_startswith(7937, indexsearch=False)
r[6] = bundle.find_by_phone_startswith("7937", indexsearch=False)
r[7] = bundle.find_by_phone("1234567890", indexsearch=False)
r[8] = bundle.find_by_phone(1234567890, indexsearch=False)
r[9] = bundle.find_by_group("item0", indexsearch=False)
r[10] = bundle.find_by_group("Item0", case=True, indexsearch=False)
r[11] = bundle.find_by_value("VCARD", fullmatch=True, indexsearch=False)
r[11] = bundle.find_by_value("VCARD", indexsearch=False)
r[12] = bundle.find_by_property("PROFILE", "VCARD", indexsearch=False)
r[13] = bundle.find_by_phone_endswith("890", indexsearch=False)
r[14] = bundle.find_by_phone_endswith(89, indexsearch=False)
r[15] = bundle.find_by_phone("1234567890", fullmatch=False, indexsearch=False)
r[16] = bundle.find_by_phone(1234567890, fullmatch=False, indexsearch=False)
for i in range(len(r)):
self.file.write(str(i) + "=" * 10 + "\n")
for vcard in r[i]:
self.file.write(str(vcard.contact_data()) + "\n")
self.file.close()
def test_indexer(self):
self.file = open(os.path.join(test_path, "log2.txt"), "w", encoding="utf-8")
r = [None for i in range(17)]
r[0] = bundle.find_by_name("Андрей", fullmatch=False, case=False)
r[1] = bundle.find_by_name("Андрей", fullmatch=True, case=True)
r[2] = bundle.find_by_name("Дима", fullmatch=True, case=True)
r[3] = bundle.find_by_phone_endswith("185")
r[4] = bundle.find_by_phone_endswith(185)
r[5] = bundle.find_by_phone_startswith(7937)
r[6] = bundle.find_by_phone_startswith("7937")
r[7] = bundle.find_by_phone("1234567890")
r[8] = bundle.find_by_phone(1234567890)
r[9] = bundle.find_by_group("item0")
r[10] = bundle.find_by_group("Item0", case=True)
r[11] = bundle.find_by_value("VCARD", fullmatch=True)
r[11] = bundle.find_by_value("VCARD")
r[12] = bundle.find_by_property("PROFILE", "VCARD")
r[13] = bundle.find_by_phone_endswith("890")
r[14] = bundle.find_by_phone_endswith(89)
r[15] = bundle.find_by_phone("1234567890", fullmatch=False)
r[16] = bundle.find_by_phone(1234567890, fullmatch=False)
for i in range(len(r)):
self.file.write(str(i) + "=" * 10 + "\n")
for vcard in r[i]:
self.file.write(str(vcard.contact_data()) + "\n")
self.file.close()
def test_difference_search(self):
self.file = open(os.path.join(test_path, "log3.txt"), "w", encoding="utf-8")
r = [None for i in range(12)]
r[0] = bundle.difference_search("phone", "12345678", wratio)
r[1] = bundle.difference_search("phone", "1234567", wratio)
r[2] = bundle.difference_search("name", "Дима", wratio)
r[3] = bundle.difference_search("name", "Андрей", wratio)
r[4] = bundle.difference_search("param", "VCARD", wratio)
r[5] = bundle.difference_search("param", "VCARD", wratio)
r[6] = bundle.difference_search("phone", "12345678", wratio, indexsearch=False)
r[7] = bundle.difference_search("phone", "1234567", wratio, indexsearch=False)
r[8] = bundle.difference_search("name", "Дима", wratio, indexsearch=False)
r[9] = bundle.difference_search("name", "Андрей", wratio, indexsearch=False)
r[10] = bundle.difference_search("param", "VCARD", wratio, indexsearch=False)
r[11] = bundle.difference_search("param", "VCARD", wratio, indexsearch=False)
for i in range(len(r)):
self.file.write(str(i) + "=" * 10 + "\n")
for vcard in r[i]:
self.file.write(str(vcard.contact_data()) + "\n")
self.file.close()
def test_util(self):
self.assertEqual(pyvcard.is_vcard(list(bundle)[0]), True)
self.assertEqual(pyvcard.is_vcard_property(list(bundle)[0][0]), True)
self.assertEqual(pyvcard.is_vcard([]), False)
self.assertEqual(pyvcard.is_vcard_property(list(bundle)[0]), False)
self.assertEqual(pyvcard.strinteger("12345-122345"), 12345122345)
def test_html(self):
conv = pyvcard.convert(bundle)
h = conv.html()
txt = h.strresult()
h.include_to_html(os.path.join(test_path, "bightml.html"))
f = open(os.path.join(test_path, "test.html"), "w", encoding="utf-8")
f.write(txt)
f.close()
parsed = pyvcard.parse_from(txt, "html").vcards()
f = open(os.path.join(test_path, "parsed.vcf"), "w", encoding="utf-8")
f.write(parsed.repr_vcard())
f.close()
f = open(os.path.join(test_path, "example.html"), "r", encoding="utf-8")
txt = f.read()
f.close()
parsed = pyvcard.parse_from(txt, "html").vcards()
def test_type(self):
for vcard in bundle:
for prop in vcard:
prop.typedvalue
def test_migrate(self):
migrated = []
for vcard in bundle:
try:
migrated.append(pyvcard.migrate_vcard(vcard).migrate("2.1"))
migrated.append(pyvcard.migrate_vcard(vcard).migrate("3.0"))
migrated.append(pyvcard.migrate_vcard(vcard).migrate("4.0"))
except Exception as e:
print(str(e))
f = open(os.path.join(test_path, "migr.vcf"), "w", encoding="utf-8")
f.write(pyvcard.vCardSet(migrated).repr_vcard())
f.close()
pyvcard.openfile(os.path.join(test_path, "migr.vcf"), encoding="utf-8", indexer=indexer).vcards()
if __name__ == "__main__":
unittest.main()