Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kristinapathak committed May 6, 2021
1 parent bb0cf13 commit 0e36e95
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
2 changes: 1 addition & 1 deletion basculechecks/metricoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func WithServer(s string) MetricOption {

func WithEndpoints(e []*regexp.Regexp) MetricOption {
return func(m *MetricValidator) {
if e != nil {
if len(e) != 0 {
m.endpoints = e
}
}
Expand Down
73 changes: 71 additions & 2 deletions basculechecks/metricoptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,75 @@

package basculechecks

import "testing"
import (
"errors"
"fmt"
"regexp"
"testing"

func TestNewMetricValidator(t *testing.T) {}
"github.com/stretchr/testify/assert"
)

func TestNewMetricValidator(t *testing.T) {
c := &CapabilitiesValidator{}
m := &AuthCapabilityCheckMeasures{}
e := []*regexp.Regexp{regexp.MustCompile(".*")}
s := "testserverrr"
tests := []struct {
description string
checker CapabilitiesChecker
measures *AuthCapabilityCheckMeasures
options []MetricOption
expectedValidator *MetricValidator
expectedErr error
}{
{
description: "Success",
checker: c,
measures: m,
options: []MetricOption{
MonitorOnly(),
WithServer(s),
WithServer(""),
WithEndpoints(e),
WithEndpoints(nil),
},
expectedValidator: &MetricValidator{
c: c,
measures: m,
server: s,
endpoints: e,
errorOut: false,
},
},
{
description: "Success with defaults",
checker: c,
measures: m,
expectedValidator: &MetricValidator{
c: c,
measures: m,
errorOut: true,
},
},
{
description: "Nil Checker Error",
measures: m,
expectedErr: ErrNilChecker,
},
{
description: "Nil Measures Error",
checker: c,
expectedErr: ErrNilMeasures,
},
}
for _, tc := range tests {
assert := assert.New(t)
m, err := NewMetricValidator(tc.checker, tc.measures, tc.options...)
assert.Equal(tc.expectedValidator, m)
assert.True(errors.Is(err, tc.expectedErr),
fmt.Errorf("error [%v] doesn't match expected error [%v]",
err, tc.expectedErr),
)
}
}
42 changes: 42 additions & 0 deletions basculechecks/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package basculechecks

import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -65,4 +66,45 @@ func TestDeterminePartnerMetric(t *testing.T) {
}

func TestDetermineEndpointMetric(t *testing.T) {
var (
goodURL = "/asnkfn/aefkijeoij/aiogj"
matchingURL = "/fnvvds jkfji/mac:12345544322345334/geigosj"
matchingEndpoint = `/fnvvds jkfji/.*/geigosj\b`
matchingRegex = regexp.MustCompile(matchingEndpoint)
matchingParsed = `/fnvvds_jkfji/.*/geigosj\b`
unusedEndpoint = `/a/b\b`
unusedRegex = regexp.MustCompile(unusedEndpoint)
)

tests := []struct {
description string
endpoints []*regexp.Regexp
u string
expectedEndpoint string
}{
{
description: "No Endpoints",
u: goodURL,
expectedEndpoint: NoneEndpoint,
},
{
description: "Endpoint Not Recognized",
endpoints: []*regexp.Regexp{unusedRegex, matchingRegex},
u: goodURL,
expectedEndpoint: NotRecognizedEndpoint,
},
{
description: "Endpoint Matched",
endpoints: []*regexp.Regexp{unusedRegex, matchingRegex},
u: matchingURL,
expectedEndpoint: matchingParsed,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
endpoint := determineEndpointMetric(tc.endpoints, tc.u)
assert.Equal(tc.expectedEndpoint, endpoint)
})
}
}

0 comments on commit 0e36e95

Please sign in to comment.