Ejemplo n.º 1
0
    def update(self, document=None, safe=True, callback=None):
        """Update a document

        :Parameters:
        - `safe` (optional): safe update operation
        - `callback` : method which will be called when update is finished
        """
        pre_update.send(instance=self)

        if not document:
            document = self.as_dict()

        database = Database()
        collection_name = database.get_collection_name(self.__collection__)
        spec = {'_id': self._id}

        message_update = message.update(collection_name, False,
                False, spec, document, safe, {})

        response, error = yield gen.Task(database.send_message, message_update)

        post_update.send(instance=self)

        if callback:
            callback((response, error))
Ejemplo n.º 2
0
    def test_not_raise_when_database_was_initiated(self):
        """[DatabaseTestCase] - Not raises ValueError when connect to inititated database"""

        database1 = Database.init("localhost:27027", dbname='test')
        database2 = Database.init("localhost:27027", dbname='test')

        database1.should.be.equal(database2)
Ejemplo n.º 3
0
    def test_not_raise_when_database_was_initiated(self):
        """[DatabaseTestCase] - Not raises ValueError when connect to inititated database"""

        database1 = Database.connect("localhost:27027", dbname="test")
        database2 = Database.connect("localhost:27027", dbname="test")

        database1.should.be.equal(database2)
Ejemplo n.º 4
0
    def tearDown(self):
        super(SignalTestCase, self).tearDown()

        class CollectionTest(Collection):
            __collection__ = "collection_test"

        CollectionTest.objects.truncate(callback=self.stop)
        self.wait()
        Database.disconnect()
Ejemplo n.º 5
0
    def truncate(self, callback=None):
        collection_name = Database().get_collection_name(
            self.collection.__collection__)
        message_delete = message.delete(collection_name, {}, True, {})

        yield gen.Task(Database().send_message, message_delete)

        if callback:
            callback()
Ejemplo n.º 6
0
    def tearDown(self):
        super(SignalTestCase, self).tearDown()

        class CollectionTest(Collection):
            __collection__ = "collection_test"

        CollectionTest.objects.truncate(callback=self.stop)
        self.wait()
        Database.disconnect()
Ejemplo n.º 7
0
    def tearDown(self):
        super(CursorTestCase, self).tearDown()

        # delete all documents
        message_delete = message.delete('mongotor_test.cursor_test',
            {}, True, {})
        Database().send_message(message_delete, callback=self.stop)
        self.wait()

        Database.disconnect()
Ejemplo n.º 8
0
    def tearDown(self):
        super(CursorTestCase, self).tearDown()

        # delete all documents
        message_delete = message.delete('mongotor_test.cursor_test', {}, True,
                                        {})
        Database().send_message(message_delete, callback=self.stop)
        self.wait()

        Database.disconnect()
Ejemplo n.º 9
0
    def _insert_document(self, document):
        message_insert = message.insert('mongotor_test.cursor_test', [document],
            True, True, {})

        node = Database().get_node(ReadPreference.PRIMARY)
        node.connection(self.stop)
        connection = self.wait()

        connection.send_message(message_insert, with_last_error=True, callback=self.stop)
        self.wait()
Ejemplo n.º 10
0
    def test_raises_erro_when_use_collection_with_not_initialized_database(
            self):
        """[CollectionTestCase] - Raises DatabaseError when use collection with a not initialized database"""
        class CollectionTest(Collection):
            __collection__ = 'collection_test'

        Database.disconnect()
        CollectionTest().save.when.called_with(callback=None) \
            .throw(DatabaseError, 'you must be initialize database before perform this action')

        Database.init(["localhost:27027", "localhost:27028"], dbname='test')
Ejemplo n.º 11
0
    def test_raises_erro_when_use_collection_with_not_initialized_database(self):
        """[CollectionTestCase] - Raises DatabaseError when use collection with a not initialized database"""

        class CollectionTest(Collection):
            __collection__ = 'collection_test'

        Database.disconnect()
        CollectionTest().save.when.called_with(callback=None) \
            .throw(DatabaseError, 'you must be initialize database before perform this action')

        Database.init(["localhost:27027", "localhost:27028"], dbname='test')
Ejemplo n.º 12
0
    def tearDown(self):
        super(CursorTestCase, self).tearDown()

        # delete all documents
        message_delete = message.delete('mongotor_test.cursor_test',
            {}, True, {})

        node = Database().get_node(ReadPreference.PRIMARY)
        node.connection(self.stop)
        connection = self.wait()

        connection.send_message(message_delete, with_last_error=True, callback=self.stop)
        self.wait()

        Database.disconnect()
Ejemplo n.º 13
0
    def test_raises_error_when_mode_is_secondary_and_secondary_is_down(self):
        """[ReplicaSetTestCase] - Raise error when mode is secondary and secondary is down"""
        os.system('make mongo-kill > /dev/null 2>&1')
        os.system('make mongo-start-node1')
        os.system('make mongo-start-arbiter')

        time.sleep(2)

        Database.connect(["localhost:27027", "localhost:27028"], dbname='test')
        Database().send_message.when.called_with('',
            read_preference=ReadPreference.SECONDARY)\
            .throw(DatabaseError)

        os.system('make mongo-start-node2')
        time.sleep(10)
Ejemplo n.º 14
0
    def test_count_documents_in_find(self):
        """[ClientTestCase] - counting documents in query"""
        db = Database.init(["localhost:27027", "localhost:27028"],
                           dbname='test')

        documents = [{
            '_id': ObjectId(),
            'param': 'shouldbeparam1'
        }, {
            '_id': ObjectId(),
            'param': 'shouldbeparam1'
        }, {
            '_id': ObjectId(),
            'param': 'shouldbeparam2'
        }]

        db.collection_test.insert(documents, callback=self.stop)
        response, error = self.wait()

        db.collection_test.find({
            "param": 'shouldbeparam1'
        }).count(callback=self.stop)
        total = self.wait()

        total.should.be.equal(2)
Ejemplo n.º 15
0
    def test_distinct_all_documents(self):
        """[ClientTestCase] - distinct among all documents"""
        db = Database.init(["localhost:27027", "localhost:27028"],
                           dbname='test')

        documents = [{
            '_id': ObjectId(),
            'param': 'shouldbeparam1',
            'uuid': 100
        }, {
            '_id': ObjectId(),
            'param': 'shouldbeparam1',
            'uuid': 100
        }, {
            '_id': ObjectId(),
            'param': 'shouldbeparam2',
            'uuid': 200
        }]

        db.collection_test.insert(documents, callback=self.stop)
        response, error = self.wait()

        db.collection_test.distinct('uuid', callback=self.stop)
        distincts = self.wait()

        distincts.should.have.length_of(2)
        distincts[0].should.be.equal(100)
        distincts[1].should.be.equal(200)
Ejemplo n.º 16
0
    def __init__(self,
                 collection,
                 spec=None,
                 fields=None,
                 snapshot=False,
                 tailable=False,
                 max_scan=None,
                 is_command=False,
                 explain=False,
                 hint=None,
                 timeout=True):

        if spec is not None and not isinstance(spec, dict):
            spec = {"_id": spec}

        self._spec = spec or {}
        self._fields = fields
        self._snapshot = snapshot
        self._tailable = tailable
        self._max_scan = max_scan
        self._hint = hint
        self._collection = collection
        self._collection_name = Database().get_collection_name(collection)
        self._timeout = timeout
        self._is_command = is_command
        self._explain = explain
Ejemplo n.º 17
0
    def test_insert_and_find_with_elemmatch(self):
        documents = [{
            '_id': ObjectId(),
            'name': 'should be name 1',
            'comment': [{'author': 'joe'}, {'author': 'ana'}]
        }, {
            '_id': ObjectId(),
            'name': 'should be name 2',
            'comment': [{'author': 'ana'}]
        }]

        db = Database.init(["localhost:27027", "localhost:27028"],
            dbname='test')
        db.articles.insert(documents, callback=self.stop)
        self.wait()

        db.articles.find({'comment.author': 'joe'}, ('comment.$.author', ), limit=-1, callback=self.stop)

        result, _ = self.wait()

        keys = result.keys()
        keys.sort()

        keys.should.be.equal(['_id', 'comment'])

        str(result['_id']).should.be.equal(str(documents[0]['_id']))
        result['comment'].should.have.length_of(1)
        result['comment'][0]['author'].should.be.equal('joe')
        _.should.be.none
Ejemplo n.º 18
0
    def save(self, safe=True, check_keys=True, callback=None):
        """Save a document

        >>> user = Users()
        >>> user.name = 'should be name'
        >>> user.save()

        :Parameters:
          - `safe` (optional): safe insert operation
          - `check_keys` (optional): check if keys start with '$' or
            contain '.', raising :class:`~pymongo.errors.InvalidName`
            in either case
        - `callback` : method which will be called when save is finished
        """
        pre_save.send(instance=self)

        client = Client(Database(), self.__collection__)
        response, error = yield gen.Task(client.insert,
                                         self.as_dict(),
                                         safe=safe,
                                         check_keys=check_keys)

        self.clean_fields()

        post_save.send(instance=self)

        if callback:
            callback((response, error))
Ejemplo n.º 19
0
    def test_send_test_message(self):
        """[DatabaseTestCase] - Send a test message to database"""

        Database.connect(["localhost:27027", "localhost:27028"], dbname="test")

        object_id = ObjectId()
        message_test = message.query(0, "mongotor_test.$cmd", 0, 1, {"driverOIDTest": object_id})

        Database().send_message(message_test, callback=self.stop)
        response, error = self.wait()

        result = response["data"][0]

        result["oid"].should.be(object_id)
        result["ok"].should.be(1.0)
        result["str"].should.be(str(object_id))
Ejemplo n.º 20
0
    def test_find_one_document(self):
        """[ClientTestCase] - find one document"""
        db = Database.init(["localhost:27027", "localhost:27028"],
                           dbname='test')

        documents = [{
            '_id': ObjectId(),
            'param': 'shouldbeparam'
        }, {
            '_id': ObjectId(),
            'param': 'shouldbeparam1'
        }, {
            '_id': ObjectId(),
            'param': 'shouldbeparam2'
        }]

        db.collection_test.insert(documents, callback=self.stop)
        response, error = self.wait()

        db.collection_test.find_one({'param': 'shouldbeparam1'},
                                    callback=self.stop)
        response, error = self.wait()

        response['_id'].should.be(documents[1]['_id'])
        error.should.be.none
Ejemplo n.º 21
0
    def test_configure_nodes(self):
        """[ReplicaSetTestCase] - Configure nodes"""

        Database.connect(["localhost:27027", "localhost:27028"], dbname='test')

        master_node = ReadPreference.select_primary_node(Database()._nodes)
        secondary_node = ReadPreference.select_node(Database()._nodes, mode=ReadPreference.SECONDARY)

        master_node.host.should.be('localhost')
        master_node.port.should.be(27027)

        secondary_node.host.should.be('localhost')
        secondary_node.port.should.be(27028)

        nodes = Database()._nodes
        nodes.should.have.length_of(2)
Ejemplo n.º 22
0
    def test_update_document(self):
        """[ClientTestCase] - update a document"""
        db = Database.init(["localhost:27027", "localhost:27028"],
                           dbname='test')

        documents = [{
            '_id': ObjectId(),
            'name': 'shouldbename'
        }, {
            '_id': ObjectId(),
            'name': 'shouldbename2'
        }]

        db.collection_test.insert(documents, callback=self.stop)
        response, error = self.wait()

        db.collection_test.update(documents[0],
                                  {'$set': {
                                      'name': 'should be a new name'
                                  }},
                                  callback=self.stop)
        response, error = self.wait()

        response['ok'].should.be.equal(1.0)
        error.should.be.none
Ejemplo n.º 23
0
    def test_raises_error_when_could_not_find_node(self):
        """[DatabaseTestCase] - Raises DatabaseError when could not find valid nodes"""

        database = Database.connect(["localhost:27030"], dbname="test")
        database.send_message.when.called_with("", callback=None).throw(
            DatabaseError, "could not find an available node"
        )
Ejemplo n.º 24
0
    def test_find_document(self):
        """[ClientTestCase] - find a document"""
        db = Database.init(["localhost:27027", "localhost:27028"],
                           dbname='test')

        documents = [{
            '_id': ObjectId(),
            'someflag': 1
        }, {
            '_id': ObjectId(),
            'someflag': 1
        }, {
            '_id': ObjectId(),
            'someflag': 2
        }]

        db.collection_test.insert(documents, callback=self.stop)
        response, error = self.wait()

        db.collection_test.find({'someflag': 1}, callback=self.stop)
        response, error = self.wait()

        response[0]['_id'].should.be(documents[0]['_id'])
        response[1]['_id'].should.be(documents[1]['_id'])
        error.should.be.none
Ejemplo n.º 25
0
    def test_send_test_message(self):
        """[DatabaseTestCase] - Send a test message to database"""

        Database.connect(["localhost:27017"], dbname='test')

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
            {'driverOIDTest': object_id})

        Database().send_message(message_test, self.stop)
        response, error = self.wait()

        result = response['data'][0]

        result['oid'].should.be(object_id)
        result['ok'].should.be(1.0)
        result['str'].should.be(str(object_id))
Ejemplo n.º 26
0
    def test_run_command(self):
        """[DatabaseTestCase] - Run a database command"""

        database = Database.init(["localhost:27027"], dbname='test')
        database.command('ismaster', callback=self.stop)

        response, error = self.wait()
        response['ok'].should.be.ok
Ejemplo n.º 27
0
    def test_run_command(self):
        """[DatabaseTestCase] - Run a database command"""

        database = Database.init(["localhost:27027"], dbname='test')
        database.command('ismaster', callback=self.stop)

        response, error = self.wait()
        response['ok'].should.be.ok
Ejemplo n.º 28
0
    def test_check_connections_when_use_cursors(self):
        """[ConnectionPoolTestCase] - check connections when use cursors"""
        db = Database.init('localhost:27027', dbname='test', maxconnections=10, maxusage=29)

        try:
            for i in range(2):
                db.cards.insert({'_id': ObjectId(), 'range': i}, callback=self.stop)
                self.wait()

            db._nodes[0].pool._connections.should.be.equal(0)

            db.cards.find({}, callback=self.stop)
            self.wait()

            db._nodes[0].pool._connections.should.be.equal(0)
        finally:
            Database.disconnect()
Ejemplo n.º 29
0
    def test_run_command(self):
        """[DatabaseTestCase] - Run a database command"""

        database = Database.connect(["localhost:27027"], dbname="test")
        database.command("ismaster", callback=self.stop)

        response, error = self.wait()
        response["ok"].should.be.ok
Ejemplo n.º 30
0
    def test_raises_error_when_mode_is_secondary_and_secondary_is_down(self):
        """[ReplicaSetTestCase] - Raise error when mode is secondary and secondary is down"""
        os.system('make mongo-kill-node2')
        time.sleep(1)  # stops are fast

        try:
            db = Database.init(["localhost:27027", "localhost:27028"],
                               dbname='test')
            db._connect(callback=self.stop)
            self.wait()

            Database().send_message.when.called_with((None, ''),
                read_preference=ReadPreference.SECONDARY)\
                .throw(DatabaseError)
        finally:
            os.system('make mongo-start-node2')
            time.sleep(5)  # wait to become secondary again
Ejemplo n.º 31
0
    def test_send_test_message(self):
        """[DatabaseTestCase] - Send a test message to database"""

        Database.init(["localhost:27027", "localhost:27028"], dbname='test')

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
            {'driverOIDTest': object_id})

        Database().send_message(message_test, callback=self.stop)
        response, _ = self.wait()
        response = helpers._unpack_response(response)

        result = response['data'][0]

        result['oid'].should.be(object_id)
        result['ok'].should.be(1.0)
        result['str'].should.be(str(object_id))
Ejemplo n.º 32
0
    def tearDown(self):
        super(CursorTestCase, self).tearDown()

        # delete all documents
        message_delete = message.delete('mongotor_test.cursor_test', {}, True,
                                        {})

        Database().get_node(ReadPreference.PRIMARY, callback=self.stop)
        node = self.wait()
        node.connection(self.stop)
        connection = self.wait()

        connection.send_message(message_delete,
                                with_last_error=True,
                                callback=self.stop)
        self.wait()

        Database.disconnect()
Ejemplo n.º 33
0
    def test_send_test_message(self):
        """[DatabaseTestCase] - Send a test message to database"""

        Database.init(["localhost:27027", "localhost:27028"], dbname='test')

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
                                     {'driverOIDTest': object_id})

        Database().send_message(message_test, callback=self.stop)
        response, _ = self.wait()
        response = helpers._unpack_response(response)

        result = response['data'][0]

        result['oid'].should.be(object_id)
        result['ok'].should.be(1.0)
        result['str'].should.be(str(object_id))
Ejemplo n.º 34
0
    def find_one(self, query, callback):
        client = Client(Database(), self.collection.__collection__)
        result, error = yield gen.Task(client.find_one, query)

        instance = None
        if result:
            instance = self.collection.create(result, cleaned=True)

        callback(instance)
Ejemplo n.º 35
0
    def test_raises_error_when_could_not_find_node(self):
        """[DatabaseTestCase] - Raises DatabaseError when could not find valid nodes"""

        database = Database.init(["localhost:27030"], dbname='test')

        def send_message():
            database.send_message('', callback=self.stop)
            self.wait()

        send_message.when.called.throw(DatabaseError, 'could not find an available node')
Ejemplo n.º 36
0
    def test_configure_nodes(self):
        """[ReplicaSetTestCase] - Configure nodes"""

        db = Database.init(["localhost:27027", "localhost:27028"],
                           dbname='test')
        db._connect(callback=self.stop)
        self.wait()

        master_node = ReadPreference.select_primary_node(Database()._nodes)
        secondary_node = ReadPreference.select_node(
            Database()._nodes, mode=ReadPreference.SECONDARY)

        master_node.host.should.be('localhost')
        master_node.port.should.be(27027)

        secondary_node.host.should.be('localhost')
        secondary_node.port.should.be(27028)

        nodes = Database()._nodes
        nodes.should.have.length_of(2)
Ejemplo n.º 37
0
    def test_raises_error_when_could_not_find_node(self):
        """[DatabaseTestCase] - Raises DatabaseError when could not find valid nodes"""

        database = Database.init(["localhost:27030"], dbname='test')

        def send_message():
            database.send_message('', callback=self.stop)
            self.wait()

        send_message.when.called.throw(DatabaseError,
                                       'could not find an available node')
Ejemplo n.º 38
0
    def find(self, query, callback, **kw):
        client = Client(Database(), self.collection.__collection__)
        result, error = yield gen.Task(client.find, query, **kw)

        items = []

        if result:
            for item in result:
                items.append(self.collection.create(item, cleaned=True))

        callback(items)
Ejemplo n.º 39
0
    def save(self, safe=True, callback=None):
        """Save a document

        :Parameters:
        - `safe` (optional): safe insert operation 
        - `callback` : method which will be called when save is finished
        """
        pre_save.send(instance=self)

        database = Database()
        collection_name = database.get_collection_name(self.__collection__)

        message_insert = message.insert(collection_name, [self.as_dict()],
            True, safe, {})

        response, error = yield gen.Task(database.send_message, message_insert)

        post_save.send(instance=self)

        if callback:
            callback((response, error))
Ejemplo n.º 40
0
    def remove(self, safe=True, callback=None):
        """Remove a document

        :Parameters:
        - `safe` (optional): safe remove operation
        - `callback` : method which will be called when remove is finished
        """
        pre_remove.send(instance=self)

        database = Database()
        collection_name = database.get_collection_name(self.__collection__)

        message_delete = message.delete(collection_name, {'_id': self._id},
            safe, {})

        response, error = yield gen.Task(database.send_message, message_delete)

        post_remove.send(instance=self)

        if callback:
            callback((response, error))
Ejemplo n.º 41
0
    def test_aggregate_collection(self):
        """[ClientTestCase] - aggregate command"""
        db = Database.init(["localhost:27027", "localhost:27028"],
                           dbname='test')

        documents = [{
            "title": "this is my title",
            "author": "bob",
            "posted": datetime.now(),
            "pageViews": 5,
            "tags": ["good", "fun"],
        }, {
            "title": "this is my title",
            "author": "joe",
            "posted": datetime.now(),
            "pageViews": 5,
            "tags": ["good"],
        }]

        db.articles.insert(documents, callback=self.stop)
        response, error = self.wait()

        try:
            pipeline = {
                "$project": {
                    "author": 1,
                    "tags": 1,
                }
            }, {
                "$unwind": "$tags"
            }, {
                "$group": {
                    "_id": {
                        "tags": "$tags"
                    },
                    "authors": {
                        "$addToSet": "$author"
                    }
                }
            }
            db.articles.aggregate(pipeline, callback=self.stop)

            response = self.wait()

            response['result'][0]['_id'].should.be.equal({u'tags': u'fun'})
            response['result'][0]['authors'].should.be.equal([u'bob'])

            response['result'][1]['_id'].should.be.equal({u'tags': u'good'})
            response['result'][1]['authors'].should.be.equal([u'joe', u'bob'])
        finally:
            db.articles.remove({}, callback=self.stop)
            self.wait()
Ejemplo n.º 42
0
    def _insert_document(self, document):
        message_insert = message.insert('mongotor_test.cursor_test',
                                        [document], True, True, {})

        Database().get_node(ReadPreference.PRIMARY, callback=self.stop)
        node = self.wait()
        node.connection(self.stop)
        connection = self.wait()

        connection.send_message(message_insert,
                                with_last_error=True,
                                callback=self.stop)
        self.wait()
Ejemplo n.º 43
0
    def test_insert_a_single_document(self):
        """[ClientTestCase] - insert a single document with client"""

        db = Database.init(["localhost:27027", "localhost:27028"],
            dbname='test')

        document = {'_id': ObjectId(), 'name': 'shouldbename'}

        db.collection_test.insert(document, callback=self.stop)
        response, error = self.wait()

        response['ok'].should.be.equal(1.0)
        error.should.be.none
Ejemplo n.º 44
0
    def count(self, query=None, callback=None):
        command = {"count": self.collection.__collection__}

        if query:
            command["query"] = query

        result, error = yield gen.Task(Database().command, command)

        total = 0
        if result and len(result) > 0 and 'n' in result:
            total = int(result['n'])

        callback(total)
Ejemplo n.º 45
0
    def test_insert_a_single_document(self):
        """[ClientTestCase] - insert a single document with client"""

        db = Database.init(["localhost:27027", "localhost:27028"],
                           dbname='test')

        document = {'_id': ObjectId(), 'name': 'shouldbename'}

        db.collection_test.insert(document, callback=self.stop)
        response, error = self.wait()

        response['ok'].should.be.equal(1.0)
        error.should.be.none
Ejemplo n.º 46
0
    def test_insert_a_document_list(self):
        """[ClientTestCase] - insert a list of document with client"""

        db = Database.connect(["localhost:27027", "localhost:27028"],
            dbname='test')

        documents = [{'_id': ObjectId(), 'name': 'shouldbename'},
            {'_id': ObjectId(), 'name': 'shouldbename2'}]

        db.collection_test.insert(documents, callback=self.stop)
        response, error = self.wait()

        response['data'][0]['ok'].should.be.equal(1.0)
        error.should.be.none
Ejemplo n.º 47
0
    def test_find_on_secondary(self):
        """[SecondaryPreferredTestCase] - test find document from secondary"""
        db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test',
            read_preference=ReadPreference.SECONDARY_PREFERRED)

        doc = {'_id': ObjectId()}
        db.test.insert(doc, callback=self.stop)
        self.wait()

        time.sleep(2)
        db.test.find_one(doc, callback=self.stop)
        doc_found, error = self.wait()

        doc_found.should.be.eql(doc)
Ejemplo n.º 48
0
    def find(self, skip=0, limit=0, sort=None, callback=None):
        self._ordering = sort

        message_query = message.query(self._query_options(),
                                      self._collection_name, skip, limit,
                                      self._query_spec(), self._fields)

        response, error = yield gen.Task(Database().send_message,
                                         message_query)

        # close cursor
        if response and response.get('cursor_id'):
            cursor_id = response['cursor_id']
            Database().send_message(message.kill_cursors([cursor_id]),
                                    callback=None)

        if error:
            callback((None, error))
        else:
            if limit == -1 and len(response['data']) == 1:
                callback((response['data'][0], None))
            else:
                callback((response['data'], None))
Ejemplo n.º 49
0
    def test_find_returning_fields(self):
        """[CursorTestCase] - Find and return only selectd fields"""

        document1 = {
            '_id': ObjectId(),
            'name': 'should be name 1',
            'comment': [{
                'author': 'joe'
            }, {
                'author': 'ana'
            }]
        }
        self._insert_document(document1)

        document2 = {
            '_id': ObjectId(),
            'name': 'should be name 2',
            'comment': [{
                'author': 'ana'
            }]
        }
        self._insert_document(document2)

        document3 = {
            '_id': ObjectId(),
            'name': 'should be name 3',
            'comment': [{
                'author': 'june'
            }]
        }
        self._insert_document(document3)

        cursor = Cursor(Database(),
                        'cursor_test', {'comment.author': 'joe'},
                        ('comment.$.author', ),
                        limit=-1)
        cursor.find(callback=self.stop)

        result, _ = self.wait()

        keys = result.keys()
        keys.sort()

        keys.should.be.equal(['_id', 'comment'])

        str(result['_id']).should.be.equal(str(document1['_id']))
        result['comment'].should.have.length_of(1)
        result['comment'][0]['author'].should.be.equal('joe')
        _.should.be.none
Ejemplo n.º 50
0
    def test_aggregate_collection(self):
        """[ClientTestCase] - aggregate command"""
        db = Database.init(["localhost:27027", "localhost:27028"],
            dbname='test')

        documents = [{
            "title": "this is my title",
            "author": "bob",
            "posted": datetime.now(),
            "pageViews": 5,
            "tags": ["good", "fun"],
        }, {
            "title": "this is my title",
            "author": "joe",
            "posted": datetime.now(),
            "pageViews": 5,
            "tags": ["good"],
        }]

        db.articles.insert(documents, callback=self.stop)
        response, error = self.wait()

        try:
            pipeline = {
                "$project": {
                     "author": 1,
                     "tags": 1,
                }
            }, {
                "$unwind": "$tags"
            }, {
                "$group": {
                     "_id": {"tags": "$tags"},
                     "authors": {"$addToSet": "$author"}
                }
            }
            db.articles.aggregate(pipeline, callback=self.stop)

            response = self.wait()

            response['result'][0]['_id'].should.be.equal({u'tags': u'fun'})
            response['result'][0]['authors'].should.be.equal([u'bob'])

            response['result'][1]['_id'].should.be.equal({u'tags': u'good'})
            response['result'][1]['authors'].should.be.equal([u'joe', u'bob'])
        finally:
            db.articles.remove({}, callback=self.stop)
            self.wait()
Ejemplo n.º 51
0
    def test_count_all_documents(self):
        """[ClientTestCase] - counting among all documents"""
        db = Database.init(["localhost:27027", "localhost:27028"],
            dbname='test')

        documents = [{'_id': ObjectId(), 'param': 'shouldbeparam1'},
            {'_id': ObjectId(), 'param': 'shouldbeparam1'},
            {'_id': ObjectId(), 'param': 'shouldbeparam2'}]

        db.collection_test.insert(documents, callback=self.stop)
        response, error = self.wait()

        db.collection_test.count(callback=self.stop)
        total = self.wait()

        total.should.be.equal(3)
Ejemplo n.º 52
0
    def test_count_documents_in_find(self):
        """[ClientTestCase] - counting documents in query"""
        db = Database.connect(["localhost:27027", "localhost:27028"],
            dbname='test')

        documents = [{'_id': ObjectId(), 'param': 'shouldbeparam1'},
            {'_id': ObjectId(), 'param': 'shouldbeparam1'},
            {'_id': ObjectId(), 'param': 'shouldbeparam2'}]

        db.collection_test.insert(documents, callback=self.stop)
        response, error = self.wait()

        db.collection_test.find({"param": 'shouldbeparam1'}).count(callback=self.stop)
        total = self.wait()

        total.should.be.equal(2)
Ejemplo n.º 53
0
    def test_remove_document_by_spec(self):
        """[ClientTestCase] - remove a document by spec"""
        db = Database.init(["localhost:27027", "localhost:27028"],
            dbname='test')

        documents = [{'_id': ObjectId(), 'name': 'shouldbename'},
            {'_id': ObjectId(), 'name': 'shouldbename2'}]

        db.collection_test.insert(documents, callback=self.stop)
        response, error = self.wait()

        db.collection_test.remove({'name': 'shouldbename'}, callback=self.stop)
        response, error = self.wait()

        response['ok'].should.be.equal(1.0)
        error.should.be.none
Ejemplo n.º 54
0
    def test_raises_error_when_mode_is_secondary_and_secondary_is_down(self):
        """[ReplicaSetTestCase] - Raise error when mode is secondary and secondary is down"""
        os.system("make mongo-kill-node2")
        time.sleep(1)  # stops are fast

        try:
            db = Database.init(["localhost:27027", "localhost:27028"], dbname="test")
            db._connect(callback=self.stop)
            self.wait()

            Database().send_message.when.called_with((None, ""), read_preference=ReadPreference.SECONDARY).throw(
                DatabaseError
            )
        finally:
            os.system("make mongo-start-node2")
            time.sleep(5)  # wait to become secondary again
Ejemplo n.º 55
0
    def test_update_document(self):
        """[ClientTestCase] - update a document"""
        db = Database.init(["localhost:27027", "localhost:27028"],
            dbname='test')

        documents = [{'_id': ObjectId(), 'name': 'shouldbename'},
            {'_id': ObjectId(), 'name': 'shouldbename2'}]

        db.collection_test.insert(documents, callback=self.stop)
        response, error = self.wait()

        db.collection_test.update(documents[0], {'$set': {'name':
            'should be a new name'}}, callback=self.stop)
        response, error = self.wait()

        response['ok'].should.be.equal(1.0)
        error.should.be.none
Ejemplo n.º 56
0
    def test_distinct_documents_in_find(self):
        """[ClientTestCase] - distinct documents in query"""
        db = Database.init(["localhost:27027", "localhost:27028"],
            dbname='test')

        documents = [{'_id': ObjectId(), 'param': 'shouldbeparam1', 'uuid': 100},
            {'_id': ObjectId(), 'param': 'shouldbeparam1', 'uuid': 100},
            {'_id': ObjectId(), 'param': 'shouldbeparam2', 'uuid': 200}]

        db.collection_test.insert(documents, callback=self.stop)
        response, error = self.wait()

        db.collection_test.find({"param": 'shouldbeparam1'}).distinct('uuid', callback=self.stop)
        distincts = self.wait()

        distincts.should.have.length_of(1)
        distincts[0].should.be.equal(100)