Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SamYSF committed Sep 12, 2024
1 parent 60544d0 commit 7802f5a
Show file tree
Hide file tree
Showing 3 changed files with 360 additions and 0 deletions.
131 changes: 131 additions & 0 deletions pkg/server/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ limitations under the License.
package server

import (
"google.golang.org/protobuf/types/known/timestamppb"
"testing"
"time"

atest "github.com/linuxsuren/api-testing/pkg/testing"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -149,3 +151,132 @@ func TestToNormalSuite(t *testing.T) {
},
}))
}

func TestConvertToGRPCHistoryTestCase(t *testing.T) {
now := time.Now().UTC()
result := ConvertToGRPCHistoryTestCase(atest.HistoryTestCase{
CreateTime: now,
SuiteParam: defaultMap,
SuiteSpec: atest.APISpec{
Kind: "http",
URL: "/v1",
RPC: &atest.RPCDesc{
Raw: "fake",
},
Secure: &atest.Secure{
KeyFile: "fake",
},
},
Data: atest.TestCase{
Request: atest.Request{
Header: defaultMap,
},
Expect: atest.Response{
BodyFieldsExpect: defaultInterMap,
},
},
})
assert.Equal(t, result.Request.Header, defaultPairs)
assert.Equal(t, result.SuiteParam, defaultPairs)
assert.Equal(t, result.Response.BodyFieldsExpect, defaultPairs)
assert.Equal(t, "fake", result.SuiteSpec.Secure.Key)
assert.Equal(t, timestamppb.New(now), result.CreateTime)
}

func TestToNormalTestCaseResult(t *testing.T) {
assert.Equal(t, atest.TestCaseResult{
Body: "body",
Error: "error",
Header: defaultMap,
Id: "id",
Output: "output",
StatusCode: 200,
}, ToNormalTestCaseResult(&TestCaseResult{
Body: "body",
Error: "error",
Header: defaultPairs,
Id: "id",
Output: "output",
StatusCode: 200,
}))
}

func TestToGRPCHistoryTestCaseResult(t *testing.T) {
t.Run("TestCaseResult is empty", func(t *testing.T) {
historyTestResult := atest.HistoryTestResult{
Message: "test message",
Error: "test error",
CreateTime: time.Now(),
Data: atest.HistoryTestCase{
ID: "test-id",
},
TestCaseResult: nil,
}

result := ToGRPCHistoryTestCaseResult(historyTestResult)

assert.Equal(t, 0, len(result.TestCaseResult))
assert.Equal(t, historyTestResult.Message, result.Message)
assert.Equal(t, historyTestResult.Error, result.Error)
})

t.Run("TestCaseResult is not empty", func(t *testing.T) {
now := time.Now().UTC()

result := ToGRPCHistoryTestCaseResult(atest.HistoryTestResult{
Message: "fake message",
CreateTime: now,
Data: atest.HistoryTestCase{
ID: "fake id",
},
TestCaseResult: []atest.TestCaseResult{
{
StatusCode: 200,
Output: "fake output",
},
{
Output: "fake output 2",
},
},
})

assert.Equal(t, 2, len(result.TestCaseResult))
assert.Equal(t, "fake message", result.Message)
assert.Equal(t, now, result.CreateTime.AsTime())
assert.Equal(t, "fake output", result.TestCaseResult[0].Output)
assert.Equal(t, "fake output 2", result.TestCaseResult[1].Output)
})
}

func TestToGRPCTestSuiteSpec(t *testing.T) {

t.Run("empty", func(t *testing.T) {
assert.Equal(t, &APISpec{}, ToGRPCTestSuiteSpec(atest.APISpec{}))
})

t.Run("fields", func(t *testing.T) {
assert.Equal(t, &APISpec{
Url: "/v1",
Kind: "http",
Rpc: &RPC{
Raw: "fake",
},
Secure: &Secure{
Key: "fake",
},
}, ToGRPCTestSuiteSpec(atest.APISpec{
Kind: "http",
URL: "/v1",
RPC: &atest.RPCDesc{
Raw: "fake",
},
Secure: &atest.Secure{
KeyFile: "fake",
},
}))
})
}

var defaultInterMap = map[string]interface{}{"foo": "bar"}
var defaultMap map[string]string = map[string]string{"foo": "bar"}
var defaultPairs []*Pair = []*Pair{{Key: "foo", Value: "bar"}}
208 changes: 208 additions & 0 deletions pkg/testing/remote/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ limitations under the License.
package remote

import (
"google.golang.org/protobuf/types/known/timestamppb"
"testing"
"time"

server "github.com/linuxsuren/api-testing/pkg/server"
atest "github.com/linuxsuren/api-testing/pkg/testing"
"github.com/stretchr/testify/assert"
)

func TestConvert(t *testing.T) {
now := time.Now().UTC()
t.Run("convertToNormalTestSuite, empty object", func(t *testing.T) {
assert.Equal(t, &atest.TestSuite{
Param: map[string]string{},
Expand Down Expand Up @@ -123,6 +126,211 @@ func TestConvert(t *testing.T) {
assert.Equal(t, defaultPairs, result.Response.BodyFieldsExpect)
assert.Equal(t, defaultPairs, result.Response.Header)
})

t.Run("convertHistoryToGRPCTestCase", func(t *testing.T) {
result := ConvertHistoryToGRPCTestCase(&server.HistoryTestCase{
CaseName: "fake",
Request: &server.Request{
Header: defaultPairs,
},
Response: &server.Response{
BodyFieldsExpect: defaultPairs,
},
})
if !assert.NotNil(t, result) {
return
}
assert.Equal(t, defaultMap, result.Request.Header)
assert.Equal(t, defaultInterMap, result.Expect.BodyFieldsExpect)
assert.Equal(t, "fake", result.Name)
})

t.Run("convertToNormalHistoryTestCase", func(t *testing.T) {
assert.Equal(t, atest.HistoryTestCase{
CreateTime: now,
SuiteParam: defaultMap,
SuiteSpec: atest.APISpec{
Kind: "http",
URL: "/v1",
RPC: &atest.RPCDesc{
Raw: "fake",
},
Secure: &atest.Secure{
KeyFile: "fake",
},
},
Data: atest.TestCase{
Request: atest.Request{
API: "/v1",
Header: defaultMap,
Query: map[string]interface{}{},
Form: map[string]string{},
},
Expect: atest.Response{
BodyFieldsExpect: defaultInterMap,
Header: map[string]string{},
},
},
}, ConvertToNormalHistoryTestCase(&server.HistoryTestCase{
CreateTime: timestamppb.New(now),
SuiteParam: defaultPairs,
SuiteSpec: &server.APISpec{
Url: "/v1",
Kind: "http",
Rpc: &server.RPC{
Raw: "fake",
},
Secure: &server.Secure{
Key: "fake",
},
},
Request: &server.Request{
Header: defaultPairs,
Query: nil,
Api: "/v1",
},
Response: &server.Response{
BodyFieldsExpect: defaultPairs,
},
}))
})

t.Run("convertToNormalHistoryTestSuite, empty object", func(t *testing.T) {
assert.Equal(t, &atest.HistoryTestSuite{}, ConvertToNormalHistoryTestSuite(&HistoryTestSuite{}))
})

t.Run("convertToNormalHistoryTestSuite, normal object", func(t *testing.T) {
assert.Equal(t, &atest.HistoryTestSuite{
HistorySuiteName: "fake",
Items: []atest.HistoryTestCase{
{
CreateTime: now,
SuiteParam: defaultMap,
SuiteSpec: atest.APISpec{
Kind: "http",
URL: "/v1",
RPC: &atest.RPCDesc{
Raw: "fake",
},
Secure: &atest.Secure{
KeyFile: "fake",
},
},
},
},
}, ConvertToNormalHistoryTestSuite(&HistoryTestSuite{
HistorySuiteName: "fake",
Items: []*server.HistoryTestCase{
{
CreateTime: timestamppb.New(now),
SuiteParam: defaultPairs,
SuiteSpec: &server.APISpec{
Url: "/v1",
Kind: "http",
Rpc: &server.RPC{
Raw: "fake",
},
Secure: &server.Secure{
Key: "fake",
},
},
},
},
}))
})

t.Run("convertToGRPCHistoryTestCase", func(t *testing.T) {
result := ConvertToGRPCHistoryTestCase(atest.HistoryTestCase{
SuiteParam: defaultMap,
SuiteSpec: atest.APISpec{
Secure: &atest.Secure{
KeyFile: "fake",
},
},
Data: atest.TestCase{
Request: atest.Request{
Header: defaultMap,
},
Expect: atest.Response{
BodyFieldsExpect: defaultInterMap,
},
},
})
assert.Equal(t, defaultPairs, result.SuiteParam)
assert.Equal(t, defaultPairs, result.Request.Header)
assert.Equal(t, defaultPairs, result.Response.BodyFieldsExpect)
assert.Equal(t, "fake", result.SuiteSpec.Secure.Key)
})

t.Run("convertToGRPCHistoryTestCaseResult", func(t *testing.T) {
result := ConvertToGRPCHistoryTestCaseResult(atest.TestCaseResult{
Body: "fake body",
Output: "fake output",
}, &atest.TestSuite{
Param: defaultMap,
Spec: atest.APISpec{
Secure: &atest.Secure{
KeyFile: "fake",
},
},
Items: []atest.TestCase{
{
Request: atest.Request{
Header: defaultMap,
},
Expect: atest.Response{
BodyFieldsExpect: defaultInterMap,
},
},
},
})
assert.Equal(t, defaultPairs, result.Data.SuiteParam)
assert.Equal(t, defaultPairs, result.Data.Request.Header)
assert.Equal(t, defaultPairs, result.Data.Response.BodyFieldsExpect)
assert.Equal(t, "fake", result.Data.SuiteSpec.Secure.Key)
assert.Equal(t, "fake output", result.TestCaseResult[0].Output)
assert.Equal(t, "fake body", result.TestCaseResult[0].Body)
})

t.Run("convertToNormalTestCaseResult", func(t *testing.T) {
assert.Equal(t, atest.HistoryTestResult{
CreateTime: now,
Data: atest.HistoryTestCase{
SuiteParam: defaultMap,
CreateTime: now,
},
TestCaseResult: []atest.TestCaseResult{
{
Body: "fake body",
Output: "fake output",
Header: defaultMap,
},
{
Body: "fake body 2",
Output: "fake output 2",
Header: defaultMap,
},
},
}, ConvertToNormalTestCaseResult(&server.HistoryTestResult{
CreateTime: timestamppb.New(now),
Data: &server.HistoryTestCase{
SuiteParam: defaultPairs,
CreateTime: timestamppb.New(now),
},
TestCaseResult: []*server.TestCaseResult{
{
Body: "fake body",
Output: "fake output",
Header: defaultPairs,
},
{
Body: "fake body 2",
Output: "fake output 2",
Header: defaultPairs,
},
},
}))
})
}

var defaultInterMap = map[string]interface{}{"foo": "bar"}
Expand Down
Loading

0 comments on commit 7802f5a

Please sign in to comment.