source: misc/tools/demos/test_flood_database.py @ 3759

Last change on this file since 3759 was 3759, checked in by ole, 17 years ago

Nothing

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1""" Stress test GA flood database
2
3This script tests that the flood database is live.
4Then proceeds to crawl through the site based on a html file containing 1005 level 1 links. Detail links below are followed automatically.
5
6If the database works, this test should complete with 3715 Detail links
7responding succesfully
8
9
10Ole Nielsen, RAMP 2006
11x9048
12"""
13
14
15
16b_version = False
17
18import urllib   
19import string
20#from caching import cache
21
22if b_version is True:
23    base_url = 'http://www-b.ga.gov.au/oracle/flood/'
24    servlet_base_url = 'http://www-b.ga.gov.au/'
25else:
26    base_url = 'http://www.ga.gov.au:8500/oracle/flood/'
27    servlet_base_url = 'http://www.ga.gov.au:8500/'   
28   
29
30def test_site():
31    """Test flood database
32    """
33 
34    top_url = 'flood_input.jsp'
35
36    url = base_url+top_url
37    live = False 
38    print 'Testing %s. ' %url, 
39    T = get_page(url)
40
41    for line in T:
42        #print line.strip()
43        if line.startswith('<title>Geoscience Australia: Online Flood Search'):
44            live = True
45            break
46
47    if live is True:   
48        print 'OK: Page is live'
49    else:
50        msg = 'Page %s is not live' %url         
51        raise msg
52
53    print
54
55
56def test_database(filename):
57    """Read list of studies from html and try them one by one
58    """
59
60    print 'Reading %s' %filename
61    fid = open(filename)
62
63    total_detail = 0
64    for i, link in enumerate(get_individual_studies(fid.readlines())):
65        url = base_url+htmlmap(link)
66     
67        live = False 
68        print 'Testing link %d: %s...' %(i, link[:72]),
69        T = get_page(url)
70
71        live = False 
72        for line in T:
73            if line.startswith('<tr><th>Dataset Name</th>') or\
74                   line.startswith('<h2>Study title only available.</h2>'):
75                live = True
76                break
77           
78        if live is True:   
79            print 'OK: Link %d is live' %i
80        else:
81            print 
82            print 'HTML page that failed:', T
83            #for line in T:
84            #    print line.strip()
85               
86            msg = 'FAIL: Link %d is not live: %s' %(i,url)
87            raise msg
88
89
90        # Second tier links
91        for j, link in enumerate(get_second_tier_links(T)):
92            url = servlet_base_url+htmlmap(link)
93     
94            live = False 
95            print 'Testing detail %d: %s...' %(j, link[:80]),
96            T = get_page(url)
97
98            live = False
99            for line in T:
100                if line.startswith('<tr><td><h3>Participants in '):
101                  live = True
102                  break
103
104            if live is True:   
105                print 'OK: Detail link %d is live (total=%d)' %(j, total_detail)
106            else:
107                print 
108                print 'HTML page that failed:', T
109                #for line in T:
110                #    print line.strip()
111                   
112                msg = 'FAIL: Detail link %d is not live (total=%d)' %(j, total_detail)
113                raise msg
114               
115            total_detail += 1           
116
117
118def get_second_tier_links(lines):
119    """Scan html lines for flood details and yield
120    """   
121    for line in lines:
122     
123        index = line.find('<a href="/servlet/FloodDetailServlet?sno')
124        if index >= 0:
125            start_index = line.find('servlet', index)
126            end_index = line.find('">', start_index)
127            yield line[start_index:end_index]
128
129     
130
131def get_individual_studies(lines):
132    """Scan html lines for individual flood studies and yield links
133    """ 
134 
135    for line in lines:
136        index = line.find('<a href="%sflood_infolist.jsp?sno' %base_url)
137        if index >= 0:
138            start_index = line.find('flood_infolist', index)
139            end_index = line.find('">', start_index)
140
141            yield line[start_index:end_index]
142
143     
144def get_page(URL):
145    """General routine for getting and caching URL pages
146    """
147 
148    F = urllib.urlopen(URL)   
149    T = F.readlines()
150    return(T)
151
152
153
154def htmlmap(s):
155    import types
156    from string import replace, strip
157
158    s = replace(s,' ','%20')   
159    return(strip(s))
160         
161
162print 'Testing main site'
163test_site()
164
165print 'Reading html file'
166if b_version is True:
167    test_database('flood_studies_all_b.html')
168else:
169    test_database('flood_studies_all.html')
170       
Note: See TracBrowser for help on using the repository browser.