mercredi 21 octobre 2020

How to encapsulate class method for other methods

I want to encapsulate 'non-GET' methods to one and parametrize it. How to make it?

I thought this could work, but I don't know how to write it in properly way: URL, NAME, METHOD will be different.

list_of_dicts = [
{url:    ,
method:   
}
]

Here is the code:

from locust import HttpUser, task, TaskSet, between, exception, events
import logging
import sys, os
sys.path.append(os.getcwd())
import settings

class MyUser(HttpUser):
    wait_time = between(5, 9)
    host = settings.host
    logins = [elem*3 for elem in settings.alphabetic] + \
        [elem*3 + "1" for elem in settings.alphabetic] + \
        [elem*3 + "2" for elem in settings.alphabetic]

    passwd = 'radio'
    login_pass = [(elem, "radio") for elem in logins]


    def on_start(self):
        if len(MyUser.login_pass) > 0:
            self.user, self.passwd_for_object = MyUser.login_pass.pop()
        self.client.verify = False
        self.login()

    def on_stop(self):
        pass

    def login(self):
        default_headers = {'X-Username': self.user, 'X-Password': MyUser.passwd}
        self.client.request(method="POST", url="login.xhtml", headers=default_headers,
                            name="---ON START---LOGIN")
        logging.info('Login with %s username and %s password', self.user, self.passwd_for_object)

@task
def delete_virtual_meters(self):
    """DELETE will not be performed if Virtual Meter does not exist"""
    default_headers = {'X-Username': self.user, 'X-Password': self.passwd}
    #for number in (3, 4, 5, 6, 7, 8): <- it wokrs
    for number in range(33,166):
        response = self.client.request(method="GET", url="api/virtualmeter/%s" % number,
                                       headers=default_headers, catch_response=True,
                                       name="Check if Virtual Meter %s exists" % number)
        if response.ok and number not in MyUser.deleted_numbers_virtual_meter:
            MyUser.deleted_numbers_virtual_meter[number] = True
            self.client.request(method="DELETE", url="api/virtualmeter/%s" % number,
                                headers=default_headers,
                                name="Delete existing Virtual Meter with ID %s" % number)
        else:
            response.failure(exc="Nothing to DELETE")

@task
def delete_additional_info(self):
    """DELETE will not be performed if Additional Info does not exist"""
    default_headers = {'X-Username': self.user, 'X-Password': self.passwd}
    for number in range(30, 102):
        response = self.client.request(method="GET", url="api/additionalInfo/%s" % number,
                                       headers=default_headers, catch_response=True,
                                       name="Check if Additional Info %s exists" % number)
        if response.ok and number not in MyUser.deleted_number_info:
            try:
                MyUser.deleted_number_info[number] = True
                self.client.request(method="DELETE", url="api/additionalInfo/%s" % number,
                                    headers=default_headers,
                                    name="Delete existing Additional Info with ID %s" % number)
            except AssertionError:
                response.failure(exc="Nothing to DELETE")

And one more thing - there is still problem with the inheritance of MyUser Class.

Aucun commentaire:

Enregistrer un commentaire