Python Forum
PyTest >> Yaml parsed data to create api test request
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyTest >> Yaml parsed data to create api test request
#1
I have a YAML file in db. I am pulling yaml and getting the required param to build PyTest requests.
Seems like I am good till getting expected params. Now the problem is, how to pass those params in PyTest code block in the loop?
kind of new to Python.. could anyone please help me with this?

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
##Pulling data from db where in column YAML is placed##

mycur1 = cnx.cursor()

descselector1 = ("select des from t_uc;")
mycur1.execute(descselector1)
descr1 = mycur1.fetchall() ##Gives us the list of tuples
#print(descr1)

mycur2 = cnx1.cursor()

CSIdselector = ("select * from r_cnt_se_i_map;")
mycur2.execute(CSIdselector)
CSIDmapping =  mycur2.fetchall()
print(CSIDmapping)

##Fetch expected params from YAML##  
class PSLoader(yaml.SafeLoader):
    pass

def constructor(loader, node):
    return loader.construct_mapping(node)

# to handle the custom tag
PSLoader.add_constructor(
    'tag:yaml.org,2002:c.tcs.ucm.model.uCCo.Cig',
    constructor
)

for entry, in desc1:
    parsed = yaml.load(entry, Loader=PSLoader)

    try:
        mt = parsed['requests'][0]['param']['mt']
        num = parsed['requests'][0]['param']['num']
        aMB = parsed['requests'][0]['param']['aMB']
        uc = parsed['uC'][0]['ucId']


        cSId = parsed['requests'][0]['param']['cSId']
        for i in cSId:
            if cSId == ['10']:
                EId = cnx1.cursor(buffered=True)
                GetRandomEId = (
                    "select id from Etable order by rand() limit 1;")
                EID.execute(GetRandomEId)
                for row in EID:
                    RandomEId = (row[0])
                    print("10>>RandomEId>>", RandomEId)

            elif cSId == ['11']:
                VID = cnx1.cursor(buffered=True)
                GetRandomVId = (
                    "select id from Vtable order by rand() limit 1;")
                VID.execute(RandomVId)
                for row in VID:
                    RandomVId = (row[0])
                    print("11>>RandomVId>>", RandomVId)

            elif cSId == ['12']:
                PID = cnx1.cursor(buffered=True)
                GetRandomPId = (
                    "select id from Ptable order by rand() limit 1;")
                PID.execute(GetRandomPId)
                for row in PID:
                    RandomPId = (row[0])
                    print("12>>RandomPId>>", RandomPId)

            elif cSId == ['13']:
                RId = cnx1.cursor(buffered=True)
                GetRId = (
                    "select id from Rtable order by rand() limit 1;")
                RId.execute(GetRId)
                for row in RId:
                    RId = (row[0])
                    print("13>>RId>>", RId)

        test_list = (mt, cSId, num, aMB, uc)
        print(test_list)
    except KeyError:
        pass


#above code block give op like this:
##12>>PId>> b03433232321211s35ck
##('getmt1', ['12'], 1, F, 'iP/hPg/PFYo')
##11>>>> eqerefcdap07rd3lx
##('getmt2', ['11'], 2, F, 'isP/hPg/VFYo')
##13>>>> eqeeeqerefcdap07rd3lx
##('getmt3', ['13'], 2, F, 'isP/hPg/V_FY_o')
##('getmt4', ['13'], 12, F, 'isP/hPg/VFYo')
##('getmt5', ['11', '12'], 32, F, 'is-P/hPg/V-FY-o')

#create pytest requests

def test_allAPIsResponse():
            fURL = (url + "UC")
            headers = {'Accept': "application/json", 'Content-Type': "application/json", 'Accept-Encoding': "gzip, deflate",
                       'Cache-Control': "no-cache", 'Token': "null"}
            req_json = "{ \r\n\"reqP\": { \r\n \"cIid\":[\"[csID]<VID or PID or RID>\"] \r\n } \r\n}" #need to pass cSId here
            uid = {"userId": "Test1"}
            response = requests.request("POST", fURL, data=req_json, headers=headers, params=uid)
            jsondata = json.loads(response.content)
assert response.status_code == 200, "Test pass"

def test_allAPIsResponseCont():
            fURL = (url + uc)
            headers = {'Accept': "application/json", 'Content-Type': "application/json", 'Accept-Encoding': "gzip, deflate",
                       'Cache-Control': "no-cache", 'Token': "null"}
            req_json = "{ \r\n\"reqP\": { \r\n \"cIid\":[\"<VID or PID or RID>\"] \r\n } \r\n}" #no need to pass cSId here
            uid = {"userId": "Test1"}
            response = requests.request("POST", fURL, data=req_json, headers=headers, params=uid)
            jsondata = json.loads(response.content)
assert jsondata["rec"]["recO"]["recOO"][0]["csID"] == 1
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ex of post req:
url = "http:/<domain>:<port>/coer/ucc/is-P%2fhPg%2fV-FY-o?userId=test1"
payload = "{\n\"reqparam\" : {\n\"aMB\":f,\n\"csID\":\"b03433232321211s35ck\"}\n}"
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Content-Type': 'text/plain'
}

response = requests.request("POST", url, headers=headers, data = payload)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 240 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 589 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to make a test data file for the full length of definition? MDRI 6 937 Apr-16-2021, 01:47 AM
Last Post: MDRI
  Fetching data from multiple tables in a single request. swaroop 0 476 Jan-09-2021, 04:23 PM
Last Post: swaroop
  How to run a pytest test for each item in a list arielma 0 611 Jan-06-2021, 10:40 PM
Last Post: arielma
  An important question is how to create a zigzag in price data? epsilon 0 359 Nov-18-2020, 08:06 PM
Last Post: epsilon
  getting back both: the map and the data from a request via overpass-turbo.eu apollo 0 565 Sep-26-2020, 11:28 AM
Last Post: apollo
  import yaml error for python3 maiya 4 5,251 Jul-15-2020, 06:07 PM
Last Post: Gribouillis
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 1,667 Jun-18-2020, 08:07 AM
Last Post: buran
  Pytest and rootdirectory Master_Sergius 4 1,054 Jun-01-2020, 05:05 PM
Last Post: Master_Sergius

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020