| Conditions | 12 |
| Total Lines | 101 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like test_CreateTransactionUnitTest.__PyxbDeserialization() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from authorizenet.constants import constants |
||
| 20 | def __PyxbDeserialization(self, lastElement = None): |
||
| 21 | loggingfilename = utility.helper.getproperty(constants.propertiesloggingfilename) |
||
| 22 | logginglevel = utility.helper.getproperty(constants.propertiesexecutionlogginglevel) |
||
| 23 | |||
| 24 | deserializedObject = None |
||
| 25 | deserializedBadObject = None |
||
| 26 | |||
| 27 | if (None == loggingfilename): |
||
| 28 | loggingfilename = constants.defaultLogFileName |
||
| 29 | if (None == logginglevel): |
||
| 30 | logginglevel = constants.defaultLoggingLevel |
||
| 31 | |||
| 32 | logging.basicConfig(filename=loggingfilename, level=logginglevel, format=constants.defaultlogformat) |
||
| 33 | |||
| 34 | merchantAuth = apicontractsv1.merchantAuthenticationType() |
||
| 35 | merchantAuth.name = "unknown" |
||
| 36 | merchantAuth.transactionKey = "anon" |
||
| 37 | |||
| 38 | creditCard = apicontractsv1.creditCardType() |
||
| 39 | creditCard.cardNumber = "4111111111111111" |
||
| 40 | creditCard.expirationDate = "2020-12" |
||
| 41 | |||
| 42 | payment = apicontractsv1.paymentType() |
||
| 43 | payment.creditCard = creditCard |
||
| 44 | |||
| 45 | transactionrequest = apicontractsv1.transactionRequestType() |
||
| 46 | transactionrequest.transactionType = "authCaptureTransaction" |
||
| 47 | transactionrequest.amount = Decimal( 6.99) |
||
| 48 | transactionrequest.payment = payment |
||
| 49 | |||
| 50 | createtransactionrequest = apicontractsv1.createTransactionRequest() |
||
| 51 | createtransactionrequest.merchantAuthentication = merchantAuth |
||
| 52 | createtransactionrequest.transactionRequest = transactionrequest |
||
| 53 | createtransactionrequest.refId = "MerchantID-0001" |
||
| 54 | |||
| 55 | logging.debug( "Request: %s " % datetime.datetime.now()) |
||
| 56 | logging.debug( " : %s " % createtransactionrequest ) |
||
| 57 | |||
| 58 | try: |
||
| 59 | xmlRequest = createtransactionrequest.toxml(encoding=constants.xml_encoding, element_name='createTransactionRequest') |
||
| 60 | xmlRequest = xmlRequest.replace(constants.nsNamespace1, '') |
||
| 61 | xmlRequest = xmlRequest.replace(constants.nsNamespace2, '') |
||
| 62 | ##print ("xmlRequest %s " %xmlRequest) |
||
| 63 | logging.debug( "Xml Request: %s" % xmlRequest) |
||
| 64 | except Exception as ex: |
||
| 65 | logging.debug( "Xml Exception: %s" % ex) |
||
| 66 | |||
| 67 | badXmlElement = None |
||
| 68 | |||
| 69 | if (lastElement == None): |
||
| 70 | try: |
||
| 71 | deserializedObject = apicontractsv1.CreateFromDocument(xmlRequest) |
||
| 72 | self.assertIsNotNone(deserializedObject, "Null deserializedObject ") |
||
| 73 | |||
| 74 | if type(createtransactionrequest) == type(deserializedObject): |
||
| 75 | ##print (" for good xml objects are equal") |
||
| 76 | logging.debug( "createtransactionrequest object is equal to deserializedObject") |
||
| 77 | else: |
||
| 78 | ##print ("for good xml some error: objects are NOT equal" ) |
||
| 79 | logging.debug( "createtransactionrequest object is NOT equal to deserializedObject") |
||
| 80 | |||
| 81 | deseriaziedObjectXmlRequest = deserializedObject.toxml(encoding=constants.xml_encoding, element_name='deserializedObject') |
||
| 82 | deseriaziedObjectXmlRequest = deseriaziedObjectXmlRequest.replace(constants.nsNamespace1, '') |
||
| 83 | deseriaziedObjectXmlRequest = deseriaziedObjectXmlRequest.replace(constants.nsNamespace2, '') |
||
| 84 | logging.debug( "Good Dom Request: %s " % deseriaziedObjectXmlRequest ) |
||
| 85 | ##print ( "Good De-serialized XML: %s \n" % deseriaziedObjectXmlRequest ) |
||
| 86 | except Exception as ex: |
||
| 87 | logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args ) |
||
| 88 | else: |
||
| 89 | if (lastElement == False): |
||
| 90 | try: |
||
| 91 | splitString = "<amount>" |
||
| 92 | lines = xmlRequest.split( splitString) |
||
| 93 | badXmlElement = "<badElement>BadElement</badElement>" |
||
| 94 | badXmlRequest = lines[0] + badXmlElement + splitString + lines[1] |
||
| 95 | logging.debug( "Bad XmlRequest: %s" % badXmlRequest) |
||
| 96 | ##print ("ElementInMidXML Request: %s \n" %badXmlRequest) |
||
| 97 | except Exception as ex: |
||
| 98 | ##print ("ElementInMidXML can not be inserted: %s, %s",type(ex), ex.args) |
||
| 99 | logging.debug( "ElementInMidXML can not be inserted: %s, %s" ,type(ex), ex.args) |
||
| 100 | if (lastElement == True): |
||
| 101 | try: |
||
| 102 | splitStringAtLast = "</payment>" |
||
| 103 | lines = xmlRequest.split( splitStringAtLast) |
||
| 104 | badXmlElementAtLast = "<badElementAtLast>BadElementAtLast</badElementAtLast>" |
||
| 105 | badXmlRequest = lines[0] + badXmlElementAtLast + splitStringAtLast + lines[1] |
||
| 106 | logging.debug( "Bad XmlRequest at Last: %s" % badXmlRequest) |
||
| 107 | ##print ("ElementAtLastXML Request: %s \n" %badXmlRequest) |
||
| 108 | except Exception as ex: |
||
| 109 | ##print ("ElementAtLastXML can not be inserted: %s, %s",type(ex), ex.args) |
||
| 110 | logging.debug("ElementAtLastXML can not be inserted: %s, %s",type(ex), ex.args) |
||
| 111 | try: |
||
| 112 | deserializedBadObject = apicontractsv1.CreateFromDocument(badXmlRequest) |
||
| 113 | self.assertIsNotNone(deserializedBadObject, "Null deserializedObject ") |
||
| 114 | badDomXml = deserializedBadObject.toxml(encoding=constants.xml_encoding, element_name='deserializedBadObject') |
||
| 115 | badDomXml = badDomXml.replace(constants.nsNamespace1, '') |
||
| 116 | badDomXml = badDomXml.replace(constants.nsNamespace2, '') |
||
| 117 | logging.debug( "Bad Dom Request: %s " % badDomXml ) |
||
| 118 | ##print ("Bad Dom De-serialized: %s \n" %badDomXml) |
||
| 119 | except Exception as ex: |
||
| 120 | logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args ) |
||
| 121 | ##print ("Exception while de-serializing bad dom: %s, %s",type(ex), ex.args) |
||
| 216 |