Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

pythonWrapper.test.ts 3.2 KB

You have to be logged in to leave a comment. Sign In
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
  1. import * as pythonWrapper from '../src/python/wrapper';
  2. import { promises as fs } from 'fs';
  3. import { PythonShell } from 'python-shell';
  4. jest.mock('../src/esm');
  5. jest.mock('python-shell');
  6. describe('Python Wrapper', () => {
  7. beforeEach(() => {
  8. jest.clearAllMocks();
  9. });
  10. describe('runPython', () => {
  11. it('should correctly run a Python script with provided arguments', async () => {
  12. jest.spyOn(fs, 'writeFile').mockResolvedValue();
  13. jest.spyOn(fs, 'unlink').mockResolvedValue();
  14. const mockPythonShellRun = PythonShell.run as jest.Mock;
  15. mockPythonShellRun.mockResolvedValue(['{"type": "final_result", "data": "test result"}']);
  16. const result = await pythonWrapper.runPython('testScript.py', 'testMethod', [
  17. 'arg1',
  18. { key: 'value' },
  19. ]);
  20. expect(result).toEqual('test result');
  21. expect(mockPythonShellRun).toHaveBeenCalledWith('wrapper.py', expect.any(Object));
  22. expect(mockPythonShellRun.mock.calls[0][1].args).toEqual([
  23. expect.stringContaining('testScript.py'),
  24. 'testMethod',
  25. expect.stringContaining('promptfoo-python-input-json'),
  26. ]);
  27. expect(fs.unlink).toHaveBeenCalledTimes(1);
  28. });
  29. it('should return an failure reason if the Python script execution fails', async () => {
  30. const mockPythonShellRun = PythonShell.run as jest.Mock;
  31. mockPythonShellRun.mockRejectedValue(new Error('Test Error'));
  32. const result = await pythonWrapper.runPython('testScript.py', 'testMethod', ['arg1']);
  33. expect(result).toEqual({
  34. pass: false,
  35. score: 0,
  36. reason: 'Failed to execute Python script: Test Error',
  37. });
  38. });
  39. it('should handle Python script returning incorrect result type', async () => {
  40. jest.spyOn(fs, 'writeFile').mockResolvedValue();
  41. jest.spyOn(fs, 'unlink').mockResolvedValue();
  42. const mockPythonShellRun = PythonShell.run as jest.Mock;
  43. mockPythonShellRun.mockResolvedValue([
  44. '{"type": "unexpected_result", "data": "test result"}',
  45. ]);
  46. await expect(
  47. pythonWrapper.runPython('testScript.py', 'testMethod', ['arg1']),
  48. ).rejects.toThrow(
  49. 'The Python script `call_api` function must return a dict with an `output`',
  50. );
  51. });
  52. });
  53. describe('runPythonCode', () => {
  54. it('should execute Python code from a string', async () => {
  55. const mockPythonShellRun = PythonShell.run as jest.Mock;
  56. mockPythonShellRun.mockResolvedValue([
  57. '{"type": "final_result", "data": "execution result"}',
  58. ]);
  59. jest.spyOn(fs, 'writeFile').mockResolvedValue();
  60. const code = 'print("Hello, world!")';
  61. const result = await pythonWrapper.runPythonCode(code, 'main', []);
  62. expect(result).toEqual('execution result');
  63. expect(mockPythonShellRun).toHaveBeenCalledWith('wrapper.py', expect.any(Object));
  64. expect(fs.writeFile).toHaveBeenCalledWith(expect.stringContaining('.py'), code);
  65. });
  66. it('should clean up the temporary file after execution', async () => {
  67. jest.spyOn(fs, 'writeFile').mockResolvedValue();
  68. jest.spyOn(fs, 'unlink').mockResolvedValue();
  69. await pythonWrapper.runPythonCode('print("cleanup test")', 'main', []);
  70. expect(fs.unlink).toHaveBeenCalledTimes(2);
  71. });
  72. });
  73. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...