From 59624d75cb99219c726f10e73bda2bbcae1b3301 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Tue, 9 Feb 2016 13:00:45 -0400 Subject: [PATCH] Fix GitLab issue #250 "Empty optional lookup metadata trigger validation error". Thanks to LeVon Smoker for the find and for the proposed fix. Reference: https://groups.google.com/forum/#!topic/mayan-edms/VUGRl4xX-1c --- mayan/apps/metadata/models.py | 2 +- mayan/apps/metadata/tests/test_models.py | 30 +++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/mayan/apps/metadata/models.py b/mayan/apps/metadata/models.py index 8b4784b27a..619a19aa4a 100644 --- a/mayan/apps/metadata/models.py +++ b/mayan/apps/metadata/models.py @@ -126,7 +126,7 @@ class MetadataType(models.Model): if self.lookup: lookup_options = self.get_lookup_values() - if value not in lookup_options: + if value and value not in lookup_options: raise ValidationError( _('Value is not one of the provided options.') ) diff --git a/mayan/apps/metadata/tests/test_models.py b/mayan/apps/metadata/tests/test_models.py index 4826a7c64a..3d96286a64 100644 --- a/mayan/apps/metadata/tests/test_models.py +++ b/mayan/apps/metadata/tests/test_models.py @@ -63,8 +63,9 @@ class MetadataTestCase(TestCase): self.document.metadata_value_of.test, TEST_DEFAULT_VALUE ) - def test_lookup(self): + def test_lookup_with_incorrect_value(self): self.metadata_type.lookup = TEST_LOOKUP_TEMPLATE + self.metadata_type.save() document_metadata = DocumentMetadata( document=self.document, metadata_type=self.metadata_type, @@ -76,8 +77,15 @@ class MetadataTestCase(TestCase): document_metadata.full_clean() document_metadata.save() - # Should not return error - document_metadata.value = TEST_CORRECT_LOOKUP_VALUE + def test_lookup_with_correct_value(self): + self.metadata_type.lookup = TEST_LOOKUP_TEMPLATE + self.metadata_type.save() + + document_metadata = DocumentMetadata( + document=self.document, metadata_type=self.metadata_type, + value=TEST_CORRECT_LOOKUP_VALUE + ) + document_metadata.full_clean() document_metadata.save() @@ -85,6 +93,22 @@ class MetadataTestCase(TestCase): self.document.metadata_value_of.test, TEST_CORRECT_LOOKUP_VALUE ) + def test_empty_optional_lookup(self): + """ + Checks for GitLab issue #250 + Empty optional lookup metadata trigger validation error + """ + + self.metadata_type.lookup = TEST_LOOKUP_TEMPLATE + self.metadata_type.save() + + document_metadata = DocumentMetadata( + document=self.document, metadata_type=self.metadata_type + ) + + document_metadata.full_clean() + document_metadata.save() + def test_validation(self): self.metadata_type.validation = TEST_DATE_VALIDATOR