@@ -3,16 +3,19 @@ package staticbackend
3
3
import (
4
4
"io"
5
5
"net/http"
6
+ "net/url"
7
+ "strings"
6
8
"testing"
7
9
8
10
"github.com/staticbackendhq/core/internal"
9
11
)
10
12
11
13
func TestFunctionsExecuteDBOperations (t * testing.T ) {
12
14
code := `
13
- log("works here");
14
15
function handle(body) {
16
+ log(body);
15
17
var o = {
18
+ from: body.from,
16
19
desc: "yep",
17
20
done: false,
18
21
subobj: {
@@ -31,6 +34,10 @@ func TestFunctionsExecuteDBOperations(t *testing.T) {
31
34
log("ERROR: getting doc by id");
32
35
log(getRes.content);
33
36
return;
37
+ } else if (getRes.content.from != "val from unit test") {
38
+ log("ERROR: asserting data from request body");
39
+ log(getRes.content);
40
+ return;
34
41
}
35
42
36
43
var updata = getRes.content;
@@ -52,6 +59,7 @@ func TestFunctionsExecuteDBOperations(t *testing.T) {
52
59
if (qres.content.results.length != 1) {
53
60
log("ERROR");
54
61
log("expected results to have 1 doc, got: " + qres.content.results.length);
62
+ log(qres);
55
63
return;
56
64
}
57
65
@@ -81,7 +89,10 @@ func TestFunctionsExecuteDBOperations(t *testing.T) {
81
89
t .Errorf ("add: expected status 200 got %s" , addResp .Status )
82
90
}
83
91
84
- execResp := dbReq (t , funexec .exec , "POST" , "/" , data , true )
92
+ val := url.Values {}
93
+ val .Add ("from" , "val from unit test" )
94
+
95
+ execResp := dbReq (t , funexec .exec , "POST" , "/fn/exec/unittest" , val , false , true )
85
96
if execResp .StatusCode != http .StatusOK {
86
97
b , err := io .ReadAll (execResp .Body )
87
98
if err != nil {
@@ -92,4 +103,32 @@ func TestFunctionsExecuteDBOperations(t *testing.T) {
92
103
t .Log (string (b ))
93
104
t .Errorf ("expected status 200 got %s" , execResp .Status )
94
105
}
106
+
107
+ infoResp := dbReq (t , funexec .info , "GET" , "/fn/info/unittest" , nil , true )
108
+
109
+ var checkFn internal.ExecData
110
+ if err := parseBody (infoResp .Body , & checkFn ); err != nil {
111
+ t .Fatal (err )
112
+ }
113
+ defer infoResp .Body .Close ()
114
+
115
+ var errorLines []string
116
+ foundError := false
117
+ for _ , h := range checkFn .History {
118
+ for _ , line := range h .Output {
119
+ if strings .Index (line , "ERROR" ) > - 1 {
120
+ errorLines = h .Output
121
+ foundError = true
122
+ break
123
+ }
124
+ }
125
+
126
+ if foundError {
127
+ break
128
+ }
129
+ }
130
+
131
+ if foundError {
132
+ t .Errorf ("found error in function exec log: %v" , errorLines )
133
+ }
95
134
}
0 commit comments