Merge pull request #1025 from MRigal/feature/259-improve-error-detection-for-invalid-query
Improve error message for invalid query
This commit is contained in:
		| @@ -983,8 +983,13 @@ class BaseDocument(object): | |||||||
|                 if hasattr(getattr(field, 'field', None), 'lookup_member'): |                 if hasattr(getattr(field, 'field', None), 'lookup_member'): | ||||||
|                     new_field = field.field.lookup_member(field_name) |                     new_field = field.field.lookup_member(field_name) | ||||||
|                 else: |                 else: | ||||||
|                    # Look up subfield on the previous field |                     # Look up subfield on the previous field or raise | ||||||
|                     new_field = field.lookup_member(field_name) |                     try: | ||||||
|  |                         new_field = field.lookup_member(field_name) | ||||||
|  |                     except AttributeError: | ||||||
|  |                         raise LookUpError('Cannot resolve subfield or operator {} ' | ||||||
|  |                                           'on the field {}'.format( | ||||||
|  |                                               field_name, field.name)) | ||||||
|                 if not new_field and isinstance(field, ComplexBaseField): |                 if not new_field and isinstance(field, ComplexBaseField): | ||||||
|                     if hasattr(field.field, 'document_type') and cls._dynamic \ |                     if hasattr(field.field, 'document_type') and cls._dynamic \ | ||||||
|                             and field.field.document_type._dynamic: |                             and field.field.document_type._dynamic: | ||||||
|   | |||||||
| @@ -44,7 +44,7 @@ def query(_doc_cls=None, _field_operation=False, **query): | |||||||
|         if len(parts) > 1 and parts[-1] in MATCH_OPERATORS: |         if len(parts) > 1 and parts[-1] in MATCH_OPERATORS: | ||||||
|             op = parts.pop() |             op = parts.pop() | ||||||
|  |  | ||||||
|         #if user escape field name by __ |         # if user escape field name by __ | ||||||
|         if len(parts) > 1 and parts[-1] == "": |         if len(parts) > 1 and parts[-1] == "": | ||||||
|             parts.pop() |             parts.pop() | ||||||
|  |  | ||||||
|   | |||||||
| @@ -224,6 +224,15 @@ class TransformTest(unittest.TestCase): | |||||||
|         self.assertEqual(1, Doc.objects(item__type__="axe").count()) |         self.assertEqual(1, Doc.objects(item__type__="axe").count()) | ||||||
|         self.assertEqual(1, Doc.objects(item__name__="Heroic axe").count()) |         self.assertEqual(1, Doc.objects(item__name__="Heroic axe").count()) | ||||||
|  |  | ||||||
|  |     def test_understandable_error_raised(self): | ||||||
|  |         class Event(Document): | ||||||
|  |             title = StringField() | ||||||
|  |             location = GeoPointField() | ||||||
|  |  | ||||||
|  |         box = [(35.0, -125.0), (40.0, -100.0)] | ||||||
|  |         # I *meant* to execute location__within_box=box | ||||||
|  |         events = Event.objects(location__within=box) | ||||||
|  |         self.assertRaises(InvalidQueryError, lambda: events.count()) | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     unittest.main() |     unittest.main() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user