Skip to content

Instantly share code, notes, and snippets.

@edgabaldi
Created December 2, 2014 18:23
Show Gist options
  • Save edgabaldi/b25a3409ef39bc1bb7bd to your computer and use it in GitHub Desktop.
Save edgabaldi/b25a3409ef39bc1bb7bd to your computer and use it in GitHub Desktop.
unittest for django admin calling a celery task
class PreparaLeilaoAdmin(admin.ModelAdmin):
model = Leilao
extra = 0
can_delete = False
form = LeilaoForm
def save_model(self, request, obj, form, change):
super(PreparaLeilaoAdmin, self).save_model(request, obj, form, change)
arquivo = form.cleaned_data.get('arquivo')
if arquivo:
task_importar_arquivo.delay(arquivo)
from mock import patch, Mock
from django.test import TestCase
from django.contrib.admin import AdminSite
from plugins.prepara_leilao.admin import PreparaLeilaoAdmin
from plugins.prepara_leilao.models import Leilao
from plugins.prepara_leilao.forms import LeilaoForm
class MockRequest(object):
pass
class MockSuperUser(object):
def has_perm(self, perm):
return True
request = MockRequest()
request.user = MockSuperUser()
class PreparaLeilaoAdminTest(TestCase):
def setUp(self):
site = AdminSite()
self.ma = PreparaLeilaoAdmin(Leilao, site)
def test_model(self):
self.assertEqual(self.ma.model, Leilao)
def test_extra(self):
self.assertEqual(self.ma.extra, 0)
def test_can_delete(self):
self.assertFalse(self.ma.can_delete)
def test_form(self):
self.assertEqual(self.ma.form, LeilaoForm)
@patch('plugins.prepara_leilao.tasks.task_importar_arquivo.delay')
def test_save_model(self, _delay):
dummy_obj = Mock()
dummy_form = Mock()
dummy_form.cleaned_data = {
'arquivo' : 'foo',
}
self.ma.save_model(request, dummy_obj, dummy_form, False)
_delay.assert_called_once_with('foo')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment