info_outline

Solutions will be available when this assignment is resolved, or after a few failing attempts.

Form Fields

In this assignment you'll implement the models (high level classes) to create a clone of Google Forms. You're in charge of implementing the Form fields. Most form fields are similar (they have a title and a help text and accept an answer), so we don't want to duplicate a lot of logic.

You should rely primarily on the tests to make this assignment work.

Form creation and submitting answers

A form field is going to be created by passing a title and optionally a help text. MultipleChoiceFields will also accept options. Once you have created your field you can invoke the submit_answer() method. And after an answer was recorded, you can invoke the is_valid() method that will return if the answer was valid.

If you try to invoke is_valid in a form field that hasn't recorded an answer yet (an "unbound" field) that'll result in a ValueError exception raised. Examples:

text_field = TextField(
      "What do you like about RMOTR?",
      help_text="Mentors? Classes? Assignments?")
print(text_field.help_text)  # "Mentors? Classes? Assignments?"

text_field.is_valid()  # WARNING! Raises ValueError

text_field.submit_answer('Mentoring!')

text_field.is_valid()  # True

Test Cases

test multiple choice field is valid - Run Test

def test_multiple_choice_field_is_valid():
    multi_field = MultipleChoiceField(
        "What's your preferred language?",
        ['Python', 'Ruby', 'Javascript'],
        help_text="There's only one correct answer ;)")

    multi_field.submit_answer('Python')
    assert multi_field.is_valid()

    assert multi_field.help_text == "There's only one correct answer ;)"

test url field is valid - Run Test

def test_url_field_is_valid():
    twitter_field = URLField("Twitter profile URL?")
    twitter_field.submit_answer('https://twitter.com/rmotr_com')
    assert twitter_field.is_valid()

test url form field unbound - Run Test

import pytest

def test_url_form_field_unbound():
    twitter_field = URLField("Twitter profile URL?")

    with pytest.raises(ValueError):
        twitter_field.is_valid()

test email field is valid - Run Test

def test_email_field_is_valid():
    email_field = EmailField("What's your email?")
    email_field.submit_answer('questions@rmotr.com')
    assert email_field.is_valid()

test email form field unbound - Run Test

import pytest

def test_email_form_field_unbound():
    email_field = EmailField("What's your email?")

    with pytest.raises(ValueError):
        email_field.is_valid()

test email field is invalid - Run Test

def test_email_field_is_invalid():
    email_field = EmailField("What's your email?")
    email_field.submit_answer('INVALID EMAIL')
    assert not email_field.is_valid()

test text form field unbound - Run Test

import pytest

def test_text_form_field_unbound():
    text_field = TextField(
        "What do you like about RMOTR?",
        help_text="Mentors? Classes? Assignments?")

    with pytest.raises(ValueError):
        text_field.is_valid()

    assert text_field.help_text == "Mentors? Classes? Assignments?"

test url field is invalid - Run Test

def test_url_field_is_invalid():
    twitter_field = URLField("Twitter profile URL?")
    twitter_field.submit_answer('INVALID URL')

    assert not twitter_field.is_valid()

test multiple choice form field unbound - Run Test

import pytest

def test_multiple_choice_form_field_unbound():
    multi_field = MultipleChoiceField(
        "What's your preferred language?",
        ['Python', 'Ruby', 'Javascript'],
        help_text="There's only one correct answer ;)")

    with pytest.raises(ValueError):
        multi_field.is_valid()

    assert multi_field.help_text == "There's only one correct answer ;)"

test text form fields - Run Test

def test_text_form_fields():
    text_field = TextField(
        "What do you like about RMOTR?",
        help_text="Mentors? Classes? Assignments?")

    text_field.submit_answer('Mentoring, for sure!')

    assert text_field.is_valid()

test multiple choice field invalid - Run Test

def test_multiple_choice_field_invalid():
    multi_field = MultipleChoiceField(
        "What's your preferred language?",
        ['Python', 'Ruby', 'Javascript'])

    multi_field.submit_answer('Invalid Language')
    assert not multi_field.is_valid()
class FormField(object): def __init__(self, title, help_text=None): pass def submit_answer(self, answer): pass def get_answer(self): pass def is_valid(self): raise NotImplementedError() class TextField(FormField): pass class EmailField(FormField): pass class URLField(FormField): pass class MultipleChoiceField(FormField): def __init__(self, title, options, help_text=None): pass