Пример #1
0
    def test_kernel_handler(self):
        # GET kernel with given id
        kid = self.kern_api.start().json()["id"]
        r = self.kern_api.get(kid)
        kern1 = r.json()
        self.assertEqual(r.status_code, 200)
        assert isinstance(kern1, dict)
        self.assertIn("id", kern1)
        self.assertEqual(kern1["id"], kid)

        # Request a bad kernel id and check that a JSON
        # message is returned!
        bad_id = "111-111-111-111-111"
        with assert_http_error(404, "Kernel does not exist: " + bad_id):
            self.kern_api.get(bad_id)

        # DELETE kernel with id
        r = self.kern_api.shutdown(kid)
        self.assertEqual(r.status_code, 204)
        kernels = self.kern_api.list().json()
        self.assertEqual(kernels, [])

        # Request to delete a non-existent kernel id
        bad_id = "111-111-111-111-111"
        with assert_http_error(404, "Kernel does not exist: " + bad_id):
            self.kern_api.shutdown(bad_id)
Пример #2
0
    def test_kernel_handler(self):
        # GET kernel with given id
        kid = self.kern_api.start().json()['id']
        r = self.kern_api.get(kid)
        kern1 = r.json()
        self.assertEqual(r.status_code, 200)
        assert isinstance(kern1, dict)
        self.assertIn('id', kern1)
        self.assertEqual(kern1['id'], kid)

        # Request a bad kernel id and check that a JSON
        # message is returned!
        bad_id = '111-111-111-111-111'
        with assert_http_error(404, 'Kernel does not exist: ' + bad_id):
            self.kern_api.get(bad_id)

        # DELETE kernel with id
        r = self.kern_api.shutdown(kid)
        self.assertEqual(r.status_code, 204)
        kernels = self.kern_api.list().json()
        self.assertEqual(kernels, [])

        # Request to delete a non-existent kernel id
        bad_id = '111-111-111-111-111'
        with assert_http_error(404, 'Kernel does not exist: ' + bad_id):
            self.kern_api.shutdown(bad_id)
Пример #3
0
    def test_save(self):
        resp = self.nb_api.read('a.ipynb', 'foo')
        nbcontent = json.loads(resp.text)['content']
        nb = to_notebook_json(nbcontent)
        ws = new_worksheet()
        nb.worksheets = [ws]
        ws.cells.append(new_heading_cell(u'Created by test ³'))

        nbmodel= {'name': 'a.ipynb', 'path':'foo', 'content': nb}
        resp = self.nb_api.save('a.ipynb', path='foo', body=json.dumps(nbmodel))

        nbfile = pjoin(self.notebook_dir.name, 'foo', 'a.ipynb')
        with io.open(nbfile, 'r', encoding='utf-8') as f:
            newnb = read(f, format='ipynb')
        self.assertEqual(newnb.worksheets[0].cells[0].source,
                         u'Created by test ³')
        nbcontent = self.nb_api.read('a.ipynb', 'foo').json()['content']
        newnb = to_notebook_json(nbcontent)
        self.assertEqual(newnb.worksheets[0].cells[0].source,
                         u'Created by test ³')

        # Save and rename
        nbmodel= {'name': 'a2.ipynb', 'path':'foo/bar', 'content': nb}
        resp = self.nb_api.save('a.ipynb', path='foo', body=json.dumps(nbmodel))
        saved = resp.json()
        self.assertEqual(saved['name'], 'a2.ipynb')
        self.assertEqual(saved['path'], 'foo/bar')
        assert os.path.isfile(pjoin(self.notebook_dir.name,'foo','bar','a2.ipynb'))
        assert not os.path.isfile(pjoin(self.notebook_dir.name, 'foo', 'a.ipynb'))
        with assert_http_error(404):
            self.nb_api.read('a.ipynb', 'foo')
Пример #4
0
    def test_save(self):
        resp = self.nb_api.read('a.ipynb', 'foo')
        nbcontent = json.loads(resp.text)['content']
        nb = to_notebook_json(nbcontent)
        ws = new_worksheet()
        nb.worksheets = [ws]
        ws.cells.append(new_heading_cell(u'Created by test ³'))

        nbmodel = {'name': 'a.ipynb', 'path': 'foo', 'content': nb}
        resp = self.nb_api.save(
            'a.ipynb', path='foo', body=json.dumps(nbmodel))

        nbfile = pjoin(self.notebook_dir.name, 'foo', 'a.ipynb')
        with io.open(nbfile, 'r', encoding='utf-8') as f:
            newnb = read(f, format='ipynb')
        self.assertEqual(newnb.worksheets[0].cells[0].source,
                         u'Created by test ³')
        nbcontent = self.nb_api.read('a.ipynb', 'foo').json()['content']
        newnb = to_notebook_json(nbcontent)
        self.assertEqual(newnb.worksheets[0].cells[0].source,
                         u'Created by test ³')

        # Save and rename
        nbmodel = {'name': 'a2.ipynb', 'path': 'foo/bar', 'content': nb}
        resp = self.nb_api.save(
            'a.ipynb', path='foo', body=json.dumps(nbmodel))
        saved = resp.json()
        self.assertEqual(saved['name'], 'a2.ipynb')
        self.assertEqual(saved['path'], 'foo/bar')
        assert os.path.isfile(
            pjoin(self.notebook_dir.name, 'foo', 'bar', 'a2.ipynb'))
        assert not os.path.isfile(
            pjoin(self.notebook_dir.name, 'foo', 'a.ipynb'))
        with assert_http_error(404):
            self.nb_api.read('a.ipynb', 'foo')
Пример #5
0
    def test_save(self):
        resp = self.api.read('a.ipynb', 'foo')
        nbcontent = json.loads(resp.text)['content']
        nb = from_dict(nbcontent)
        nb.cells.append(new_markdown_cell(u'Created by test ³'))

        nbmodel= {'name': 'a.ipynb', 'path':'foo', 'content': nb, 'type': 'notebook'}
        resp = self.api.save('a.ipynb', path='foo', body=json.dumps(nbmodel))

        nbfile = pjoin(self.notebook_dir.name, 'foo', 'a.ipynb')
        with io.open(nbfile, 'r', encoding='utf-8') as f:
            newnb = read(f, as_version=4)
        self.assertEqual(newnb.cells[0].source,
                         u'Created by test ³')
        nbcontent = self.api.read('a.ipynb', 'foo').json()['content']
        newnb = from_dict(nbcontent)
        self.assertEqual(newnb.cells[0].source,
                         u'Created by test ³')

        # Save and rename
        nbmodel= {'name': 'a2.ipynb', 'path':'foo/bar', 'content': nb, 'type': 'notebook'}
        resp = self.api.save('a.ipynb', path='foo', body=json.dumps(nbmodel))
        saved = resp.json()
        self.assertEqual(saved['name'], 'a2.ipynb')
        self.assertEqual(saved['path'], 'foo/bar')
        assert os.path.isfile(pjoin(self.notebook_dir.name,'foo','bar','a2.ipynb'))
        assert not os.path.isfile(pjoin(self.notebook_dir.name, 'foo', 'a.ipynb'))
        with assert_http_error(404):
            self.api.read('a.ipynb', 'foo')
Пример #6
0
    def test_get_text_file_contents(self):
        for d, name in self.dirs_nbs:
            path = url_path_join(d, name + '.txt')
            model = self.api.read(path).json()
            self.assertEqual(model['name'], u'%s.txt' % name)
            self.assertEqual(model['path'], path)
            self.assertIn('content', model)
            self.assertEqual(model['format'], 'text')
            self.assertEqual(model['type'], 'file')
            self.assertEqual(model['content'], self._txt_for_name(name))

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.api.read('foo/q.txt')

        # Specifying format=text should fail on a non-UTF-8 file
        with assert_http_error(400):
            self.api.read('foo/bar/baz.blob', type='file', format='text')
Пример #7
0
    def test_get_text_file_contents(self):
        for d, name in self.dirs_nbs:
            path = url_path_join(d, name + ".txt")
            model = self.api.read(path).json()
            self.assertEqual(model["name"], u"%s.txt" % name)
            self.assertEqual(model["path"], path)
            self.assertIn("content", model)
            self.assertEqual(model["format"], "text")
            self.assertEqual(model["type"], "file")
            self.assertEqual(model["content"], self._txt_for_name(name))

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.api.read("foo/q.txt")

        # Specifying format=text should fail on a non-UTF-8 file
        with assert_http_error(400):
            self.api.read("foo/bar/baz.blob", type_="file", format="text")
Пример #8
0
    def test_get_text_file_contents(self):
        for d, name in self.dirs_nbs:
            path = url_path_join(d, name + '.txt')
            model = self.api.read(path).json()
            self.assertEqual(model['name'], u'%s.txt' % name)
            self.assertEqual(model['path'], path)
            self.assertIn('content', model)
            self.assertEqual(model['format'], 'text')
            self.assertEqual(model['type'], 'file')
            self.assertEqual(model['content'], self._txt_for_name(name))

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.api.read('foo/q.txt')

        # Specifying format=text should fail on a non-UTF-8 file
        with assert_http_error(400):
            self.api.read('foo/bar/baz.blob', type='file', format='text')
Пример #9
0
    def test_get_contents(self):
        for d, name in self.dirs_nbs:
            nb = self.nb_api.read('%s.ipynb' % name, d+'/').json()
            self.assertEqual(nb['name'], u'%s.ipynb' % name)
            self.assertIn('content', nb)
            self.assertIn('metadata', nb['content'])
            self.assertIsInstance(nb['content']['metadata'], dict)

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.nb_api.read('q.ipynb', 'foo')
Пример #10
0
    def test_get_contents(self):
        for d, name in self.dirs_nbs:
            nb = self.nb_api.read('%s.ipynb' % name, d + '/').json()
            self.assertEqual(nb['name'], u'%s.ipynb' % name)
            self.assertIn('content', nb)
            self.assertIn('metadata', nb['content'])
            self.assertIsInstance(nb['content']['metadata'], dict)

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.nb_api.read('q.ipynb', 'foo')
Пример #11
0
    def test_get_text_file_contents(self):
        for d, name in self.dirs_nbs:
            model = self.api.read(u'%s.txt' % name, d + '/').json()
            self.assertEqual(model['name'], u'%s.txt' % name)
            self.assertIn('content', model)
            self.assertEqual(model['format'], 'text')
            self.assertEqual(model['type'], 'file')
            self.assertEqual(model['content'], self._txt_for_name(name))

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.api.read('q.txt', 'foo')
Пример #12
0
    def test_delete(self):
        newsession = self.sess_api.create('foo/nb1.ipynb').json()
        sid = newsession['id']

        resp = self.sess_api.delete(sid)
        self.assertEqual(resp.status_code, 204)

        sessions = self.sess_api.list().json()
        self.assertEqual(sessions, [])

        with assert_http_error(404):
            self.sess_api.get(sid)
Пример #13
0
    def test_get_text_file_contents(self):
        for d, name in self.dirs_nbs:
            model = self.api.read(u'%s.txt' % name, d+'/').json()
            self.assertEqual(model['name'], u'%s.txt' % name)
            self.assertIn('content', model)
            self.assertEqual(model['format'], 'text')
            self.assertEqual(model['type'], 'file')
            self.assertEqual(model['content'], self._txt_for_name(name))

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.api.read('q.txt', 'foo')
Пример #14
0
    def test_delete(self):
        newsession = self.sess_api.create("foo/nb1.ipynb").json()
        sid = newsession["id"]

        resp = self.sess_api.delete(sid)
        self.assertEqual(resp.status_code, 204)

        sessions = self.sess_api.list().json()
        self.assertEqual(sessions, [])

        with assert_http_error(404):
            self.sess_api.get(sid)
Пример #15
0
    def test_get_binary_file_contents(self):
        for d, name in self.dirs_nbs:
            model = self.api.read(u'%s.blob' % name, d+'/').json()
            self.assertEqual(model['name'], u'%s.blob' % name)
            self.assertIn('content', model)
            self.assertEqual(model['format'], 'base64')
            self.assertEqual(model['type'], 'file')
            b64_data = base64.encodestring(self._blob_for_name(name)).decode('ascii')
            self.assertEqual(model['content'], b64_data)

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.api.read('q.txt', 'foo')
Пример #16
0
    def test_get_binary_file_contents(self):
        for d, name in self.dirs_nbs:
            model = self.api.read(u'%s.blob' % name, d + '/').json()
            self.assertEqual(model['name'], u'%s.blob' % name)
            self.assertIn('content', model)
            self.assertEqual(model['format'], 'base64')
            self.assertEqual(model['type'], 'file')
            b64_data = base64.encodestring(
                self._blob_for_name(name)).decode('ascii')
            self.assertEqual(model['content'], b64_data)

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.api.read('q.txt', 'foo')
Пример #17
0
    def test_get_binary_file_contents(self):
        for d, name in self.dirs_nbs:
            path = url_path_join(d, name + ".blob")
            model = self.api.read(path).json()
            self.assertEqual(model["name"], u"%s.blob" % name)
            self.assertEqual(model["path"], path)
            self.assertIn("content", model)
            self.assertEqual(model["format"], "base64")
            self.assertEqual(model["type"], "file")
            b64_data = base64.encodestring(self._blob_for_name(name)).decode("ascii")
            self.assertEqual(model["content"], b64_data)

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.api.read("foo/q.txt")
Пример #18
0
    def test_get_binary_file_contents(self):
        for d, name in self.dirs_nbs:
            path = url_path_join(d, name + '.blob')
            model = self.api.read(path).json()
            self.assertEqual(model['name'], u'%s.blob' % name)
            self.assertEqual(model['path'], path)
            self.assertIn('content', model)
            self.assertEqual(model['format'], 'base64')
            self.assertEqual(model['type'], 'file')
            self.assertEqual(
                base64.decodestring(model['content'].encode('ascii')),
                self._blob_for_name(name),
            )

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.api.read('foo/q.txt')
Пример #19
0
    def test_get_binary_file_contents(self):
        for d, name in self.dirs_nbs:
            path = url_path_join(d, name + '.blob')
            model = self.api.read(path).json()
            self.assertEqual(model['name'], u'%s.blob' % name)
            self.assertEqual(model['path'], path)
            self.assertIn('content', model)
            self.assertEqual(model['format'], 'base64')
            self.assertEqual(model['type'], 'file')
            self.assertEqual(
                base64.decodestring(model['content'].encode('ascii')),
                self._blob_for_name(name),
            )

        # Name that doesn't exist - should be a 404
        with assert_http_error(404):
            self.api.read('foo/q.txt')
Пример #20
0
 def test_rename_existing(self):
     with assert_http_error(409):
         self.nb_api.rename('a.ipynb', 'foo', 'b.ipynb')
Пример #21
0
 def test_rename_existing(self):
     with assert_http_error(409):
         self.api.rename("foo/a.ipynb", "foo/b.ipynb")
Пример #22
0
 def test_get_nonexistant_kernelspec(self):
     with assert_http_error(404):
         self.ks_api.kernel_spec_info('nonexistant')
Пример #23
0
 def test_mkdir_hidden_400(self):
     with assert_http_error(400):
         resp = self.api.mkdir(u'å b/.hidden')
Пример #24
0
    def test_get_bad_type(self):
        with assert_http_error(400):
            self.api.read(u"unicodé", type_="file")  # this is a directory

        with assert_http_error(400):
            self.api.read(u"unicodé/innonascii.ipynb", type_="directory")
Пример #25
0
 def test_copy_put_400(self):
     with assert_http_error(400):
         resp = self.api.copy_put(u'å b/ç d.ipynb', u'å b/cøpy.ipynb')
 def test_get_nonexistant_kernelspec(self):
     with assert_http_error(404):
         self.ks_api.kernel_spec_info('nonexistant')
Пример #27
0
    def test_get_bad_type(self):
        with assert_http_error(400):
            self.api.read(u'unicodé', type='file')  # this is a directory

        with assert_http_error(400):
            self.api.read(u'unicodé/innonascii.ipynb', type='directory')
Пример #28
0
 def test_copy_dir_400(self):
     # can't copy directories
     with assert_http_error(400):
         resp = self.api.copy(u'å b', u'foo')
Пример #29
0
 def test_get_contents_no_such_file(self):
     # Name that doesn't exist - should be a 404
     with assert_http_error(404):
         self.api.read('foo/q.ipynb')
 def test_from_file_404(self):
     with assert_http_error(404):
         self.nbconvert_api.from_file('html', 'foo', 'thisdoesntexist.ipynb')
 def test_from_file_404(self):
     with assert_http_error(404):
         self.nbconvert_api.from_file("html", "foo", "thisdoesntexist.ipynb")
Пример #32
0
    def test_get_nonexistant_resource(self):
        with assert_http_error(404):
            self.ks_api.kernel_resource('nonexistant', 'resource.txt')

        with assert_http_error(404):
            self.ks_api.kernel_resource('sample', 'nonexistant.txt')
Пример #33
0
 def test_copy_dir_400(self):
     # can't copy directories
     with assert_http_error(400):
         resp = self.api.copy(u"å b", u"foo")
Пример #34
0
 def test_list_nonexistant_dir(self):
     with assert_http_error(404):
         self.api.list("nonexistant")
 def test_from_file_404(self):
     with assert_http_error(404):
         self.nbconvert_api.from_file('html', 'foo',
                                      'thisdoesntexist.ipynb')
Пример #36
0
    def test_get_bad_type(self):
        with assert_http_error(400):
            self.api.read(u'unicodé', type='file')  # this is a directory

        with assert_http_error(400):
            self.api.read(u'unicodé/innonascii.ipynb', type='directory')
 def test_get_nonexistant_resource(self):
     with assert_http_error(404):
         self.ks_api.kernel_resource('nonexistant', 'resource.txt')
     
     with assert_http_error(404):
         self.ks_api.kernel_resource('sample', 'nonexistant.txt')
Пример #38
0
 def test_copy_dir_400(self):
     # can't copy directories
     with assert_http_error(400):
         resp = self.api.copy(u'å b', u'foo')
Пример #39
0
 def test_list_nonexistant_dir(self):
     with assert_http_error(404):
         self.nb_api.list('nonexistant')
Пример #40
0
 def test_delete_non_empty_dir(self):
     """delete non-empty dir raises 400"""
     with assert_http_error(400):
         self.api.delete(u'å b')
Пример #41
0
 def test_rename_existing(self):
     with assert_http_error(409):
         self.nb_api.rename('a.ipynb', 'foo', 'b.ipynb')
Пример #42
0
 def test_list_nonexistant_dir(self):
     with assert_http_error(404):
         self.nb_api.list('nonexistant')
Пример #43
0
 def test_mkdir_hidden_400(self):
     with assert_http_error(400):
         resp = self.api.mkdir(u'å b/.hidden')
Пример #44
0
 def test_delete_non_empty_dir(self):
     """delete non-empty dir raises 400"""
     with assert_http_error(400):
         self.api.delete(u'å b')
Пример #45
0
 def test_get_contents_no_such_file(self):
     # Name that doesn't exist - should be a 404
     with assert_http_error(404):
         self.api.read('foo/q.ipynb')
Пример #46
0
 def test_copy_put_400(self):
     with assert_http_error(400):
         resp = self.api.copy_put(u'å b/ç d.ipynb', u'å b/cøpy.ipynb')