aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lesana/types.py29
-rw-r--r--tests/data/wrong/items/139770330d344a2f9d73945fab3bf47b.yaml16
-rw-r--r--tests/data/wrong/items/7496e7b7763b44d994ed07c134e66bdc.yaml14
-rw-r--r--tests/data/wrong/settings.yaml6
-rw-r--r--tests/test_collection.py4
5 files changed, 62 insertions, 7 deletions
diff --git a/lesana/types.py b/lesana/types.py
index 101e957..77f790d 100644
--- a/lesana/types.py
+++ b/lesana/types.py
@@ -39,6 +39,25 @@ class LesanaType:
"""
return value
+ def allowed_value(self, value):
+ """
+ Check whether a value is allowed in this field.
+
+ Return the value itself or raise LesanaValueError if the value
+ isn't valid.
+ """
+ if not value:
+ return value
+ valid_values = self.field.get('values')
+ if not valid_values:
+ return value
+ if value in valid_values:
+ return value
+ raise LesanaValueError("Value {} is not allowed in field {}".format(
+ value,
+ self.field.get('name')
+ ))
+
def _to_index_text(self, value):
"""
Prepare a value for indexing.
@@ -91,7 +110,7 @@ class LesanaString(LesanaType):
def load(self, data):
if not data:
return data
- return str(data)
+ return self.allowed_value(str(data))
def empty(self):
return ""
@@ -114,7 +133,7 @@ class LesanaInt(LesanaType):
if not data:
return data
try:
- return int(data)
+ return self.allowed_value(int(data))
except ValueError:
raise LesanaValueError(
"Invalid value for integer field: {}".format(data)
@@ -169,7 +188,7 @@ class LesanaFloat(LesanaType):
if not data:
return data
try:
- return float(data)
+ return self.allowed_value(float(data))
except ValueError:
raise LesanaValueError(
"Invalid value for float field: {}".format(data)
@@ -192,7 +211,7 @@ class LesanaDecimal(LesanaType):
if not data:
return data
try:
- return decimal.Decimal(data)
+ return self.allowed_value(decimal.Decimal(data))
except decimal.InvalidOperation:
raise LesanaValueError(
"Invalid value for decimal field: {}".format(data)
@@ -398,7 +417,7 @@ class LesanaList(LesanaType):
# empty for this type means an empty list
return []
try:
- return [self.sub_type.load(x) for x in data]
+ return [self.allowed_value(self.sub_type.load(x)) for x in data]
except TypeError:
raise LesanaValueError(
"Invalid value for list field: {}".format(data)
diff --git a/tests/data/wrong/items/139770330d344a2f9d73945fab3bf47b.yaml b/tests/data/wrong/items/139770330d344a2f9d73945fab3bf47b.yaml
new file mode 100644
index 0000000..84d2648
--- /dev/null
+++ b/tests/data/wrong/items/139770330d344a2f9d73945fab3bf47b.yaml
@@ -0,0 +1,16 @@
+name: 'invalid owner'
+description: |
+ This entry has an owner that is not on the list of allowed owners.
+position: ''
+# # number (integer): Enter an integer here
+number: 0
+# # float (float): Enter a floating point number here
+float: 0.0
+# # price (decimal): prices are never float!
+price: '0'
+things: []
+# # cloud (cloud): There is no cloud type
+cloud:
+category: ''
+owners:
+ - them
diff --git a/tests/data/wrong/items/7496e7b7763b44d994ed07c134e66bdc.yaml b/tests/data/wrong/items/7496e7b7763b44d994ed07c134e66bdc.yaml
new file mode 100644
index 0000000..e0ca190
--- /dev/null
+++ b/tests/data/wrong/items/7496e7b7763b44d994ed07c134e66bdc.yaml
@@ -0,0 +1,14 @@
+name: 'Invalid category value'
+description: |
+ An entry where the category isn't in the list of valid values
+position: ''
+# # number (integer): Enter an integer here
+number: 0
+# # float (float): Enter a floating point number here
+float: 0.0
+# # price (decimal): prices are never float!
+price: '0'
+things: []
+# # cloud (cloud): There is no cloud type
+cloud:
+category: 'zeroeth'
diff --git a/tests/data/wrong/settings.yaml b/tests/data/wrong/settings.yaml
index 6b3a3bb..73ce6d9 100644
--- a/tests/data/wrong/settings.yaml
+++ b/tests/data/wrong/settings.yaml
@@ -32,3 +32,9 @@ fields:
- first
- second
- third
+ - name: owners
+ type: list
+ list: string
+ values:
+ - me
+ - myself
diff --git a/tests/test_collection.py b/tests/test_collection.py
index 00389cd..659671d 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -574,7 +574,7 @@ class testCollectionWithErrors(unittest.TestCase):
self.assertIsNotNone(self.collection.settings)
self.assertIsNotNone(self.collection.stemmer)
# Fields with no "index" entry are not indexed
- self.assertEqual(len(self.collection.settings['fields']), 9)
+ self.assertEqual(len(self.collection.settings['fields']), 10)
self.assertEqual(len(self.collection.indexed_fields), 3)
def test_init(self):
@@ -583,7 +583,7 @@ class testCollectionWithErrors(unittest.TestCase):
self.collection.settings['name'],
"Lesana collection with certain errors",
)
- self.assertEqual(len(self.collection.settings['fields']), 9)
+ self.assertEqual(len(self.collection.settings['fields']), 10)
self.assertIsNotNone(self.collection.stemmer)
self.assertEqual(len(self.collection.indexed_fields), 3)