can we create unit tests for custom widgets in a django app?

Viewed 633

I'm new with unit testing in django, I have referred to the django documentation to write unit tests for views, forms and models. But a question came to my mind about widgets if they could be also tested, since unit tests are made to test every single unit in the project.

For example I have a widget that let me write multiple emails in one inputText using a plugin and the widget as follow is made to change ' to " so that the plugin can work.

class MultiEmailWidget(Input):
def value_from_datadict(self, data, files, name):
    if name in data:
        if data[name]:
            return data[name].replace(']' ,'').replace('[','').replace('"' ,'').replace(',' , '\n')
    return None
def render_value(self , value):
    """
    change ' to " because multiple_email.js do split with "
    """

    if value:
        return str(value).replace("'",'"')
    return value

def render(self, name, value, attrs=None):
    value = self.render_value(value)
    return super(MultiEmailWidget, self).render(name, value, attrs)

Is this widget testable ? Or do somebody have some useful links about testing widgets in django ? (if this is testable ofcourse)

Thank you in advance :)

0 Answers
Related