P
P
Pavel Ivanov2015-12-24 11:31:28
Django
Pavel Ivanov, 2015-12-24 11:31:28

How to put a mock on a class object or on a class in such a way that it doesn't do anything?

For example, I have this logic on the test side in Django:

from rest_framework.test import APITestCase, APIClient
import unittest
from mock import patch
from rest_framework.reverse import reverse
from rest_framework import status
from django.test import TestCase
from django.contrib.auth.models import User

class APITest(unittest.TestCase):
    def setUp(self):
        self.client = APIClient(enforce_csrf_checks=False)
        self.user = User.objects.create_user('test_user', '[email protected]', 'password')
        self.user.save()

    def test_auth_get(self):
        with patch('app.views.RabbitMq.run_rabbitmq'):
            self.client.login(username='test_user', password='password')
            self.client.force_authenticate(user=self.user)
            response = self.client.get(reverse('user-list'), format='json')
            print response
            self.assertEqual(response.status_code, 200)

according to urls.py:
from django.conf.urls import patterns, include, url
from rest_framework import routers
from views import UserViewSet, GroupViewSet

router = routers.SimpleRouter()
router.register(r'users', UserViewSet)
router.register(r'groups', GroupViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]

and i want to mock run_rabbitmq in views.py:
from django.contrib.auth.models import User, Group
import pika
from django.http import HttpResponse
from rest_framework import viewsets
from serializers import UserSerializer, GroupSerializer

class RabbitMq:
    def __init__(self):
        pass

    def run_rabbitmq(self):
        connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
        channel = connection.channel()
        channel.queue_declare(queue='test')
        channel.basic_publish(exchange='', routing_key='test', body='Hello World!')
        connection.close()

def rabbitmq_wrap(OriginalClass):
    orig_init = OriginalClass.__init__
    def __init__(self, *args, **kws):
        rabbit = RabbitMq()
        rabbit.run_rabbitmq()
        orig_init(self, *args, **kws)

    OriginalClass.__init__ = __init__
    return OriginalClass

@rabbitmq_wrap
class Record:
    def __init__(self):
        pass # here is the logic of writing to database

class UserViewSet(viewsets.ModelViewSet, Rabbitmq):
    record = Record()

    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer

class GroupViewSet(viewsets.ModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer

def my_view(request):
    return HttpResponse('<h1>Hello!</h1>')

in such a way that it simply does nothing, returns nothing, and does not make any connections (in my case, you need to write to the database during the test without sending a message to the rabbitmq queue).
Given this: https://docs.python.org/3/library/unittest.mock.ht...
how do I patch a class instance inside a decorator?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tanker John, 2015-12-31
@Kuzmichik

with patch.object('app.views.RabbitMq', 'run_rabbitmq'):
    # bla bla bla

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question