Skip to content
Browse files

Added support for non-ObjectId _id fields, both in insert() and find(…

…). This is something that both MongoDB and SleepyMongoose support, but Mongate didn't.

Also added a unit test that verifies both of these fixes.
  • Loading branch information...
1 parent b0b1bd3 commit fddd93e8166b99af5ead98dc105df4200a057c73 @apetresc apetresc committed Jun 25, 2011
Showing with 24 additions and 3 deletions.
  1. +10 −2 mongate/collection.py
  2. +14 −1 tests/test_collection.py
View
12 mongate/collection.py
@@ -115,7 +115,11 @@ def find(self, criteria={}, fields=False):
def _process_response_object(self, response_object):
for result in response_object['results']:
- result['_id'] = result['_id']['$oid']
+ try:
+ result['_id'] = result['_id']['$oid']
+ except:
+ # This document is not using an ObjectId _id
+ pass
return response_object['results']
def _create_find_get_params(self, criteria, fields=False):
@@ -149,7 +153,11 @@ def insert(self, document):
'POST',
raise_error=self._raise_collection_error
)
- return response_object['oids'][0]['$oid']
+ response = response_object['oids'][0]
+ try:
+ return response['$oid']
+ except TypeError:
+ return response
def _create_url(self, action):
if self.connection.is_https():
View
15 tests/test_collection.py
@@ -61,7 +61,7 @@ def test_collection_find_by_key(self):
})
self.assertEqual('tasty', retrieved_collection[0]['apple'])
-
+
def test_collection_find_with_invalid_characters(self):
oid = self.collection.insert({
'name': 'Benjamin & Company',
@@ -73,6 +73,19 @@ def test_collection_find_with_invalid_characters(self):
})
self.assertEqual('Software Developer?', retrieved_collection[0]['profession'])
+
+ def test_collection_find_with_nonoid_id(self):
+ self.collection.insert({
+ '_id' : 100,
+ 'name' : 'Benjamin',
+ 'profession' : 'Software Developer'
+ })
+
+ retrieved_collection = self.collection.find({
+ 'name': 'Benjamin'
+ })
+
+ self.assertEqual(100, retrieved_collection[0]['_id'])
def test_collection_find_key_that_doesnt_exist(self):
self._insert_data()

0 comments on commit fddd93e

Please sign in to comment.
Something went wrong with that request. Please try again.