Skip to content

Commit

Permalink
Add ShouldTimeEqual assertion
Browse files Browse the repository at this point in the history
Signed-off-by: marc audefroy <marc.audefroy@corp.ovh.com>
  • Loading branch information
marcaudefroy committed Feb 10, 2022
1 parent 3d44aa6 commit d223f86
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ Builtin variables:
* ShouldHappenAfter - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldHappenAfter.yml)
* ShouldHappenOnOrAfter - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldHappenOnOrAfter.yml)
* ShouldHappenBetween - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldHappenBetween.yml)
* ShouldTimeEqual - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldTimeEqual.yml)

#### `Must` keywords

Expand Down
36 changes: 36 additions & 0 deletions assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var assertMap = map[string]AssertFunc{
"ShouldHappenAfter": ShouldHappenAfter,
"ShouldHappenOnOrAfter": ShouldHappenOnOrAfter,
"ShouldHappenBetween": ShouldHappenBetween,
"ShouldTimeEqual": ShouldTimeEqual,
}

func Get(s string) (AssertFunc, bool) {
Expand Down Expand Up @@ -1047,6 +1048,41 @@ func ShouldHappenBetween(actual interface{}, expected ...interface{}) error {
return fmt.Errorf("expected '%v' to be between '%v' and '%v' ", actualTime, min, max)
}

// ShouldTimeEqual receives exactly 2 time.Time arguments and does a time equality check.
// The arguments have to respect the date format RFC3339, as 2006-01-02T15:04:00+07:00
//
// Example of testsuite file:
//
// name: test ShouldTimeEqual
// vars:
// time_expected: 2006-01-02T13:04:00Z
// time: 2006-01-02T15:04:00+02:00
// testcases:
// - name: test assertion
// steps:
// - type: exec
// script: "echo {{.time}}"
// assertions:
// - result.systemout ShouldTimeEqual "{{.time_expected}}"
func ShouldTimeEqual(actual interface{}, expected ...interface{}) error {
if err := need(1, expected); err != nil {
return err
}

actualTime, err := getTimeFromString(actual)
if err != nil {
return err
}
expectedTime, err := getTimeFromString(expected[0])
if err != nil {
return err
}
if actualTime.Equal(expectedTime) {
return nil
}
return fmt.Errorf("expected '%v' to be time equals to '%v' ", actualTime, expectedTime)
}

func getTimeFromString(in interface{}) (time.Time, error) {
if t, isTime := in.(time.Time); isTime {
return t, nil
Expand Down
52 changes: 52 additions & 0 deletions assertions/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,3 +1344,55 @@ func TestShouldHappenBetween(t *testing.T) {
})
}
}

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

Parisloc, _ := time.LoadLocation("Europe/Paris")
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "ok",
args: args{
actual: time.Now(),
expected: []interface{}{time.Now().In(Parisloc)},
},
},
{
name: "ko",
args: args{
actual: time.Now(),
expected: []interface{}{time.Now().Add(1 * time.Second)},
},
wantErr: true,
},
{
name: "ok",
args: args{
actual: "2006-01-02T15:04:00+02:00",
expected: []interface{}{"2006-01-02T13:04:00Z"},
},
},
{
name: "ko",
args: args{
actual: "2006-01-02T15:04:00+07:00",
expected: []interface{}{"2006-01-02T15:04:05Z"},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ShouldTimeEqual(tt.args.actual, tt.args.expected...); (err != nil) != tt.wantErr {
t.Errorf("ShouldTimeEqual() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
11 changes: 11 additions & 0 deletions tests/assertions/ShouldTimeEqual.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: test ShouldTimeEqual - use RFC3339 format
vars:
time_expected: 2006-01-02T13:04:00Z
time: 2006-01-02T15:04:00+02:00
testcases:
- name: test assertion
steps:
- type: exec
script: "echo {{.time}}"
assertions:
- result.systemout ShouldTimeEqual "{{.time_expected}}"

0 comments on commit d223f86

Please sign in to comment.