python_feature_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package integrationtests_test
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/langgenius/dify-sandbox/internal/core/runner/types"
  6. "github.com/langgenius/dify-sandbox/internal/service"
  7. )
  8. func TestPythonBase64(t *testing.T) {
  9. // Test case for base64
  10. resp := service.RunPython3Code(`
  11. import base64
  12. print(base64.b64decode(base64.b64encode(b"hello world")).decode())
  13. `, "", &types.RunnerOptions{
  14. EnableNetwork: true,
  15. })
  16. if resp.Code != 0 {
  17. t.Error(resp)
  18. }
  19. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") {
  20. t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  21. }
  22. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  23. t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  24. }
  25. }
  26. func TestPythonJSON(t *testing.T) {
  27. // Test case for json
  28. resp := service.RunPython3Code(`
  29. import json
  30. print(json.dumps({"hello": "world"}))
  31. `, "", &types.RunnerOptions{
  32. EnableNetwork: true,
  33. })
  34. if resp.Code != 0 {
  35. t.Error(resp)
  36. }
  37. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello": "world"}`) {
  38. t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  39. }
  40. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  41. t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  42. }
  43. }
  44. func TestPythonHttp(t *testing.T) {
  45. // Test case for http
  46. resp := service.RunPython3Code(`
  47. import requests
  48. print(requests.get("https://www.bilibili.com").content)
  49. `, "", &types.RunnerOptions{
  50. EnableNetwork: true,
  51. })
  52. if resp.Code != 0 {
  53. t.Error(resp)
  54. }
  55. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "bilibili") {
  56. t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  57. }
  58. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  59. t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  60. }
  61. }