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
  Create X Number of Variables and Assign Data RockBlok 8 920 Nov-14-2023, 08:46 AM
Last Post: perfringo
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 1,081 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  Pytest Installed, but VS Code Won’t Access Pytest AstralWeeks 9 3,170 Sep-13-2023, 03:00 PM
Last Post: AstralWeeks
  Pytest mocks anthonyrmoss78 0 444 May-30-2023, 08:28 PM
Last Post: anthonyrmoss78
  Create simple live plot of stock data dram 2 2,901 Jan-27-2023, 04:34 AM
Last Post: CucumberNox
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,866 Dec-12-2022, 08:22 PM
Last Post: jh67
  Create a function for writing to SQL data to csv mg24 4 1,145 Oct-01-2022, 04:30 AM
Last Post: mg24
  Unable to request image from FORM Data usman 0 985 Aug-18-2022, 06:23 PM
Last Post: usman
  Append data to Yaml section tbaror 0 6,961 Feb-09-2022, 06:56 PM
Last Post: tbaror
  how can I correct the Bad Request error on my curl request tomtom 8 5,045 Oct-03-2021, 06:32 AM
Last Post: tomtom

Forum Jump:

User Panel Messages

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