-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
fcb0abf
commit 2f90e83
Showing
4 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
kernel/scala/src/test/java/com/twosigma/beakerx/scala/ScalaAutotranslationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.twosigma.beakerx.scala; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.twosigma.beakerx.BeakerXCommRepositoryMock; | ||
import com.twosigma.beakerx.KernelSetUpFixtureTest; | ||
import com.twosigma.beakerx.NamespaceClient; | ||
import com.twosigma.beakerx.NamespaceClientTest; | ||
import com.twosigma.beakerx.evaluator.EvaluatorTest; | ||
import com.twosigma.beakerx.kernel.CloseKernelAction; | ||
import com.twosigma.beakerx.kernel.CustomMagicCommandsEmptyImpl; | ||
import com.twosigma.beakerx.kernel.Kernel; | ||
import com.twosigma.beakerx.kernel.KernelSocketsFactory; | ||
import com.twosigma.beakerx.kernel.comm.Comm; | ||
import com.twosigma.beakerx.message.Message; | ||
import com.twosigma.beakerx.scala.evaluator.BeakerxObjectFactoryImpl; | ||
import com.twosigma.beakerx.scala.evaluator.ScalaEvaluator; | ||
import com.twosigma.beakerx.scala.kernel.Scala; | ||
import com.twosigma.beakerx.scala.kernel.ScalaBeakerXJsonSerializer; | ||
import org.junit.Test; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static com.twosigma.beakerx.MessageFactoryTest.getExecuteRequestMessage; | ||
import static com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForIdleMessage; | ||
import static com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForResult; | ||
import static com.twosigma.beakerx.evaluator.EvaluatorTest.getCacheFolderFactory; | ||
import static com.twosigma.beakerx.evaluator.EvaluatorTest.getTestTempFolderFactory; | ||
import static com.twosigma.beakerx.evaluator.TestBeakerCellExecutor.cellExecutor; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ScalaAutotranslationTest extends KernelSetUpFixtureTest { | ||
|
||
private NamespaceClientTest.AutotranslationServiceTestImpl autotranslationService; | ||
|
||
@Override | ||
protected Kernel createKernel(String sessionId, KernelSocketsFactory kernelSocketsFactory, CloseKernelAction closeKernelAction) { | ||
autotranslationService = new NamespaceClientTest.AutotranslationServiceTestImpl(); | ||
NamespaceClient nc = new NamespaceClient(autotranslationService, new ScalaBeakerXJsonSerializer(), new BeakerXCommRepositoryMock()); | ||
|
||
ScalaEvaluator evaluator = new ScalaEvaluator( | ||
sessionId, | ||
sessionId, | ||
cellExecutor(), | ||
new BeakerxObjectFactoryImpl(), | ||
getTestTempFolderFactory(), | ||
EvaluatorTest.KERNEL_PARAMETERS, | ||
nc); | ||
return new Scala(sessionId, evaluator, kernelSocketsFactory, closeKernelAction, getCacheFolderFactory(), new CustomMagicCommandsEmptyImpl(), new BeakerXCommRepositoryMock()); | ||
} | ||
|
||
|
||
@Test | ||
public void transformFromJsonToScalaObject() throws Exception { | ||
//given | ||
autotranslationService.update("bar", list()); | ||
//when | ||
String code = "beakerx.bar"; | ||
Message message = getExecuteRequestMessage(code); | ||
kernelSocketsService.handleMsg(message); | ||
//then | ||
Optional<Message> idleMessage = waitForIdleMessage(kernelSocketsService.getKernelSockets()); | ||
assertThat(idleMessage).isPresent(); | ||
Optional<Message> result = waitForResult(kernelSocketsService.getKernelSockets()); | ||
verifyScalaObject(result.get()); | ||
} | ||
|
||
private void verifyScalaObject(Message message) { | ||
Map actual = ((Map) message.getContent().get(Comm.DATA)); | ||
String value = (String) actual.get("text/plain"); | ||
assertThat(value).isNotEmpty(); | ||
assertThat(value).contains("2"); | ||
} | ||
|
||
@Test | ||
public void transformFromJsonToScalaListHead() throws Exception { | ||
//given | ||
autotranslationService.update("bar", list()); | ||
//when | ||
String code = "beakerx.bar.as[List[Any]].head"; | ||
Message message = getExecuteRequestMessage(code); | ||
kernelSocketsService.handleMsg(message); | ||
//then | ||
Optional<Message> idleMessage = waitForIdleMessage(kernelSocketsService.getKernelSockets()); | ||
assertThat(idleMessage).isPresent(); | ||
Optional<Message> result = waitForResult(kernelSocketsService.getKernelSockets()); | ||
verifyScalaListHead(result.get()); | ||
} | ||
|
||
private void verifyScalaListHead(Message message) { | ||
Map actual = ((Map) message.getContent().get(Comm.DATA)); | ||
String value = (String) actual.get("text/plain"); | ||
assertThat(value).isNotEmpty(); | ||
assertThat(value).isEqualTo("1"); | ||
} | ||
|
||
|
||
private String list() { | ||
List<Integer> maps = new LinkedList<>(); | ||
maps.add(1); | ||
maps.add(2); | ||
maps.add(3); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
try { | ||
return objectMapper.writeValueAsString(maps); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(); | ||
} | ||
|
||
} | ||
|
||
|
||
} |
145 changes: 145 additions & 0 deletions
145
test/ipynb/scala/SupportScalaObjectInAutotranslation.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%javascript\n", | ||
"beakerx.bar = [23, 48, 55, 100];" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%%scala\n", | ||
"beakerx.bar.as[List[Int]].head" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%scala\n", | ||
"val list = beakerx.bar.as[List[Any]]\n", | ||
"list.head" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%scala\n", | ||
"beakerx.bar.as[List[Any]](1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%scala\n", | ||
"beakerx.bar.as[List[Any]].isEmpty" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%scala\n", | ||
"1 :: beakerx.bar.as[List[Any]]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%scala\n", | ||
"beakerx.bar.asInstanceOf[List[Any]].head" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%javascript\n", | ||
"beakerx.foo = 11;" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%scala\n", | ||
"beakerx.foo.as[Int] + 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%scala\n", | ||
"val cc = beakerx.foo.as[Int] \n", | ||
"cc + 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Groovy", | ||
"language": "groovy", | ||
"name": "groovy" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "groovy", | ||
"file_extension": ".groovy", | ||
"mimetype": "", | ||
"name": "Groovy", | ||
"nbconverter_exporter": "", | ||
"version": "2.4.3" | ||
}, | ||
"toc": { | ||
"base_numbering": 1, | ||
"nav_menu": {}, | ||
"number_sections": false, | ||
"sideBar": false, | ||
"skip_h1_title": false, | ||
"title_cell": "Table of Contents", | ||
"title_sidebar": "Contents", | ||
"toc_cell": false, | ||
"toc_position": {}, | ||
"toc_section_display": false, | ||
"toc_window_display": false | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |