Example #1
0
    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
        })
Example #2
0
    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()
Example #3
0
    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)
Example #4
0
    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)
Example #5
0
    def post(self, id=None):
        """
        Create a revision manually without the stack

        :param id: BSON id
        :return: JSON
        """
        collection_name = self.request.headers.get("collection")

        if not collection_name:
            self.raise_error(400, "Missing a collection name header")

        self.client = BaseAsyncMotorDocument("%s_revisions" % collection_name)

        super(self.__class__, self).post(id)
Example #6
0
    def delete(self, id):
        """
        Delete a revision by ID

        :param id: BSON id
        :return:
        """

        collection_name = self.request.headers.get("collection")

        if not collection_name:
            self.raise_error(400, "Missing a collection name header")

        self.client = BaseAsyncMotorDocument("%s_revisions" % collection_name)

        super(self.__class__, self).delete(id)
Example #7
0
    def delete(self, bulk_id):
        """Update many objects with a single toa

        :param str bulk_id: The bulk id for the job you want to delete
        """

        collection_name = self.request.headers.get("collection")

        if not collection_name:
            self.raise_error(400, "Missing a collection name header")

        self.revisions = BaseAsyncMotorDocument("%s_revisions" %
                                                collection_name)

        self.logger.info("Deleting revisions with bulk_id %s" % (bulk_id))

        result = yield self.revisions.collection.remove(
            {"meta.bulk_id": bulk_id})

        self.write(result)
Example #8
0
    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()
Example #9
0
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)
Example #10
0
 def setUp(self):
     super(self.__class__, self).setUp()
     self.collection = BaseAsyncMotorDocument("test_fixture", settings)
     self.stack = AsyncSchedulableDocumentRevisionStack(
         "test_fixture", settings)
     self.setup_database()
Example #11
0
class TestAsyncRevisionStackAndManagerFunctions(BaseAsyncTest):
    """ Test the Mongo Client funcitons here"""
    mini_doc = {"my doc": "little bitty doc"}

    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": mini_doc,
        "patch": {
            "foo": "bar"
        }
    }

    three_min_past_now = time.mktime(
        (datetime.datetime.now() - datetime.timedelta(minutes=3)).timetuple())
    two_min_past_now = time.mktime(
        (datetime.datetime.now() - datetime.timedelta(minutes=2)).timetuple())
    one_min_past_now = time.mktime(
        (datetime.datetime.now() - datetime.timedelta(minutes=1)).timetuple())
    one_min_from_now = time.mktime(
        (datetime.datetime.now() + datetime.timedelta(minutes=1)).timetuple())

    three_min_ahead_of_now = time.mktime(
        (datetime.datetime.now() + datetime.timedelta(minutes=3)).timetuple())
    now = time.mktime(datetime.datetime.now().timetuple())

    def setUp(self):
        super(self.__class__, self).setUp()
        self.collection = BaseAsyncMotorDocument("test_fixture", settings)
        self.stack = AsyncSchedulableDocumentRevisionStack(
            "test_fixture", settings)
        self.setup_database()

    @tornado.gen.coroutine
    def setup_database(self):
        self.collection.collection.drop()
        self.stack.revisions.collection.drop()
        #self.stack.previews.collection.drop()

    @tornado.testing.gen_test
    def test_push_on_stack(self):
        """Test that a revision is pushed onto the stack and stored into mongo"""
        id = yield self.stack.push(self.test_fixture, self.three_min_past_now)
        self.assertIsInstance(id, str)
        obj_id = ObjectId(id)
        self.assertIsInstance(obj_id, ObjectId)

    @tornado.testing.gen_test
    def test_patch_is_converted_and_storeable(self):
        """Test that a patch can be set with dot namespace safely and applied asynchronously via pop"""
        master_id = yield self.collection.insert(self.test_fixture)
        patch = {"patch.baz": True}
        self.stack = AsyncSchedulableDocumentRevisionStack("test_fixture",
                                                           settings,
                                                           master_id=master_id)
        yield self.stack.push(patch, self.three_min_past_now)
        yield self.stack.pop()
        obj = yield self.collection.find_one_by_id(master_id)

        self.assertEqual(obj.get("patch").get("foo"), "bar")
        self.assertEqual(obj.get("patch").get("baz"), True)

    @tornado.testing.gen_test
    def test_list_of_revisions(self):
        """Test that we can create a list of revisions from a given document in a collection"""
        master_id = yield self.collection.insert(self.test_fixture)
        self.stack = AsyncSchedulableDocumentRevisionStack("test_fixture",
                                                           settings,
                                                           master_id=master_id)
        id1 = yield self.stack.push(self.test_fixture, self.three_min_past_now)
        id2 = yield self.stack.push(self.test_fixture,
                                    self.three_min_ahead_of_now)

        revisions = yield self.stack.list()
        self.assertEqual(len(revisions),
                         1,
                         msg="Did not receive the correct number of revisions")
        self.assertEqual(revisions[0].get("id"),
                         id1,
                         msg="The order doesn't appear to be correct")

    @tornado.testing.gen_test
    def test_peek_on_stack(self):
        """Test that we get a single object off the top of the stack"""
        master_id = yield self.collection.insert(self.test_fixture)
        self.stack = AsyncSchedulableDocumentRevisionStack("test_fixture",
                                                           settings,
                                                           master_id=master_id)
        id1 = yield self.stack.push(
            self.test_fixture,
            time.mktime(
                datetime.datetime.strptime('24052010', "%d%m%Y").timetuple()))
        id2 = yield self.stack.push(
            self.test_fixture,
            time.mktime(datetime.datetime.now().timetuple()))
        peeked_obj = yield self.stack.peek()

        self.assertIsNotNone(peeked_obj)
        self.assertIsInstance(peeked_obj, dict)
        self.assertEqual(id1, peeked_obj.get("id"))
        self.assertEqual(peeked_obj.get("action"), "update")

    @gen_test
    def test_pop_off_stack(self):
        """Test that our stack.pop method updates all relevant data correctly"""
        master_id = yield self.collection.insert(self.mini_doc)
        self.stack = AsyncSchedulableDocumentRevisionStack("test_fixture",
                                                           settings,
                                                           master_id=master_id)
        self.mini_doc[test_attr] = test_val
        id1 = yield self.stack.push(self.mini_doc, self.three_min_past_now)
        id2 = yield self.stack.push(self.mini_doc, self.now)
        obj = yield self.stack.pop()
        self.assertIsInstance(obj, dict)
        self.assertEqual(obj["processed"], True)
        self.assertEqual(obj["snapshot"][test_attr], test_val)

        obj_check = yield self.collection.find_one_by_id(master_id)
        self.assertEqual(test_val, obj_check.get(test_attr))

    @gen_test
    def test_publish_scheduled_pop_off_stack_w_manager(self):
        """Test that we can schedule a revision for 3 min ago and that the revision is applied through the manager"""
        master_id = yield self.collection.insert(self.mini_doc)
        manager = AsyncRevisionStackManager(settings)
        stack = AsyncSchedulableDocumentRevisionStack("test_fixture",
                                                      settings,
                                                      master_id=master_id)

        self.mini_doc[test_attr] = test_val
        yield stack.push(self.mini_doc, self.three_min_past_now)

        # Run the publish method manually
        yield manager.publish()

        #Make sure the live document contains our test values
        obj_check = yield self.collection.find_one_by_id(master_id)
        self.assertEqual(test_val, obj_check.get(test_attr))

    @gen_test
    def test_publish_with_insert_action(self):
        """Test that we can schedule a new collection object"""
        stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings)
        meta = {"comment": "foo"}
        bson_id = yield stack.push(self.mini_doc,
                                   toa=self.three_min_past_now,
                                   meta=meta)
        revisions = yield stack.list()
        self.assertEqual(len(revisions), 1)
        revision = revisions[0]
        self.assertEqual(revision.get("action"), "insert")
        # # Run the publish method manually
        # yield stack.pop()
        #
        # #Make sure the live document contains our test values
        # obj_check = yield self.collection.find_one_by_id(master_id)
        # self.assertIsNone(obj_check, "Object did not delete")

    @gen_test
    def test_publish_with_delete_action(self):
        """Test that we can delete a document on a schedule"""
        master_id = yield self.collection.insert(self.mini_doc)
        manager = AsyncRevisionStackManager(settings)
        stack = AsyncSchedulableDocumentRevisionStack("test_fixture",
                                                      settings,
                                                      master_id=master_id)
        meta = {"comment": "foo"}
        yield stack.push(None, toa=self.three_min_past_now, meta=meta)

        # Run the publish method manually
        yield stack.pop()

        #Make sure the live document contains our test values
        obj_check = yield self.collection.find_one_by_id(master_id)
        self.assertIsNone(obj_check, "Object did not delete")

    @raises(RevisionActionNotValid)
    @gen_test
    def test_stack_push_can_be_invalid_based_on_object_type(self):
        """Test that if we push the wrong type into a patch attribute, we fail correctly"""
        master_id = yield self.collection.insert(self.mini_doc)
        manager = AsyncRevisionStackManager(settings)
        stack = AsyncSchedulableDocumentRevisionStack("test_fixture",
                                                      settings,
                                                      master_id=master_id)

        yield stack.push("foo", toa=self.three_min_past_now)
        yield stack.push(False, toa=self.three_min_past_now)

    @gen_test
    def test_stack_can_produce_snapshot_of_future_revision_of_update_type(
            self):
        """Test that the stack can create a future state of a document"""
        master_id = yield self.collection.insert(self.test_fixture)

        stack = AsyncSchedulableDocumentRevisionStack("test_fixture",
                                                      settings,
                                                      master_id=master_id)

        update = {test_attr: test_val}

        yield stack.push(update, self.three_min_past_now)

        update["new_persistent"] = True
        yield stack.push(update, self.two_min_past_now)

        update["foo"] = "baz"
        id = yield stack.push(update, self.one_min_from_now)

        response = yield stack.preview(id)
        snapshot = response.get("snapshot")
        self.assertIsInstance(snapshot, dict)

        self.assertEqual(snapshot.get("bool_val"), True)
        self.assertEqual(snapshot.get("new_persistent"), True)
        self.assertEqual(snapshot.get("foo"), "baz")

    @gen_test(timeout=50)
    def test_stack_can_produce_snapshot_of_future_revision_of_insert_type(
            self):
        """Test that the stack can create a future state of a new yet to be created document"""

        stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings)

        fixture = self.test_fixture

        yield stack.push(self.test_fixture, self.three_min_past_now)

        fixture["baz"] = "bop"
        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.one_min_past_now)

        response = yield stack.preview(id)
        snapshot = response.get("snapshot")

        self.assertIsInstance(snapshot, dict)
        self.assertEqual(snapshot.get("bool_val"), True)
        self.assertEqual(snapshot.get("new_persistent"), True)
        self.assertEqual(snapshot.get("baz"), "bit")

    @gen_test(timeout=50)
    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)
Example #12
0
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)
Example #13
0
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
        })
Example #14
0
class TestAsyncRevisionStackAndManagerFunctions(BaseAsyncTest):
    """ Test the Mongo Client funcitons here"""
    mini_doc = {
        "my doc" : "little bitty doc"
    }

    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" : mini_doc,
        "patch" :  {
            "foo": "bar"
        }
    }

    three_min_past_now = time.mktime((datetime.datetime.now() - datetime.timedelta(minutes=3)).timetuple())
    two_min_past_now = time.mktime((datetime.datetime.now() - datetime.timedelta(minutes=2)).timetuple())
    one_min_past_now = time.mktime((datetime.datetime.now() - datetime.timedelta(minutes=1)).timetuple())
    one_min_from_now = time.mktime((datetime.datetime.now() + datetime.timedelta(minutes=1)).timetuple())

    three_min_ahead_of_now = time.mktime((datetime.datetime.now() + datetime.timedelta(minutes=3)).timetuple())
    now = time.mktime(datetime.datetime.now().timetuple())

    def setUp(self):
        super(self.__class__, self).setUp()
        self.collection = BaseAsyncMotorDocument("test_fixture", settings)
        self.stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings)
        self.setup_database()

    @tornado.gen.coroutine
    def setup_database(self):
        self.collection.collection.drop()
        self.stack.revisions.collection.drop()
        #self.stack.previews.collection.drop()

    @tornado.testing.gen_test
    def test_push_on_stack(self):
        """Test that a revision is pushed onto the stack and stored into mongo"""
        id = yield self.stack.push(self.test_fixture, self.three_min_past_now)
        self.assertIsInstance(id, str)
        obj_id = ObjectId(id)
        self.assertIsInstance(obj_id, ObjectId)

    @tornado.testing.gen_test
    def test_patch_is_converted_and_storeable(self):
        """Test that a patch can be set with dot namespace safely and applied asynchronously via pop"""
        master_id = yield self.collection.insert(self.test_fixture)
        patch = {
            "patch.baz" : True
        }
        self.stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings, master_id=master_id)
        yield self.stack.push(patch, self.three_min_past_now)
        yield self.stack.pop()
        obj = yield self.collection.find_one_by_id(master_id)

        self.assertEqual(obj.get("patch").get("foo"), "bar")
        self.assertEqual(obj.get("patch").get("baz"), True)


    @tornado.testing.gen_test
    def test_list_of_revisions(self):
        """Test that we can create a list of revisions from a given document in a collection"""
        master_id = yield self.collection.insert(self.test_fixture)
        self.stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings, master_id=master_id)
        id1 = yield self.stack.push(self.test_fixture, self.three_min_past_now)
        id2 = yield self.stack.push(self.test_fixture, self.three_min_ahead_of_now)

        revisions = yield self.stack.list()
        self.assertEqual(len(revisions), 1,msg="Did not receive the correct number of revisions")
        self.assertEqual(revisions[0].get("id"), id1, msg="The order doesn't appear to be correct")

    @tornado.testing.gen_test
    def test_peek_on_stack(self):
        """Test that we get a single object off the top of the stack"""
        master_id = yield self.collection.insert(self.test_fixture)
        self.stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings, master_id=master_id)
        id1 = yield self.stack.push(self.test_fixture, time.mktime(datetime.datetime.strptime('24052010', "%d%m%Y").timetuple()))
        id2 = yield self.stack.push(self.test_fixture, time.mktime(datetime.datetime.now().timetuple()))
        peeked_obj = yield self.stack.peek()

        self.assertIsNotNone(peeked_obj)
        self.assertIsInstance(peeked_obj, dict)
        self.assertEqual(id1, peeked_obj.get("id"))
        self.assertEqual(peeked_obj.get("action"), "update")

    @gen_test
    def test_pop_off_stack(self):
        """Test that our stack.pop method updates all relevant data correctly"""
        master_id = yield self.collection.insert(self.mini_doc)
        self.stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings,master_id=master_id)
        self.mini_doc[test_attr] = test_val
        id1 = yield self.stack.push(self.mini_doc, self.three_min_past_now)
        id2 = yield self.stack.push(self.mini_doc, self.now)
        obj = yield self.stack.pop()
        self.assertIsInstance(obj, dict)
        self.assertEqual(obj["processed"], True)
        self.assertEqual(obj["snapshot"][test_attr], test_val)

        obj_check = yield self.collection.find_one_by_id(master_id)
        self.assertEqual(test_val, obj_check.get(test_attr))

    @gen_test
    def test_publish_scheduled_pop_off_stack_w_manager(self):
        """Test that we can schedule a revision for 3 min ago and that the revision is applied through the manager"""
        master_id = yield self.collection.insert(self.mini_doc)
        manager = AsyncRevisionStackManager(settings)
        stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings, master_id=master_id)

        self.mini_doc[test_attr] = test_val
        yield stack.push(self.mini_doc, self.three_min_past_now)

        # Run the publish method manually
        yield manager.publish()

        #Make sure the live document contains our test values
        obj_check = yield self.collection.find_one_by_id(master_id)
        self.assertEqual(test_val, obj_check.get(test_attr))

    @gen_test
    def test_publish_with_insert_action(self):
        """Test that we can schedule a new collection object"""
        stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings)
        meta = {
            "comment" : "foo"
        }
        bson_id = yield stack.push(self.mini_doc, toa=self.three_min_past_now, meta=meta)
        revisions = yield stack.list()
        self.assertEqual(len(revisions), 1)
        revision = revisions[0]
        self.assertEqual(revision.get("action"), "insert")
        # # Run the publish method manually
        # yield stack.pop()
        #
        # #Make sure the live document contains our test values
        # obj_check = yield self.collection.find_one_by_id(master_id)
        # self.assertIsNone(obj_check, "Object did not delete")


    @gen_test
    def test_publish_with_delete_action(self):
        """Test that we can delete a document on a schedule"""
        master_id = yield self.collection.insert(self.mini_doc)
        manager = AsyncRevisionStackManager(settings)
        stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings, master_id=master_id)
        meta = {
            "comment" : "foo"
        }
        yield stack.push(None, toa=self.three_min_past_now, meta=meta)

        # Run the publish method manually
        yield stack.pop()

        #Make sure the live document contains our test values
        obj_check = yield self.collection.find_one_by_id(master_id)
        self.assertIsNone(obj_check, "Object did not delete")

    @raises(RevisionActionNotValid)
    @gen_test
    def test_stack_push_can_be_invalid_based_on_object_type(self):
        """Test that if we push the wrong type into a patch attribute, we fail correctly"""
        master_id = yield self.collection.insert(self.mini_doc)
        manager = AsyncRevisionStackManager(settings)
        stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings, master_id=master_id)

        yield stack.push("foo", toa=self.three_min_past_now)
        yield stack.push(False, toa=self.three_min_past_now)

    @gen_test
    def test_stack_can_produce_snapshot_of_future_revision_of_update_type(self):
        """Test that the stack can create a future state of a document"""
        master_id = yield self.collection.insert(self.test_fixture)

        stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings, master_id=master_id)

        update = {
            test_attr: test_val
        }

        yield stack.push(update, self.three_min_past_now)

        update["new_persistent"] = True
        yield stack.push(update, self.two_min_past_now)

        update["foo"] = "baz"
        id = yield stack.push(update, self.one_min_from_now)

        response = yield stack.preview(id)
        snapshot = response.get("snapshot")
        self.assertIsInstance(snapshot, dict)

        self.assertEqual(snapshot.get("bool_val"), True)
        self.assertEqual(snapshot.get("new_persistent"), True)
        self.assertEqual(snapshot.get("foo"), "baz")

    @gen_test(timeout=50)
    def test_stack_can_produce_snapshot_of_future_revision_of_insert_type(self):
        """Test that the stack can create a future state of a new yet to be created document"""

        stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings)

        fixture = self.test_fixture

        yield stack.push(self.test_fixture, self.three_min_past_now)

        fixture["baz"] = "bop"
        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.one_min_past_now)


        response = yield stack.preview(id)
        snapshot = response.get("snapshot")

        self.assertIsInstance(snapshot, dict)
        self.assertEqual(snapshot.get("bool_val"), True)
        self.assertEqual(snapshot.get("new_persistent"), True)
        self.assertEqual(snapshot.get("baz"), "bit")

    @gen_test(timeout=50)
    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)
Example #15
0
    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})
Example #16
0
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})
Example #17
0
 def setUp(self):
     super(self.__class__, self).setUp()
     self.collection = BaseAsyncMotorDocument("test_fixture", settings)
     self.stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings)
     self.setup_database()
Example #18
0
 def initialize(self):
     super(self.__class__, self).initialize()
     self.object_name = "comment"
     self.client = BaseAsyncMotorDocument(self.object_name,
                                          self.settings,
                                          schema=self.SCHEMA)