Skip to content

Commit

Permalink
Merge pull request #618 from kuter/additional_tests_for_TagField
Browse files Browse the repository at this point in the history
Additional tests for TagField
  • Loading branch information
Asif Saif Uddin committed Apr 10, 2019
2 parents 545ffc6 + d65a1cb commit 01513c1
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django import forms
from django.test import TestCase
from django.test.utils import override_settings

from taggit.forms import TagField
from taggit.models import Tag


def _test_parse_tags(tagstring):
if "," in tagstring:
return tagstring.split(",")
else:
raise ValueError()


@override_settings(TAGGIT_TAGS_FROM_STRING="tests.test_forms._test_parse_tags")
class TagFieldTests(TestCase):
def test_should_return_error_on_clean_if_not_comma_separated(self):
class TestForm(forms.Form):
tag = TagField()

excpected_error = "Please provide a comma-separated list of tags."

form = TestForm({"tag": "not-comma-separated"})

self.assertFalse(form.is_valid())
self.assertIn(excpected_error, form.errors["tag"])

def test_should_always_return_False_on_has_change_if_disabled(self):
class TestForm(forms.Form):
tag = TagField(disabled=True)

form = TestForm(initial={"tag": "foo,bar"}, data={"tag": ["a,b,c"]})

self.assertTrue(form.is_valid())
self.assertFalse(form.has_changed())

def test_should_return_True_if_form_has_changed(self):
class TestForm(forms.Form):
tag = TagField()

form = TestForm(initial={"tag": [Tag(name="a")]}, data={"tag": ["b"]})

self.assertTrue(form.has_changed())

def test_should_return_False_if_form_has_not_changed(self):
class TestForm(forms.Form):
tag = TagField()

form = TestForm(
initial={"tag": [Tag(name="foo-bar")]}, data={"tag": ["foo-bar"]}
)

self.assertFalse(form.has_changed())

0 comments on commit 01513c1

Please sign in to comment.