##// END OF EJS Templates
Update session tests
Jessica B. Hamrick -
Show More
@@ -1,191 +1,203
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'services/kernels/js/kernel',
8 'services/kernels/js/kernel',
9 ], function(IPython, $, utils, kernel) {
9 ], function(IPython, $, utils, kernel) {
10 "use strict";
10 "use strict";
11
11
12 var Session = function (options) {
12 var Session = function (options) {
13 this.id = null;
13 this.id = null;
14 this.notebook_model = {
14 this.notebook_model = {
15 name: options.notebook_name,
15 name: options.notebook_name,
16 path: options.notebook_path
16 path: options.notebook_path
17 };
17 };
18 this.kernel_model = {
18 this.kernel_model = {
19 id: null,
19 id: null,
20 name: options.kernel_name
20 name: options.kernel_name
21 };
21 };
22
22
23 this.base_url = options.base_url;
23 this.base_url = options.base_url;
24 this.ws_url = options.ws_url;
24 this.ws_url = options.ws_url;
25 this.session_service_url = utils.url_join_encode(this.base_url, 'api/sessions');
25 this.session_service_url = utils.url_join_encode(this.base_url, 'api/sessions');
26 this.session_url = null;
26 this.session_url = null;
27
27
28 this.notebook = options.notebook;
28 this.notebook = options.notebook;
29 this.kernel = null;
29 this.kernel = null;
30 this.events = options.events;
30 this.events = options.events;
31 };
31 };
32
32
33 /**
33 /**
34 * GET /api/sessions
34 * GET /api/sessions
35 */
35 */
36 Session.prototype.list = function (success, error) {
36 Session.prototype.list = function (success, error) {
37 $.ajax(this.session_service_url, {
37 $.ajax(this.session_service_url, {
38 processData: false,
38 processData: false,
39 cache: false,
39 cache: false,
40 type: "GET",
40 type: "GET",
41 dataType: "json",
41 dataType: "json",
42 success: success,
42 success: success,
43 error: this._on_error(error)
43 error: this._on_error(error)
44 });
44 });
45 };
45 };
46
46
47 /**
47 /**
48 * POST /api/sessions
48 * POST /api/sessions
49 */
49 */
50 Session.prototype.start = function (success, error) {
50 Session.prototype.start = function (success, error) {
51 if (this.kernel !== null) {
52 throw new Error("session has already been started");
53 };
54
51 var that = this;
55 var that = this;
52 var on_success = function (data, status, xhr) {
56 var on_success = function (data, status, xhr) {
53 var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels");
57 var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels");
54 that.kernel = new kernel.Kernel(
58 that.kernel = new kernel.Kernel(
55 kernel_service_url, that.ws_url, that.notebook,
59 kernel_service_url, that.ws_url, that.notebook,
56 that.kernel_model.id, that.kernel_model.name);
60 that.kernel_model.id, that.kernel_model.name);
57 that.kernel._kernel_started(data.kernel);
61 that.kernel._kernel_started(data.kernel);
58 if (success) {
62 if (success) {
59 success(data, status, xhr);
63 success(data, status, xhr);
60 }
64 }
61 };
65 };
62 var on_error = function (xhr, status, err) {
66 var on_error = function (xhr, status, err) {
63 that.events.trigger('status_dead.Kernel');
67 that.events.trigger('status_dead.Kernel');
64 if (error) {
68 if (error) {
65 error(xhr, status, err);
69 error(xhr, status, err);
66 }
70 }
67 };
71 };
68
72
69 $.ajax(this.session_service_url, {
73 $.ajax(this.session_service_url, {
70 processData: false,
74 processData: false,
71 cache: false,
75 cache: false,
72 type: "POST",
76 type: "POST",
73 data: JSON.stringify(this._get_model()),
77 data: JSON.stringify(this._get_model()),
74 dataType: "json",
78 dataType: "json",
75 success: this._on_success(on_success),
79 success: this._on_success(on_success),
76 error: this._on_error(on_error)
80 error: this._on_error(on_error)
77 });
81 });
78 };
82 };
79
83
80 /**
84 /**
81 * GET /api/sessions/[:session_id]
85 * GET /api/sessions/[:session_id]
82 */
86 */
83 Session.prototype.get_info = function (success, error) {
87 Session.prototype.get_info = function (success, error) {
84 $.ajax(this.session_url, {
88 $.ajax(this.session_url, {
85 processData: false,
89 processData: false,
86 cache: false,
90 cache: false,
87 type: "GET",
91 type: "GET",
88 dataType: "json",
92 dataType: "json",
89 success: this._on_success(success),
93 success: this._on_success(success),
90 error: this._on_error(error)
94 error: this._on_error(error)
91 });
95 });
92 };
96 };
93
97
94 /**
98 /**
95 * PATCH /api/sessions/[:session_id]
99 * PATCH /api/sessions/[:session_id]
96 */
100 */
97 Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) {
101 Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) {
102 if (notebook_name !== undefined) {
98 this.notebook_model.name = notebook_name;
103 this.notebook_model.name = notebook_name;
104 }
105 if (notebook_path !== undefined) {
99 this.notebook_model.path = notebook_path;
106 this.notebook_model.path = notebook_path;
107 }
108 if (kernel_name !== undefined) {
100 this.kernel_model.name = kernel_name;
109 this.kernel_model.name = kernel_name;
110 }
111
112 console.log(JSON.stringify(this._get_model()));
101
113
102 $.ajax(this.session_url, {
114 $.ajax(this.session_url, {
103 processData: false,
115 processData: false,
104 cache: false,
116 cache: false,
105 type: "PATCH",
117 type: "PATCH",
106 data: JSON.stringify(this._get_model()),
118 data: JSON.stringify(this._get_model()),
107 dataType : "json",
119 dataType: "json",
108 success: this._on_success(success),
120 success: this._on_success(success),
109 error: this._on_error(error)
121 error: this._on_error(error)
110 });
122 });
111 };
123 };
112
124
113 Session.prototype.rename_notebook = function (name, path, success, error) {
125 Session.prototype.rename_notebook = function (name, path, success, error) {
114 this.change(name, path, this.kernel_model.name, success, error);
126 this.change(name, path, undefined, success, error);
115 };
127 };
116
128
117 /**
129 /**
118 * DELETE /api/sessions/[:session_id]
130 * DELETE /api/sessions/[:session_id]
119 */
131 */
120 Session.prototype.delete = function (success, error) {
132 Session.prototype.delete = function (success, error) {
121 if (this.kernel) {
133 if (this.kernel) {
122 this.kernel._kernel_dead();
134 this.kernel._kernel_dead();
123 }
135 }
124
136
125 $.ajax(this.session_url, {
137 $.ajax(this.session_url, {
126 processData: false,
138 processData: false,
127 cache: false,
139 cache: false,
128 type: "DELETE",
140 type: "DELETE",
129 dataType: "json",
141 dataType: "json",
130 success: this._on_success(success),
142 success: this._on_success(success),
131 error: this._on_error(error)
143 error: this._on_error(error)
132 });
144 });
133 };
145 };
134
146
135 Session.prototype._get_model = function () {
147 Session.prototype._get_model = function () {
136 return {
148 return {
137 notebook: this.notebook_model,
149 notebook: this.notebook_model,
138 kernel: this.kernel_model
150 kernel: this.kernel_model
139 };
151 };
140 };
152 };
141
153
142 Session.prototype._update_model = function (data) {
154 Session.prototype._update_model = function (data) {
143 if (data && data.id) {
155 if (data && data.id) {
144 this.id = data.id;
156 this.id = data.id;
145 this.session_url = utils.url_join_encode(this.session_service_url, this.id);
157 this.session_url = utils.url_join_encode(this.session_service_url, this.id);
146 }
158 }
147 if (data && data.notebook) {
159 if (data && data.notebook) {
148 this.notebook_model.name = data.notebook.name;
160 this.notebook_model.name = data.notebook.name;
149 this.notebook_model.path = data.notebook.path;
161 this.notebook_model.path = data.notebook.path;
150 }
162 }
151 if (data && data.kernel) {
163 if (data && data.kernel) {
152 this.kernel_model.name = data.kernel.name;
164 this.kernel_model.name = data.kernel.name;
153 this.kernel_model.id = data.kernel.id;
165 this.kernel_model.id = data.kernel.id;
154 }
166 }
155 };
167 };
156
168
157 Session.prototype._on_success = function (success) {
169 Session.prototype._on_success = function (success) {
158 var that = this;
170 var that = this;
159 return function (data, status, xhr) {
171 return function (data, status, xhr) {
160 that._update_model(data);
172 that._update_model(data);
161 if (success) {
173 if (success) {
162 success(data, status, xhr);
174 success(data, status, xhr);
163 }
175 }
164 };
176 };
165 };
177 };
166
178
167 Session.prototype._on_error = function (error) {
179 Session.prototype._on_error = function (error) {
168 return function (xhr, status, err) {
180 return function (xhr, status, err) {
169 utils.log_ajax_error(xhr, status, err);
181 utils.log_ajax_error(xhr, status, err);
170 if (error) {
182 if (error) {
171 error(xhr, status, err);
183 error(xhr, status, err);
172 }
184 }
173 };
185 };
174 };
186 };
175
187
176
188
177 var SessionAlreadyStarting = function (message) {
189 var SessionAlreadyStarting = function (message) {
178 this.name = "SessionAlreadyStarting";
190 this.name = "SessionAlreadyStarting";
179 this.message = (message || "");
191 this.message = (message || "");
180 };
192 };
181
193
182 SessionAlreadyStarting.prototype = Error.prototype;
194 SessionAlreadyStarting.prototype = Error.prototype;
183
195
184 // For backwards compatability.
196 // For backwards compatability.
185 IPython.Session = Session;
197 IPython.Session = Session;
186
198
187 return {
199 return {
188 Session: Session,
200 Session: Session,
189 SessionAlreadyStarting: SessionAlreadyStarting
201 SessionAlreadyStarting: SessionAlreadyStarting
190 };
202 };
191 });
203 });
@@ -1,24 +1,110
1
1
2 //
2 //
3 // Tests for the Session object
3 // Tests for the Session object
4 //
4 //
5
5
6 casper.notebook_test(function () {
6 casper.notebook_test(function () {
7 var that = this;
8 var get_info = function () {
9 return that.evaluate(function () {
10 return JSON.parse(JSON.stringify(IPython.notebook.session._get_model()));
11 });
12 };
13
14 // test that the kernel is running
7 this.then(function () {
15 this.then(function () {
8 this.test.assert(this.kernel_running(), 'session: kernel is running');
16 this.test.assert(this.kernel_running(), 'session: kernel is running');
9 });
17 });
10
18
19 // test list
11 this.thenEvaluate(function () {
20 this.thenEvaluate(function () {
12 IPython.notebook.session.delete();
21 IPython._sessions = null;
22 IPython.notebook.session.list(function (data) {
23 IPython._sessions = data;
24 });
25 });
26 this.waitFor(function () {
27 return this.evaluate(function () {
28 return IPython._sessions !== null;
29 });
30 });
31 this.then(function () {
32 var num_sessions = this.evaluate(function () {
33 return IPython._sessions.length;
34 });
35 this.test.assertEquals(num_sessions, 1, 'one session running');
13 });
36 });
14
37
38 // test get_info
39 var session_info = get_info();
40 this.thenEvaluate(function () {
41 IPython._session_info = null;
42 IPython.notebook.session.get_info(function (data) {
43 IPython._session_info = data;
44 });
45 });
15 this.waitFor(function () {
46 this.waitFor(function () {
16 return this.evaluate(function () {
47 return this.evaluate(function () {
17 return IPython.notebook.kernel.is_fully_disconnected();
48 return IPython._session_info !== null;
18 });
49 });
19 });
50 });
51 this.then(function () {
52 var new_session_info = this.evaluate(function () {
53 return IPython._session_info;
54 });
55 this.test.assertEquals(session_info.notebook.name, new_session_info.notebook.name, 'session: notebook name correct');
56 this.test.assertEquals(session_info.notebook.path, new_session_info.notebook.path, 'session: notebook path correct');
57 this.test.assertEquals(session_info.kernel.name, new_session_info.kernel.name, 'session: kernel name correct');
58 this.test.assertEquals(session_info.kernel.id, new_session_info.kernel.id, 'session: kernel id correct');
59 });
60
61 // test rename_notebook
62 //
63 // TODO: the PATCH request isn't supported by phantom, so this test always
64 // fails, see https://github.com/ariya/phantomjs/issues/11384
65 // when this is fixed we can properly run this test
66 //
67 // this.thenEvaluate(function () {
68 // IPython._renamed = false;
69 // IPython.notebook.session.rename_notebook(
70 // "foo",
71 // "bar",
72 // function (data) {
73 // IPython._renamed = true;
74 // }
75 // );
76 // });
77 // this.waitFor(function () {
78 // return this.evaluate(function () {
79 // return IPython._renamed;
80 // });
81 // });
82 // this.then(function () {
83 // var info = get_info();
84 // this.test.assertEquals(info.notebook.name, "foo", "notebook was renamed");
85 // this.test.assertEquals(info.notebook.path, "bar", "notebook path was changed");
86 // });
20
87
88 // test delete
89 this.thenEvaluate(function () {
90 IPython.notebook.session.delete();
91 });
92 this.waitFor(function () {
93 return this.evaluate(function () {
94 return IPython.notebook.kernel.is_fully_disconnected();
95 });
96 });
21 this.then(function () {
97 this.then(function () {
22 this.test.assert(!this.kernel_running(), 'session deletes kernel');
98 this.test.assert(!this.kernel_running(), 'session deletes kernel');
23 });
99 });
100
101 // test start after delete -- should throw an error
102 this.then(function () {
103 var start = function () {
104 this.evaluate(function () {
105 IPython.notebook.session.start();
106 });
107 };
108 this.test.assertRaises(start, [], 'error raised on start after delete');
109 });
24 });
110 });
General Comments 0
You need to be logged in to leave comments. Login now