How to call an external function, without sending the 'self'?

19

I am using a class attribute to save the View that I want to test in a Django application, like this:

# TESTS.PY
class OrderTests(TestCase, ShopTest):
    _VIEW = views.order

    def test_gateway_answer(self):
        url = 'whatever url'
        request = self.request_factory(url, 'GET')
        self._VIEW(request, **{'sku': order.sku})


# VIEWS.PY
def order(request, sku)
     ...

But during execution, as I call an attribute of my object OrderTests Python sends self as argument, which does not match the signature of the order function and causes all kinds of problems.

Is there any way to make Python not send self in this case?

    
asked by anonymous 31.12.2013 / 15:16

4 answers

6

Ideally, you should use the staticmethod decorator of Python in the _VIEW attribute declaration. After all, what you want is to access it in a static way (regardless of the instance of the test class OrderTests ).

# TESTS.PY
class OrderTests(TestCase, ShopTest):
    _VIEW = staticmethod(views.order)

    def test_gateway_answer(self):
        url = 'whatever url'
        request = self.request_factory(url, 'GET')
        self._VIEW(request, **{'sku': order.sku})


# VIEWS.PY
def order(request, sku)
     ...

link

    
03.02.2014 / 03:46
7

You can use the __func__ property of your method ( im_func , if Python

31.12.2013 / 22:23
3

In Python you can call a method or property from within a class in two ways, but instead consider the following class:

class Pessoa:
    def __init__(self, nome = "nenhum"):
        self.nome = nome

    def set_nome(self, nome):
        self.nome = nome

    def get_nome(self):
        return self.nome

- Passing object to class when calling method:

amigos = [Pessoa("Lucas"), Pessoa("Julia"), Pessoa("Bruna")]

for amigo in amigos:
    print(Pessoa.get_nome(amigo))

In this method you use the class to call methods and properties, but the first argument must be an object of this class, since it needs to remove this information from somewhere. I usually use this method when I have a list of objects of the same type, as it eases access to methods and properties, apparently reading your question I got the impression that it used that way.

- Calling the method through the object:

maria = Pessoa("Maria")

print(maria.get_nome())

This method is most commonly used and is very similar to what happens in object-oriented languages like Java and C #. In order for "Python not to send self " as described in your question, you need to use this method. Just a correction, you said order is a function, if order is asking as the first argument self means that it is contained in a class, then it is not called "function" but "method" .

    
31.12.2013 / 18:31
1

You can only make a wrapper of the function you want to test.

# TESTS.PY
class OrderTests(TestCase, ShopTest):
    def _VIEW(self, *args, **kwargs):
        views.order(*args, **kwargs)

    def test_gateway_answer(self):
        url = 'whatever url'
        request = self.request_factory(url, 'GET')
        self._VIEW(request, **{'sku': order.sku})


# VIEWS.PY
def order(request, sku)
     ...

It's an extra line of code, but make it clear what you're doing.

    
02.02.2014 / 19:03