Skip to content
Browse files

Merge branch 'master' into mongoset

Conflicts:
	brubeck/queryset.py
  • Loading branch information...
2 parents 2615eb7 + 3ef8bb0 commit 7a3d19db370025913412815bbc33c9927fdea8b2 @gone committed Mar 30, 2012
Showing with 213 additions and 23 deletions.
  1. +23 −22 brubeck/queryset.py
  2. +1 −1 tests/fixtures/request_handler_fixtures.py
  3. +189 −0 tests/test_queryset.py
View
45 brubeck/queryset.py
@@ -1,6 +1,6 @@
from request_handling import FourOhFourException
-
+
class AbstractQueryset(object):
"""The design of the `AbstractQueryset` attempts to map RESTful calls
directly to CRUD calls. It also attempts to be compatible with a single
@@ -26,7 +26,7 @@ class AbstractQueryset(object):
MSG_CREATED = 'Created'
MSG_NOTFOUND = 'Not Found'
MSG_FAILED = 'Failed'
-
+
def __init__(self, db_conn=None, api_id='id'):
self.db_conn = db_conn
self.api_id = api_id
@@ -78,7 +78,7 @@ def destroy(self, item_ids):
###
### Create Functions
-
+
def create_one(self, shield):
raise NotImplementedError
@@ -131,14 +131,14 @@ def __init__(self, **kw):
super(DictQueryset, self).__init__(db_conn=dict(), **kw)
### Create Functions
-
+
def create_one(self, shield):
if shield.id in self.db_conn:
status = self.MSG_UPDATED
else:
status = self.MSG_CREATED
- shield_key = str(getattr(shield, self.api_id))
+ shield_key = str(getattr(shield, self.api_id))
self.db_conn[shield_key] = shield.to_python()
return (status, shield)
@@ -153,16 +153,18 @@ def read_all(self):
def read_one(self, iid):
iid = str(iid) # TODO Should be cleaner
- try:
- return (self.MSG_UPDATED, self.db_conn[iid])
- except KeyError:
- raise FourOhFourException
+ if iid in self.db_conn:
+ return (self.MSG_OK, self.db_conn[iid])
+ else:
+ return (self.MSG_FAILED, iid)
def read_many(self, ids):
return [self.read_one(iid) for iid in ids]
- ### Update Functions
+ def read_many(self, ids):
+ return [self.read_one(iid) for iid in ids]
+ ### Update Functions
def update_one(self, shield):
shield_key = str(getattr(shield, self.api_id))
self.db_conn[shield_key] = shield.to_python()
@@ -175,29 +177,28 @@ def update_many(self, shields):
### Destroy Functions
def destroy_one(self, item_id):
-
+
try:
- shield = self.db_conn[item_id]
+ datum = self.db_conn[item_id]
del self.db_conn[item_id]
except KeyError:
raise FourOhFourException
-
- return (self.MSG_UPDATED, {'_id':item_id})
+ return (self.MSG_UPDATED, datum)
def destroy_many(self, ids):
statuses = [self.destroy_one(iid) for iid in ids]
return statuses
from pymongo.errors import OperationFailure
-
+
class MongoQueryset(AbstractQueryset):
""" A Queryset that deals with talking to mongodb"""
def __init__(self, db_conn=None, api_id='_id'):
""" A mongoqueryset takes a collection as it's fundamental unit."""
self.collection = db_conn
self.api_id=api_id
-
+
def _get_id(self, shield):
getattr(shield, self.api_id)
@@ -218,7 +219,7 @@ def read_many(self, ids):
if len(matches) != len(ids):
raise FourOhFourException
return [(self.MSG_OK, datum) for datum in matches]
-
+
def create_one(self, shield):
try:
self.collection.insert(shield.to_python(),
@@ -227,7 +228,7 @@ def create_one(self, shield):
return (self.MSG_CREATED, shield)
except OperationFailure as e:
return (self.MSG_FAILED, e) ## should we return the error here?
-
+
def create_many(self, shields):
import pdb
pdb.set_trace()
@@ -243,7 +244,7 @@ def update_one(self, shield):
return (self.MSG_UPDATED, shield)
except OperationFailure as e:
return (self.MSG_FAILED, e)
-
+
def update_many(self, shields):
try:
ret = []
@@ -254,15 +255,15 @@ def update_many(self, shields):
return ret
except OperationFailure as e:
return (self.MSG_FAILED, e)
-
+
def destroy_one(self, item_id):
self.read_one(item_id) #to check for 404
- try:
+ try:
self.collection.remove({self.api_id:item_id}, safe=True)
return (self.MSG_UPDATED, {'_id':item_id})
except OperationFailure as e:
return (self.MSG_FAILED, e)
-
+
def destroy_many(self, item_ids):
self.read_many(item_ids) #to check for 404
try:
View
2 tests/fixtures/request_handler_fixtures.py
@@ -21,7 +21,7 @@
##
HTTP_RESPONSE_OBJECT_ROOT = 'HTTP/1.1 200 OK\r\nContent-Length: ' + str(len(TEST_BODY_OBJECT_HANDLER)) + '\r\n\r\n' + TEST_BODY_OBJECT_HANDLER
HTTP_RESPONSE_METHOD_ROOT = 'HTTP/1.1 200 OK\r\nContent-Length: ' + str(len(TEST_BODY_METHOD_HANDLER)) + '\r\n\r\n' + TEST_BODY_METHOD_HANDLER
-HTTP_RESPONSE_JSON_OBJECT_ROOT = 'HTTP/1.1 200 OK\r\nContent-Length: 90\r\n\r\n{"status_code":200,"status_msg":"OK","message":"Take five dude","timestamp":1320456118809}'
+HTTP_RESPONSE_JSON_OBJECT_ROOT = 'HTTP/1.1 200 OK\r\nContent-Length: 90\r\nContent-Type: application/json\r\n\r\n{"status_code":200,"status_msg":"OK","message":"Take five dude","timestamp":1320456118809}'
HTTP_RESPONSE_OBJECT_ROOT_WITH_COOKIE = 'HTTP/1.1 200 OK\r\nSet-Cookie: key=value\r\nContent-Length: ' + str(len(TEST_BODY_OBJECT_HANDLER)) + '\r\n\r\n' + TEST_BODY_OBJECT_HANDLER
View
189 tests/test_queryset.py
@@ -0,0 +1,189 @@
+#!/usr/bin/env python
+
+import unittest
+import sys
+import brubeck
+from handlers.method_handlers import simple_handler_method
+from brubeck.request_handling import Brubeck, WebMessageHandler, JSONMessageHandler
+from brubeck.mongrel2 import to_bytes, Request
+from brubeck.request_handling import(
+ cookie_encode, cookie_decode,
+ cookie_is_encoded, http_response
+)
+from handlers.object_handlers import(
+ SimpleWebHandlerObject, CookieWebHandlerObject,
+ SimpleJSONHandlerObject, CookieAddWebHandlerObject,
+ PrepareHookWebHandlerObject, InitializeHookWebHandlerObject
+)
+from fixtures import request_handler_fixtures as FIXTURES
+
+from brubeck.autoapi import AutoAPIBase
+from brubeck.queryset import DictQueryset, AbstractQueryset
+
+from dictshield.document import Document
+from dictshield.fields import StringField
+from brubeck.request_handling import FourOhFourException
+
+##TestDocument
+class TestDoc(Document):
+ id = StringField(required=True, id_field=True)
+ data = StringField()
+
+###
+### Tests for ensuring that the autoapi returns good data
+###
+class TestQuerySetPrimitives(unittest.TestCase):
+ """
+ a test class for brubeck's queryset objects' core operations.
+ """
+
+ def setUp(self):
+ self.queryset = AbstractQueryset()
+
+
+ def create(self):
+ pass
+
+ def read(self):
+ pass
+
+ def update(self):
+ pass
+
+ def destroy(self):
+ pass
+
+
+class TestDictQueryset(unittest.TestCase):
+ """
+ a test class for brubeck's dictqueryset's operations.
+ """
+
+
+ def setUp(self):
+ self.queryset = DictQueryset()
+
+ def seed_reads(self):
+ shields = [TestDoc(id="foo"), TestDoc(id="bar"), TestDoc(id="baz")]
+ self.queryset.create_many(shields)
+ return shields
+
+
+ def test__create_one(self):
+ shield = TestDoc(id="foo")
+ status, return_shield = self.queryset.create_one(shield)
+ self.assertEqual(self.queryset.MSG_CREATED, status)
+ self.assertEqual(shield, return_shield)
+
+ status, return_shield = self.queryset.create_one(shield)
+ self.assertEqual(self.queryset.MSG_UPDATED, status)
+
+
+ def test__create_many(self):
+ shield0 = TestDoc(id="foo")
+ shield1 = TestDoc(id="bar")
+ shield2 = TestDoc(id="baz")
+ statuses = self.queryset.create_many([shield0, shield1, shield2])
+ for status, datum in statuses:
+ self.assertEqual(self.queryset.MSG_CREATED, status)
+
+ shield3 = TestDoc(id="bloop")
+ statuses = self.queryset.create_many([shield0, shield3, shield2])
+ status, datum = statuses[1]
+ self.assertEqual(self.queryset.MSG_CREATED, status)
+ status, datum = statuses[0]
+ self.assertEqual(self.queryset.MSG_UPDATED, status)
+
+ def test__read_all(self):
+ shields = self.seed_reads()
+ statuses = self.queryset.read_all()
+
+ for status, datum in statuses:
+ self.assertEqual(self.queryset.MSG_OK, status)
+
+ actual = sorted([datum for trash, datum in statuses])
+ expected = sorted([shield.to_python() for shield in shields])
+ self.assertEqual(expected, actual)
+
+ def test__read_one(self):
+ shields = self.seed_reads()
+ for shield in shields:
+ status, datum = self.queryset.read_one(shield.id)
+ self.assertEqual(self.queryset.MSG_OK, status)
+ self.assertEqual(datum, shield.to_python())
+ bad_key = 'DOESNTEXISIT'
+ status, datum = self.queryset.read(bad_key)
+ self.assertEqual(bad_key, datum)
+ self.assertEqual(self.queryset.MSG_FAILED, status)
+
+ def test__read_many(self):
+ shields = self.seed_reads()
+ expected = [shield.to_python() for shield in shields]
+ responses = self.queryset.read_many([s.id for s in shields])
+ for status, datum in responses:
+ self.assertEqual(self.queryset.MSG_OK, status)
+ self.assertTrue(datum in expected)
+
+ bad_ids = [s.id for s in shields]
+ bad_ids.append('DOESNTEXISIT')
+ status, iid = self.queryset.read_many(bad_ids)[-1]
+ self.assertEqual(self.queryset.MSG_FAILED, status)
+
+
+ def test_update_one(self):
+ shields = self.seed_reads()
+ test_shield = shields[0]
+ test_shield.data = "foob"
+ status, datum = self.queryset.update_one(test_shield)
+
+ self.assertEqual(self.queryset.MSG_UPDATED, status)
+ self.assertEqual('foob', datum['data'])
+
+ status, datum = self.queryset.read_one(test_shield.id)
+ self.assertEqual('foob', datum['data'])
+
+
+ def test_update_many(self):
+ shields = self.seed_reads()
+ for shield in shields:
+ shield.data = "foob"
+ responses = self.queryset.update_many(shields)
+ for status, datum in responses:
+ self.assertEqual(self.queryset.MSG_UPDATED, status)
+ self.assertEqual('foob', datum['data'])
+ for status, datum in self.queryset.read_all():
+ self.assertEqual('foob', datum['data'])
+
+
+ def test_destroy_one(self):
+ shields = self.seed_reads()
+ test_shield = shields[0]
+ status, datum = self.queryset.destroy_one(test_shield.id)
+ self.assertEqual(self.queryset.MSG_UPDATED, status)
+
+ status, datum = self.queryset.read_one(test_shield.id)
+ self.assertEqual(test_shield.id, datum)
+ self.assertEqual(self.queryset.MSG_FAILED, status)
+
+
+ def test_destroy_many(self):
+ shields = self.seed_reads()
+ shield_to_keep = shields.pop()
+ responses = self.queryset.destroy_many([shield.id for shield in shields])
+ for status, datum in responses:
+ self.assertEqual(self.queryset.MSG_UPDATED, status)
+
+ responses = self.queryset.read_many([shield.id for shield in shields])
+ for status, datum in responses:
+ self.assertEqual(self.queryset.MSG_FAILED, status)
+
+ status, datum = self.queryset.read_one(shield_to_keep.id)
+ self.assertEqual(self.queryset.MSG_OK, status)
+ self.assertEqual(shield_to_keep.to_python(), datum)
+
+
+##
+## This will run our tests
+##
+if __name__ == '__main__':
+ unittest.main()

0 comments on commit 7a3d19d

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