Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' into mongoset

Conflicts:
	brubeck/queryset.py
  • Loading branch information...
commit 7a3d19db370025913412815bbc33c9927fdea8b2 2 parents 2615eb7 + 3ef8bb0
Ben Beecher authored
45 brubeck/queryset.py
... ... @@ -1,6 +1,6 @@
1 1 from request_handling import FourOhFourException
2 2
3   -
  3 +
4 4 class AbstractQueryset(object):
5 5 """The design of the `AbstractQueryset` attempts to map RESTful calls
6 6 directly to CRUD calls. It also attempts to be compatible with a single
@@ -26,7 +26,7 @@ class AbstractQueryset(object):
26 26 MSG_CREATED = 'Created'
27 27 MSG_NOTFOUND = 'Not Found'
28 28 MSG_FAILED = 'Failed'
29   -
  29 +
30 30 def __init__(self, db_conn=None, api_id='id'):
31 31 self.db_conn = db_conn
32 32 self.api_id = api_id
@@ -78,7 +78,7 @@ def destroy(self, item_ids):
78 78 ###
79 79
80 80 ### Create Functions
81   -
  81 +
82 82 def create_one(self, shield):
83 83 raise NotImplementedError
84 84
@@ -131,14 +131,14 @@ def __init__(self, **kw):
131 131 super(DictQueryset, self).__init__(db_conn=dict(), **kw)
132 132
133 133 ### Create Functions
134   -
  134 +
135 135 def create_one(self, shield):
136 136 if shield.id in self.db_conn:
137 137 status = self.MSG_UPDATED
138 138 else:
139 139 status = self.MSG_CREATED
140 140
141   - shield_key = str(getattr(shield, self.api_id))
  141 + shield_key = str(getattr(shield, self.api_id))
142 142 self.db_conn[shield_key] = shield.to_python()
143 143 return (status, shield)
144 144
@@ -153,16 +153,18 @@ def read_all(self):
153 153
154 154 def read_one(self, iid):
155 155 iid = str(iid) # TODO Should be cleaner
156   - try:
157   - return (self.MSG_UPDATED, self.db_conn[iid])
158   - except KeyError:
159   - raise FourOhFourException
  156 + if iid in self.db_conn:
  157 + return (self.MSG_OK, self.db_conn[iid])
  158 + else:
  159 + return (self.MSG_FAILED, iid)
160 160
161 161 def read_many(self, ids):
162 162 return [self.read_one(iid) for iid in ids]
163 163
164   - ### Update Functions
  164 + def read_many(self, ids):
  165 + return [self.read_one(iid) for iid in ids]
165 166
  167 + ### Update Functions
166 168 def update_one(self, shield):
167 169 shield_key = str(getattr(shield, self.api_id))
168 170 self.db_conn[shield_key] = shield.to_python()
@@ -175,21 +177,20 @@ def update_many(self, shields):
175 177 ### Destroy Functions
176 178
177 179 def destroy_one(self, item_id):
178   -
  180 +
179 181 try:
180   - shield = self.db_conn[item_id]
  182 + datum = self.db_conn[item_id]
181 183 del self.db_conn[item_id]
182 184 except KeyError:
183 185 raise FourOhFourException
184   -
185   - return (self.MSG_UPDATED, {'_id':item_id})
  186 + return (self.MSG_UPDATED, datum)
186 187
187 188 def destroy_many(self, ids):
188 189 statuses = [self.destroy_one(iid) for iid in ids]
189 190 return statuses
190 191
191 192 from pymongo.errors import OperationFailure
192   -
  193 +
193 194 class MongoQueryset(AbstractQueryset):
194 195 """ A Queryset that deals with talking to mongodb"""
195 196
@@ -197,7 +198,7 @@ def __init__(self, db_conn=None, api_id='_id'):
197 198 """ A mongoqueryset takes a collection as it's fundamental unit."""
198 199 self.collection = db_conn
199 200 self.api_id=api_id
200   -
  201 +
201 202 def _get_id(self, shield):
202 203 getattr(shield, self.api_id)
203 204
@@ -218,7 +219,7 @@ def read_many(self, ids):
218 219 if len(matches) != len(ids):
219 220 raise FourOhFourException
220 221 return [(self.MSG_OK, datum) for datum in matches]
221   -
  222 +
222 223 def create_one(self, shield):
223 224 try:
224 225 self.collection.insert(shield.to_python(),
@@ -227,7 +228,7 @@ def create_one(self, shield):
227 228 return (self.MSG_CREATED, shield)
228 229 except OperationFailure as e:
229 230 return (self.MSG_FAILED, e) ## should we return the error here?
230   -
  231 +
231 232 def create_many(self, shields):
232 233 import pdb
233 234 pdb.set_trace()
@@ -243,7 +244,7 @@ def update_one(self, shield):
243 244 return (self.MSG_UPDATED, shield)
244 245 except OperationFailure as e:
245 246 return (self.MSG_FAILED, e)
246   -
  247 +
247 248 def update_many(self, shields):
248 249 try:
249 250 ret = []
@@ -254,15 +255,15 @@ def update_many(self, shields):
254 255 return ret
255 256 except OperationFailure as e:
256 257 return (self.MSG_FAILED, e)
257   -
  258 +
258 259 def destroy_one(self, item_id):
259 260 self.read_one(item_id) #to check for 404
260   - try:
  261 + try:
261 262 self.collection.remove({self.api_id:item_id}, safe=True)
262 263 return (self.MSG_UPDATED, {'_id':item_id})
263 264 except OperationFailure as e:
264 265 return (self.MSG_FAILED, e)
265   -
  266 +
266 267 def destroy_many(self, item_ids):
267 268 self.read_many(item_ids) #to check for 404
268 269 try:
2  tests/fixtures/request_handler_fixtures.py
@@ -21,7 +21,7 @@
21 21 ##
22 22 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
23 23 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
24   -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}'
  24 +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}'
25 25
26 26 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
27 27
189 tests/test_queryset.py
... ... @@ -0,0 +1,189 @@
  1 +#!/usr/bin/env python
  2 +
  3 +import unittest
  4 +import sys
  5 +import brubeck
  6 +from handlers.method_handlers import simple_handler_method
  7 +from brubeck.request_handling import Brubeck, WebMessageHandler, JSONMessageHandler
  8 +from brubeck.mongrel2 import to_bytes, Request
  9 +from brubeck.request_handling import(
  10 + cookie_encode, cookie_decode,
  11 + cookie_is_encoded, http_response
  12 +)
  13 +from handlers.object_handlers import(
  14 + SimpleWebHandlerObject, CookieWebHandlerObject,
  15 + SimpleJSONHandlerObject, CookieAddWebHandlerObject,
  16 + PrepareHookWebHandlerObject, InitializeHookWebHandlerObject
  17 +)
  18 +from fixtures import request_handler_fixtures as FIXTURES
  19 +
  20 +from brubeck.autoapi import AutoAPIBase
  21 +from brubeck.queryset import DictQueryset, AbstractQueryset
  22 +
  23 +from dictshield.document import Document
  24 +from dictshield.fields import StringField
  25 +from brubeck.request_handling import FourOhFourException
  26 +
  27 +##TestDocument
  28 +class TestDoc(Document):
  29 + id = StringField(required=True, id_field=True)
  30 + data = StringField()
  31 +
  32 +###
  33 +### Tests for ensuring that the autoapi returns good data
  34 +###
  35 +class TestQuerySetPrimitives(unittest.TestCase):
  36 + """
  37 + a test class for brubeck's queryset objects' core operations.
  38 + """
  39 +
  40 + def setUp(self):
  41 + self.queryset = AbstractQueryset()
  42 +
  43 +
  44 + def create(self):
  45 + pass
  46 +
  47 + def read(self):
  48 + pass
  49 +
  50 + def update(self):
  51 + pass
  52 +
  53 + def destroy(self):
  54 + pass
  55 +
  56 +
  57 +class TestDictQueryset(unittest.TestCase):
  58 + """
  59 + a test class for brubeck's dictqueryset's operations.
  60 + """
  61 +
  62 +
  63 + def setUp(self):
  64 + self.queryset = DictQueryset()
  65 +
  66 + def seed_reads(self):
  67 + shields = [TestDoc(id="foo"), TestDoc(id="bar"), TestDoc(id="baz")]
  68 + self.queryset.create_many(shields)
  69 + return shields
  70 +
  71 +
  72 + def test__create_one(self):
  73 + shield = TestDoc(id="foo")
  74 + status, return_shield = self.queryset.create_one(shield)
  75 + self.assertEqual(self.queryset.MSG_CREATED, status)
  76 + self.assertEqual(shield, return_shield)
  77 +
  78 + status, return_shield = self.queryset.create_one(shield)
  79 + self.assertEqual(self.queryset.MSG_UPDATED, status)
  80 +
  81 +
  82 + def test__create_many(self):
  83 + shield0 = TestDoc(id="foo")
  84 + shield1 = TestDoc(id="bar")
  85 + shield2 = TestDoc(id="baz")
  86 + statuses = self.queryset.create_many([shield0, shield1, shield2])
  87 + for status, datum in statuses:
  88 + self.assertEqual(self.queryset.MSG_CREATED, status)
  89 +
  90 + shield3 = TestDoc(id="bloop")
  91 + statuses = self.queryset.create_many([shield0, shield3, shield2])
  92 + status, datum = statuses[1]
  93 + self.assertEqual(self.queryset.MSG_CREATED, status)
  94 + status, datum = statuses[0]
  95 + self.assertEqual(self.queryset.MSG_UPDATED, status)
  96 +
  97 + def test__read_all(self):
  98 + shields = self.seed_reads()
  99 + statuses = self.queryset.read_all()
  100 +
  101 + for status, datum in statuses:
  102 + self.assertEqual(self.queryset.MSG_OK, status)
  103 +
  104 + actual = sorted([datum for trash, datum in statuses])
  105 + expected = sorted([shield.to_python() for shield in shields])
  106 + self.assertEqual(expected, actual)
  107 +
  108 + def test__read_one(self):
  109 + shields = self.seed_reads()
  110 + for shield in shields:
  111 + status, datum = self.queryset.read_one(shield.id)
  112 + self.assertEqual(self.queryset.MSG_OK, status)
  113 + self.assertEqual(datum, shield.to_python())
  114 + bad_key = 'DOESNTEXISIT'
  115 + status, datum = self.queryset.read(bad_key)
  116 + self.assertEqual(bad_key, datum)
  117 + self.assertEqual(self.queryset.MSG_FAILED, status)
  118 +
  119 + def test__read_many(self):
  120 + shields = self.seed_reads()
  121 + expected = [shield.to_python() for shield in shields]
  122 + responses = self.queryset.read_many([s.id for s in shields])
  123 + for status, datum in responses:
  124 + self.assertEqual(self.queryset.MSG_OK, status)
  125 + self.assertTrue(datum in expected)
  126 +
  127 + bad_ids = [s.id for s in shields]
  128 + bad_ids.append('DOESNTEXISIT')
  129 + status, iid = self.queryset.read_many(bad_ids)[-1]
  130 + self.assertEqual(self.queryset.MSG_FAILED, status)
  131 +
  132 +
  133 + def test_update_one(self):
  134 + shields = self.seed_reads()
  135 + test_shield = shields[0]
  136 + test_shield.data = "foob"
  137 + status, datum = self.queryset.update_one(test_shield)
  138 +
  139 + self.assertEqual(self.queryset.MSG_UPDATED, status)
  140 + self.assertEqual('foob', datum['data'])
  141 +
  142 + status, datum = self.queryset.read_one(test_shield.id)
  143 + self.assertEqual('foob', datum['data'])
  144 +
  145 +
  146 + def test_update_many(self):
  147 + shields = self.seed_reads()
  148 + for shield in shields:
  149 + shield.data = "foob"
  150 + responses = self.queryset.update_many(shields)
  151 + for status, datum in responses:
  152 + self.assertEqual(self.queryset.MSG_UPDATED, status)
  153 + self.assertEqual('foob', datum['data'])
  154 + for status, datum in self.queryset.read_all():
  155 + self.assertEqual('foob', datum['data'])
  156 +
  157 +
  158 + def test_destroy_one(self):
  159 + shields = self.seed_reads()
  160 + test_shield = shields[0]
  161 + status, datum = self.queryset.destroy_one(test_shield.id)
  162 + self.assertEqual(self.queryset.MSG_UPDATED, status)
  163 +
  164 + status, datum = self.queryset.read_one(test_shield.id)
  165 + self.assertEqual(test_shield.id, datum)
  166 + self.assertEqual(self.queryset.MSG_FAILED, status)
  167 +
  168 +
  169 + def test_destroy_many(self):
  170 + shields = self.seed_reads()
  171 + shield_to_keep = shields.pop()
  172 + responses = self.queryset.destroy_many([shield.id for shield in shields])
  173 + for status, datum in responses:
  174 + self.assertEqual(self.queryset.MSG_UPDATED, status)
  175 +
  176 + responses = self.queryset.read_many([shield.id for shield in shields])
  177 + for status, datum in responses:
  178 + self.assertEqual(self.queryset.MSG_FAILED, status)
  179 +
  180 + status, datum = self.queryset.read_one(shield_to_keep.id)
  181 + self.assertEqual(self.queryset.MSG_OK, status)
  182 + self.assertEqual(shield_to_keep.to_python(), datum)
  183 +
  184 +
  185 +##
  186 +## This will run our tests
  187 +##
  188 +if __name__ == '__main__':
  189 + unittest.main()

0 comments on commit 7a3d19d

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