Skip to content

Commit b261a62

Browse files
authored
feat: Optimize testing.http (#788)
* feat: Optimize testing.http * add Bind method * chore: update mocks * add test cases * chore: update mocks * optimize test --------- Co-authored-by: hwbrzzl <hwbrzzl@users.noreply.github.com>
1 parent 8c0a04a commit b261a62

8 files changed

Lines changed: 271 additions & 99 deletions

File tree

contracts/testing/test_request.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@ import (
66
)
77

88
type TestRequest interface {
9+
Get(uri string) (TestResponse, error)
10+
Post(uri string, body io.Reader) (TestResponse, error)
11+
Put(uri string, body io.Reader) (TestResponse, error)
12+
Delete(uri string, body io.Reader) (TestResponse, error)
13+
Patch(uri string, body io.Reader) (TestResponse, error)
14+
Head(uri string) (TestResponse, error)
15+
Options(uri string) (TestResponse, error)
16+
17+
Bind(value any) TestRequest
918
FlushHeaders() TestRequest
19+
WithBasicAuth(username, password string) TestRequest
20+
WithContext(ctx context.Context) TestRequest
1021
WithCookies(cookies map[string]string) TestRequest
1122
WithCookie(key, value string) TestRequest
12-
WithContext(ctx context.Context) TestRequest
13-
WithHeaders(headers map[string]string) TestRequest
1423
WithHeader(key, value string) TestRequest
24+
WithHeaders(headers map[string]string) TestRequest
1525
WithoutHeader(key string) TestRequest
1626
WithToken(token string, ttype ...string) TestRequest
17-
WithBasicAuth(username, password string) TestRequest
1827
WithoutToken() TestRequest
1928
WithSession(attributes map[string]any) TestRequest
20-
Get(uri string) (TestResponse, error)
21-
Post(uri string, body io.Reader) (TestResponse, error)
22-
Put(uri string, body io.Reader) (TestResponse, error)
23-
Patch(uri string, body io.Reader) (TestResponse, error)
24-
Delete(uri string, body io.Reader) (TestResponse, error)
25-
Head(uri string) (TestResponse, error)
26-
Options(uri string) (TestResponse, error)
2729
}

mocks/testing/TestRequest.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testing/assertable_json.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package testing
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"testing"
76

testing/assertable_json_test.go

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,72 @@ package testing
33
import (
44
"testing"
55

6-
"github.com/stretchr/testify/assert"
6+
"github.com/stretchr/testify/suite"
77

88
contractstesting "github.com/goravel/framework/contracts/testing"
99
)
1010

11-
func TestNewAssertableJSON(t *testing.T) {
11+
type AssertableJsonTestSuite struct {
12+
suite.Suite
13+
}
14+
15+
func TestAssertableJsonTestSuite(t *testing.T) {
16+
json = &testJson{}
17+
18+
suite.Run(t, new(AssertableJsonTestSuite))
19+
}
20+
21+
func (s *AssertableJsonTestSuite) SetupTest() {
22+
23+
}
24+
25+
func (s *AssertableJsonTestSuite) TearDownSuite() {
26+
json = nil
27+
}
28+
29+
func (s *AssertableJsonTestSuite) TestNewAssertableJSON() {
1230
validJSON := `{"key1": "value1", "key2": [1, 2, 3]}`
1331
invalidJSON := `{"key1": "value1", "key2": [1, 2, 3]`
1432

15-
assertable, err := NewAssertableJSON(t, validJSON)
16-
assert.NoError(t, err)
17-
assert.NotNil(t, assertable)
33+
assertable, err := NewAssertableJSON(s.T(), validJSON)
34+
s.NoError(err)
35+
s.NotNil(assertable)
1836

19-
assertable, err = NewAssertableJSON(t, invalidJSON)
20-
assert.Error(t, err)
21-
assert.Nil(t, assertable)
37+
assertable, err = NewAssertableJSON(s.T(), invalidJSON)
38+
s.Error(err)
39+
s.Nil(assertable)
2240
}
2341

24-
func TestCount(t *testing.T) {
42+
func (s *AssertableJsonTestSuite) TestCount() {
2543
jsonStr := `{"items": [1, 2, 3], "otherKey": "value"}`
26-
assertable, err := NewAssertableJSON(t, jsonStr)
27-
assert.NoError(t, err)
44+
assertable, err := NewAssertableJSON(s.T(), jsonStr)
45+
s.NoError(err)
2846

2947
assertable.Count("items", 3)
3048

3149
//assertable.Count("items", 4)
3250
}
3351

34-
func TestHas(t *testing.T) {
52+
func (s *AssertableJsonTestSuite) TestHas() {
3553
jsonStr := `{
3654
"key1": "value1",
3755
"key2": [1, 2, 3],
3856
"nested": {"deep": "value"},
3957
"nullKey": null
4058
}`
41-
assertable, err := NewAssertableJSON(t, jsonStr)
42-
assert.NoError(t, err)
59+
assertable, err := NewAssertableJSON(s.T(), jsonStr)
60+
s.NoError(err)
4361

4462
assertable.Has("key1")
4563
assertable.Has("nullKey")
4664

4765
//assertable.Has("nonExistingKey")
4866
}
4967

50-
func TestHasAll(t *testing.T) {
68+
func (s *AssertableJsonTestSuite) TestHasAll() {
5169
jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}`
52-
assertable, err := NewAssertableJSON(t, jsonStr)
53-
assert.NoError(t, err)
70+
assertable, err := NewAssertableJSON(s.T(), jsonStr)
71+
s.NoError(err)
5472

5573
// Test all keys exist
5674
assertable.HasAll([]string{"key1", "key2"})
@@ -59,10 +77,10 @@ func TestHasAll(t *testing.T) {
5977
//assertable.HasAll([]string{"key1", "nonExistingKey"})
6078
}
6179

62-
func TestHasAny(t *testing.T) {
80+
func (s *AssertableJsonTestSuite) TestHasAny() {
6381
jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}`
64-
assertable, err := NewAssertableJSON(t, jsonStr)
65-
assert.NoError(t, err)
82+
assertable, err := NewAssertableJSON(s.T(), jsonStr)
83+
s.NoError(err)
6684

6785
// Test at least one key exists
6886
assertable.HasAny([]string{"key1", "key2"})
@@ -71,10 +89,10 @@ func TestHasAny(t *testing.T) {
7189
//assertable.HasAny([]string{"nonExistingKey1", "nonExistingKey2"})
7290
}
7391

74-
func TestMissing(t *testing.T) {
92+
func (s *AssertableJsonTestSuite) TestMissing() {
7593
jsonStr := `{"key1": "value1"}`
76-
assertable, err := NewAssertableJSON(t, jsonStr)
77-
assert.NoError(t, err)
94+
assertable, err := NewAssertableJSON(s.T(), jsonStr)
95+
s.NoError(err)
7896

7997
// Test key is missing
8098
assertable.Missing("nonExistingKey")
@@ -83,10 +101,10 @@ func TestMissing(t *testing.T) {
83101
//assertable.Missing("key1")
84102
}
85103

86-
func TestMissingAll(t *testing.T) {
104+
func (s *AssertableJsonTestSuite) TestMissingAll() {
87105
jsonStr := `{"key1": "value1"}`
88-
assertable, err := NewAssertableJSON(t, jsonStr)
89-
assert.NoError(t, err)
106+
assertable, err := NewAssertableJSON(s.T(), jsonStr)
107+
s.NoError(err)
90108

91109
// Test all keys are missing
92110
assertable.MissingAll([]string{"nonExistingKey1", "nonExistingKey2"})
@@ -95,7 +113,7 @@ func TestMissingAll(t *testing.T) {
95113
//assertable.MissingAll([]string{"key1"})
96114
}
97115

98-
func TestWhere(t *testing.T) {
116+
func (s *AssertableJsonTestSuite) TestWhere() {
99117
jsonStr := `{
100118
"key1": "value1",
101119
"intKey": 42,
@@ -104,8 +122,8 @@ func TestWhere(t *testing.T) {
104122
"objKey": {"nested": "value"},
105123
"arrayKey": [1, 2, 3]
106124
}`
107-
assertable, err := NewAssertableJSON(t, jsonStr)
108-
assert.NoError(t, err)
125+
assertable, err := NewAssertableJSON(s.T(), jsonStr)
126+
s.NoError(err)
109127

110128
// Test correct value
111129
assertable.Where("key1", "value1")
@@ -123,10 +141,10 @@ func TestWhere(t *testing.T) {
123141
//assertable.Where("key1", "wrongValue")
124142
}
125143

126-
func TestWhereNot(t *testing.T) {
144+
func (s *AssertableJsonTestSuite) TestWhereNot() {
127145
jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}`
128-
assertable, err := NewAssertableJSON(t, jsonStr)
129-
assert.NoError(t, err)
146+
assertable, err := NewAssertableJSON(s.T(), jsonStr)
147+
s.NoError(err)
130148

131149
// Test value is not as expected
132150
assertable.WhereNot("key1", "wrongValue")
@@ -135,11 +153,11 @@ func TestWhereNot(t *testing.T) {
135153
//assertable.WhereNot("key1", "value1")
136154
}
137155

138-
func TestFirst(t *testing.T) {
156+
func (s *AssertableJsonTestSuite) TestFirst() {
139157
jsonStr := `{"items": [{"id": 1}, {"id": 2}]}`
140158

141-
assertable, err := NewAssertableJSON(t, jsonStr)
142-
assert.NoError(t, err)
159+
assertable, err := NewAssertableJSON(s.T(), jsonStr)
160+
s.NoError(err)
143161

144162
// Test fetching the first item
145163
assertable.First("items", func(item contractstesting.AssertableJSON) {
@@ -155,11 +173,11 @@ func TestFirst(t *testing.T) {
155173
//emptyAssertable.First("items", func(item contractstesting.AssertableJSON) {})
156174
}
157175

158-
func TestHasWithScope(t *testing.T) {
176+
func (s *AssertableJsonTestSuite) TestHasWithScope() {
159177
jsonStr := `{"items": [{"id": 1}, {"id": 2}]}`
160178

161-
assertable, err := NewAssertableJSON(t, jsonStr)
162-
assert.NoError(t, err)
179+
assertable, err := NewAssertableJSON(s.T(), jsonStr)
180+
s.NoError(err)
163181

164182
// Test has with correct length
165183
assertable.HasWithScope("items", 2, func(item contractstesting.AssertableJSON) {
@@ -173,34 +191,35 @@ func TestHasWithScope(t *testing.T) {
173191
//assertable.HasWithScope("nonExistingKey", 0, func(item contractstesting.AssertableJSON) {})
174192
}
175193

176-
func TestEach(t *testing.T) {
194+
func (s *AssertableJsonTestSuite) TestEach() {
177195
jsonStr := `{
178196
"items": [{"id": 1}, {"id": 2}],
179197
"mixedTypes": [42, "string", {"key": "value"}],
180198
"nonArray": "value"
181199
}`
182200

183-
assertable, err := NewAssertableJSON(t, jsonStr)
184-
assert.NoError(t, err)
201+
assertable, err := NewAssertableJSON(s.T(), jsonStr)
202+
s.NoError(err)
185203

186204
// Test iterating over each item
187205
callCount := 0
188206
assertable.Each("items", func(item contractstesting.AssertableJSON) {
189207
item.Where("id", float64(callCount+1))
190208
callCount++
191209
})
192-
assert.Equal(t, 2, callCount)
210+
s.Equal(2, callCount)
193211

194212
// Test with a non-existing key
195213
//assertable.Each("nonExistingKey", func(item contractstesting.AssertableJSON) {})
196214

197215
// Test with an empty array
198216
emptyJsonStr := `{"items": []}`
199-
emptyAssertable, err := NewAssertableJSON(t, emptyJsonStr)
200-
assert.NoError(t, err)
217+
emptyAssertable, err := NewAssertableJSON(s.T(), emptyJsonStr)
218+
s.NoError(err)
219+
201220
emptyCallCount := 0
202221
emptyAssertable.Each("items", func(item contractstesting.AssertableJSON) {
203222
emptyCallCount++
204223
})
205-
assert.Equal(t, 0, emptyCallCount)
224+
s.Equal(0, emptyCallCount)
206225
}

testing/service_provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
const Binding = "goravel.testing"
1313

1414
var (
15+
json foundation.Json
1516
artisanFacade contractsconsole.Artisan
1617
routeFacade contractsroute.Route
1718
sessionFacade contractsession.Manager
@@ -41,4 +42,6 @@ func (receiver *ServiceProvider) Boot(app foundation.Application) {
4142
if sessionFacade == nil {
4243
color.Errorln(errors.SessionFacadeNotSet.SetModule(errors.ModuleTesting))
4344
}
45+
46+
json = app.GetJson()
4447
}

0 commit comments

Comments
 (0)