Hi,

I would like to write a test that verifies that a particular message is shown 
when a model is saved in the admin that meets certain criteria (it's a warning 
message).

First off I need to figure out how to post to the admin change view from test 
code. I figured it would be easier to start with an existing model instance and 
edit it rather than try to add a new one, so I've tried code that does the 
following:

- create a new model instance and save it
- get the change view URL for it
- construct a ModelForm with the model instance
- POST to the change URL using the form's initial dictionary as data
(full code below)

The POST returns some validation errors. Is there an easy way to construct the 
data that the admin change view needs? Obviously I could look at the code that 
builds the form on the admin page and add a POST parameter for every input 
field I find on the form, but this would require reading (and writing) a lot of 
code.

As promised here's the (non-working) code I have so far:

from django.contrib import admin
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import RequestFactory
from goway.admin import TripAdmin
from goway.models import Trip
from goway.tests.service.utils import create_trip


class TripAdminTests(TestCase):
    def setUp(self):
        self.factory = RequestFactory()

    def test_message_when_no_derived_primary_destination(self):
        # This is an attempt to create a trip and then post it unchanged to the 
admin change view.
        # The next step would be to check that there was a "Couldn't 
automatically work out primary destination, please choose a primary destination 
manually." message.
        # Couldn't get the post to the trip change view to work though - got 
validation errors.
        old_get_inline_instances = TripAdmin.get_inline_instances
        # Hack get_inline_instances() to return an empty list to avoid 
ValidationError: [u'ManagementForm data is missing or has been tampered with']
        TripAdmin.get_inline_instances = self.get_inline_instances
        try:
            user = User.objects.create_superuser('testadmin', 
'nob...@devrx.org', 'secret')
            self.client.login(username=user.username, password='secret')

            trip = create_trip()
            change_url = reverse('admin:goway_trip_change', args=(trip.pk,))
            request = self.factory.get(change_url)
            request.user = user

            trip_admin = TripAdmin(Trip, admin.site)
            form_class = trip_admin.get_form(request, trip)
            form = form_class(instance=trip)

            response = self.client.post(change_url, data=form.initial)

            errors = response.context['adminform'].form.errors
            self.assertEqual(0, len(errors), msg='Got validation errors: %s' % 
errors)

            messages = response.context['messages']
            print messages
        finally:
            TripAdmin.get_inline_instances = old_get_inline_instances

    def get_inline_instances(self, *args, **kwargs):
        return []

...and here's the output:

Failure
Traceback (most recent call last):
  File 
"/Users/francis/Code/Bright/python/goway/apps/goway/tests/service/trip_admin.py",
 line 41, in test_message_when_no_derived_primary_destination
    self.assertEqual(0, len(errors), msg='Got validation errors: %s' % errors)
AssertionError: 0 != 7 : Got validation errors: <ul 
class="errorlist"><li>map<ul class="errorlist"><li>Select a valid choice. That 
choice is not one of the available choices.</li></ul></li><li>tour_itinerary<ul 
class="errorlist"><li>Select a valid choice. That choice is not one of the 
available choices.</li></ul></li><li>primary_destination<ul 
class="errorlist"><li>Select a valid choice. That choice is not one of the 
available choices.</li></ul></li><li>placeholder_page<ul 
class="errorlist"><li>Select a valid choice. That choice is not one of the 
available choices.</li></ul></li><li>dynamic_package<ul 
class="errorlist"><li>Select a valid choice. That choice is not one of the 
available choices.</li></ul></li><li>excursion<ul class="errorlist"><li>Select 
a valid choice. That choice is not one of the available 
choices.</li></ul></li><li>types<ul class="errorlist"><li>This field is 
required.</li></ul></li></ul>

It isn't critical that I add a test for this - it's not the end of the world if 
the warning message doesn't get shown - but it would be nice to know how to do 
it if there's an easy way!

Thanks,

Francis

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to