test_discounts.py

<---Back

Select Code Highlighting Style:

BRIGHT | Seashell | Darkness

Select Font Size:

Small | Normal | Large
../www/get_script_html_files/strip/test_discounts.py
    1 #!/usr/bin/python2
    2 
    3 #######################################
    4 #
    5 # This script does component test for functionality:
    6 # generate discount rate depending on number of books
    7 #
    8 # Version: 1.2
    9 #
   10 #######################################
   11 
   12 #import everything from lib.py
   13 
   14 from lib import *
   15 
   16 #import everything from qalib.py - this is our library for test functions
   17 
   18 from qalib import *
   19 
   20 #import function get_discount() from shopping_cart.py
   21 
   22 from shopping_cart import get_discount
   23 
   24 #get dictionary of values from web form
   25 
   26 form = cgi.FieldStorage()
   27 
   28 #run_flag is a flag. If run_flag == 1, we run test
   29 
   30 try:
   31     run_flag = form['run_flag'].value
   32 except:
   33     run_flag = 0
   34 
   35 #function to run test
   36 
   37 def get_actual_result():
   38 
   39     #here is the deal: we feed numbers 1-20000 inclusively to function
   40     #get_discount() and see discount rate given to us.
   41     #Then we create HTML table with results
   42 
   43     min_number = 1
   44     max_number = 20000
   45 
   46 
   47     #rs (result) dictionary
   48     rs_dict = {}
   49 
   50     #create a dictionary
   51 
   52     for i in range(min_number,max_number+1):
   53 
   54         discount = get_discount(i)
   55 
   56         rs_dict[i] = discount
   57 
   58     #now we have a dict where key is number of books and value is discount rate
   59     #Example: {1:0,2:0}
   60 
   61     #next task is to do sorting to find ranges (equivalent classes) where the same rate applies
   62 
   63     range_list  = []
   64 
   65     #format of each element of this list:
   66     #<first number of range>,<last number of range>,<discount>
   67 
   68     for i in range(min_number,max_number+1):
   69 
   70         discount = rs_dict[i] #get discount for each number of books
   71 
   72         if i == min_number: #in case if this is first number
   73 
   74             start_range = min_number
   75             end_range = min_number
   76             pre_discount = None
   77 
   78         if pre_discount != discount: #if new discount rate is found
   79 
   80             if i != min_number:
   81 
   82                 #create element of range_list and append it to range_list
   83 
   84                 new_range = str(pre_start_range)+","+str(i-1)+","+str(pre_discount)
   85                 range_list.append(new_range)
   86 
   87                 start_range = i
   88 
   89         #two variables below are needed to transfer
   90         #values of corresponding variables from current iteration
   91         #into next iteration
   92 
   93         pre_discount    = discount
   94         pre_start_range = start_range
   95 
   96     return range_list
   97 
   98 #function to display first page
   99 
  100 def page_one(message = ''):
  101 
  102     html = """
  103 
  104     <center>
  105 
  106     <h2>Test Discounts</h2>
  107 
  108     <p><font color='red'><b>%s</b></font>
  109 
  110     <hr>
  111 
  112     <p><b>EXPECTED RESULT:</b>
  113 
  114     <p><a href = '../test_discounts_ER.txt'>test_discounts_ER.txt</a>
  115 
  116     <hr>
  117 
  118     <p><b>ACTUAL RESULT:</b>
  119 
  120     <form action='./test_discounts.py'>
  121 
  122     <input type='hidden' name='run_flag' value='1'>
  123 
  124     <p><input type='submit' value='Run Test'>
  125 
  126     </form>
  127 
  128     </center>
  129 
  130 """ % message
  131 
  132     return html
  133 
  134 #function to display second page
  135 
  136 def page_two():
  137 
  138     #get list with actual results
  139     #we'll use 'a_' to start variable names of actual stuff
  140     #and 'e_' to start variable names of expected stuff
  141 
  142     a_list = get_actual_result()
  143 
  144     html = """
  145 
  146     <center>
  147 
  148     <h3>Test Results</h3>
  149 
  150     <table border=1 align=center cellpadding=5>
  151 
  152     <tr>
  153 
  154         <td colspan=2 align=center>EXPECTED</td>
  155         <td colspan=2 align=center>ACTUAL</td>
  156         <td>RESULT</td>
  157 
  158     </tr>
  159 
  160     <tr>
  161 
  162         <td align=center><b>Number of books</b></td>
  163         <td align=center><b>Discount Rate</b></td>
  164         <td align=center><b>Number of books</b></td>
  165         <td align=center><b>Discount Rate</b></td>
  166 
  167     </tr>"""
  168 
  169     #get expected results
  170 
  171     e_dict = {}
  172     e_list = []
  173 
  174     file = open('../www/test_discounts_ER.txt','r')
  175 
  176     for i in file.readlines():
  177 
  178         start_range = i.split(',')[0]
  179         end_range   = i.split(',')[1]
  180         discount    = i.split(',')[2]
  181 
  182         #remove unprintable chars at the end of the line
  183         discount = re.sub('\r','',discount)
  184         discount = re.sub('\n','',discount)
  185 
  186         e_dict[discount] = start_range+','+end_range
  187 
  188         e_list.append(start_range+','+end_range+','+discount)
  189 
  190     file.close()
  191 
  192     e_discount_list = e_dict.keys()
  193     e_discount_list.sort()
  194 
  195     #go through each expected discount rate
  196 
  197     for e_discount in e_discount_list:
  198 
  199         e_start_range   = e_dict[e_discount].split(',')[0]
  200         e_end_range = e_dict[e_discount].split(',')[1]
  201 
  202         #this flag is needed to indicate if corresponding discount
  203         #rate was found in a_list or not
  204 
  205         is_found = 0
  206 
  207         for i in a_list:
  208 
  209             a_start_range   = i.split(',')[0]
  210             a_end_range = i.split(',')[1]
  211             a_discount  = i.split(',')[2]
  212 
  213             if e_discount == a_discount:
  214 
  215                 is_found = 1
  216 
  217                 if e_start_range == a_start_range and e_end_range == a_end_range:
  218 
  219                     result      = 'PASS'
  220                     bg_color    = 'greenyellow'
  221                 else:
  222                     result      = 'FAIL'
  223                     bg_color    = 'red'
  224 
  225                 break
  226 
  227 
  228         #case when expected discount was not found at all
  229 
  230         if is_found ==0:
  231 
  232             a_start_range   = 'none'
  233             a_end_range = 'none'
  234             a_discount  = 'none'
  235             result      = 'FAIL'
  236             bg_color    = 'red'
  237 
  238 
  239         #create html
  240 
  241         html = html + """
  242 
  243                 <tr>
  244 
  245                     <td align=center>%s-%s</td>
  246                     <td align=center>%s</td>
  247 
  248                     <td align=center>%s-%s</td>
  249                     <td align=center>%s</td>
  250 
  251                     <td align=center bgcolor = '%s'>%s</td>
  252 
  253                 </tr>""" %  (e_start_range,e_end_range,e_discount,\
  254                         a_start_range,a_end_range,a_discount,\
  255                         bg_color,result)
  256 
  257     html = html + "</center></table><hr>"
  258 
  259     #show raw test data
  260 
  261     html = html + "<p><b>Test Data:</b><p>Expected: %s<p>Actual: %s" % (str(e_list),str(a_list))
  262 
  263 
  264     return html
  265 
  266 print 'Content-Type: text/html\n\n'
  267 
  268 
  269 if int(run_flag) == 0:
  270 
  271     html = page_one()
  272 
  273 else:
  274 
  275     html = page_two()
  276 
  277 html = get_qa_html(html)
  278 
  279 print html
  280