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

update variables and executor loading #708

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
15 changes: 7 additions & 8 deletions assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type AssertionApplied struct {
IsOK bool `json:"isOK" yml:"-"`
}

func applyAssertions(ctx context.Context, r interface{}, tc TestCase, stepNumber int, rangedIndex int, step TestStep, defaultAssertions *StepAssertions) AssertionsApplied {
func applyAssertions(ctx context.Context, vars *H, tc *TestCase, stepNumber int, rangedIndex int, step TestStep, defaultAssertions *StepAssertions) AssertionsApplied {
var sa StepAssertions
var errors []Failure
var systemerr, systemout string
Expand All @@ -46,12 +46,11 @@ func applyAssertions(ctx context.Context, r interface{}, tc TestCase, stepNumber
sa = *defaultAssertions
}

executorResult := GetExecutorResult(r)

isOK := true
localVars := *vars
assertions := []AssertionApplied{}
for _, assertion := range sa.Assertions {
errs := check(ctx, tc, stepNumber, rangedIndex, assertion, executorResult)
errs := check(ctx, *tc, stepNumber, rangedIndex, assertion, localVars)
isAssertionOK := true
if errs != nil {
errors = append(errors, *errs)
Expand All @@ -64,12 +63,12 @@ func applyAssertions(ctx context.Context, r interface{}, tc TestCase, stepNumber
})
}

if _, ok := executorResult["result.systemerr"]; ok {
systemerr = fmt.Sprintf("%v", executorResult["result.systemerr"])
if _, ok := localVars["result.systemerr"]; ok {
systemerr = fmt.Sprintf("%v", localVars["result.systemerr"])
}

if _, ok := executorResult["result.systemout"]; ok {
systemout = fmt.Sprintf("%v", executorResult["result.systemout"])
if _, ok := localVars["result.systemout"]; ok {
systemout = fmt.Sprintf("%v", localVars["result.systemout"])
}

return AssertionsApplied{
Expand Down
39 changes: 39 additions & 0 deletions assertion_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package venom

import (
"context"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
Expand All @@ -22,3 +24,40 @@ func Test_splitAssertion(t *testing.T) {
}
}
}

func TestCheckBranchWithOR(t *testing.T) {
tc := TestCase{}
vars := map[string]interface{}{}
vars["result.statuscode"] = 501
vars["is_feature_supported"] = "false"
branch := map[string]interface{}{}

firstSetOfAssertions := []interface{}{`is_feature_supported ShouldEqual true`, `result.statuscode ShouldEqual 200`}
secondSetOfAssertions := []interface{}{`is_feature_supported ShouldEqual false`, `result.statuscode ShouldEqual 501`}

branch["or"] = []interface{}{
map[string]interface{}{"and": firstSetOfAssertions},
map[string]interface{}{"and": secondSetOfAssertions},
}

failure := checkBranch(context.Background(), tc, 0, 0, branch, vars)
assert.Nil(t, failure)
}
func TestCheckBranchWithORFailing(t *testing.T) {
tc := TestCase{}
vars := map[string]interface{}{}
vars["result.statuscode"] = 400
vars["is_feature_supported"] = "false"
branch := map[string]interface{}{}

firstSetOfAssertions := []interface{}{`result.statuscode ShouldEqual 200`}
secondSetOfAssertions := []interface{}{`result.statuscode ShouldEqual 501`}

branch["or"] = []interface{}{
map[string]interface{}{"and": firstSetOfAssertions},
map[string]interface{}{"and": secondSetOfAssertions},
}

failure := checkBranch(context.Background(), tc, 0, 0, branch, vars)
assert.NotNil(t, failure)
}
36 changes: 31 additions & 5 deletions assertions/assertions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assertions

import (
"bytes"
"encoding/json"
"fmt"
"math"
Expand Down Expand Up @@ -81,9 +82,21 @@ func ShouldBeArray(actual interface{}, expected ...interface{}) error {
if err := need(0, expected); err != nil {
return err
}
t := []interface{}{}
_, err := cast.ToSliceE(actual)
if err != nil {
strValue := fmt.Sprintf("%v", actual)
err2 := json.Unmarshal([]byte(strValue), &t)
if err2 != nil {
return fmt.Errorf("expected: %v to be an array but was not", actual)
}
x := bytes.TrimLeft([]byte(strValue), " \t\r\n")
isArray := len(x) > 0 && x[0] == '['
if isArray {
return nil
}
return fmt.Errorf("expected: %v to be an array but was not", actual)

}
return nil
}
Expand All @@ -93,8 +106,19 @@ func ShouldBeMap(actual interface{}, expected ...interface{}) error {
return err
}
_, err := cast.ToStringMapE(actual)
t := map[string]interface{}{}
if err != nil {
return fmt.Errorf("expected: %v to be a map but was not", actual)
strValue := fmt.Sprintf("%v", actual)
err2 := json.Unmarshal([]byte(strValue), &t)
if err2 != nil {
return fmt.Errorf("expected: %v to be a map but was not", actual)
}
x := bytes.TrimLeft([]byte(strValue), " \t\r\n")
isObject := len(x) > 0 && x[0] == '{'
if isObject {
return nil
}
return fmt.Errorf("expected: %v to be an map but was not", actual)
}
return nil
}
Expand Down Expand Up @@ -239,10 +263,12 @@ func ShouldBeNil(actual interface{}, expected ...interface{}) error {

// ShouldNotExist receives a single parameter and ensures that it is nil, blank or zero value
func ShouldNotExist(actual interface{}, expected ...interface{}) error {
if ShouldBeNil(actual) != nil ||
ShouldBeBlank(actual) != nil ||
ShouldBeZeroValue(actual) != nil {
return fmt.Errorf("expected not exist but it was")
if ShouldBeNil(actual) != nil {
if ShouldBeBlank(actual) != nil {
if ShouldBeZeroValue(actual) != nil {
return fmt.Errorf("expected not exist but it was")
}
}
}
return nil
}
Expand Down
122 changes: 122 additions & 0 deletions assertions/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,55 @@ func TestShouldBeEmpty(t *testing.T) {
})
}
}
func TestShouldNotExist(t *testing.T) {
type args struct {
actual interface{}
expected []interface{}
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "is nil",
args: args{
actual: nil,
},
wantErr: false,
},
{
name: "is empty",
args: args{
actual: "",
},
wantErr: false,
},
{
name: "is zero value",
args: args{
actual: 0,
},
wantErr: false,
},
{
name: "is_not_empty",
args: args{
actual: map[string]interface{}{"a": ""},
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ShouldNotExist(tt.args.actual, tt.args.expected...); (err != nil) != tt.wantErr {
t.Errorf("ShouldNotExist() error = %v, wantErr %v", err, tt.wantErr)
}
})
}

}

func TestShouldNotBeEmpty(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -1622,3 +1671,76 @@ func TestShouldJSONEqual(t *testing.T) {
})
}
}

func TestShouldBeArray(t *testing.T) {
type args struct {
actual interface{}
expected []interface{}
}

tests := []struct {
name string
args args
wantErr bool
}{
// Objects and arrays
{
name: "json array",
args: args{
actual: `[{"amount":10.0},{"amount":20.0}]`,
expected: []interface{}{},
},
},
{
name: "array",
args: args{
actual: []interface{}{`1`, `2`},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ShouldBeArray(tt.args.actual, tt.args.expected...); (err != nil) != tt.wantErr {
t.Errorf("ShouldBeArray() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestShouldBeMap(t *testing.T) {
type args struct {
actual interface{}
expected []interface{}
}
aMap := map[string]interface{}{}
aMap["key"] = "value"
aMap["another"] = 123

tests := []struct {
name string
args args
wantErr bool
}{
// Objects and arrays
{
name: "json map",
args: args{
actual: `{"amount":10.0}`,
expected: []interface{}{},
},
},
{
name: "map",
args: args{
actual: aMap,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ShouldBeMap(tt.args.actual, tt.args.expected...); (err != nil) != tt.wantErr {
t.Errorf("ShouldBeArray() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/venom/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func New() *cobra.Command {
return rootCmd
}

//AddCommands adds child commands to the root command rootCmd.
// AddCommands adds child commands to the root command rootCmd.
func addCommands(cmd *cobra.Command) {
cmd.AddCommand(run.Cmd)
cmd.AddCommand(version.Cmd)
Expand Down
16 changes: 16 additions & 0 deletions cmd/venom/root/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package root

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestRunCmd(t *testing.T) {
var validArgs []string

validArgs = append(validArgs, "run", "../../../tests/assertions")

rootCmd := New()
rootCmd.SetArgs(validArgs)
assert.Equal(t, 3, len(rootCmd.Commands()))
}
Loading