def test_edit_render(self, **kwargs): """ The form can be instantiated with a (freshly created) UserAS. The instantiated form should not show any changes if the same data is submitted. """ form_data = self._get_initial_dict() form_data.update(**kwargs) # Create a UserAS with the given data create_form = UserASForm(user=get_testuser(), data=form_data) self.assertIsNotNone(create_form.as_table()) self.assertTrue(create_form.is_valid(), create_form.errors) user_as = create_form.save() self.assertIsNotNone(user_as) # Check that the form can be instantiated for the object just created # and check that the forms initial values are the same as the # previously submitted values. edit_form_1 = UserASForm(user=get_testuser(), instance=user_as, data=form_data) self.assertIsNotNone(edit_form_1.as_table()) self.assertTrue(edit_form_1.is_valid(), edit_form_1.errors) self.assertFalse(edit_form_1.has_changed(), edit_form_1.changed_data) user_as_edited_1 = edit_form_1.save() self.assertEqual(user_as.pk, user_as_edited_1.pk) # Do it again! edit_form_2 = UserASForm(user=get_testuser(), instance=user_as, data=form_data) self.assertTrue(edit_form_2.is_valid(), edit_form_2.errors) self.assertFalse(edit_form_2.has_changed(), edit_form_2.changed_data) user_as_edited_2 = edit_form_2.save() self.assertEqual(user_as.pk, user_as_edited_2.pk)
def test_edit(self, initial_data, new_data): # Create a UserAS with the given data create_form = UserASForm(user=get_testuser(), data=initial_data) self.assertIsNotNone(create_form.as_table()) self.assertTrue(create_form.is_valid(), create_form.errors) user_as = create_form.save() self.assertIsNotNone(user_as) # Submittng the new data on the previously created instance should change it edit_form_1_data = _get_form_fields_for_edit(new_data, user_as) edit_form_1 = UserASForm(user=get_testuser(), instance=user_as, data=edit_form_1_data) rendered_1 = edit_form_1.as_table() self.assertIsNotNone(rendered_1) rendered_formset_1 = edit_form_1.attachment_conf_form_set.as_table() self.assertIsNotNone(rendered_formset_1) self.assertTrue(edit_form_1.is_valid(), edit_form_1.errors) if initial_data == new_data: self.assertFalse(edit_form_1.has_changed(), _get_forms_change_data(edit_form_1)) else: self.assertTrue(edit_form_1.has_changed()) user_as_edited_1 = edit_form_1.save() self.assertEqual(user_as.pk, user_as_edited_1.pk) links_edited_1 = [ iface.link().pk for iface in user_as.interfaces.all() ] self.assertEqual(len(links_edited_1), new_data['form-TOTAL_FORMS']) if initial_data == new_data: return # no need to do this _again_ # Submittng the ~same data on the previously edited instance should have no further changes edit_form_2_data = _get_form_fields_for_edit(new_data, user_as) edit_form_2 = UserASForm(user=get_testuser(), instance=user_as, data=edit_form_2_data) self.assertFalse(edit_form_2.has_changed(), _get_forms_change_data(edit_form_2)) self.assertTrue(edit_form_2.is_valid(), edit_form_2.errors) links_edited_2 = [ iface.link().pk for iface in user_as.interfaces.all() ] self.assertEqual(links_edited_1, links_edited_2)
def test_create_check(self, form_data): # Create a UserAS with the given data create_form = UserASForm(user=get_testuser(), data=form_data) self.assertIsNotNone(create_form.as_table()) self.assertTrue(create_form.is_valid(), create_form.errors) user_as = create_form.save() self.assertIsNotNone(user_as.pk) links = [iface.link().pk for iface in user_as.interfaces.all()] self.assertEqual(len(links), form_data['form-TOTAL_FORMS']) # Submittng the ~same data on the previously created instance should have no changes form_data_2 = _get_form_fields_for_edit(form_data, user_as) edit_form = UserASForm(user=get_testuser(), instance=user_as, data=form_data_2) self.assertFalse(edit_form.has_changed(), _get_forms_change_data(edit_form)) self.assertTrue(edit_form.is_valid(), edit_form.errors) # Also, if we save the updated form, nothing should change user_as_edited = edit_form.save() self.assertEqual(user_as.pk, user_as_edited.pk) links_edited = [iface.link().pk for iface in user_as.interfaces.all()] self.assertEqual(links, links_edited)