Beispiel #1
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)
Beispiel #2
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)