forked from Submersible/node-python-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_typescript.ts
181 lines (152 loc) · 5.14 KB
/
test_typescript.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { test } from 'tap';
import { join as path_join } from 'path';
import { promisify } from 'bluebird';
import { pythonBridge, PythonException, isPythonException } from './index';
const mkdirTemp = promisify(require('temp').mkdir);
test('readme', t => {
t.test('example', async assert => {
const python = pythonBridge();
try {
await python.ex`import math`;
const x = await python`math.sqrt(9)`;
assert.equal(x, 3);
const list = [3, 4, 2, 1];
const sorted = await python`sorted(${list})`;
assert.deepEqual(sorted, list.sort());
} finally {
python.end();
}
});
t.test('expression', async assert => {
let python = pythonBridge();
try {
// Interpolates arguments using JSON serialization.
assert.deepEqual([1, 3, 4, 6], await python`sorted(${[6, 4, 1, 3]})`);
// Passing key-value arguments
const obj = {hello: 'world', foo: 'bar'};
assert.deepEqual(
{baz: 123, hello: 'world', foo: 'bar'},
await python`dict(baz=123, **${obj})`
);
} finally {
python.end();
}
});
t.test('execute', async assert => {
const python = pythonBridge();
try {
const a = 123, b = 321;
python.ex`
def hello(a, b):
return a + b
`;
assert.equal(a + b, await python`hello(${a}, ${b})`);
} finally {
python.end();
}
});
t.test('lock', async assert => {
const python = pythonBridge();
try {
const x: number = await python.lock(async python =>{
await python.ex`hello = 123`;
return await python`hello + 321`;
});
assert.equal(x, 444);
// Recommended to define function in Python
} finally {
python.end();
}
});
t.test('lock recommended', async assert => {
const python = pythonBridge();
try {
const x: number = await python.lock(async python =>{
await python.ex`hello = 123`;
return await python`hello + 321`;
});
assert.equal(x, 444);
} finally {
python.end();
}
});
t.test('stdout', async assert => {
const python = pythonBridge({stdio: ['pipe', 'pipe', process.stderr]})
try {
const tempdir = await mkdirTemp('node-python-bridge-test');
const OUTPUT = path_join(tempdir, 'output.txt');
const { delay, promisifyAll } = require('bluebird');
const { createWriteStream, readFileAsync } = promisifyAll(require('fs'));
const fileWriter = createWriteStream(OUTPUT);
python.stdout.pipe(fileWriter);
// listen on Python process's stdout
const stdinToStdout = python.ex`
import sys
for line in sys.stdin:
sys.stdout.write(line)
sys.stdout.flush()
`;
// write to Python process's stdin
python.stdin.write('hello\n');
await delay(10);
python.stdin.write('world\n');
// close python's stdin, and wait for python to finish writing
python.stdin.end();
await stdinToStdout;
// assert file contents is the same as what was written
const fileContents = await readFileAsync(OUTPUT, {encoding: 'utf8'});
assert.equal(fileContents.replace(/\r/g, ''), 'hello\nworld\n');
} finally {
python.end();
}
});
t.test('kill', async assert => {
let python = pythonBridge();
let {TimeoutError} = require('bluebird');
try {
await python.ex`
from time import sleep
sleep(9000)
`.timeout(100);
assert.ok(false); // should not reach this
} catch (e) {
if (e instanceof TimeoutError) {
python.kill('SIGKILL');
python = pythonBridge();
} else {
throw e;
}
}
python.end();
});
t.test('exceptions', async assert => {
let python = pythonBridge();
try {
await python.ex`
hello = 123
print(hello + world)
world = 321
`;
assert.ok(false);
} catch (e) {
assert.ok(e instanceof PythonException);
}
async function pyDivide(numerator, denominator) {
try {
await python`${numerator} / ${denominator}`;
} catch (e) {
if (isPythonException('ZeroDivisionError', e)) {
return Infinity;
}
throw e;
}
}
async function main() {
assert.equal(Infinity, await pyDivide(1, 0));
assert.equal(1 / 0, await pyDivide(1, 0));
}
await main();
python.end();
});
t.end();
});