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

python.integration.test.ts 4.9 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
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
  1. import { exec } from 'child_process';
  2. import fs from 'fs';
  3. import path from 'path';
  4. import util from 'util';
  5. import dedent from 'dedent';
  6. import logger from '../../src/logger';
  7. import { runPython } from '../../src/python/pythonUtils';
  8. const execPromise = util.promisify(exec);
  9. describe('pythonUtils Integration Tests', () => {
  10. const scriptsDir = path.join(__dirname, 'scripts');
  11. beforeAll(() => {
  12. if (!fs.existsSync(scriptsDir)) {
  13. fs.mkdirSync(scriptsDir);
  14. }
  15. fs.writeFileSync(
  16. path.join(scriptsDir, 'simple.py'),
  17. dedent`
  18. import json
  19. import sys
  20. def main(*args):
  21. message = ' '.join(str(arg) for arg in args)
  22. return {
  23. 'message': message,
  24. 'success': True
  25. }
  26. def print_to_stdout(*args):
  27. message = ' '.join(str(arg) for arg in args)
  28. print(message)
  29. return main(*args)
  30. class TestClass:
  31. @classmethod
  32. def class_method(cls, *args):
  33. return main(*args)
  34. async def async_function(*args):
  35. return main(*args)
  36. `,
  37. );
  38. fs.writeFileSync(
  39. path.join(scriptsDir, 'with_imports.py'),
  40. dedent`
  41. import os
  42. import datetime
  43. def get_env_and_date():
  44. return {
  45. 'env': os.environ.get('TEST_ENV', 'not set'),
  46. 'date': str(datetime.datetime.now().date())
  47. }
  48. `,
  49. );
  50. });
  51. afterAll(() => {
  52. fs.rmSync(path.join(scriptsDir), { recursive: true, force: true });
  53. });
  54. it('should be able to call Python directly', async () => {
  55. const { stdout } = await execPromise('python --version');
  56. expect(stdout).toContain('Python');
  57. });
  58. it('should successfully run a simple Python script', async () => {
  59. const result = await runPython(path.join(scriptsDir, 'simple.py'), 'main', ['Hello, World!']);
  60. expect(result).toEqual({
  61. message: 'Hello, World!',
  62. success: true,
  63. });
  64. }, 10000);
  65. it('should handle multiple arguments', async () => {
  66. const result = await runPython(path.join(scriptsDir, 'simple.py'), 'main', [
  67. 'Multiple',
  68. 'Arguments',
  69. ]);
  70. expect(result).toEqual({
  71. message: 'Multiple Arguments',
  72. success: true,
  73. });
  74. });
  75. it('should handle empty string argument', async () => {
  76. const result = await runPython(path.join(scriptsDir, 'simple.py'), 'main', ['']);
  77. expect(result).toEqual({
  78. message: '',
  79. success: true,
  80. });
  81. });
  82. it('should handle non-string argument', async () => {
  83. const result = await runPython(path.join(scriptsDir, 'simple.py'), 'main', [123]);
  84. expect(result).toEqual({
  85. message: '123',
  86. success: true,
  87. });
  88. });
  89. it('should throw an error for non-existent script', async () => {
  90. const nonExistentPath = path.join(scriptsDir, 'non_existent.py');
  91. await expect(runPython(nonExistentPath, 'main', ['test'])).rejects.toThrow(expect.any(Error));
  92. });
  93. it('should handle Python script that prints to stdout', async () => {
  94. const result = await runPython(path.join(scriptsDir, 'simple.py'), 'print_to_stdout', [
  95. 'Print to stdout',
  96. ]);
  97. expect(result).toEqual({
  98. message: 'Print to stdout',
  99. success: true,
  100. });
  101. });
  102. it('should handle class methods', async () => {
  103. const result = await runPython(path.join(scriptsDir, 'simple.py'), 'TestClass.class_method', [
  104. 'Class method',
  105. ]);
  106. expect(result).toEqual({
  107. message: 'Class method',
  108. success: true,
  109. });
  110. });
  111. it('should handle async functions', async () => {
  112. const result = await runPython(path.join(scriptsDir, 'simple.py'), 'async_function', [
  113. 'Async function',
  114. ]);
  115. expect(result).toEqual({
  116. message: 'Async function',
  117. success: true,
  118. });
  119. });
  120. it('should handle scripts with imports', async () => {
  121. const result = await runPython(
  122. path.join(scriptsDir, 'with_imports.py'),
  123. 'get_env_and_date',
  124. [],
  125. );
  126. expect(result).toHaveProperty('env');
  127. expect(result).toHaveProperty('date');
  128. expect((result as any).env).toBe('not set');
  129. expect(new Date((result as any).date)).toBeInstanceOf(Date);
  130. });
  131. it('should handle scripts with environment variables', async () => {
  132. process.env.TEST_ENV = 'test_value';
  133. const result = await runPython(
  134. path.join(scriptsDir, 'with_imports.py'),
  135. 'get_env_and_date',
  136. [],
  137. );
  138. expect((result as any).env).toBe('test_value');
  139. delete process.env.TEST_ENV;
  140. });
  141. it('should log debug messages', async () => {
  142. jest.clearAllMocks();
  143. const result = await runPython(path.join(scriptsDir, 'simple.py'), 'main', ['Debug Test']);
  144. expect(result).toEqual({
  145. message: 'Debug Test',
  146. success: true,
  147. });
  148. expect(logger.debug).toHaveBeenCalledWith(
  149. expect.stringContaining('Running Python wrapper with args'),
  150. );
  151. expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('Python script'));
  152. });
  153. });
Tip!

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

Comments

Loading...