Repository: jazzband/django-robots (language: python)

Issue:
Not able to create Url with model_bakery: too long pattern throws exception
If I try to create Url model with model_bakery without any parameters, I have got this error:
```
django.db.utils.DataError: value too long for type character varying(255)
```

The problem is, that `pattern` parameter (generated by model_bakery) gets lengthen in `save()` method by leading `/` and doesn't fit in the field anymore.

The solution is very simple: add `default=""` to the pattern field.

There is also second problem with the save method - if user enters pattern exactly 255 characters long in the admin, it will throw an exception rather than showing validation error.


Discussion / hints:

Fix patch:
diff --git a/src/robots/migrations/0003_alter_url_pattern.py b/src/robots/migrations/0003_alter_url_pattern.py
new file mode 100644
index 0000000..7d74a20
--- /dev/null
+++ b/src/robots/migrations/0003_alter_url_pattern.py
@@ -0,0 +1,24 @@
+# Generated by Django 4.0.6 on 2022-08-19 08:56
+
+import django.core.validators
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("robots", "0002_alter_id_fields"),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name="url",
+            name="pattern",
+            field=models.CharField(
+                help_text="Case-sensitive. A missing trailing slash does also match to files which start with the name of the pattern, e.g., '/admin' matches /admin.html too. Some major search engines allow an asterisk (*) as a wildcard and a dollar sign ($) to match the end of the URL, e.g., '/*.jpg$'.",
+                max_length=255,
+                validators=[django.core.validators.MaxLengthValidator(254)],
+                verbose_name="pattern",
+            ),
+        ),
+    ]
diff --git a/src/robots/models.py b/src/robots/models.py
index f277f62..62279a6 100644
--- a/src/robots/models.py
+++ b/src/robots/models.py
@@ -1,4 +1,5 @@
 from django.contrib.sites.models import Site
+from django.core.validators import MaxLengthValidator
 from django.db import models
 from django.utils.text import get_text_list
 from django.utils.translation import gettext_lazy as _
@@ -21,6 +22,7 @@ class Url(models.Model):
             " (*) as a wildcard and a dollar sign ($) to "
             "match the end of the URL, e.g., '/*.jpg$'."
         ),
+        validators=[MaxLengthValidator(254)],
     )
 
     class Meta:


Test patch:
diff --git a/src/robots/tests.py b/src/robots/tests.py
index 8e80965..e9aceb0 100644
--- a/src/robots/tests.py
+++ b/src/robots/tests.py
@@ -3,6 +3,7 @@
 from django.contrib.auth import SESSION_KEY
 from django.contrib.auth.models import AnonymousUser
 from django.contrib.sites.models import Site
+from django.core.exceptions import ValidationError
 from django.http import SimpleCookie
 from django.test import RequestFactory, TestCase
 from django.utils.encoding import force_str
@@ -173,3 +174,24 @@ def test_cached_sitemap(self):
             response.render()
             content = force_str(response.content)
             self.assertTrue("Sitemap: http://example.com/other/sitemap.xml" in content)
+
+
+class UrlModelTests(TestCase):
+    def test_str(self):
+        url = Url.objects.create(pattern="/")
+        self.assertEqual(str(url), "/")
+
+    def test_pattern(self):
+        url = Url.objects.create(pattern="foo")
+        self.assertEqual(url.pattern, "/foo")
+
+    def test_pattern_slash(self):
+        url = Url.objects.create(pattern="/foo")
+        self.assertEqual(url.pattern, "/foo")
+
+    def test_pattern_too_long(self):
+        """Patern 256 chars long should not throw field too long error"""
+        with self.assertRaises(ValidationError):
+            r = Url(pattern="a" * 255)
+            r.full_clean()
+            r.save()