Skip to content

Commit fe35500

Browse files
committed
Merge pull request #18 from dexteradeus/feature/choices_fix
Allow choice fields with human readable key tuples
2 parents 2147a98 + b526275 commit fe35500

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

eve_mongoengine/schema.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ def process_field(cls, field, lowercase):
138138
if field.unique:
139139
fdict['unique'] = True
140140
if field.choices:
141-
fdict['allowed'] = field.choices
141+
allowed = []
142+
for choice in field.choices:
143+
if isinstance(choice, (list, tuple)):
144+
allowed.append(choice[0])
145+
else:
146+
allowed.append(choice)
147+
fdict['allowed'] = tuple(allowed)
142148
if getattr(field, 'max_length', None) is not None:
143149
fdict['maxlength'] = field.max_length
144150
if getattr(field, 'min_length', None) is not None:

tests/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ class LimitedDoc(Document):
7676
d = StringField(max_length=10)
7777
e = StringField(min_length=10)
7878
f = IntField(min_value=5, max_value=10)
79+
g = StringField(choices=[
80+
['val1', 'test value 1'],
81+
('val2', 'test value 2'),
82+
['val3', 'test value 3']])
7983

8084
class WrongDoc(Document):
8185
updated = IntField() # this is bad name

tests/test_post.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ def test_post_invalid_schema_type(self):
4949
def test_post_invalid_schema_limits(self):
5050
# break min_length
5151
response = self.client.post('/limiteddoc/',
52-
data='{"a": "hi", "b": "ho", "c": "x", "d": "<10 chars", "e": "<10 chars"}',
52+
data='{"a": "hi", "b": "ho", "c": "x", "d": "<10 chars", "e": "<10 chars", "g": "val1"}',
5353
content_type='application/json')
5454
self.assertEqual(response.status_code, POST_VALIDATION_ERROR_CODE)
5555
json_data = response.get_json()
5656
self.assertDictEqual(json_data[config.ISSUES], {'e': "min length is 10"})
5757
# break max_length
5858
response = self.client.post('/limiteddoc/',
59-
data='{"a": "hi", "b": "ho", "c": "x", "d": "string > 10 chars", "e": "some very long text"}',
59+
data='{"a": "hi", "b": "ho", "c": "x", "d": "string > 10 chars", "e": "some very long text", "g": "val2"}',
6060
content_type='application/json')
6161
self.assertEqual(response.status_code, POST_VALIDATION_ERROR_CODE)
6262
json_data = response.get_json()
@@ -71,6 +71,25 @@ def test_post_invalid_schema_required(self):
7171
json_data = response.get_json()
7272
self.assertDictEqual(json_data[config.ISSUES], {'a': "required field"})
7373

74+
def test_post_invalid_schema_choice(self):
75+
response = self.client.post('/limiteddoc/',
76+
data='{"a": "hi", "b": "ho", "c": "a"}',
77+
content_type='application/json')
78+
self.assertEqual(response.status_code, POST_VALIDATION_ERROR_CODE)
79+
json_data = response.get_json()
80+
self.assertDictEqual(json_data[config.ISSUES], {'c': 'unallowed value a'})
81+
response = self.client.post('/limiteddoc/',
82+
data='{"a": "hi", "b": "ho", "g": "val4"}',
83+
content_type='application/json')
84+
self.assertEqual(response.status_code, POST_VALIDATION_ERROR_CODE)
85+
json_data = response.get_json()
86+
self.assertDictEqual(json_data[config.ISSUES], {'g': 'unallowed value val4'})
87+
response = self.client.post('/limiteddoc/',
88+
data='{"a": "hi", "b": "ho", "g": "test value 1"}',
89+
content_type='application/json')
90+
self.assertEqual(response.status_code, POST_VALIDATION_ERROR_CODE)
91+
json_data = response.get_json()
92+
self.assertDictEqual(json_data[config.ISSUES], {'g': 'unallowed value test value 1'})
7493

7594
def test_post_invalid_schema_unique(self):
7695
response = self.client.post('/limiteddoc/',

0 commit comments

Comments
 (0)