def test_form_save(self): formClass = modelform_factory(self.TARGET.__class__) original_version = self.TARGET._get_test_revision_number() version_field_name = self.TARGET.RevisionMetaInfo.field.name form = formClass(self._get_form_data( ** {version_field_name: VersionFieldSigner().sign(original_version)}), instance=self.TARGET) self.assertTrue(form.is_valid(), form.errors) obj = form.save() self.assertIsNotNone(obj.pk) # self.assertGreater(obj._get_test_revision_number(), original_version) self.assertNotEqual(obj._get_test_revision_number(), original_version) form = formClass(self._get_form_data( **{ version_field_name: VersionFieldSigner().sign(obj._get_test_revision_number()) }), instance=obj) self.assertTrue(form.is_valid(), form.errors) pre_save_version = obj._get_test_revision_number() obj_after = form.save() self.assertIsNotNone(obj_after.pk) self.assertNotEqual(obj_after._get_test_revision_number(), pre_save_version) self.assertTrue(bool(obj_after._get_test_revision_number()), "'%s'" % obj_after._get_test_revision_number())
def test(self): f = VersionField() self.assertEqual(1, f.clean(VersionFieldSigner().sign(1))) self.assertEqual(1, f.clean(VersionFieldSigner().sign('1'))) self.assertEqual(0, f.clean(None)) self.assertEqual(0, f.clean('')) self.assertRaises(VersionError, f.clean, '100') self.assertRaises(VersionError, f.clean, VersionFieldSigner().sign(1.5))
def test_form_is_valid(self): with self.settings(CONCURRECY_SANITY_CHECK=True): obj, __ = TestIssue3Model.objects.get_or_create(username='******') Form = modelform_factory(TestIssue3Model, ConcurrentForm) data = {'username': "******", 'revision': VersionFieldSigner().sign(1)} form = Form(data) self.assertRaises(InconsistencyError, form.is_valid)
def test_sanity_signer(self): target, __ = SimpleConcurrentModel.objects.get_or_create(username='******') url = reverse('admin:demo_simpleconcurrentmodel_change', args=[target.pk]) res = self.app.get(url, user='******') form = res.form version1 = int(str(form['version'].value).split(":")[0]) form['version'] = VersionFieldSigner().sign(version1) form['date_field'] = 'esss2010-09-01' response = form.submit() self.assertIn('original', response.context) self.assertTrue(response.context['adminform'].form.errors, response.context['adminform'].form.errors) form = response.context['adminform'].form version2 = int(str(form['version'].value()).split(":")[0]) self.assertEqual(version1, version2)
def test_is_valid(self): obj, __ = TestIssue3Model.objects.get_or_create(username='******') Form = modelform_factory(TestIssue3Model, ConcurrentForm) data = { 'username': '******', 'last_name': None, 'date_field': None, 'char_field': None, 'version': 'abc', 'id': 1, 'revision': VersionFieldSigner().sign(obj.revision) } form = Form(data, instance=obj) obj.save() # save again simulate concurrent editing self.assertRaises(ValueError, form.save)
def test_save(self): obj, __ = TestIssue3Model.objects.get_or_create(username='******') obj_copy = TestIssue3Model.objects.get(pk=obj.pk) Form = modelform_factory(TestIssue3Model, ConcurrentForm) data = { 'username': '******', 'last_name': None, 'date_field': None, 'char_field': None, 'version': 'abc', 'id': 1, 'revision': VersionFieldSigner().sign(obj.revision) } form = Form(data, instance=obj) obj_copy.save() # save self.assertFalse(form.is_valid()) self.assertIn(_('Record Modified'), form.non_field_errors())
def test_stack(self): m, __ = TestModel0.objects.get_or_create(username="******", last_name="1") copy = TestModel0.objects.get(pk=m.pk) url = reverse('admin:concurrency_testmodel0_change', args=[m.pk]) data = { 'username': '******', 'last_name': None, 'version': VersionFieldSigner().sign(m.version), 'char_field': None, '_continue': 1, 'date_field': '2010-09-01' } copy.save() r = self.client.post(url, data, follow=True) self.assertEqual(r.status_code, 409) self.assertIn('target', r.context) self.assertIn('saved', r.context) self.assertEqual(r.context['saved'].version, copy.version) self.assertEqual(r.context['target'].version, m.version) self.assertEqual(r.context['request_path'], url)