Got some tests working
parent
ca1019765b
commit
bf9b3e6b75
|
|
@ -7,7 +7,7 @@ It can be configured to add a confirmation page upon saving changes and/or addit
|
|||
|
||||
Typical Usage:
|
||||
|
||||
from admin_confirm.admin import AdminConfirmMixin
|
||||
from admin_confirm import AdminConfirmMixin
|
||||
|
||||
class MyModelAdmin(AdminConfirmMixin, ModelAdmin):
|
||||
confirm_change = True
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
|
||||
from .admin import AdminConfirmMixin
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ from django.template.response import TemplateResponse
|
|||
from django.contrib.admin.options import TO_FIELD_VAR
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
class AdminConfirmMixin(object):
|
||||
|
||||
class AdminConfirmMixin:
|
||||
# Should we ask for confirmation for changes?
|
||||
confirm_change = None
|
||||
|
||||
|
|
@ -16,7 +17,7 @@ class AdminConfirmMixin(object):
|
|||
confirmation_fields = None
|
||||
|
||||
# Custom templates (designed to be over-ridden in subclasses)
|
||||
change_confirmation_template = None
|
||||
confirmation_template = None
|
||||
|
||||
def get_confirmation_fields(self, request, obj=None):
|
||||
"""
|
||||
|
|
@ -38,7 +39,7 @@ class AdminConfirmMixin(object):
|
|||
|
||||
return TemplateResponse(
|
||||
request,
|
||||
self.change_confirmation_template
|
||||
self.confirmation_template
|
||||
or [
|
||||
"admin/{}/{}/change_confirmation.html".format(
|
||||
app_label, opts.model_name
|
||||
|
|
|
|||
|
|
@ -0,0 +1,69 @@
|
|||
from unittest import mock
|
||||
from requests import Request
|
||||
from django.test import TestCase
|
||||
from django.contrib.admin.sites import AdminSite
|
||||
|
||||
from tests.market.admin import ItemAdmin, InventoryAdmin, ShopAdmin
|
||||
from tests.market.models import Item
|
||||
from tests.factories import ItemFactory, InventoryFactory, ShopFactory
|
||||
|
||||
|
||||
class TestAdminConfirmMixin(TestCase):
|
||||
@mock.patch('django.contrib.admin.options.changeform_view')
|
||||
def test_add_without_confirm(self, mock_super):
|
||||
ItemAdmin.confirm_add = None
|
||||
admin = ItemAdmin(Item, AdminSite())
|
||||
request = Request('POST', 'url', data={'name': 'name', 'price': 2.0, 'currency': Item.VALID_CURRENCIES[0]})
|
||||
|
||||
confirmation_template = admin.render_change_confirmation(request, context={}).template_name
|
||||
# object_id = None for adding
|
||||
actual_template = admin.changeform_view(request).template_name
|
||||
mock_super.assert_called_once()
|
||||
self.assertNotEqual(confirmation_template, actual_template)
|
||||
|
||||
def test_change_with_no_options(self):
|
||||
|
||||
|
||||
def test_with_confirm_change(self):
|
||||
pass
|
||||
|
||||
def test_with_confirm_add(self):
|
||||
pass
|
||||
|
||||
def test_get_confirmation_fields_should_default_if_not_set(self):
|
||||
expected_fields = [f.name for f in Item._meta.fields if f.name != 'id']
|
||||
ItemAdmin.confirmation_fields = None
|
||||
admin = ItemAdmin(Item, AdminSite())
|
||||
actual_fields = admin.get_confirmation_fields(Request('GET', 'url'))
|
||||
self.assertEqual(expected_fields, actual_fields)
|
||||
|
||||
def test_get_confirmation_fields_if_set(self):
|
||||
expected_fields = ['name', 'currency']
|
||||
ItemAdmin.confirmation_fields = expected_fields
|
||||
admin = ItemAdmin(Item, AdminSite())
|
||||
actual_fields = admin.get_confirmation_fields(Request('GET', 'url'))
|
||||
self.assertEqual(expected_fields, actual_fields)
|
||||
|
||||
def test_custom_template(self):
|
||||
expected_template = 'my_custom_template.html'
|
||||
ItemAdmin.confirmation_template = expected_template
|
||||
admin = ItemAdmin(Item, AdminSite())
|
||||
actual_template = admin.render_change_confirmation(Request('POST', 'url'), context={}).template_name
|
||||
self.assertEqual(expected_template, actual_template)
|
||||
|
||||
|
||||
|
||||
class TestAdminConfirmMixinConfirmChange(TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class TestAdminConfirmMixinConfirmAdd(TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class TestAdminConfirmMixinConfirmChangeWithFields(TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class TestAdminConfirmMixinConfirmChangeWithFields(TestCase):
|
||||
pass
|
||||
|
|
@ -1 +1,2 @@
|
|||
Django>=1.7.0
|
||||
factory-boy~=3.0.1
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
import factory
|
||||
|
||||
from random import choice
|
||||
|
||||
from tests.market.models import Item, Shop, Inventory
|
||||
|
||||
|
||||
class ItemFactory(factory.django.DjangoModelFactory):
|
||||
class Meta:
|
||||
model = Item
|
||||
|
||||
name = factory.Faker('name')
|
||||
price = factory.Faker('price')
|
||||
currency = factory.LazyAttribute(lambda _: choice(Item.VALID_CURRENCIES))
|
||||
|
||||
|
||||
class ShopFactory(factory.django.DjangoModelFactory):
|
||||
class Meta:
|
||||
model = Shop
|
||||
|
||||
name = factory.Faker('name')
|
||||
|
||||
|
||||
class InventoryFactory(factory.django.DjangoModelFactory):
|
||||
class Meta:
|
||||
model = Inventory
|
||||
|
||||
shop = factory.SubFactory(ShopFactory)
|
||||
item = factory.SubFactory(ItemFactory)
|
||||
quantity = factory.Sequence(lambda n: n)
|
||||
|
||||
Loading…
Reference in New Issue