Skip to content

Commit

Permalink
chore: move testing.http to a single folder (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl authored Jan 10, 2025
1 parent 634f3b3 commit bc907a5
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 134 deletions.
23 changes: 13 additions & 10 deletions testing/assertable_json.go → testing/http/assertable_json.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package testing
package http

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"

"github.com/goravel/framework/contracts/foundation"
contractstesting "github.com/goravel/framework/contracts/testing"
"github.com/goravel/framework/support/maps"
)

type AssertableJson struct {
t *testing.T
json string
json foundation.Json
jsonStr string
decoded map[string]any
}

func NewAssertableJSON(t *testing.T, jsonStr string) (contractstesting.AssertableJSON, error) {
func NewAssertableJSON(t *testing.T, json foundation.Json, jsonStr string) (contractstesting.AssertableJSON, error) {
var decoded map[string]any
err := json.Unmarshal([]byte(jsonStr), &decoded)
if err != nil {
Expand All @@ -25,7 +27,8 @@ func NewAssertableJSON(t *testing.T, jsonStr string) (contractstesting.Assertabl

return &AssertableJson{
t: t,
json: jsonStr,
json: json,
jsonStr: jsonStr,
decoded: decoded,
}, nil
}
Expand Down Expand Up @@ -111,9 +114,9 @@ func (r *AssertableJson) First(key string, callback func(contractstesting.Assert
}

firstItem := array[0]
itemJson, err := json.Marshal(firstItem)
itemJson, err := r.json.Marshal(firstItem)
if assert.NoError(r.t, err, "Failed to marshal the first item") {
newJson, err := NewAssertableJSON(r.t, string(itemJson))
newJson, err := NewAssertableJSON(r.t, r.json, string(itemJson))
if assert.NoError(r.t, err, "Failed to create AssertableJSON for first item") {
callback(newJson)
}
Expand All @@ -138,12 +141,12 @@ func (r *AssertableJson) HasWithScope(key string, length int, callback func(cont
}

if len(array) > 0 {
itemJson, err := json.Marshal(array[0])
itemJson, err := r.json.Marshal(array[0])
if !assert.NoError(r.t, err, "Failed to marshal the first item of array") {
return r
}

newJson, err := NewAssertableJSON(r.t, string(itemJson))
newJson, err := NewAssertableJSON(r.t, r.json, string(itemJson))
if !assert.NoError(r.t, err, "Failed to create AssertableJSON for first item in scoped array") {
return r
}
Expand All @@ -166,12 +169,12 @@ func (r *AssertableJson) Each(key string, callback func(contractstesting.Asserta
}

for _, item := range array {
itemJson, err := json.Marshal(item)
itemJson, err := r.json.Marshal(item)
if !assert.NoError(r.t, err) {
continue
}

newJson, err := NewAssertableJSON(r.t, string(itemJson))
newJson, err := NewAssertableJSON(r.t, r.json, string(itemJson))
if !assert.NoError(r.t, err) {
continue
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testing
package http

import (
"testing"
Expand All @@ -13,35 +13,28 @@ type AssertableJsonTestSuite struct {
}

func TestAssertableJsonTestSuite(t *testing.T) {
json = &testJson{}

suite.Run(t, new(AssertableJsonTestSuite))
}

func (s *AssertableJsonTestSuite) SetupTest() {

}

func (s *AssertableJsonTestSuite) TearDownSuite() {
json = nil
}

func (s *AssertableJsonTestSuite) TestNewAssertableJSON() {
validJSON := `{"key1": "value1", "key2": [1, 2, 3]}`
invalidJSON := `{"key1": "value1", "key2": [1, 2, 3]`

assertable, err := NewAssertableJSON(s.T(), validJSON)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, validJSON)
s.NoError(err)
s.NotNil(assertable)

assertable, err = NewAssertableJSON(s.T(), invalidJSON)
assertable, err = NewAssertableJSON(s.T(), &testJson{}, invalidJSON)
s.Error(err)
s.Nil(assertable)
}

func (s *AssertableJsonTestSuite) TestCount() {
jsonStr := `{"items": [1, 2, 3], "otherKey": "value"}`
assertable, err := NewAssertableJSON(s.T(), jsonStr)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr)
s.NoError(err)

assertable.Count("items", 3)
Expand All @@ -56,7 +49,7 @@ func (s *AssertableJsonTestSuite) TestHas() {
"nested": {"deep": "value"},
"nullKey": null
}`
assertable, err := NewAssertableJSON(s.T(), jsonStr)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr)
s.NoError(err)

assertable.Has("key1")
Expand All @@ -67,7 +60,7 @@ func (s *AssertableJsonTestSuite) TestHas() {

func (s *AssertableJsonTestSuite) TestHasAll() {
jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}`
assertable, err := NewAssertableJSON(s.T(), jsonStr)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr)
s.NoError(err)

// Test all keys exist
Expand All @@ -79,7 +72,7 @@ func (s *AssertableJsonTestSuite) TestHasAll() {

func (s *AssertableJsonTestSuite) TestHasAny() {
jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}`
assertable, err := NewAssertableJSON(s.T(), jsonStr)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr)
s.NoError(err)

// Test at least one key exists
Expand All @@ -91,7 +84,7 @@ func (s *AssertableJsonTestSuite) TestHasAny() {

func (s *AssertableJsonTestSuite) TestMissing() {
jsonStr := `{"key1": "value1"}`
assertable, err := NewAssertableJSON(s.T(), jsonStr)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr)
s.NoError(err)

// Test key is missing
Expand All @@ -103,7 +96,7 @@ func (s *AssertableJsonTestSuite) TestMissing() {

func (s *AssertableJsonTestSuite) TestMissingAll() {
jsonStr := `{"key1": "value1"}`
assertable, err := NewAssertableJSON(s.T(), jsonStr)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr)
s.NoError(err)

// Test all keys are missing
Expand All @@ -122,7 +115,7 @@ func (s *AssertableJsonTestSuite) TestWhere() {
"objKey": {"nested": "value"},
"arrayKey": [1, 2, 3]
}`
assertable, err := NewAssertableJSON(s.T(), jsonStr)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr)
s.NoError(err)

// Test correct value
Expand All @@ -143,7 +136,7 @@ func (s *AssertableJsonTestSuite) TestWhere() {

func (s *AssertableJsonTestSuite) TestWhereNot() {
jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}`
assertable, err := NewAssertableJSON(s.T(), jsonStr)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr)
s.NoError(err)

// Test value is not as expected
Expand All @@ -156,7 +149,7 @@ func (s *AssertableJsonTestSuite) TestWhereNot() {
func (s *AssertableJsonTestSuite) TestFirst() {
jsonStr := `{"items": [{"id": 1}, {"id": 2}]}`

assertable, err := NewAssertableJSON(s.T(), jsonStr)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr)
s.NoError(err)

// Test fetching the first item
Expand All @@ -176,7 +169,7 @@ func (s *AssertableJsonTestSuite) TestFirst() {
func (s *AssertableJsonTestSuite) TestHasWithScope() {
jsonStr := `{"items": [{"id": 1}, {"id": 2}]}`

assertable, err := NewAssertableJSON(s.T(), jsonStr)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr)
s.NoError(err)

// Test has with correct length
Expand All @@ -198,7 +191,7 @@ func (s *AssertableJsonTestSuite) TestEach() {
"nonArray": "value"
}`

assertable, err := NewAssertableJSON(s.T(), jsonStr)
assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr)
s.NoError(err)

// Test iterating over each item
Expand All @@ -214,7 +207,7 @@ func (s *AssertableJsonTestSuite) TestEach() {

// Test with an empty array
emptyJsonStr := `{"items": []}`
emptyAssertable, err := NewAssertableJSON(s.T(), emptyJsonStr)
emptyAssertable, err := NewAssertableJSON(s.T(), &testJson{}, emptyJsonStr)
s.NoError(err)

emptyCallCount := 0
Expand Down
29 changes: 19 additions & 10 deletions testing/test_request.go → testing/http/test_request.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testing
package http

import (
"context"
Expand All @@ -9,6 +9,9 @@ import (
"net/http/httptest"
"testing"

"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/contracts/route"
"github.com/goravel/framework/contracts/session"
contractstesting "github.com/goravel/framework/contracts/testing"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/support/collect"
Expand All @@ -22,15 +25,21 @@ type TestRequest struct {
bind any
defaultHeaders map[string]string
defaultCookies map[string]string
json foundation.Json
route route.Route
session session.Manager
sessionAttributes map[string]any
}

func NewTestRequest(t *testing.T) contractstesting.TestRequest {
func NewTestRequest(t *testing.T, json foundation.Json, route route.Route, session session.Manager) contractstesting.TestRequest {
return &TestRequest{
t: t,
ctx: context.Background(),
defaultHeaders: make(map[string]string),
defaultCookies: make(map[string]string),
json: json,
route: route,
session: session,
sessionAttributes: make(map[string]any),
}
}
Expand Down Expand Up @@ -153,23 +162,23 @@ func (r *TestRequest) call(method string, uri string, body io.Reader) (contracts
req.AddCookie(&cookie)
}

if routeFacade == nil {
if r.route == nil {
r.t.Fatal(errors.RouteFacadeNotSet.SetModule(errors.ModuleTesting))
}

response, err := routeFacade.Test(req)
response, err := r.route.Test(req)
if err != nil {
return nil, err
}

testResponse := NewTestResponse(r.t, response)
testResponse := NewTestResponse(r.t, response, r.json, r.session)
if r.bind != nil {
body, err := testResponse.Content()
if err != nil {
return nil, err
}

if err := json.Unmarshal([]byte(body), r.bind); err != nil {
if err := r.json.Unmarshal([]byte(body), r.bind); err != nil {
return nil, err
}
}
Expand All @@ -182,18 +191,18 @@ func (r *TestRequest) setSession() error {
return nil
}

if sessionFacade == nil {
if r.session == nil {
return errors.SessionFacadeNotSet
}

// Retrieve session driver
driver, err := sessionFacade.Driver()
driver, err := r.session.Driver()
if err != nil {
return err
}

// Build session
session, err := sessionFacade.BuildSession(driver)
session, err := r.session.BuildSession(driver)
if err != nil {
return err
}
Expand All @@ -209,6 +218,6 @@ func (r *TestRequest) setSession() error {
}

// Release session
sessionFacade.ReleaseSession(session)
r.session.ReleaseSession(session)
return nil
}
15 changes: 5 additions & 10 deletions testing/test_request_test.go → testing/http/test_request_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testing
package http

import (
"bytes"
Expand Down Expand Up @@ -31,24 +31,19 @@ func TestTestRequestSuite(t *testing.T) {
func (s *TestRequestSuite) SetupTest() {
s.mockRoute = mocksroute.NewRoute(s.T())
s.mockSessionManager = mockssession.NewManager(s.T())
json = &testJson{}
routeFacade = s.mockRoute
sessionFacade = s.mockSessionManager

s.testRequest = &TestRequest{
t: s.T(),
ctx: context.Background(),
defaultHeaders: make(map[string]string),
defaultCookies: make(map[string]string),
sessionAttributes: make(map[string]any),
json: &testJson{},
route: s.mockRoute,
session: s.mockSessionManager,
}
}

func (s *TestRequestSuite) TearDownTest() {
json = nil
routeFacade = nil
sessionFacade = nil
}

func (s *TestRequestSuite) TestBindAndCall() {
s.mockRoute.EXPECT().Test(httptest.NewRequest("GET", "/", nil).WithContext(context.Background())).Return(&http.Response{
Body: io.NopCloser(bytes.NewBufferString(`{"name": "John", "age": 30}`)),
Expand Down
Loading

0 comments on commit bc907a5

Please sign in to comment.