-
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
440d1f2
commit b89f3fe
Showing
6 changed files
with
176 additions
and
5 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
37 changes: 37 additions & 0 deletions
37
kernel/kotlin/src/main/java/com/twosigma/beakerx/kotlin/autotranslation/NSClientProxy.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,37 @@ | ||
/* | ||
* 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.kotlin.autotranslation; | ||
|
||
import com.twosigma.beakerx.BeakerXClient; | ||
import com.twosigma.beakerx.BeakerXClientManager; | ||
|
||
public class NSClientProxy { | ||
|
||
public static NSClientProxy API = new NSClientProxy(); | ||
private BeakerXClient client; | ||
|
||
private NSClientProxy() { | ||
client = BeakerXClientManager.get(); | ||
} | ||
|
||
public Object get(String key) { | ||
return client.get(key); | ||
} | ||
|
||
public Object set(String key, Object value) { | ||
return client.set(key, value); | ||
} | ||
} |
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
117 changes: 117 additions & 0 deletions
117
...el/kotlin/src/test/java/com/twosigma/beakerx/kotlin/kernel/KotlinAutotranslationTest.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,117 @@ | ||
/* | ||
* 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.kotlin.kernel; | ||
|
||
import com.twosigma.beakerx.BeakerXCommRepositoryMock; | ||
import com.twosigma.beakerx.DefaultBeakerXJsonSerializer; | ||
import com.twosigma.beakerx.KernelSetUpFixtureTest; | ||
import com.twosigma.beakerx.NamespaceClient; | ||
import com.twosigma.beakerx.NamespaceClientTest; | ||
import com.twosigma.beakerx.kernel.CloseKernelAction; | ||
import com.twosigma.beakerx.kernel.EvaluatorParameters; | ||
import com.twosigma.beakerx.kernel.KernelFunctionality; | ||
import com.twosigma.beakerx.kernel.KernelSocketsFactory; | ||
import com.twosigma.beakerx.kernel.comm.Comm; | ||
import com.twosigma.beakerx.kotlin.evaluator.KotlinEvaluator; | ||
import com.twosigma.beakerx.message.Message; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
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 KotlinAutotranslationTest extends KernelSetUpFixtureTest { | ||
|
||
private NamespaceClientTest.AutotranslationServiceTestImpl autotranslationService; | ||
|
||
@Override | ||
protected KernelFunctionality createKernel(String sessionId, KernelSocketsFactory kernelSocketsFactory, CloseKernelAction closeKernelAction) { | ||
autotranslationService = new NamespaceClientTest.AutotranslationServiceTestImpl(); | ||
NamespaceClient nc = new NamespaceClient(autotranslationService, new DefaultBeakerXJsonSerializer(), new BeakerXCommRepositoryMock()); | ||
KotlinEvaluator evaluator = new KotlinEvaluator(sessionId, sessionId, cellExecutor(), getTestTempFolderFactory(), getEvaluatorParameters(), nc); | ||
return new Kotlin(sessionId, evaluator, kernelSocketsFactory, closeKernelAction, getCacheFolderFactory(), new BeakerXCommRepositoryMock()); | ||
} | ||
|
||
private EvaluatorParameters getEvaluatorParameters() { | ||
KotlinDefaultVariables defaultVariables = new KotlinDefaultVariables(); | ||
HashMap kernelParameters = new HashMap(); | ||
kernelParameters.put(defaultVariables.IMPORTS, defaultVariables.getImports()); | ||
return new EvaluatorParameters(kernelParameters); | ||
} | ||
|
||
@Test | ||
public void getAndSetAutotranslation() throws InterruptedException { | ||
//given | ||
String code = "beakerx[\"bar\"] = mapOf(\"name\" to \"John\", \"lastName\" to \"Smith\", \"age\" to \"32\")\n" + | ||
"(beakerx[\"bar\"] as Map<String, String>)[\"name\"]"; | ||
String value = "John"; | ||
//when | ||
Message message = getExecuteRequestMessage(code); | ||
kernelSocketsService.handleMsg(message); | ||
//then | ||
Optional<Message> idleMessage = waitForIdleMessage(kernelSocketsService.getKernelSockets()); | ||
assertThat(idleMessage).isPresent(); | ||
Message result = waitForResult(kernelSocketsService.getKernelSockets()).get(); | ||
verifyResultValue(result, value); | ||
} | ||
|
||
@Test | ||
public void kotlinAutotranslationSetValue() throws InterruptedException { | ||
//given | ||
String code = "beakerx[\"bar\"] = mapOf(\"key\" to \"value\")"; | ||
String serializedMap = "{\"type\":\"TableDisplay\",\"columnNames\":[\"Key\",\"Value\"],\"values\":[[\"key\",\"value\"]],\"subtype\":\"Dictionary\"}"; | ||
//when | ||
Message message = getExecuteRequestMessage(code); | ||
kernelSocketsService.handleMsg(message); | ||
//then | ||
Optional<Message> idleMessage = waitForIdleMessage(kernelSocketsService.getKernelSockets()); | ||
assertThat(idleMessage).isPresent(); | ||
String result = autotranslationService.get("bar"); | ||
assertThat(result).isEqualTo(serializedMap); | ||
} | ||
|
||
@Test | ||
public void kotlinAutotranslationGetValue() throws InterruptedException { | ||
//given | ||
String code = "(beakerx[\"bar\"] as Map<String, String>)[\"key\"]"; | ||
String serializedMap = "{\"type\":\"TableDisplay\",\"columnNames\":[\"Key\",\"Value\"],\"values\":[[\"key\",\"value\"]],\"subtype\":\"Dictionary\"}"; | ||
String value = "value"; | ||
//when | ||
autotranslationService.update("bar", serializedMap); | ||
Message message = getExecuteRequestMessage(code); | ||
kernelSocketsService.handleMsg(message); | ||
//then | ||
Optional<Message> idleMessage = waitForIdleMessage(kernelSocketsService.getKernelSockets()); | ||
assertThat(idleMessage).isPresent(); | ||
Message result = waitForResult(kernelSocketsService.getKernelSockets()).get(); | ||
verifyResultValue(result, value); | ||
|
||
} | ||
|
||
private void verifyResultValue(Message message, String value) { | ||
Map actual = ((Map) message.getContent().get(Comm.DATA)); | ||
String result = (String) actual.get("text/plain"); | ||
assertThat(result).isEqualTo(value); | ||
} | ||
} |