test/test_parser.py
changeset 0 ea0e45971cea
child 12 c57439b500cb
equal deleted inserted replaced
-1:000000000000 0:ea0e45971cea
       
     1 import unittest
       
     2 
       
     3 import os
       
     4 import sys
       
     5 sys.path.append('../')
       
     6 
       
     7 import time
       
     8 
       
     9 import simplejson
       
    10 from glob import glob
       
    11 
       
    12 from pywhois.parser import WhoisEntry, cast_date
       
    13 
       
    14 class TestParser(unittest.TestCase):
       
    15     def test_com_expiration(self):
       
    16         data = """
       
    17             Status: ok
       
    18             Updated Date: 14-apr-2008
       
    19             Creation Date: 14-apr-2008
       
    20             Expiration Date: 14-apr-2009
       
    21             
       
    22             >>> Last update of whois database: Sun, 31 Aug 2008 00:18:23 UTC <<<
       
    23         """
       
    24         w = WhoisEntry.load('urlowl.com', data)
       
    25         expires = w.get('expiration_date')
       
    26         self.assertEquals(expires, ['14-apr-2009'])
       
    27 
       
    28     def test_cast_date(self):
       
    29         dates = ['14-apr-2008', '2008-04-14']
       
    30         for d in dates:
       
    31             r = time.strftime('%Y-%m-%d', cast_date(d))
       
    32             self.assertEquals(r, '2008-04-14')
       
    33 
       
    34     def test_com_allsamples(self):
       
    35         """
       
    36         Iterate over all of the sample/whois/*.com files, read the data,
       
    37         parse it, and compare to the expected values in sample/expected/.
       
    38         Only keys defined in keys_to_test will be tested.
       
    39         
       
    40         To generate fresh expected value dumps, see NOTE below.
       
    41         """
       
    42         keys_to_test = ['domain_name', 'expiration_date', 'updated_date', 'creation_date', 'status']
       
    43         fail = 0
       
    44         for path in glob('test/samples/whois/*.com'):
       
    45             # Parse whois data
       
    46             domain = os.path.basename(path)
       
    47             whois_fp = open(path)
       
    48             data = whois_fp.read()
       
    49             
       
    50             w = WhoisEntry.load(domain, data)
       
    51             results = {}
       
    52             for key in keys_to_test:
       
    53                 results[key] = w.get(key)
       
    54 
       
    55             # Load expected result
       
    56             expected_fp = open(os.path.join('test/samples/expected/', domain))
       
    57             expected_results = simplejson.load(expected_fp)
       
    58             
       
    59             # NOTE: Toggle condition below to write expected results from the parse results
       
    60             # This will overwrite the existing expected results. Only do this if you've manually
       
    61             # confirmed that the parser is generating correct values at its current state.
       
    62             if False:
       
    63                 expected_fp = open(os.path.join('test/samples/expected/', domain), 'w')
       
    64                 expected_results = simplejson.dump(results, expected_fp)
       
    65                 continue
       
    66             
       
    67             # Compare each key
       
    68             for key in results:
       
    69                 result = results.get(key)
       
    70                 expected = expected_results.get(key)
       
    71                 if expected != result:
       
    72                     print "%s \t(%s):\t %s != %s" % (domain, key, result, expected)
       
    73                     fail += 1
       
    74             
       
    75         if fail:
       
    76             self.fail("%d sample whois attributes were not parsed properly!" % fail)