Commits

Melissa Garza committed 7326444

Working on getting ModelFormSetMixin tests to work

  • Participants
  • Parent commits 1e27d90

Comments (0)

Files changed (5)

File testsite/templates/model_form.html

+{% extends "home.html" %}
+
+{% block content %}
+    {% if form %}
+    <form method="post" action="">{% csrf_token %}
+        {{ form }}
+        <input type="submit" value="Submit" />
+    </form>
+    {% endif %}
+{% endblock content %}

File testsite/testapp/forms.py

-from django.forms import ModelForm
-from models import TestModel, OtherTestModel
+from django import forms
+from django.forms.models import inlineformset_factory
+from models import Author, Book
 
-class TestModelForm(ModelForm):
+class AuthorForm(forms.ModelForm):
 
     class Meta:
-        model = TestModel
-        fields = ['name']
+        model = Author
+        fields = ('name',)
 
 
-class OtherTestModelForm(ModelForm):
+class BookForm(forms.ModelForm):
 
     class Meta:
-        model = OtherTestModel
-        fields = ['title']
+        model = Book
+        fields = ('author', 'title', 'published',)
+
+
+BookFormSet = inlineformset_factory(Author, Book)

File testsite/testapp/models.py

 from datetime import datetime
 from django.contrib.auth.models import User
 from django.db import models
-from django.utils.timezone import utc
 
 
-class TestModel(models.Model):
+class Author(models.Model):
     user = models.ForeignKey(User)
     name = models.CharField(max_length=100, default='Name')
+    created = models.DateTimeField(default=datetime.now())
     active = models.BooleanField(default=True)
-    created = models.DateTimeField(default=datetime.now())
 
     def __unicode__(self):
         return self.name
 
-class OtherTestModel(models.Model):
-    user = models.ForeignKey(User)
-    title = models.CharField(max_length=100, default='Name')
-    visible = models.BooleanField(default=True)
+class Book(models.Model):
+    author = models.ForeignKey(Author)
+    title = models.CharField(max_length=100, default='Title')
     created = models.DateTimeField(default=datetime.now())
+    published = models.BooleanField(default=True)
 
     def __unicode__(self):
-        return self.name
+        return self.title

File testsite/testapp/tests.py

 from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
 from django.test import Client, TestCase
-from models import TestModel
+from models import Author
 
 
 class authenticated_client(object):
 
     def setUp(self):
         self.user = User.objects.create_user('john', 'john@foo.com', '123')
-        self.test_model = TestModel.objects.create(user=self.user)
+        self.test_model = Author.objects.create(user=self.user)
         self.client = Client()
 
     def test_deactivation_get(self):
         url = reverse('deactivate', args=[self.test_model.pk])
         response = self.client.get(url)
         self.assertEqual(response.status_code, 200)
-        self.assertTrue(TestModel.objects.get(pk=self.test_model.pk).active)
+        self.assertTrue(Author.objects.get(pk=self.test_model.pk).active)
 
     def test_deactivation_post(self):
         url = reverse('deactivate', args=[self.test_model.pk])
         response = self.client.post(url)
         self.assertEqual(response.status_code, 302)
-        self.assertTrue(TestModel.objects.filter(pk=self.test_model.pk).exists())
-        self.assertFalse(TestModel.objects.get(pk=self.test_model.pk).active)
+        self.assertTrue(Author.objects.filter(pk=self.test_model.pk).exists())
+        self.assertFalse(Author.objects.get(pk=self.test_model.pk).active)
 
 
 class FilteredListMixinTestCase(TestCase):
 
     def setUp(self):
         self.user = User.objects.create_user('john', 'john@foo.com', '123')
-        TestModel.objects.create(user=self.user, created=datetime(2013, 1, 1))
-        TestModel.objects.create(user=self.user, created=datetime(2012, 1, 1))
-        TestModel.objects.create(user=self.user, created=datetime(2011, 1, 1))
-        TestModel.objects.create(user=self.user, created=datetime(2010, 1, 1), active=False)
+        Author.objects.create(user=self.user, created=datetime(2013, 1, 1))
+        Author.objects.create(user=self.user, created=datetime(2012, 1, 1))
+        Author.objects.create(user=self.user, created=datetime(2011, 1, 1))
+        Author.objects.create(user=self.user, created=datetime(2010, 1, 1), active=False)
         self.client = Client()
 
     def test_filtered_list(self):
         url = '%s%s' % (reverse('filtered_list'), '?active=True')
         response = self.client.get(url)
-        test_models = response.context_data['testmodel_list']
+        test_models = response.context_data['author_list']
         self.assertEqual(response.status_code, 200)
         self.assertEqual(test_models.count(), 3)
 
     def test_filtered_list_multiple(self):
         url = '%s%s' % (reverse('filtered_list'), '?active=1&created__gte=2012-1-1')
         response = self.client.get(url)
-        test_models = response.context_data['testmodel_list']
+        test_models = response.context_data['author_list']
         self.assertEqual(response.status_code, 200)
         self.assertEqual(test_models.count(), 2)
 
     def test_filtered_list_ignores(self):
         url = '%s%s' % (reverse('filtered_list'), '?random=1')
         response = self.client.get(url)
-        test_models = response.context_data['testmodel_list']
+        test_models = response.context_data['author_list']
         self.assertEqual(response.status_code, 200)
         self.assertEqual(test_models.count(), 4)
 
         self.user = User.objects.create_user('john', 'john@foo.com', '123')
         self.client = Client()
 
-    def test_model_form_set(self):
+    def test_model_form_set_get(self):
         url = reverse('model_form_set')
         response = self.client.get(url)
         self.assertEqual(response.status_code, 200)
 
+    def test_model_form_set_post(self):
+        field_info = {
+            'name': 'john',
+        }
+        url = reverse('model_form_set')
+        response = self.client.post(url, field_info)
+        self.assertEqual(response.status_code, 200)
+
 
 class ObjectOwnerMixinTestCase(TestCase):
 
     def setUp(self):
         self.owner = User.objects.create_user('john', 'john@foo.com', '123')
         self.other_user = User.objects.create_user('bob', 'bob@foo.com', '123')
-        self.test_model = TestModel.objects.create(user=self.owner)
+        self.test_model = Author.objects.create(user=self.owner)
 
     def test_object_ownership_get(self):
         url = reverse('object_owner', args=[self.test_model.pk])
     def setUp(self):
         self.owner = User.objects.create_user('john', 'john@foo.com', '123')
         self.other_user = User.objects.create_user('bob', 'bob@foo.com', '123')
-        self.test_model = TestModel.objects.create(user=self.owner)
+        self.test_model = Author.objects.create(user=self.owner)
 
     def test_ownership_is_owner(self):
         url = reverse('ownership', args=[self.test_model.pk])

File testsite/testapp/views.py

 from django.core.urlresolvers import reverse_lazy
 from django.views.generic import CreateView, DeleteView, DetailView, ListView, TemplateView, UpdateView
-from forms import TestModelForm, OtherTestModelForm
-from models import TestModel, OtherTestModel
+from forms import AuthorForm, BookForm, BookFormSet
+from models import Author, Book
 from viewclass_mixins.views import DeactivateMixin, FilteredListMixin, HttpCacheMixin, LoginMixin, ModelFormSetMixin,\
     ObjectOwnerMixin, OwnershipMixin, StaffRequiredMixin, SuperuserRequiredMixin
 
 
 
 class DeactivateView(DeactivateMixin, DeleteView):
-    model = TestModel
+    model = Author
     success_url = reverse_lazy('success')
     template_name = 'home.html'
 
 
 class FilteredListView(FilteredListMixin, ListView):
-    model = TestModel
+    model = Author
     template_name = "home.html"
 
 
 
 
 class ModelFormSetView(ModelFormSetMixin, CreateView):
-    model = TestModel
-    formset_classes = [TestModelForm, OtherTestModelForm]
-    template_name = "home.html"
+    model = Author
+    form_class = AuthorForm
+    formset_classes = [BookFormSet]
+    template_name = "model_form.html"
 
 
 class ObjectOwnerView(ObjectOwnerMixin, UpdateView):
-    model = TestModel
-    owner_model = TestModel
+    model = Author
+    owner_model = Author
     owner_field = 'user'
     template_name = "home.html"
 
 
 class OwnershipView(OwnershipMixin, DetailView):
-    model = TestModel
+    model = Author
     template_name = "home.html"