| author | Mario D. Santana <mario@elorangutan.com> |
| Fri, 15 Apr 2016 18:29:24 -0600 | |
| changeset 97 | 44522cd37b07 |
| parent 70 | 1fe2c20adeba |
| child 102 | e8cb8d1367c0 |
| permissions | -rw-r--r-- |
| 70 | 1 |
from __future__ import print_function |
2 |
from __future__ import unicode_literals |
|
3 |
from __future__ import division |
|
4 |
from __future__ import absolute_import |
|
5 |
from future import standard_library |
|
6 |
standard_library.install_aliases() |
|
7 |
from builtins import * |
|
| 0 | 8 |
import unittest |
9 |
||
10 |
import os |
|
11 |
import sys |
|
12 |
sys.path.append('../')
|
|
13 |
||
| 12 | 14 |
import datetime |
| 0 | 15 |
|
16 |
import simplejson |
|
17 |
from glob import glob |
|
18 |
||
| 12 | 19 |
from whois.parser import WhoisEntry, cast_date |
| 0 | 20 |
|
21 |
class TestParser(unittest.TestCase): |
|
22 |
def test_com_expiration(self): |
|
23 |
data = """ |
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
24 |
Status: ok |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
25 |
Updated Date: 14-apr-2008 |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
26 |
Creation Date: 14-apr-2008 |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
27 |
Expiration Date: 14-apr-2009 |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
28 |
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
29 |
>>> Last update of whois database: Sun, 31 Aug 2008 00:18:23 UTC <<< |
| 0 | 30 |
""" |
31 |
w = WhoisEntry.load('urlowl.com', data)
|
|
| 12 | 32 |
expires = w.expiration_date.strftime('%Y-%m-%d')
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
33 |
self.assertEqual(expires, '2009-04-14') |
| 0 | 34 |
|
35 |
def test_cast_date(self): |
|
36 |
dates = ['14-apr-2008', '2008-04-14'] |
|
37 |
for d in dates: |
|
| 12 | 38 |
r = cast_date(d).strftime('%Y-%m-%d')
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
39 |
self.assertEqual(r, '2008-04-14') |
| 0 | 40 |
|
41 |
def test_com_allsamples(self): |
|
42 |
""" |
|
43 |
Iterate over all of the sample/whois/*.com files, read the data, |
|
44 |
parse it, and compare to the expected values in sample/expected/. |
|
45 |
Only keys defined in keys_to_test will be tested. |
|
46 |
||
47 |
To generate fresh expected value dumps, see NOTE below. |
|
48 |
""" |
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
49 |
keys_to_test = ['domain_name', 'expiration_date', 'updated_date', |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
50 |
'creation_date', 'status'] |
| 0 | 51 |
fail = 0 |
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
52 |
total = 0 |
| 0 | 53 |
for path in glob('test/samples/whois/*.com'):
|
54 |
# Parse whois data |
|
55 |
domain = os.path.basename(path) |
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
56 |
with open(path) as whois_fp: |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
57 |
data = whois_fp.read() |
| 0 | 58 |
|
59 |
w = WhoisEntry.load(domain, data) |
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
60 |
results = {key: w.get(key) for key in keys_to_test}
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
61 |
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
62 |
# NOTE: Toggle condition below to write expected results from the |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
63 |
# parse results This will overwrite the existing expected results. |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
64 |
# Only do this if you've manually confirmed that the parser is |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
65 |
# generating correct values at its current state. |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
66 |
if False: |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
67 |
def date2str4json(obj): |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
68 |
if isinstance(obj, datetime.datetime): |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
69 |
return str(obj) |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
70 |
raise TypeError( |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
71 |
'{} is not JSON serializable'.format(repr(obj)))
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
72 |
outfile_name = os.path.join('test/samples/expected/', domain)
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
73 |
with open(outfile_name, 'w') as outfil: |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
74 |
expected_results = simplejson.dump(results, outfil, |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
75 |
default=date2str4json) |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
76 |
continue |
| 0 | 77 |
|
78 |
# Load expected result |
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
79 |
with open(os.path.join('test/samples/expected/', domain)) as infil:
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
80 |
expected_results = simplejson.load(infil) |
| 0 | 81 |
|
82 |
# Compare each key |
|
83 |
for key in results: |
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
84 |
total += 1 |
| 0 | 85 |
result = results.get(key) |
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
86 |
if isinstance(result, datetime.datetime): |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
87 |
result = str(result) |
| 0 | 88 |
expected = expected_results.get(key) |
89 |
if expected != result: |
|
| 70 | 90 |
print("%s \t(%s):\t %s != %s" % (domain, key, result, expected))
|
| 0 | 91 |
fail += 1 |
92 |
||
93 |
if fail: |
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
94 |
self.fail("%d/%d sample whois attributes were not parsed properly!"
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
70
diff
changeset
|
95 |
% (fail, total)) |
| 12 | 96 |
|
97 |
||
98 |
if __name__ == '__main__': |
|
99 |
unittest.main() |