class TestMongoDbStore(unittest.TestCase):

    def setUp(self):
        self.mongodb_store = MongoDbStore('localhost', 27017, 'nete_integration_test', 'documents')

    def tearDown(self):
        self.mongodb_store.connection.drop_database('nete_integration_test')

    def test_get_returns_document_saved_with_create(self):
        doc = {u'_id': uuid.UUID('d64c57c2f6b34e35857b14dd2c3a03ef'), 'foo': 'bar'}

        self.mongodb_store.create(doc)

        saved_doc = self.mongodb_store.get(uuid.UUID('d64c57c2f6b34e35857b14dd2c3a03ef'))

        self.assertEquals(doc, saved_doc)
class TestMongoDbStore(unittest.TestCase):
    def setUp(self):
        self.mongodb_store = MongoDbStore('localhost', 27017,
                                          'nete_integration_test', 'documents')

    def tearDown(self):
        self.mongodb_store.connection.drop_database('nete_integration_test')

    def test_get_returns_document_saved_with_create(self):
        doc = {
            u'_id': uuid.UUID('d64c57c2f6b34e35857b14dd2c3a03ef'),
            'foo': 'bar'
        }

        self.mongodb_store.create(doc)

        saved_doc = self.mongodb_store.get(
            uuid.UUID('d64c57c2f6b34e35857b14dd2c3a03ef'))

        self.assertEquals(doc, saved_doc)
Esempio n. 3
0
class RestApiTest(tornado.testing.AsyncHTTPTestCase):
    nete_db = MongoDbStore('127.0.0.1', 27017, 'nete_test', 'notes')
    app = RestApplication(nete_db)

    def setUp(self):
        super(RestApiTest, self).setUp()
        self.client = tornado.httpclient.AsyncHTTPClient(self.io_loop)

    def tearDown(self):
        super(RestApiTest, self).tearDown()

    def get_app(self):
        return self.app

    def test_http_get_returns_saved_document(self):
        put_request = tornado.httpclient.HTTPRequest(self.get_url('/'),
                                                     method='PUT',
                                                     body='{"foo": "bar"}')
        self.client.fetch(put_request, self.stop)
        response = self.wait()
        json_response = json.loads(response.body)

        self.client.fetch(self.get_url('/%s' % json_response['_id']),
                          self.stop)
        response = self.wait()
        json_response = json.loads(response.body)

        self.assertEquals({
            'foo': 'bar',
            '_id': json_response['_id']
        }, json_response)

    def test_http_get_non_existing_document_returns_failure(self):
        self.client.fetch(self.get_url('/bar'), self.stop)
        response = self.wait()
        json_response = json.loads(response.body)

        self.assertEquals(False, json_response.get('success'))
 def setUp(self):
     connection_mock = mock.MagicMock()
     with mock.patch("pymongo.Connection", connection_mock):
         self.mongodb_store = MongoDbStore("localhost", 27017, "nete", "documents")
     self.collection_mock = self.mongodb_store.collection
class TestMongoDbStore(unittest.TestCase):
    def setUp(self):
        connection_mock = mock.MagicMock()
        with mock.patch("pymongo.Connection", connection_mock):
            self.mongodb_store = MongoDbStore("localhost", 27017, "nete", "documents")
        self.collection_mock = self.mongodb_store.collection

    def test_get_reads_document_from_mongodb(self):
        self.collection_mock.find_one = mock.MagicMock(
            return_value={"_id": uuid.UUID("bde705f7237744d2a9723aa14fc30ed6"), "foo": "bar"}
        )

        self.assertEquals(
            {"_id": uuid.UUID("bde705f7237744d2a9723aa14fc30ed6"), "foo": "bar"},
            self.mongodb_store.get(uuid.UUID("bde705f7237744d2a9723aa14fc30ed6")),
        )

    def test_get_by_id_with_non_existing_id_raises_DocumentNotFound(self):
        self.collection_mock.find_one = mock.MagicMock(return_value=None)

        with self.assertRaises(DocumentNotFound):
            self.mongodb_store.get(uuid.UUID("bde705f7237744d2a9723aa14fc30ed6"))

    def test_get_children_returns_children_of_given_parent_id(self):
        self.collection_mock.find = mock.MagicMock(
            return_value=[
                {
                    "_id": uuid.UUID("ab35b60a-1e59-4002-b149-45ad86895d94"),
                    "_parent_id": uuid.UUID("20270beb-3e39-4eef-9c18-05885946355f"),
                },
                {
                    "_id": uuid.UUID("3f17f5b6-572d-41a1-b9bc-18c9240ce483"),
                    "_parent_id": uuid.UUID("20270beb-3e39-4eef-9c18-05885946355f"),
                },
            ]
        )

        self.assertEquals(
            [
                {
                    "_id": uuid.UUID("ab35b60a-1e59-4002-b149-45ad86895d94"),
                    "_parent_id": uuid.UUID("20270beb-3e39-4eef-9c18-05885946355f"),
                },
                {
                    "_id": uuid.UUID("3f17f5b6-572d-41a1-b9bc-18c9240ce483"),
                    "_parent_id": uuid.UUID("20270beb-3e39-4eef-9c18-05885946355f"),
                },
            ],
            self.mongodb_store.get_children(uuid.UUID("20270beb-3e39-4eef-9c18-05885946355f")),
        )

    def test_get_children_returns_root_documents_when_parent_id_is_None(self):
        self.collection_mock.find = mock.MagicMock(
            return_value=[
                {"_id": uuid.UUID("ab35b60a-1e59-4002-b149-45ad86895d94"), "_parent_id": None},
                {"_id": uuid.UUID("3f17f5b6-572d-41a1-b9bc-18c9240ce483")},
            ]
        )

        self.assertEquals(
            [
                {"_id": uuid.UUID("ab35b60a-1e59-4002-b149-45ad86895d94"), "_parent_id": None},
                {"_id": uuid.UUID("3f17f5b6-572d-41a1-b9bc-18c9240ce483")},
            ],
            self.mongodb_store.get_children(None),
        )

    def test_create_saves_document_in_database(self):
        self.collection_mock.save = mock.MagicMock(return_value=uuid.UUID("3aeea398f18149248206d469fc9a7b90"))

        self.mongodb_store.create({"_id": uuid.UUID("3aeea398f18149248206d469fc9a7b90"), "foo": "bar"})

        self.collection_mock.save.assert_called_once_with(
            {"_id": uuid.UUID("3aeea398f18149248206d469fc9a7b90"), "foo": "bar"}, safe=True
        )

    def test_create_adds_id_if_missing(self):
        with mock.patch("uuid.uuid4") as uuid4_mock:
            uuid4_mock.return_value = uuid.UUID("3aeea398f18149248206d469fc9a7b90")
            self.mongodb_store.create({"foo": "bar"})

        self.collection_mock.save.assert_called_once_with(
            {"_id": uuid.UUID("3aeea398f18149248206d469fc9a7b90"), "foo": "bar"}, safe=True
        )

    def test_create_returns_document_id(self):
        self.collection_mock.save = mock.MagicMock(return_value=uuid.UUID("3aeea398f18149248206d469fc9a7b90"))

        doc_id = self.mongodb_store.create({"_id": uuid.UUID("3aeea398f18149248206d469fc9a7b90"), "foo": "bar"})

        self.assertEquals(uuid.UUID("3aeea398f18149248206d469fc9a7b90"), doc_id)

    def test_create_returns_document_id_on_successful_save(self):
        self.collection_mock.save = mock.MagicMock(return_value=uuid.UUID("3aeea398f18149248206d469fc9a7b90"))

        doc_id = self.mongodb_store.create({"_id": uuid.UUID("3aeea398f18149248206d469fc9a7b90"), "foo": "bar"})

        self.assertEquals(uuid.UUID("3aeea398f18149248206d469fc9a7b90"), doc_id)

    def test_update_updates_document(self):
        self.collection_mock.update = mock.MagicMock()

        self.mongodb_store.update({"_id": "some id", "foo": "bar"})

        self.collection_mock.update.assert_called_once_with("some id", {"$set": {"foo": "bar"}}, safe=True)

    def test_update_raises_DocumentNotFound_if_document_does_not_exist(self):
        self.collection_mock.update = mock.MagicMock()
        self.collection_mock.update.return_value = {"updatedExisting": False}

        with self.assertRaises(DocumentNotFound):
            self.mongodb_store.update({"_id": "some id", "foo": "bar"})

    def test_delete_removes_file_from_filesystem(self):
        self.collection_mock.remove = mock.MagicMock()

        self.mongodb_store.delete("foo/bar")

        self.collection_mock.remove.assert_called_once_with({"path": "foo/bar"}, safe=True)

    def test_delete_non_existing_document_raises_DocumentNotFound(self):
        self.collection_mock.remove = mock.MagicMock(side_effect=NeteException())

        with self.assertRaises(NeteException):
            self.mongodb_store.delete("foo/bar")
Esempio n. 6
0
 def setUp(self):
     connection_mock = mock.MagicMock()
     with mock.patch('pymongo.Connection', connection_mock):
         self.mongodb_store = MongoDbStore('localhost', 27017, 'nete',
                                           'documents')
     self.collection_mock = self.mongodb_store.collection
Esempio n. 7
0
class TestMongoDbStore(unittest.TestCase):
    def setUp(self):
        connection_mock = mock.MagicMock()
        with mock.patch('pymongo.Connection', connection_mock):
            self.mongodb_store = MongoDbStore('localhost', 27017, 'nete',
                                              'documents')
        self.collection_mock = self.mongodb_store.collection

    def test_get_reads_document_from_mongodb(self):
        self.collection_mock.find_one = mock.MagicMock(
            return_value={
                '_id': uuid.UUID('bde705f7237744d2a9723aa14fc30ed6'),
                'foo': 'bar'
            })

        self.assertEquals(
            {
                '_id': uuid.UUID('bde705f7237744d2a9723aa14fc30ed6'),
                'foo': 'bar'
            },
            self.mongodb_store.get(
                uuid.UUID('bde705f7237744d2a9723aa14fc30ed6')))

    def test_get_by_id_with_non_existing_id_raises_DocumentNotFound(self):
        self.collection_mock.find_one = mock.MagicMock(return_value=None)

        with self.assertRaises(DocumentNotFound):
            self.mongodb_store.get(
                uuid.UUID('bde705f7237744d2a9723aa14fc30ed6'))

    def test_get_children_returns_children_of_given_parent_id(self):
        self.collection_mock.find = mock.MagicMock(return_value=[
            {
                '_id': uuid.UUID('ab35b60a-1e59-4002-b149-45ad86895d94'),
                '_parent_id': uuid.UUID('20270beb-3e39-4eef-9c18-05885946355f')
            }, {
                '_id': uuid.UUID('3f17f5b6-572d-41a1-b9bc-18c9240ce483'),
                '_parent_id': uuid.UUID('20270beb-3e39-4eef-9c18-05885946355f')
            }
        ])

        self.assertEquals(
            [{
                '_id': uuid.UUID('ab35b60a-1e59-4002-b149-45ad86895d94'),
                '_parent_id': uuid.UUID('20270beb-3e39-4eef-9c18-05885946355f')
            }, {
                '_id': uuid.UUID('3f17f5b6-572d-41a1-b9bc-18c9240ce483'),
                '_parent_id': uuid.UUID('20270beb-3e39-4eef-9c18-05885946355f')
            }],
            self.mongodb_store.get_children(
                uuid.UUID('20270beb-3e39-4eef-9c18-05885946355f')))

    def test_get_children_returns_root_documents_when_parent_id_is_None(self):
        self.collection_mock.find = mock.MagicMock(return_value=[
            {
                '_id': uuid.UUID('ab35b60a-1e59-4002-b149-45ad86895d94'),
                '_parent_id': None
            }, {
                '_id': uuid.UUID('3f17f5b6-572d-41a1-b9bc-18c9240ce483')
            }
        ])

        self.assertEquals(
            [{
                '_id': uuid.UUID('ab35b60a-1e59-4002-b149-45ad86895d94'),
                '_parent_id': None
            }, {
                '_id': uuid.UUID('3f17f5b6-572d-41a1-b9bc-18c9240ce483')
            }], self.mongodb_store.get_children(None))

    def test_create_saves_document_in_database(self):
        self.collection_mock.save = mock.MagicMock(
            return_value=uuid.UUID('3aeea398f18149248206d469fc9a7b90'))

        self.mongodb_store.create({
            '_id':
            uuid.UUID('3aeea398f18149248206d469fc9a7b90'),
            'foo':
            'bar'
        })

        self.collection_mock.save.assert_called_once_with(
            {
                '_id': uuid.UUID('3aeea398f18149248206d469fc9a7b90'),
                'foo': 'bar'
            },
            safe=True)

    def test_create_adds_id_if_missing(self):
        with mock.patch('uuid.uuid4') as uuid4_mock:
            uuid4_mock.return_value = uuid.UUID(
                '3aeea398f18149248206d469fc9a7b90')
            self.mongodb_store.create({'foo': 'bar'})

        self.collection_mock.save.assert_called_once_with(
            {
                '_id': uuid.UUID('3aeea398f18149248206d469fc9a7b90'),
                'foo': 'bar'
            },
            safe=True)

    def test_create_returns_document_id(self):
        self.collection_mock.save = mock.MagicMock(
            return_value=uuid.UUID('3aeea398f18149248206d469fc9a7b90'))

        doc_id = self.mongodb_store.create({
            '_id':
            uuid.UUID('3aeea398f18149248206d469fc9a7b90'),
            'foo':
            'bar'
        })

        self.assertEquals(uuid.UUID('3aeea398f18149248206d469fc9a7b90'),
                          doc_id)

    def test_create_returns_document_id_on_successful_save(self):
        self.collection_mock.save = mock.MagicMock(
            return_value=uuid.UUID('3aeea398f18149248206d469fc9a7b90'))

        doc_id = self.mongodb_store.create({
            '_id':
            uuid.UUID('3aeea398f18149248206d469fc9a7b90'),
            'foo':
            'bar'
        })

        self.assertEquals(uuid.UUID('3aeea398f18149248206d469fc9a7b90'),
                          doc_id)

    def test_update_updates_document(self):
        self.collection_mock.update = mock.MagicMock()

        self.mongodb_store.update({'_id': 'some id', 'foo': 'bar'})

        self.collection_mock.update.assert_called_once_with(
            'some id', {'$set': {
                'foo': 'bar'
            }}, safe=True)

    def test_update_raises_DocumentNotFound_if_document_does_not_exist(self):
        self.collection_mock.update = mock.MagicMock()
        self.collection_mock.update.return_value = {'updatedExisting': False}

        with self.assertRaises(DocumentNotFound):
            self.mongodb_store.update({'_id': 'some id', 'foo': 'bar'})

    def test_delete_removes_file_from_filesystem(self):
        self.collection_mock.remove = mock.MagicMock()

        self.mongodb_store.delete('foo/bar')

        self.collection_mock.remove.assert_called_once_with(
            {'path': 'foo/bar'}, safe=True)

    def test_delete_non_existing_document_raises_DocumentNotFound(self):
        self.collection_mock.remove = mock.MagicMock(
            side_effect=NeteException())

        with self.assertRaises(NeteException):
            self.mongodb_store.delete('foo/bar')
 def setUp(self):
     self.mongodb_store = MongoDbStore('localhost', 27017, 'nete_integration_test', 'documents')
Esempio n. 9
0
from nete.db.mongodb_store import MongoDbStore
import logging
import nete.rest.app
import tornado.httpserver
import tornado.ioloop
import tornado.options

API_PORT = 8888
WEB_PORT = 8080

if __name__ == '__main__':
    logging.basicConfig()
    logging.getLogger().setLevel(logging.DEBUG)
    logger = logging.getLogger(__name__)

    tornado.options.parse_command_line()

    nete_db = MongoDbStore('127.0.0.1', 27017, 'nete', 'notes')

    logging.info('Starting nete API server on port %d' % API_PORT)
    api_application = nete.rest.app.RestApplication(nete_db, debug=True)
    api_server = tornado.httpserver.HTTPServer(api_application)
    api_server.listen(API_PORT, 'localhost')

    #logging.info('Starting nete web server on port %d' % WEB_PORT)
    #web_application = nete.web.app.WebApplication(nete_db, debug=True)
    #web_server = tornado.httpserver.HTTPServer(web_application)
    #web_server.listen(WEB_PORT, '0.0.0.0')

    tornado.ioloop.IOLoop.instance().start()
Esempio n. 10
0
 def setUp(self):
     self.mongodb_store = MongoDbStore('localhost', 27017,
                                       'nete_integration_test', 'documents')