Some conclusions about the query class interface

Because the project needs, recently wrote an interface test script, so no update for a long time, but after writing, there are some gains and share with you

The written mainly query class interface, so focus on that some methods used when writing queries about the class interface

1. query by date

The results of such inquiry, in peacetime tests often need to select a date, click the query, and then list view list meets expectations

Then the date for the query class, how to construct a valid argument it

To consider one thing: how to make long-term effective date selected

If selected 2019-05-01 to 2019-05-29,

It is a problem that, when the 2020 re-execute the script, the data becomes older, even as clearing dirty database data, never finding out the results, so try not to a specific date

 

There is a workaround to get the current date, and then forward back 30 days, 50 days, etc.

For example, today is 2019-10-29, then the query data 2019-09-29 to 2019-10-29, always with reference to the current date

Get the current date, you can use built-in python datetime module

#coding:UTF-8

import datetime

now_date = datetime.datetime.now ()   # get time today 
# Print (now_date) 
END_DATE = now_date.strftime ( " % Y-% M-% d " )   # defined time today for a query end time, and the object into a string to year - month - day format output 
offset = the datetime.timedelta (days = -50)   # define an offset, i.e., the current date and time interval 
BEGIN_DATE = (now_date + offset) .strftime ( " % Y-M-% d% " )   # define query start time = current time rollback 50 days

2. The content of the response from a pool of a randomly taken assert

The random.choice using () method, a randomly taken from a set of data, it is common to extract data from a list, or string Ganso

If a query interface returns a plurality of sets of data, each data structure similar assertions made at this time, then, not every data is matched once

When following a pull hook net job search

 

Can see the result list contains a number of jobs have, for each position in a respective information json string is asserted, only one set of data can be taken;

The question is, choose which group asserts, first set? The last group? Specify a group?

The first set may achieve the purpose, the last group of rows is also

But the assertion specify a set of tricky, because the query may return the three groups, two groups may return next time

Another way is to take a random set of data is asserted,

This time can be used The random.choice () method, a random set of data extracted from all of the results returned, then the expected value of a set of data that can assertion

Example:

>>>import random
>>>numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>random.choice(numbers)
5
>>> for I in Range (. 3): # 8 cycles, each taking a random number 
  The random.choice (Numbers)
 . 4
4
9

Written before a telephone number generator (see article from the historic detail), also used random.choice ()

3. Use the last four digits of the phone number string sections assertion

Sometimes some sensitive information needs to be desensitization treatment, such as a phone number or identity card will among several numbers is encrypted, a common form below

188****8888

When queried telephone number, the returned results generally will encrypt the phone, but since it is based on a phone number to inquire if there is a response in return number information, better assert telephone number

This time you can match the last four digits of the phone number, if the match is considered successful results right
after slicing method can therefore be used to remove the four-digit phone number

>>> phone = "188****8888"
>>> phone
'188****8888'
>>> phone[-4:]
'8888'
>>>

4. Use zip () function iterates two lists, the display information combining

Scene: For example, according to a topic when queried, the actual transfer of mass participation is the number that corresponds to the theme,
such as "A theme of" numbered "11", "Theme B" numbered "22"
when writing the script, I hope play log more humane,
can print: query "theme number" 11 ", corresponding to the theme name" theme a "," data are xx bar

Since the topic name and topic number two in the list are, how we wanted to associate two lists together? Can simultaneously iteration 2 lists the name of the theme and the theme corresponding number up?

So we found the zip () function

A simple example of the Internet:

>>> list1 = ['a', 'b', 'c', 'd']
>>> list2 = ['apple', 'boy', 'cat', 'dog']
>>> for x, y in zip(list1, list2):
      print(x, 'is', y)

# Output 
A IS the Apple
b is boy
c is cat
d is dog

Complicated example, the list contains a dictionary

>>> a = [{"first_name":1,"second_name":2,"third_name":3},
         {"first_name":4,"second_name":5,"third_name":6},
         {"first_name":7,"second_name":8,"third_name":9}]

b = [{"first":11,"second":22,"third":33},
     {"first":44,"second":55,"third":66},
     {"first":77,"second":88,"third":99}]

>>> for X, Y in ZIP (A, B):
   Print ( " a theme name: {}, an id: {} ~ two theme name: {}, two id: {} ~ three topic name: {}, three ID: {} " .format (X [ " FIRST_NAME " ], Y [ " First " ], X [ " SECOND_NAME " ], Y [ " SECOND " ], X [ " third_name " ] , Y [ " THIRD " ]))
 
 Results are as follows:
A subject name: 1, an id: 11 ~ name two topics: 2, two id: 22 ~ three topic name: 3, three id: 33 
a subject name: 4, an id: 44 ~ two topic name: 5, two id: 55 ~ three topic name: 6, three id: 66 
a subject name: 7, an id: 77 ~ name two topics: 8, two id: 88 to name three topics: 9, three id: 99

 Application 5.format () function in the preparation of sql statement

python sql statement needs to write a pair of quotation marks, for example,

= original_sql " the SELECT * Movies from the WHERE MOVIE_NAME is playing = 'crazy animal city' "

The above statement movie_name field currently is a fixed value, if the handover execution environment, if the corresponding database is not "crazy animal city" this data, then the sql query will fail and return null

So we need to ensure that there is as much as possible to query the value of movie_name

 

If at this time there is an interface that can be prepared in advance or get movie_name number movie_name value

Then you can read movie_name returned interface or read prepared in advance amovie_name

 

Therefore we need to find ways to make sql statement movie_name parameterized, can be dynamically read the value passed in

See the above careful sql statement, noted sql statement comprises a pair of quotation marks, so that it is equivalent to a string, it is possible to use format () function to achieve the objective

Use the following method

= Movies [ " Crazy Animal City " , " bad guys must die " , " Frozen " ]

original_sql = " SELECT * WHERE MOVIE_NAME from Movies = '{}' " 
# Note that because the characters are used in the quotation sql package, so here {} need quotes ({} may be regarded as a predetermined position, stored the value movie_name)

for i in movies:
    sql = original_sql.format(i)
    print(sql)

operation result

 

 

Let's look at an example, if a method called get_name () returns the name of the film, a method named get_date () returns the release date of the film's
next movie should put the name and release date in sql query

original_sql = "select * from movies where movie_name = '{}' and release_date = '{}'"

sql = original_sql.format(self.get_name(), self.get_date())

Further format () function can be provided the parameters

The above example, can be changed

original_sql = "select * from movies where movie_name = '{name}' and release_date = '{date}'"

sql = original_sql.format(name=self.get_name(), date=self.get_date())

6. regular expressions to extract the Chinese characters

Sometimes a field returned interface is worth the time, it will automatically add a suffix

Such as your name: Joe Smith qt, John Doe 1, Wang Wu rw

 

In fact, it exists in the database data without the suffix, is pure Chinese characters

E.g:

select * from name_list where name = 'Joe Smith' can be found in the data #

select * from name_list where name = 'Joe Smith qt' # finding data

 

So after obtaining these data through the interface, we need to handle it, leaving only the Chinese characters, and then passed to sql query

Here is handled by way of regular expressions

import re

name = " John Doe RX " 
pattern = the re.compile (R & lt ' [\ u4e00- \ u9fa5] ' )   # define a regular expression, to remove the end of the string of numbers 
m = pattern.findall (name)   # matches, return each character list consisting 
Print (m)
real_name = "" .join (m)   # each composed of a string of characters 
Print (real_name)

operation result

 

Guess you like

Origin www.cnblogs.com/hanmk/p/11869908.html