Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optimize testing.http #788

Merged
merged 7 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions contracts/testing/test_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ import (
)

type TestRequest interface {
Get(uri string) (TestResponse, error)
Post(uri string, body io.Reader) (TestResponse, error)
Put(uri string, body io.Reader) (TestResponse, error)
Delete(uri string, body io.Reader) (TestResponse, error)
Patch(uri string, body io.Reader) (TestResponse, error)
Head(uri string) (TestResponse, error)
Options(uri string) (TestResponse, error)

Bind(value any) TestRequest
FlushHeaders() TestRequest
WithBasicAuth(username, password string) TestRequest
WithContext(ctx context.Context) TestRequest
WithCookies(cookies map[string]string) TestRequest
WithCookie(key, value string) TestRequest
WithContext(ctx context.Context) TestRequest
WithHeaders(headers map[string]string) TestRequest
WithHeader(key, value string) TestRequest
WithHeaders(headers map[string]string) TestRequest
WithoutHeader(key string) TestRequest
WithToken(token string, ttype ...string) TestRequest
WithBasicAuth(username, password string) TestRequest
WithoutToken() TestRequest
WithSession(attributes map[string]any) TestRequest
Get(uri string) (TestResponse, error)
Post(uri string, body io.Reader) (TestResponse, error)
Put(uri string, body io.Reader) (TestResponse, error)
Patch(uri string, body io.Reader) (TestResponse, error)
Delete(uri string, body io.Reader) (TestResponse, error)
Head(uri string) (TestResponse, error)
Options(uri string) (TestResponse, error)
}
48 changes: 48 additions & 0 deletions mocks/testing/TestRequest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion testing/assertable_json.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package testing

import (
"encoding/json"
"fmt"
"testing"

Expand Down
109 changes: 64 additions & 45 deletions testing/assertable_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,72 @@ package testing
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

contractstesting "github.com/goravel/framework/contracts/testing"
)

func TestNewAssertableJSON(t *testing.T) {
type AssertableJsonTestSuite struct {
suite.Suite
}

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(t, validJSON)
assert.NoError(t, err)
assert.NotNil(t, assertable)
assertable, err := NewAssertableJSON(s.T(), validJSON)
s.NoError(err)
s.NotNil(assertable)

assertable, err = NewAssertableJSON(t, invalidJSON)
assert.Error(t, err)
assert.Nil(t, assertable)
assertable, err = NewAssertableJSON(s.T(), invalidJSON)
s.Error(err)
s.Nil(assertable)
}

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

assertable.Count("items", 3)

//assertable.Count("items", 4)
}

func TestHas(t *testing.T) {
func (s *AssertableJsonTestSuite) TestHas() {
jsonStr := `{
"key1": "value1",
"key2": [1, 2, 3],
"nested": {"deep": "value"},
"nullKey": null
}`
assertable, err := NewAssertableJSON(t, jsonStr)
assert.NoError(t, err)
assertable, err := NewAssertableJSON(s.T(), jsonStr)
s.NoError(err)

assertable.Has("key1")
assertable.Has("nullKey")

//assertable.Has("nonExistingKey")
}

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

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

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

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

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

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

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

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

func TestWhere(t *testing.T) {
func (s *AssertableJsonTestSuite) TestWhere() {
jsonStr := `{
"key1": "value1",
"intKey": 42,
Expand All @@ -104,8 +122,8 @@ func TestWhere(t *testing.T) {
"objKey": {"nested": "value"},
"arrayKey": [1, 2, 3]
}`
assertable, err := NewAssertableJSON(t, jsonStr)
assert.NoError(t, err)
assertable, err := NewAssertableJSON(s.T(), jsonStr)
s.NoError(err)

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

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

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

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

assertable, err := NewAssertableJSON(t, jsonStr)
assert.NoError(t, err)
assertable, err := NewAssertableJSON(s.T(), jsonStr)
s.NoError(err)

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

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

assertable, err := NewAssertableJSON(t, jsonStr)
assert.NoError(t, err)
assertable, err := NewAssertableJSON(s.T(), jsonStr)
s.NoError(err)

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

func TestEach(t *testing.T) {
func (s *AssertableJsonTestSuite) TestEach() {
jsonStr := `{
"items": [{"id": 1}, {"id": 2}],
"mixedTypes": [42, "string", {"key": "value"}],
"nonArray": "value"
}`

assertable, err := NewAssertableJSON(t, jsonStr)
assert.NoError(t, err)
assertable, err := NewAssertableJSON(s.T(), jsonStr)
s.NoError(err)

// Test iterating over each item
callCount := 0
assertable.Each("items", func(item contractstesting.AssertableJSON) {
item.Where("id", float64(callCount+1))
callCount++
})
assert.Equal(t, 2, callCount)
s.Equal(2, callCount)

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

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

emptyCallCount := 0
emptyAssertable.Each("items", func(item contractstesting.AssertableJSON) {
emptyCallCount++
})
assert.Equal(t, 0, emptyCallCount)
s.Equal(0, emptyCallCount)
}
3 changes: 3 additions & 0 deletions testing/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
const Binding = "goravel.testing"

var (
json foundation.Json
artisanFacade contractsconsole.Artisan
routeFacade contractsroute.Route
sessionFacade contractsession.Manager
Expand Down Expand Up @@ -41,4 +42,6 @@
if sessionFacade == nil {
color.Errorln(errors.SessionFacadeNotSet.SetModule(errors.ModuleTesting))
}

json = app.GetJson()

Check warning on line 46 in testing/service_provider.go

View check run for this annotation

Codecov / codecov/patch

testing/service_provider.go#L46

Added line #L46 was not covered by tests
}
Loading
Loading