From d8506990b0432b3596696d944eb037b15a3ea2b4 Mon Sep 17 00:00:00 2001 From: mike wakerly Date: Wed, 14 Dec 2022 15:46:02 -0500 Subject: [PATCH] bugfix: fix an error with prefixes greater than 2 characters (:facepalm:) --- django_spicy_id/fields.py | 2 +- django_spicy_id/tests/fields_test.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/django_spicy_id/fields.py b/django_spicy_id/fields.py index c46d603..e29fff0 100644 --- a/django_spicy_id/fields.py +++ b/django_spicy_id/fields.py @@ -23,7 +23,7 @@ } # Validates acceptable values for the `prefix=` field parameter. -LEGAL_PREFIX_RE = re.compile("^[a-zA-Z][0-9a-z-A-Z]?$") +LEGAL_PREFIX_RE = re.compile("^[a-zA-Z][0-9a-z-A-Z]*$") def get_regex(preamble, codec, pad, char_len): diff --git a/django_spicy_id/tests/fields_test.py b/django_spicy_id/tests/fields_test.py index 9e90618..db89363 100644 --- a/django_spicy_id/tests/fields_test.py +++ b/django_spicy_id/tests/fields_test.py @@ -5,10 +5,28 @@ from django.test import TestCase from django_spicy_id import MalformedSpicyIdError, SpicyAutoField +from django_spicy_id.fields import LEGAL_PREFIX_RE from django_spicy_id.tests import models class TestFields(TestCase): + def test_prefix_re(self): + legal_prefixes = ( + "e", + "ex", + "ExampleThatsReallyLong", + ) + for p in legal_prefixes: + self.assertIsNotNone(LEGAL_PREFIX_RE.match(p)) + + illegal_prefixes = ( + "", + "🆒", + "9gag", + ) + for p in illegal_prefixes: + self.assertIsNone(LEGAL_PREFIX_RE.match(p)) + def test_field_configuration(self): with self.assertRaisesMessage(ImproperlyConfigured, "unknown encoding"): SpicyAutoField(prefix="yo", encoding="doop")