def test_stack_can_migrate_a_legacy_object_automatically(self): """Test the stack can migrate a legacy object automatically for the user""" client = BaseAsyncMotorDocument("test_fixture", settings) revisions = BaseAsyncMotorDocument("test_fixture_revisions", settings) fixture = self.test_fixture master_id = yield client.insert(fixture) stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings, master_id=master_id) fixture["baz"] = "bop" yield stack.push(self.test_fixture, self.three_min_past_now, meta={ "author": "UnitTest", "comment": "Just a test, BRO." }) fixture["new_persistent"] = True yield stack.push(fixture, self.two_min_past_now) del fixture["new_persistent"] fixture["baz"] = "bit" id = yield stack.push(fixture, self.two_min_past_now) #response = yield stack.preview(id) list = yield revisions.find({"master_id": master_id}) self.assertEqual(len(list), 4)
def test_stack_can_migrate_a_legacy_object_automatically(self): """Test the stack can migrate a legacy object automatically for the user""" client = BaseAsyncMotorDocument("test_fixture", settings) revisions = BaseAsyncMotorDocument("test_fixture_revisions", settings) fixture = self.test_fixture master_id = yield client.insert(fixture) stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings, master_id=master_id) fixture["baz"] = "bop" yield stack.push(self.test_fixture, self.three_min_past_now, meta={"author": "UnitTest", "comment": "Just a test, BRO."}) fixture["new_persistent"] = True yield stack.push(fixture, self.two_min_past_now) del fixture["new_persistent"] fixture["baz"] = "bit" id = yield stack.push(fixture, self.two_min_past_now) #response = yield stack.preview(id) list = yield revisions.find({"master_id": master_id}) self.assertEqual(len(list), 4)
class TestBaseAsyncMotorDocument(BaseAsyncTest): """ Test the Mongo Client funcitons here""" def setUp(self): """Setup the test, runs before each test""" self.mini_doc = {"my doc": "little bitty doc"} self.test_fixture = { "attr1": "attr1_val", "date1": time.mktime(datetime.datetime.now().timetuple()), "bool_val": True, "list_val": ["My Item", "Item 2", 1], "loc": [-75.22, 39.25], "sub_document": self.mini_doc } self.client = BaseAsyncMotorDocument("test_collection", settings=settings) BaseAsyncTest.setUp(self) # Setup the state of the test self.setup_database() @tornado.gen.coroutine def setup_database(self): yield self.client.collection.drop() @tornado.testing.gen_test def test_01_insert(self): """Test creating a store""" resp = yield self.client.insert(self.test_fixture) ok_(isinstance(resp, str), "Document was not created") @tornado.testing.gen_test def test_02_find_one_by_id(self): """Test get by id""" resp = yield self.client.insert(self.test_fixture) obj = yield self.client.find_one_by_id(resp.__str__()) self.assertIsInstance(obj, dict) self.assertEqual(obj.get("id"), resp) @tornado.testing.gen_test def test_03_update(self): """Test that the client updates an existing object""" resp = yield self.client.insert(self.test_fixture) obj = yield self.client.find_one_by_id(resp) obj[test_attr] = test_val resp = yield self.client.update(resp, obj) ok_(resp.get("updatedExisting"), "Update did not succeed.") @tornado.testing.gen_test def test_04_find(self): """Test that the search end point returns the correct number of items""" yield self.client.insert(self.test_fixture) yield self.client.insert(self.mini_doc) stores = yield self.client.find({}) ok_(len(stores) == 2) @tornado.testing.gen_test def test_05_location_based_search(self): """Test that you can find an object by location based in miles""" yield self.client.create_index("loc") resp = yield self.client.insert(self.test_fixture) ok_(isinstance(resp, str)) obj = yield self.client.location_based_search(-75.221, 39.251, 100) ok_(obj is not None) ok_(isinstance(obj, list)) @tornado.testing.gen_test def test_06_delete_object(self): """Test that we can DELETE an object""" resp = yield self.client.insert(self.test_fixture) delete_resp = yield self.client.delete(resp) ok_(isinstance(delete_resp, dict)) ok_(delete_resp.get("n") == 1) @tornado.testing.gen_test def test_07_patch_object(self): """Test that we can patch an object""" resp = yield self.client.insert(self.test_fixture) patch_obj = {test_attr: test_val} patch_response = yield self.client.patch(resp, patch_obj) self.assertIsInstance(patch_response, dict) self.assertEqual(patch_response.get("n"), 1) resp2 = yield self.client.find_one_by_id(resp) self.assertEqual(resp2.get(test_attr), test_val)
class TestBaseAsyncMotorDocument(BaseAsyncTest): """ Test the Mongo Client funcitons here""" def setUp(self): """Setup the test, runs before each test""" self.mini_doc = { "my doc" : "little bitty doc" } self.test_fixture = { "attr1": "attr1_val", "date1": time.mktime(datetime.datetime.now().timetuple()), "bool_val": True, "list_val": ["My Item", "Item 2", 1], "loc": [-75.22, 39.25], "sub_document" : self.mini_doc } self.client = BaseAsyncMotorDocument("test_collection", settings=settings) BaseAsyncTest.setUp(self) # Setup the state of the test self.setup_database() @tornado.gen.coroutine def setup_database(self): yield self.client.collection.drop() @tornado.testing.gen_test def test_01_insert(self): """Test creating a store""" resp = yield self.client.insert(self.test_fixture) ok_(isinstance(resp, str), "Document was not created") @tornado.testing.gen_test def test_02_find_one_by_id(self): """Test get by id""" resp = yield self.client.insert(self.test_fixture) obj = yield self.client.find_one_by_id(resp.__str__()) self.assertIsInstance(obj, dict) self.assertEqual(obj.get("id"), resp) @tornado.testing.gen_test def test_03_update(self): """Test that the client updates an existing object""" resp = yield self.client.insert(self.test_fixture) obj = yield self.client.find_one_by_id(resp) obj[test_attr] = test_val resp = yield self.client.update(resp, obj) ok_(resp.get("updatedExisting"), "Update did not succeed.") @tornado.testing.gen_test def test_04_find(self): """Test that the search end point returns the correct number of items""" yield self.client.insert(self.test_fixture) yield self.client.insert(self.mini_doc) stores = yield self.client.find({}) ok_(len(stores) == 2) @tornado.testing.gen_test def test_05_location_based_search(self): """Test that you can find an object by location based in miles""" yield self.client.create_index("loc") resp = yield self.client.insert(self.test_fixture) ok_(isinstance(resp, str)) obj = yield self.client.location_based_search(-75.221, 39.251, 100) ok_(obj != None) ok_(isinstance(obj, list)) @tornado.testing.gen_test def test_06_delete_object(self): """Test that we can DELETE an object""" resp = yield self.client.insert(self.test_fixture) delete_resp = yield self.client.delete(resp) ok_(isinstance(delete_resp, dict)) ok_(delete_resp.get("n") == 1) @tornado.testing.gen_test def test_07_patch_object(self): """Test that we can patch an object""" resp = yield self.client.insert(self.test_fixture) patch_obj = { test_attr: test_val } patch_response = yield self.client.patch(resp, patch_obj) self.assertIsInstance(patch_response, dict) self.assertEqual(patch_response.get("n"), 1) resp2 = yield self.client.find_one_by_id(resp) self.assertEqual(resp2.get(test_attr), test_val)
class BaseRevisionList(BaseRestfulMotorHandler): def initialize(self): """Initializer for the Search Handler""" self.logger = logging.getLogger(self.__class__.__name__) self.client = None @coroutine def __lazy_migration(self, master_id): """ Creates a revision for a master id that didn't previously have a revision, this allows you to easily turn on revisioning for a collection that didn't previously allow for it. :param master_id: :returns: list of objects """ collection_name = self.request.headers.get("collection") if collection_name: stack = AsyncSchedulableDocumentRevisionStack( collection_name, self.settings, master_id=master_id, ) objects = yield stack._lazy_migration(meta=self._get_meta_data()) raise Return(objects) self.raise_error( 500, "This object %s/%s didn't exist as a revision, " "we tried to create it but we failed... Sorry. " "Please check this object" % (collection_name, master_id)) raise Return(None) @coroutine def get(self, master_id): """ Get a list of revisions by master ID :param master_id: :return: """ collection_name = self.request.headers.get("collection") self.client = BaseAsyncMotorDocument("%s_revisions" % collection_name) limit = self.get_query_argument("limit", 2) add_current_revision = self.get_arg_value_as_type( "addCurrent", "false") show_history = self.get_arg_value_as_type("showHistory", "false") objects_processed = [] if isinstance(limit, unicode): limit = int(limit) objects = yield self.client.find( { "master_id": master_id, "processed": False }, orderby="toa", order_by_direction=1, page=0, limit=20) # If this is a document that should have a revision and doesn't we # orchestratioin creation of the first one if len(objects) == 0: new_revision = yield self.__lazy_migration(master_id) if not new_revision: return if show_history: objects_processed = yield self.client.find( { "master_id": master_id, "processed": True }, orderby="toa", order_by_direction=-1, page=0, limit=limit) elif add_current_revision: objects_processed = yield self.client.find( { "master_id": master_id, "processed": True }, orderby="toa", order_by_direction=-1, page=0, limit=1) if len(objects_processed) > 0: objects_processed = objects_processed[::-1] objects_processed[-1]["current"] = True objects = objects_processed + objects self.write({"count": len(objects), "results": objects})
class BaseRevisionList(BaseRestfulMotorHandler): def initialize(self): """Initializer for the Search Handler""" self.logger = logging.getLogger(self.__class__.__name__) self.client = None @coroutine def __lazy_migration(self, master_id): """ Creates a revision for a master id that didn't previously have a revision, this allows you to easily turn on revisioning for a collection that didn't previously allow for it. :param master_id: :returns: list of objects """ collection_name = self.request.headers.get("collection") if collection_name: stack = AsyncSchedulableDocumentRevisionStack(collection_name, self.settings, master_id=master_id, ) objects = yield stack._lazy_migration(meta=self._get_meta_data()) raise Return(objects) self.raise_error(500, "This object %s/%s didn't exist as a revision, " "we tried to create it but we failed... Sorry. " "Please check this object" % (collection_name, master_id)) raise Return(None) @coroutine def get(self, master_id): """ Get a list of revisions by master ID :param master_id: :return: """ collection_name = self.request.headers.get("collection") self.client = BaseAsyncMotorDocument("%s_revisions" % collection_name) limit = self.get_query_argument("limit", 2) add_current_revision = self.get_arg_value_as_type("addCurrent", "false") show_history = self.get_arg_value_as_type("showHistory", "false") objects_processed = [] if isinstance(limit, unicode): limit = int(limit) objects = yield self.client.find({"master_id": master_id, "processed": False}, orderby="toa", order_by_direction=1, page=0, limit=20) # If this is a document that should have a revision and doesn't we # orchestratioin creation of the first one if len(objects) == 0: new_revision = yield self.__lazy_migration(master_id) if not new_revision: return if show_history: objects_processed = yield self.client.find({"master_id": master_id, "processed": True}, orderby="toa", order_by_direction=-1, page=0, limit=limit) elif add_current_revision: objects_processed = yield self.client.find({"master_id": master_id, "processed": True}, orderby="toa", order_by_direction=-1, page=0, limit=1) if len(objects_processed) > 0: objects_processed = objects_processed[::-1] objects_processed[-1]["current"] = True objects = objects_processed + objects self.write({ "count": len(objects), "results": objects })