python_feature_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package integrationtests_test
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. "github.com/langgenius/dify-sandbox/internal/core/runner/types"
  7. "github.com/langgenius/dify-sandbox/internal/service"
  8. )
  9. func TestPythonBase64(t *testing.T) {
  10. // Test case for base64
  11. runMultipleTestings(t, 50, func(t *testing.T) {
  12. resp := service.RunPython3Code(`
  13. import base64
  14. print(base64.b64decode(base64.b64encode(b"hello world")).decode())
  15. `, "", &types.RunnerOptions{
  16. EnableNetwork: true,
  17. })
  18. if resp.Code != 0 {
  19. t.Fatal(resp)
  20. }
  21. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  22. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  23. }
  24. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") {
  25. t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  26. }
  27. })
  28. }
  29. func TestPythonJSON(t *testing.T) {
  30. runMultipleTestings(t, 50, func(t *testing.T) {
  31. // Test case for json
  32. resp := service.RunPython3Code(`
  33. import json
  34. print(json.dumps({"hello": "world"}))
  35. `, "", &types.RunnerOptions{
  36. EnableNetwork: true,
  37. })
  38. if resp.Code != 0 {
  39. t.Fatal(resp)
  40. }
  41. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  42. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  43. }
  44. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello": "world"}`) {
  45. t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  46. }
  47. })
  48. }
  49. func TestPythonRequests(t *testing.T) {
  50. // Test case for http
  51. runMultipleTestings(t, 1, func(t *testing.T) {
  52. resp := service.RunPython3Code(`
  53. import requests
  54. print(requests.get("https://www.bilibili.com").content)
  55. `, "", &types.RunnerOptions{
  56. EnableNetwork: true,
  57. })
  58. if resp.Code != 0 {
  59. t.Fatal(resp)
  60. }
  61. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  62. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  63. }
  64. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "bilibili") {
  65. t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  66. }
  67. })
  68. }
  69. func TestPythonHttpx(t *testing.T) {
  70. // Test case for http
  71. runMultipleTestings(t, 1, func(t *testing.T) {
  72. resp := service.RunPython3Code(`
  73. import httpx
  74. print(httpx.get("https://www.bilibili.com").content)
  75. `, "", &types.RunnerOptions{
  76. EnableNetwork: true,
  77. })
  78. if resp.Code != 0 {
  79. t.Fatal(resp)
  80. }
  81. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  82. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  83. }
  84. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "bilibili") {
  85. t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  86. }
  87. })
  88. }
  89. func TestPythonTimezone(t *testing.T) {
  90. // Test case for time
  91. runMultipleTestings(t, 1, func(t *testing.T) {
  92. resp := service.RunPython3Code(`
  93. from datetime import datetime
  94. from zoneinfo import ZoneInfo
  95. print(datetime.now(ZoneInfo("Asia/Shanghai")).isoformat())
  96. `, "", &types.RunnerOptions{
  97. EnableNetwork: true,
  98. })
  99. if resp.Code != 0 {
  100. t.Fatal(resp)
  101. }
  102. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  103. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  104. }
  105. stdout := resp.Data.(*service.RunCodeResponse).Stdout
  106. // trim \n
  107. stdout = strings.TrimSpace(stdout)
  108. // check if stdout match time format
  109. _, err := time.Parse("2006-01-02T15:04:05.000000+08:00", stdout)
  110. if err != nil {
  111. t.Fatalf("unexpected output: %s, error: %v\n", stdout, err)
  112. }
  113. })
  114. }