Refactoring Example 1: extracting redundant code-the second correction without passing the test

Source: Internet
Author: User

Yesterday restructured the OPEN-API module, very happy, I feel that I do a good job, The results yesterdayProgramAfter reconstruction, in addition to picking outCodeNo other code has been tested. I want to perform other maintenance work this morning. I think I should run the test first. The test program is not specially developed for this interface, at that time, I wrote an SDK for this OPEN-API, which is actually a test program developed for the SDK and is not well written, but even so, assert) the report still encountered an error. Therefore, I went to check the program and found several interfaces. Its function parameters are not only one parameter (Self parameter ), when I call a specific subclass of several classes, other parameters are not provided. Therefore, a runtime error occurs, it took an hour to solve the problem through the python learning manual.

 1   Class  Openapibase:  2       Def   _ Init __  (Self ):  3 Self. funpost = Self. Post  4 Self. Post = Self. Post  5       6       #  Declare the post function as a function that supports variable parameters, so that it can handle the situation that the post function of the subclass has multiple parameters.  7       Def Post (self ,* ARGs ):  8           Try  :  9 Wi = Web. Input ()  10 Token = WI. Token 11 Self. servertoken = Getservertoken (dB, token)  12               13   Web. debug (STR (self. servertoken ))  14               15               If Self. servertoken = False:  16                   Return   '  {"Result": "error", "message": "token is error "}  '  17  18               If  Checkexpires (self. servertoken ):  19                   Return   '  {"Result": "error", "message": "token is expires "}  '  20   21               #  Execute the specific code of each subclass. Note: Apply sends variable parameters to funpost.  22               Return  Apply (self. funpost, argS)  23          Except  :  24               If  Debug:  25                   Raise  26               Return   '  {"Result": "error", "message ":""}  '  27   28   Class  Getfolders (openapibase ):  29      #  This function has two parameters, which are mandatory in the business logic. Therefore, the post function of the parent class must be passed in during calling.  30       Def  Post (self, Action ):  31 Wi = Web. Input ()  32   33 Userid = self. servertoken. userid #  The subclass obtains the variable containing the identity information. This variable is created by the parent class and obtained through the attribute.  34           35           If Action ="  Person  " : #  Use action  36 List = Tnuser. User. getpersonfolders (dB, userid)  37               Return   '  {"Result": "OK", "message": "", "data": % s}  ' % (JSON. dumps (list ))  38           39          If Action = "  Project  " : #  Use action  40 Projectid = Util. unhash17 (INT (WI. ID ))  41 Folderid = Getprojectrootfolderid (dB, projectid)  42               If Folderid =-1 :  43                  Return   '  {"Result": "error", "message": "", "data": []}  '  44               45 List = Folder. getsubfolders (dB, folderid, userid)  46               For I In  List:  47 I [ "  Projectid  " ] = Util. hash17 (I [ "  Projectid  "  ])  48               Return   '  {"Result": "OK", "message": "", "data": % s}  ' % (JSON. dumps (list ))  49           50           If Action = "  Subfolder  " : #  Use action  51 Folderid = INT (WI. ID)  52 List = Folder. getsubfolders (dB, folderid, userid)  53               For I In  List:  54 I [ "  Projectid  " ] = Util. hash17 (I ["  Projectid  "  ])  55               Return   '  {"Result": "OK", "message": "", "data": % s}  ' % (JSON. dumps (list ))  56           57           Return   '  {"Result": "error", "message": "", "data": []}  ' 

 

Finally, Let's explain the test procedure:

I understand that debugging refers to single-step debugging, but the testing is based on the interface and pre-defines the input and output, and generates an alarm once the rules are not met, script programs that can be executed repeatedly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.