@@ -27,15 +27,19 @@ import (
2727 "github.com/stretchr/testify/assert"
2828 "github.com/stretchr/testify/require"
2929
30+ "github.com/funlessdev/fl-cli/test/mocks"
3031 openapi "github.com/funlessdev/fl-client-sdk-go"
3132)
3233
3334func TestFnInvoke (t * testing.T ) {
34- testFn := "test-fn "
35- testNs := "test-ns "
35+ testFn := "test_fn "
36+ testNs := "test_ns "
3637 var testArgs map [string ]interface {} = map [string ]interface {}{"name" : "Some name" }
3738
3839 testCtx := context .Background ()
40+ mockValidator := mocks .NewInputValidatorHandler (t )
41+ mockValidator .On ("ValidateName" , testFn , "function" ).Return (nil )
42+ mockValidator .On ("ValidateName" , testNs , "mod" ).Return (nil )
3943
4044 t .Run ("should send invoke request to server" , func (t * testing.T ) {
4145 server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
@@ -49,13 +53,16 @@ func TestFnInvoke(t *testing.T) {
4953 defer server .Close ()
5054
5155 c , _ := NewClient (http .DefaultClient , Config {Host : server .URL })
52- svc := & FnService {Client : c }
56+ svc := & FnService {Client : c , InputValidatorHandler : mockValidator }
5357
5458 result , err := svc .Invoke (testCtx , testFn , testNs , testArgs )
5559
5660 require .NoError (t , err )
5761 expected := map [string ]interface {}{"payload" : "some result" }
5862 assert .Equal (t , expected , result .GetData ())
63+
64+ mockValidator .AssertNumberOfCalls (t , "ValidateName" , 2 )
65+ mockValidator .AssertExpectations (t )
5966 })
6067
6168 t .Run ("should return error if request encounters an HTTP error" , func (t * testing.T ) {
@@ -72,7 +79,7 @@ func TestFnInvoke(t *testing.T) {
7279 defer server .Close ()
7380
7481 c , _ := NewClient (http .DefaultClient , Config {Host : server .URL })
75- svc := & FnService {Client : c }
82+ svc := & FnService {Client : c , InputValidatorHandler : mockValidator }
7683
7784 _ , err := svc .Invoke (testCtx , testFn , testNs , testArgs )
7885
@@ -83,12 +90,17 @@ func TestFnInvoke(t *testing.T) {
8390}
8491
8592func TestFnCreate (t * testing.T ) {
86- testFn := "test-fn "
87- testNs := "test-ns "
93+ testFn := "test_fn "
94+ testNs := "test_ns "
8895 testSource , _ := filepath .Abs ("../../test/fixtures/real.wasm" )
8996 testCode , _ := os .Open (testSource )
9097
9198 testCtx := context .Background ()
99+
100+ mockValidator := mocks .NewInputValidatorHandler (t )
101+ mockValidator .On ("ValidateName" , testFn , "function" ).Return (nil )
102+ mockValidator .On ("ValidateName" , testNs , "mod" ).Return (nil )
103+
92104 t .Run ("should send create request to server" , func (t * testing.T ) {
93105 server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
94106 assert .Equal (t , http .MethodPost , r .Method )
@@ -101,11 +113,14 @@ func TestFnCreate(t *testing.T) {
101113 defer server .Close ()
102114
103115 c , _ := NewClient (http .DefaultClient , Config {Host : server .URL })
104- svc := & FnService {Client : c }
116+ svc := & FnService {Client : c , InputValidatorHandler : mockValidator }
105117
106118 err := svc .Create (testCtx , testFn , testNs , testCode )
107119
108120 require .NoError (t , err )
121+
122+ mockValidator .AssertNumberOfCalls (t , "ValidateName" , 2 )
123+ mockValidator .AssertExpectations (t )
109124 })
110125
111126 t .Run ("should return error if request encounters an HTTP error" , func (t * testing.T ) {
@@ -122,7 +137,7 @@ func TestFnCreate(t *testing.T) {
122137 defer server .Close ()
123138
124139 c , _ := NewClient (http .DefaultClient , Config {Host : server .URL })
125- svc := & FnService {Client : c }
140+ svc := & FnService {Client : c , InputValidatorHandler : mockValidator }
126141
127142 err := svc .Create (testCtx , testFn , testNs , testCode )
128143
@@ -133,11 +148,15 @@ func TestFnCreate(t *testing.T) {
133148}
134149
135150func TestFnDelete (t * testing.T ) {
136- testFn := "test-fn "
137- testNs := "test-ns "
151+ testFn := "test_fn "
152+ testNs := "test_ns "
138153
139154 testCtx := context .Background ()
140155
156+ mockValidator := mocks .NewInputValidatorHandler (t )
157+ mockValidator .On ("ValidateName" , testFn , "function" ).Return (nil )
158+ mockValidator .On ("ValidateName" , testNs , "mod" ).Return (nil )
159+
141160 t .Run ("should send delete request to server" , func (t * testing.T ) {
142161 server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
143162 assert .Equal (t , http .MethodDelete , r .Method )
@@ -150,11 +169,14 @@ func TestFnDelete(t *testing.T) {
150169 defer server .Close ()
151170
152171 c , _ := NewClient (http .DefaultClient , Config {Host : server .URL })
153- svc := & FnService {Client : c }
172+ svc := & FnService {Client : c , InputValidatorHandler : mockValidator }
154173
155174 err := svc .Delete (testCtx , testFn , testNs )
156175
157176 require .NoError (t , err )
177+
178+ mockValidator .AssertNumberOfCalls (t , "ValidateName" , 2 )
179+ mockValidator .AssertExpectations (t )
158180 })
159181
160182 t .Run ("should return error if request encounters an HTTP error" , func (t * testing.T ) {
@@ -171,7 +193,7 @@ func TestFnDelete(t *testing.T) {
171193 defer server .Close ()
172194
173195 c , _ := NewClient (http .DefaultClient , Config {Host : server .URL })
174- svc := & FnService {Client : c }
196+ svc := & FnService {Client : c , InputValidatorHandler : mockValidator }
175197
176198 err := svc .Delete (testCtx , testFn , testNs )
177199
0 commit comments