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