예제 #1
0
    def test_1_ProjectHandler_get(self):
        with mock.patch.object(Project, 'get_doc') as mock_get:
            setup_func(mock_get, 'ok')
            self.fetch(self.url)
            mock_get.assert_called_once_with(self.database, self.project_id)

        # raise test
        with mock.patch.object(Project, 'get_doc') as mock_get:
            raise_fetch(mock_get, ValueError, 'foo')
            resp = self.fetch(self.url)
            self.assertEqual(resp.body, '{"ok": false, "msg": "foo"}')
예제 #2
0
    def test_list_projects(self):
        # this test is for ProjectsHandler
        url = '/api/v1/couch_test/project/list'
        with mock.patch.object(Project, 'list_project') as mock_list:
            setup_func(mock_list, [self.project_id])
            self.fetch(url)
            mock_list.assert_called_once_with(self.database)

        # raise test
        with mock.patch.object(Project, 'get_doc') as mock_get:
            raise_fetch(mock_get, ValueError, 'missing')
            resp = self.fetch(url)
            self.assertEqual(resp.body, '{"ok": false, "msg": "missing"}')
예제 #3
0
    def test_1_ServiceHandler_get(self):
        with mock.patch.object(Service, 'get_doc') as mock_get:
            data = {"foo": "bar"}
            setup_func(mock_get, data)
            resp = self.fetch(self.url)
            mock_get.assert_called_once_with(self.database, self.service_id)
            self.assertEqual(resp.body, json_encode(data))

        # raise test
        with mock.patch.object(Service, 'get_doc') as mock_get:
            raise_fetch(mock_get, ValueError, 'no_db_file')
            resp = self.fetch(self.url)
            self.assertEqual(resp.code, 500)
            self.assertEqual(resp.body, '{"ok": false, "msg": "no_db_file"}')
예제 #4
0
    def test_2_ProjectHandler_post(self):
        data = {"foo": "bar"}
        data_str = json_encode(data)
        add_data = data.copy()
        add_data.update(self.type)

        # application/json
        with mock.patch.object(Project, 'add_project') as mock_add:
            setup_func(mock_add, 'ok')
            self.fetch(self.url, method="POST", headers=self.json_header, body=data_str)
            mock_add.assert_called_once_with(self.database, self.project_id, add_data)

        # multipart/form-data
        with mock.patch.object(Project, 'add_project') as mock_add:
            setup_func(mock_add, 'ok')
            headers, data = self.form_data(data)
            self.fetch(self.url, method="POST", headers=headers, body=data)
            mock_add.assert_called_once_with(self.database, self.project_id, add_data)

        # raise test
        with mock.patch.object(Project, 'add_project') as mock_add:
            raise_fetch(mock_add, ValueError, 'foo')
            resp = self.fetch(self.url, method="POST", body='')
            self.assertEqual(resp.body, '{"ok": false, "msg": "foo"}')
예제 #5
0
 def test_delete_raise(self):
     with mock.patch.object(CouchServer, 'fetch') as mock_fetch:
         raise_fetch(mock_fetch, HTTPError, 500)
         resp = self.fetch('/api/v1/database/couch_test', method="DELETE")
         self.assertEqual(resp.body, '{"ok": false, "msg": "Database: couch_test not Exist"}')