Example #1
0
def test_render_form_errors():
    # Receive form with initial values
    # POST form to create view
    # Compare form.errors with MdcField(form.errors)
    postform = PostForm({})  # should yield an invalid form
    test_field = "date"
    postform.is_valid()  # ignore result
    invalid_msg = postform[test_field].field.error_messages["invalid"]
    postform.add_error(test_field, ValidationError(invalid_msg,
                                                   code="invalid"))
    err_msg = "<br />".join(postform.errors[test_field])
    ast_fld = MdcField.render(postform[test_field])
    ast_err = MdcField.errors(postform[test_field])
    ast = Div([], [ast_fld, ast_err])
    html = render_element(ast)

    test_str = f"""
<div>
<div class="mdc-text-field mdc-text-field--invalid" data-mdc-auto-init="MDCTextField">
 <input name="date" required id="id_date" class="mdc-text-field__input" type="date" />
 <label for="id_date" class="mdc-floating-label">Type = date:</label>
 <div class="mdc-line-ripple"></div>
</div>
<div class="mdc-text-field-helper-line"><div class="mdc-text-field-helper-text--validation-msg mdc-text-field-helper-text mdc-text-field-helper-text--persistent" class="mdc-text-field-helper-text--validation-msg" id="{test_field}-helper-text">{err_msg}</div></div>
</div>
"""  # noqa
    test_str = re.sub(r"\n\s*", "", test_str)
    assert html == test_str
Example #2
0
 def body(self):
     is_checked = {
         "name": "hello",
     }
     ctx = {
         "label": "Labelling:",
         "password": "******",
     }
     store = {
         "name": "this is my first store !!",
         "todos": [
             {
                 "name": "hello",
                 "id": "0",
             },
             {
                 "name": "todo number secondo !!",
                 "id": "1",
             },
         ]
     }
     ast = FormSchema(store, json.dumps(store))
     form = inject_ids(ast)
     app = Inject_ast_into_DOM(form, json.dumps(form))
     html = render_element(app, context_middleware(ctx))
     return mark_safe(html)
Example #3
0
    def render(self):

        ctx = {}

        ast = self.FormSchema()
        form = inject_ids(ast, context_middleware(ctx))
        #         app = Inject_ast_into_DOM(form, json.dumps(form))
        #         html = render_element(app, context_middleware(ctx))
        html = render_element(form, context_middleware(ctx))

        return mark_safe(html)
Example #4
0
def test_render_text(postform):
    fld = MdcField.render(postform["text"])
    html = render_element(fld)
    test_str = """
<div class="mdc-text-field" data-mdc-auto-init="MDCTextField">
 <input name="text" value="Initial value" maxlength="200" required id="id_text" class="mdc-text-field__input" type="text" />
 <label for="id_text" class="mdc-floating-label">Input Label:</label>
 <div class="mdc-line-ripple"></div>
</div>
"""  # noqa
    test_str = re.sub(r"\n\s*", "", test_str)
    assert html == test_str
Example #5
0
def test_render_date(postform):
    fld = MdcField.render(postform["date"])
    html = render_element(fld)
    test_str = """
<div class="mdc-text-field" data-mdc-auto-init="MDCTextField">
 <input name="date" value="2018-10-03" required id="id_date" class="mdc-text-field__input" type="date" />
 <label for="id_date" class="mdc-floating-label">Type = date:</label>
 <div class="mdc-line-ripple"></div>
</div>
"""  # noqa
    test_str = re.sub(r"\n\s*", "", test_str)
    assert html == test_str
Example #6
0
def test_render_checkbox(postform):
    fld = MdcField.render(postform["checkbox"])
    html = render_element(fld)
    test_str = """
<div class="mdc-form-field mdc-form-field--align-left" data-mdc-auto-init="MDCFormField">
 <div class="mdc-checkbox" data-mdc-auto-init="MDCCheckbox">
  <input name="checkbox" id="id_checkbox" checked class="mdc-checkbox__native-control" type="checkbox" />
  <div class="mdc-checkbox__background"></div>
 </div>
 <label for="id_checkbox">This is my checkbox:</label>
</div>
"""  # noqa
    test_str = re.sub(r"\n\s*", "", test_str)
    assert html == test_str
Example #7
0
def test_render_foreignkey(postform):
    from django.contrib.auth import get_user_model
    user_model = get_user_model()
    user_model.objects.create(username=user_name)

    fld = MdcField.render(postform["foreignkey"])
    html = render_element(fld)
    test_str = f"""
<div class="mdc-select" data-mdc-auto-init="MDCSelect">
 <select name="foreignkey" required id="id_foreignkey" class="mdc-select__native-control">
  <option value="" disabled></option>
  <option value="1">{user_name}</option>
 </select>
 <label for="id_foreignkey" class="mdc-floating-label">Foreignkey:</label>
 <div class="mdc-line-ripple"></div>
</div>
"""  # noqa
    test_str = re.sub(r"\n\s*", "", test_str)
    assert html == test_str
Example #8
0
def test_render_media(postform):
    fld = MdcField.render(postform["media"])
    html = render_element(fld)
    test_str = """
<div class="mdc-select" data-mdc-auto-init="MDCSelect">
 <select name="media" required id="id_media" class="mdc-select__native-control">
  <option value="" disabled></option>
  <optgroup label="Audio">
   <option value="vinyl" selected>Vinyl</option>
   <option value="cd">CD</option>
   <option value="mp3">MP3</option>
  </optgroup>
  <optgroup label="Video">
   <option value="vhs">VHS tape</option>
   <option value="dvd">DVD</option>
   <option value="blu-ray">Blu-ray</option>
  </optgroup>
 </select>
 <label for="id_media" class="mdc-floating-label">Media:</label>
 <div class="mdc-line-ripple"></div>
</div>
"""  # noqa
    test_str = re.sub(r"\n\s*", "", test_str)
    assert html == test_str