def test_send_email_to_configured_address(self): form_model = FormModel.objects.create(name='Form 1', submit_url='/form_1/', success_url='/form_1/done/', recipient_email='*****@*****.**') FormFieldModel.objects.create(parent_form=form_model, label='Str', field_type='dynamic_forms.formfields.SingleLineTextField', position=1) FormFieldModel.objects.create(parent_form=form_model, label='DT', field_type='dynamic_forms.formfields.DateTimeField', position=2) form = FormModelForm(model=form_model, data={ 'str': 'Some string to store', 'dt': datetime.datetime(2013, 8, 29, 12, 34, 56, 789000), }) self.assertTrue(form.is_valid()) self.assertEqual(mail.outbox, []) dynamic_form_send_email(form_model, form) message = mail.outbox[0] self.assertEqual(message.subject, 'Form “Form 1” submitted') self.assertEqual(message.body, '''Hello, you receive this e-mail because someone submitted the form “Form 1”. DT: Aug. 29, 2013, 12:34 p.m. Str: Some string to store ''') self.assertEqual(message.recipients(), ['*****@*****.**']) self.assertEqual(message.from_email, 'webmaster@localhost')
def test_get_mapped_data(self): fm = FormModel.objects.create(name="Form", submit_url="/form/") FormFieldModel.objects.create( parent_form=fm, label="Label 1", field_type="dynamic_forms.formfields.SingleLineTextField", position=3, _options='{"required": false}', ) FormFieldModel.objects.create( parent_form=fm, label="Label 2", field_type="dynamic_forms.formfields.SingleLineTextField", position=1, _options='{"required": false}', ) FormFieldModel.objects.create( parent_form=fm, label="Label 3", field_type="dynamic_forms.formfields.SingleLineTextField", position=2, _options='{"required": false}', ) data = {"label-1": "Value 1", "label-2": "Value 2"} form = FormModelForm(model=fm, data=data) self.assertTrue(form.is_valid()) self.assertEqual( form.get_mapped_data(), OrderedDict([("Label 2", "Value 2"), ("Label 3", ""), ("Label 1", "Value 1")]) ) self.assertEqual( form.get_mapped_data(exclude_missing=True), OrderedDict([("Label 2", "Value 2"), ("Label 1", "Value 1")]) )
def test_get_mapped_data(self): fm = FormModel.objects.create(name='Form', submit_url='/form/') FormFieldModel.objects.create(parent_form=fm, label='Label 1', field_type='dynamic_forms.formfields.SingleLineTextField', position=3, _options='{"required": false}') FormFieldModel.objects.create(parent_form=fm, label='Label 2', field_type='dynamic_forms.formfields.SingleLineTextField', position=1, _options='{"required": false}') FormFieldModel.objects.create(parent_form=fm, label='Label 3', field_type='dynamic_forms.formfields.SingleLineTextField', position=2, _options='{"required": false}') data = { 'label-1': 'Value 1', 'label-2': 'Value 2', } form = FormModelForm(model=fm, data=data) self.assertTrue(form.is_valid()) self.assertEqual(form.get_mapped_data(), OrderedDict([ ('Label 2', 'Value 2',), ('Label 3', '',), ('Label 1', 'Value 1',), ])) self.assertEqual(form.get_mapped_data(exclude_missing=True), OrderedDict([ ('Label 2', 'Value 2',), ('Label 1', 'Value 1',), ] ))
def test_get_mapped_data_no_display(self): try: key = 'tests.test_forms.CharField' registry.register(CharField) fm = FormModel.objects.create(name='Form', submit_url='/form/') FormFieldModel.objects.create( parent_form=fm, label='Label 1', field_type='dynamic_forms.formfields.SingleLineTextField', position=1) FormFieldModel.objects.create(parent_form=fm, label='Label 2', field_type=key, position=2) data = { 'label-1': 'Value 1', 'label-2': 'NOT SHOWN!', } form = FormModelForm(model=fm, data=data) self.assertTrue(form.is_valid()) self.assertEqual(form.get_mapped_data(), OrderedDict([ ( 'Label 1', 'Value 1', ), ])) finally: registry.unregister(key)
def test_get_mapped_data(self): fm = FormModel.objects.create(name='Form', submit_url='/form/') FormFieldModel.objects.create( parent_form=fm, label='Label 1', field_type='dynamic_forms.formfields.SingleLineTextField', position=3, _options='{"required": false}') FormFieldModel.objects.create( parent_form=fm, label='Label 2', field_type='dynamic_forms.formfields.SingleLineTextField', position=1, _options='{"required": false}') FormFieldModel.objects.create( parent_form=fm, label='Label 3', field_type='dynamic_forms.formfields.SingleLineTextField', position=2, _options='{"required": false}') data = { 'label-1': 'Value 1', 'label-2': 'Value 2', } form = FormModelForm(model=fm, data=data) self.assertTrue(form.is_valid()) self.assertEqual( form.get_mapped_data(), OrderedDict([ ( 'Label 2', 'Value 2', ), ( 'Label 3', '', ), ( 'Label 1', 'Value 1', ), ])) self.assertEqual( form.get_mapped_data(exclude_missing=True), OrderedDict([ ( 'Label 2', 'Value 2', ), ( 'Label 1', 'Value 1', ), ]))
def test_get_mapped_data_no_display(self): try: key = "tests.test_forms.CharField" registry.register(CharField) fm = FormModel.objects.create(name="Form", submit_url="/form/") FormFieldModel.objects.create( parent_form=fm, label="Label 1", field_type="dynamic_forms.formfields.SingleLineTextField", position=1 ) FormFieldModel.objects.create(parent_form=fm, label="Label 2", field_type=key, position=2) data = {"label-1": "Value 1", "label-2": "NOT SHOWN!"} form = FormModelForm(model=fm, data=data) self.assertTrue(form.is_valid()) self.assertEqual(form.get_mapped_data(), OrderedDict([("Label 1", "Value 1")])) finally: registry.unregister(key)
def setUp(self): action_registry.register(TestAction(), 'Some action') action_registry.register(TestOldAction(), 'Some old action') action_registry.register(TestAction2(), 'Some action2') self.fm = FormModel.objects.create( name='Form', submit_url='/form/', success_url='/done/', actions=[ 'tests.test_views.TestAction', 'tests.test_views.TestOldAction' ], form_template='dynamic_forms/form.html', success_template='dynamic_forms/form_success.html') self.field1 = FormFieldModel.objects.create( parent_form=self.fm, field_type='dynamic_forms.formfields.SingleLineTextField', label='String Field', position=1) self.field2 = FormFieldModel.objects.create( parent_form=self.fm, field_type='dynamic_forms.formfields.BooleanField', label='Field for Boolean', position=2) self.field3 = FormFieldModel.objects.create( parent_form=self.fm, field_type='dynamic_forms.formfields.DateTimeField', label='Date and time', position=3) self.form = FormModelForm(model=self.fm)
def test_generate_form_with_data(self): fm = FormModel.objects.create(name='With data', submit_url='/form/') data = {'afield': 'a value', 'anotherfield': 'another value'} form = FormModelForm(model=fm, data=data) self.assertEqual(form.data, { 'afield': 'a value', 'anotherfield': 'another value' })
class TestActions(TestCase): def setUp(self): self.form_model = FormModel.objects.create(name="Form", submit_url="/form/", success_url="/form/done/") FormFieldModel.objects.create( parent_form=self.form_model, label="Str", field_type="dynamic_forms.formfields.SingleLineTextField", position=1, ) FormFieldModel.objects.create( parent_form=self.form_model, label="DT", field_type="dynamic_forms.formfields.DateTimeField", position=2 ) self.form = FormModelForm( model=self.form_model, data={"str": "Some string to store", "dt": datetime.datetime(2013, 8, 29, 12, 34, 56, 789000)}, ) def test_store_database(self): self.assertTrue(self.form.is_valid()) dynamic_form_store_database(self.form_model, self.form) self.assertEqual(FormModelData.objects.count(), 1) data = FormModelData.objects.get() self.assertEqual(data.value, '{"Str": "Some string to store", "DT": "2013-08-29T12:34:56.789"}') @override_settings(DYNAMIC_FORMS_EMAIL_RECIPIENTS=["*****@*****.**"]) def test_send_email(self): self.assertTrue(self.form.is_valid()) self.assertEqual(mail.outbox, []) dynamic_form_send_email(self.form_model, self.form) message = mail.outbox[0] self.assertEqual(message.subject, "Form “Form” submitted") self.assertEqual( message.body, """Hello, you receive this e-mail because someone submitted the form “Form”. DT: Aug. 29, 2013, 12:34 p.m. Str: Some string to store """, ) self.assertEqual(message.recipients(), ["*****@*****.**"]) self.assertEqual(message.from_email, "webmaster@localhost")
class TestActions(TestCase): def setUp(self): self.form_model = FormModel.objects.create(name='Form', submit_url='/form/', success_url='/form/done/') FormFieldModel.objects.create(parent_form=self.form_model, label='Str', field_type='dynamic_forms.formfields.SingleLineTextField', position=1) FormFieldModel.objects.create(parent_form=self.form_model, label='DT', field_type='dynamic_forms.formfields.DateTimeField', position=2) self.form = FormModelForm(model=self.form_model, data={ 'str': 'Some string to store', 'dt': datetime.datetime(2013, 8, 29, 12, 34, 56, 789000), }) def test_store_database(self): self.assertTrue(self.form.is_valid()) action_data = dynamic_form_store_database(self.form_model, self.form) self.assertEqual(FormModelData.objects.count(), 1) data = FormModelData.objects.get() self.assertEqual( data.value, '{"Str": "Some string to store", "DT": "2013-08-29T12:34:56.789"}' ) self.assertEqual(action_data, data) @override_settings(DYNAMIC_FORMS_EMAIL_RECIPIENTS=['*****@*****.**']) def test_send_email(self): self.assertTrue(self.form.is_valid()) self.assertEqual(mail.outbox, []) dynamic_form_send_email(self.form_model, self.form) message = mail.outbox[0] self.assertEqual(message.subject, 'Form “Form” submitted') self.assertEqual(message.body, '''Hello, you receive this e-mail because someone submitted the form “Form”. DT: Aug. 29, 2013, 12:34 p.m. Str: Some string to store ''') self.assertEqual(message.recipients(), ['*****@*****.**']) self.assertEqual(message.from_email, 'webmaster@localhost')
def test_get_mapped_data_no_display(self): try: key = 'tests.test_forms.CharField' registry.register(CharField) fm = FormModel.objects.create(name='Form', submit_url='/form/') FormFieldModel.objects.create(parent_form=fm, label='Label 1', field_type='dynamic_forms.formfields.SingleLineTextField', position=1) FormFieldModel.objects.create(parent_form=fm, label='Label 2', field_type=key, position=2) data = { 'label-1': 'Value 1', 'label-2': 'NOT SHOWN!', } form = FormModelForm(model=fm, data=data) self.assertTrue(form.is_valid()) self.assertEqual(form.get_mapped_data(), OrderedDict([ ('Label 1', 'Value 1',), ])) finally: registry.unregister(key)
def test_send_email_to_configured_address(self): form_model = FormModel.objects.create( name='Form 1', submit_url='/form_1/', success_url='/form_1/done/', recipient_email='*****@*****.**') FormFieldModel.objects.create( parent_form=form_model, label='Str', field_type='dynamic_forms.formfields.SingleLineTextField', position=1) FormFieldModel.objects.create( parent_form=form_model, label='DT', field_type='dynamic_forms.formfields.DateTimeField', position=2) form = FormModelForm(model=form_model, data={ 'str': 'Some string to store', 'dt': datetime.datetime(2013, 8, 29, 12, 34, 56, 789000), }) self.assertTrue(form.is_valid()) self.assertEqual(mail.outbox, []) dynamic_form_send_email(form_model, form) message = mail.outbox[0] self.assertEqual(message.subject, 'Form “Form 1” submitted') self.assertEqual( message.body, '''Hello, you receive this e-mail because someone submitted the form “Form 1”. DT: Aug. 29, 2013, 12:34 p.m. Str: Some string to store ''') self.assertEqual(message.recipients(), ['*****@*****.**']) self.assertEqual(message.from_email, 'webmaster@localhost')
def setUp(self): self.form_model = FormModel.objects.create(name='Form', submit_url='/form/', success_url='/form/done/') FormFieldModel.objects.create(parent_form=self.form_model, label='Str', field_type='dynamic_forms.formfields.SingleLineTextField', position=1) FormFieldModel.objects.create(parent_form=self.form_model, label='DT', field_type='dynamic_forms.formfields.DateTimeField', position=2) self.form = FormModelForm(model=self.form_model, data={ 'str': 'Some string to store', 'dt': datetime.datetime(2013, 8, 29, 12, 34, 56, 789000) })
def setUp(self): self.form_model = FormModel.objects.create(name='Form', submit_url='/form/', success_url='/form/done/') FormFieldModel.objects.create( parent_form=self.form_model, label='Str', field_type='dynamic_forms.formfields.SingleLineTextField', position=1) FormFieldModel.objects.create( parent_form=self.form_model, label='DT', field_type='dynamic_forms.formfields.DateTimeField', position=2) self.form = FormModelForm(model=self.form_model, data={ 'str': 'Some string to store', 'dt': datetime.datetime( 2013, 8, 29, 12, 34, 56, 789000) })
def setUp(self): self.form_model = FormModel.objects.create(name="Form", submit_url="/form/", success_url="/form/done/") FormFieldModel.objects.create( parent_form=self.form_model, label="Str", field_type="dynamic_forms.formfields.SingleLineTextField", position=1, ) FormFieldModel.objects.create( parent_form=self.form_model, label="DT", field_type="dynamic_forms.formfields.DateTimeField", position=2 ) self.form = FormModelForm( model=self.form_model, data={"str": "Some string to store", "dt": datetime.datetime(2013, 8, 29, 12, 34, 56, 789000)}, )
class TestActions(TestCase): def setUp(self): self.form_model = FormModel.objects.create(name='Form', submit_url='/form/', success_url='/form/done/') FormFieldModel.objects.create( parent_form=self.form_model, label='Str', field_type='dynamic_forms.formfields.SingleLineTextField', position=1) FormFieldModel.objects.create( parent_form=self.form_model, label='DT', field_type='dynamic_forms.formfields.DateTimeField', position=2) self.form = FormModelForm(model=self.form_model, data={ 'str': 'Some string to store', 'dt': datetime.datetime( 2013, 8, 29, 12, 34, 56, 789000) }) def test_store_database(self): self.assertTrue(self.form.is_valid()) action_data = dynamic_form_store_database(self.form_model, self.form) self.assertEqual(FormModelData.objects.count(), 1) data = FormModelData.objects.get() self.assertEqual( data.value, '{"Str": "Some string to store", "DT": "2013-08-29T12:34:56.789"}') self.assertEqual(action_data, data) @override_settings(DYNAMIC_FORMS_EMAIL_RECIPIENTS=['*****@*****.**']) def test_send_email(self): self.assertTrue(self.form.is_valid()) self.assertEqual(mail.outbox, []) dynamic_form_send_email(self.form_model, self.form) message = mail.outbox[0] self.assertEqual(message.subject, 'Form “Form” submitted') self.assertEqual( message.body, '''Hello, you receive this e-mail because someone submitted the form “Form”. DT: Aug. 29, 2013, 12:34 p.m. Str: Some string to store ''') self.assertEqual(message.recipients(), ['*****@*****.**']) self.assertEqual(message.from_email, 'webmaster@localhost') def test_send_email_to_configured_address(self): form_model = FormModel.objects.create( name='Form 1', submit_url='/form_1/', success_url='/form_1/done/', recipient_email='*****@*****.**') FormFieldModel.objects.create( parent_form=form_model, label='Str', field_type='dynamic_forms.formfields.SingleLineTextField', position=1) FormFieldModel.objects.create( parent_form=form_model, label='DT', field_type='dynamic_forms.formfields.DateTimeField', position=2) form = FormModelForm(model=form_model, data={ 'str': 'Some string to store', 'dt': datetime.datetime(2013, 8, 29, 12, 34, 56, 789000), }) self.assertTrue(form.is_valid()) self.assertEqual(mail.outbox, []) dynamic_form_send_email(form_model, form) message = mail.outbox[0] self.assertEqual(message.subject, 'Form “Form 1” submitted') self.assertEqual( message.body, '''Hello, you receive this e-mail because someone submitted the form “Form 1”. DT: Aug. 29, 2013, 12:34 p.m. Str: Some string to store ''') self.assertEqual(message.recipients(), ['*****@*****.**']) self.assertEqual(message.from_email, 'webmaster@localhost')
def test_generate_form_wo_data(self): fm = FormModel.objects.create(name='No data', submit_url='/form/') form = FormModelForm(model=fm) self.assertEqual(form.data, {})