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.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
94
95
96
97
  1. import dedent from 'dedent';
  2. import * as fs from 'fs';
  3. import {
  4. processPythonFile,
  5. pythonPromptFunction,
  6. pythonPromptFunctionLegacy,
  7. } from '../../../src/prompts/processors/python';
  8. jest.mock('fs');
  9. jest.mock('../../../src/prompts/processors/python', () => {
  10. const actual = jest.requireActual('../../../src/prompts/processors/python');
  11. return {
  12. ...actual,
  13. pythonPromptFunction: jest.fn(),
  14. pythonPromptFunctionLegacy: jest.fn(),
  15. };
  16. });
  17. describe('processPythonFile', () => {
  18. const mockReadFileSync = jest.mocked(fs.readFileSync);
  19. it('should process a valid Python file without a function name or label', () => {
  20. const filePath = 'file.py';
  21. const fileContent = 'print("Hello, world!")';
  22. mockReadFileSync.mockReturnValue(fileContent);
  23. jest.mocked(pythonPromptFunctionLegacy).mockResolvedValueOnce('mocked result');
  24. expect(processPythonFile(filePath, {}, undefined)).toEqual([
  25. {
  26. function: expect.any(Function),
  27. label: `${filePath}: ${fileContent}`,
  28. raw: fileContent,
  29. },
  30. ]);
  31. });
  32. it('should process a valid Python file with a function name without a label', () => {
  33. const filePath = 'file.py';
  34. const fileContent = dedent`
  35. def testFunction(context):
  36. print("Hello, world!")`;
  37. mockReadFileSync.mockReturnValue(fileContent);
  38. jest.mocked(pythonPromptFunction).mockResolvedValueOnce('mocked result');
  39. expect(processPythonFile(filePath, {}, 'testFunction')).toEqual([
  40. {
  41. function: expect.any(Function),
  42. raw: fileContent,
  43. label: `file.py:testFunction`,
  44. },
  45. ]);
  46. });
  47. it('should process a valid Python file with a label without a function name', () => {
  48. const filePath = 'file.py';
  49. const fileContent = 'print("Hello, world!")';
  50. mockReadFileSync.mockReturnValue(fileContent);
  51. jest.mocked(pythonPromptFunctionLegacy).mockResolvedValueOnce('mocked result');
  52. expect(processPythonFile(filePath, { label: 'Label' }, undefined)).toEqual([
  53. {
  54. function: expect.any(Function),
  55. label: `Label`,
  56. raw: fileContent,
  57. },
  58. ]);
  59. });
  60. it('should process a valid Python file with a label and function name', () => {
  61. const filePath = 'file.py';
  62. const fileContent = dedent`
  63. def testFunction(context):
  64. print("Hello, world!")`;
  65. mockReadFileSync.mockReturnValue(fileContent);
  66. jest.mocked(pythonPromptFunction).mockResolvedValueOnce('mocked result');
  67. expect(processPythonFile(filePath, { label: 'Label' }, 'testFunction')).toEqual([
  68. {
  69. function: expect.any(Function),
  70. label: `Label`,
  71. raw: fileContent,
  72. },
  73. ]);
  74. });
  75. it('should process a valid Python file with config', () => {
  76. const filePath = 'file.py';
  77. const fileContent = 'print("Hello, world!")';
  78. mockReadFileSync.mockReturnValue(fileContent);
  79. jest.mocked(pythonPromptFunctionLegacy).mockResolvedValueOnce('mocked result');
  80. const config = { key: 'value' };
  81. expect(processPythonFile(filePath, { config }, undefined)).toEqual([
  82. {
  83. function: expect.any(Function),
  84. label: `${filePath}: ${fileContent}`,
  85. raw: fileContent,
  86. config: { key: 'value' },
  87. },
  88. ]);
  89. });
  90. });
Tip!

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

Comments

Loading...