python_feature_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. runMultipleTestings(t, 50, func(t *testing.T) {
  11. resp := service.RunPython3Code(`
  12. import base64
  13. print(base64.b64decode(base64.b64encode(b"hello world")).decode())
  14. `, "", &types.RunnerOptions{
  15. EnableNetwork: true,
  16. })
  17. if resp.Code != 0 {
  18. t.Fatal(resp)
  19. }
  20. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") {
  21. t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  22. }
  23. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  24. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  25. }
  26. })
  27. }
  28. func TestPythonJSON(t *testing.T) {
  29. runMultipleTestings(t, 50, func(t *testing.T) {
  30. // Test case for json
  31. resp := service.RunPython3Code(`
  32. import json
  33. print(json.dumps({"hello": "world"}))
  34. `, "", &types.RunnerOptions{
  35. EnableNetwork: true,
  36. })
  37. if resp.Code != 0 {
  38. t.Fatal(resp)
  39. }
  40. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello": "world"}`) {
  41. t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  42. }
  43. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  44. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  45. }
  46. })
  47. }
  48. func TestPythonHttp(t *testing.T) {
  49. // Test case for http
  50. runMultipleTestings(t, 10, func(t *testing.T) {
  51. resp := service.RunPython3Code(`
  52. import requests
  53. print(requests.get("https://www.bilibili.com").content)
  54. `, "", &types.RunnerOptions{
  55. EnableNetwork: true,
  56. })
  57. if resp.Code != 0 {
  58. t.Fatal(resp)
  59. }
  60. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "bilibili") {
  61. t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  62. }
  63. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  64. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  65. }
  66. })
  67. }