Skip to content

Commit

Permalink
Merge pull request #3 from Comcast/standUpMachine
Browse files Browse the repository at this point in the history
Unit test for conversion utilities + Using new validation library
  • Loading branch information
schmidtw authored Sep 18, 2017
2 parents 0e1c127 + a82cff8 commit 0f06048
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 205 deletions.
6 changes: 4 additions & 2 deletions src/glide.lock

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

2 changes: 2 additions & 0 deletions src/glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ homepage: https://github.com/Comcast/tr1d1um
import:
- package: github.com/Comcast/webpa-common
version: cb246362b61aaf042a936fa615a48554d111f9b4
- package: github.com/go-ozzo/ozzo-validation
version: v3.3
39 changes: 33 additions & 6 deletions src/tr1d1um/WDMP_Type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

const(
import (
"errors"
"github.com/go-ozzo/ozzo-validation"
)

const (
COMMAND_GET = "GET"
COMMAND_GET_ATTRS = "GET_ATTRIBUTES"
COMMAND_SET = "SET"
Expand All @@ -16,14 +21,15 @@ const(

ERR_UNSUCCESSFUL_DATA_PARSE = "Unsuccessful Data Parse"
)

/*
GET-Flavored structs
*/

type GetWDMP struct {
Command string `json:"command"`
Names []string `json:"names,omitempty"`
Attribute string `json:"attributes,omitempty"`
Command string `json:"command"`
Names []string `json:"names,omitempty"`
Attribute string `json:"attributes,omitempty"`
}

/*
Expand All @@ -33,8 +39,8 @@ type GetWDMP struct {
type Attr map[string]interface{}

type SetParam struct {
Name* string `json:"name"`
DataType* int32 `json:"dataType,omitempty"`
Name *string `json:"name"`
DataType *int8 `json:"dataType,omitempty"`
Value interface{} `json:"value,omitempty"`
Attributes Attr `json:"attributes,omitempty"`
}
Expand Down Expand Up @@ -68,3 +74,24 @@ type DeleteRowWDMP struct {
Row string `json:"row"`
}

/* Validation functions */

//Applicable for the SET and TEST_SET
func (sp SetParam) Validate() error {
return validation.ValidateStruct(&sp,
validation.Field(&sp.Name, validation.NotNil),
validation.Field(&sp.DataType, validation.NotNil),
validation.Field(&sp.Value, validation.Required))
}

func ValidateSETAttrParams(params []SetParam) (err error) {
if params == nil || len(params) == 0 {
return errors.New("invalid list of params")
}
for _, param := range params {
if err = validation.Validate(param.Attributes, validation.Required.Error("invalid attr")); err != nil {
return
}
}
return
}
64 changes: 64 additions & 0 deletions src/tr1d1um/WDMP_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

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

/* tests correct usage of library */

func TestValidateSET(t *testing.T) {
assert := assert.New(t)
sp := SetParam{Attributes: Attr{"0": 0}, Name: &name, Value: value, DataType: &dataType}

t.Run("Perfect", func(t *testing.T) {
assert.Nil(sp.Validate())
})

t.Run("DataTypeIssue", func(t *testing.T) {
sp.DataType = nil
dataTypeErr := sp.Validate()
assert.Contains(dataTypeErr, "dataType")
sp.DataType = &dataType // put back value
})

t.Run("ValueIssue", func(t *testing.T) {
sp.Value = nil
valueErr := sp.Validate()
assert.Contains(valueErr, "value")
sp.Value = value
})

t.Run("Name", func(t *testing.T) {
sp.Name = nil
nameErr := sp.Validate()
assert.Contains(nameErr, "name")
})
}

func TestValidateSETAttrParams(t *testing.T) {
assert := assert.New(t)

t.Run("ZeroCases", func(t *testing.T) {
errNil := ValidateSETAttrParams(nil)
errEmpty := ValidateSETAttrParams([]SetParam{})

assert.NotNil(errNil)
assert.NotNil(errEmpty)
assert.Contains(errNil.Error(), "invalid list")
assert.Contains(errEmpty.Error(), "invalid list")
})

t.Run("InvalidAttr", func(t *testing.T) {
param := SetParam{Attributes: Attr{}}
err := ValidateSETAttrParams([]SetParam{param})
assert.NotNil(err)
assert.Contains(err.Error(), "invalid attr")
})

t.Run("Ideal", func(t *testing.T) {
param := SetParam{Attributes: Attr{"notify": 0}}
err := ValidateSETAttrParams([]SetParam{param})
assert.Nil(err)
})
}
Loading

0 comments on commit 0f06048

Please sign in to comment.