From 1218f18af977b57bd9db8956e821dc5ca54c96a8 Mon Sep 17 00:00:00 2001 From: 9seconds Date: Fri, 12 Mar 2021 14:02:02 +0300 Subject: [PATCH] Add tests for config type port --- config/type_metric_prefix_test.go | 109 +++++++++++++++++++++++++++++ config/type_port.go | 2 +- config/type_port_test.go | 111 ++++++++++++++++++++++++++++++ config/type_url.go | 2 +- 4 files changed, 222 insertions(+), 2 deletions(-) create mode 100644 config/type_metric_prefix_test.go create mode 100644 config/type_port_test.go diff --git a/config/type_metric_prefix_test.go b/config/type_metric_prefix_test.go new file mode 100644 index 0000000..1954f62 --- /dev/null +++ b/config/type_metric_prefix_test.go @@ -0,0 +1,109 @@ +package config_test + +import ( + "encoding/json" + "testing" + + "github.com/9seconds/mtg/v2/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +type typeMetricPrefixTestStruct struct { + Value config.TypeMetricPrefix `json:"value"` +} + +type TypeMetricPrefixTestSuite struct { + suite.Suite +} + +func (suite *TypeMetricPrefixTestSuite) TestUnmarshalFail() { + testData := []string{ + "aaa.aaa", + "aaa-bbb", + "aaa:ccc", + "metric prefix", + } + + for _, v := range testData { + data, err := json.Marshal(map[string]string{ + "value": v, + }) + suite.NoError(err) + + suite.T().Run(v, func(t *testing.T) { + assert.Error(t, json.Unmarshal(data, &typeMetricPrefixTestStruct{})) + }) + } +} + +func (suite *TypeMetricPrefixTestSuite) TestUnmarshalOk() { + testData := []string{ + "mtg", + "mtg111", + } + + for _, v := range testData { + value := v + + data, err := json.Marshal(map[string]string{ + "value": v, + }) + suite.NoError(err) + + suite.T().Run(v, func(t *testing.T) { + testStruct := &typeMetricPrefixTestStruct{} + + assert.NoError(t, json.Unmarshal(data, testStruct)) + assert.Equal(t, value, testStruct.Value.Value("")) + }) + } +} + +func (suite *TypeMetricPrefixTestSuite) TestMarshalOk() { + testData := []string{ + "mtg", + "mtg111", + } + + for _, v := range testData { + value := v + + data, err := json.Marshal(map[string]string{ + "value": v, + }) + suite.NoError(err) + + suite.T().Run(v, func(t *testing.T) { + testStruct := &typeMetricPrefixTestStruct{} + + assert.NoError(t, json.Unmarshal(data, testStruct)) + assert.Equal(t, value, testStruct.Value.String()) + + marshalled, err := testStruct.Value.MarshalText() + assert.NoError(t, err) + assert.Equal(t, value, string(marshalled)) + }) + } +} + +func (suite *TypeMetricPrefixTestSuite) TestValue() { + testStruct := &typeMetricPrefixTestStruct{} + + suite.Equal("mtg", testStruct.Value.Value("mtg")) + suite.Equal("vvv", testStruct.Value.Value("vvv")) + + data, err := json.Marshal(map[string]string{ + "value": "aaa", + }) + suite.NoError(err) + suite.NoError(json.Unmarshal(data, testStruct)) + + suite.Equal("aaa", testStruct.Value.Value("mtg")) + suite.Equal("aaa", testStruct.Value.Value("vvv")) +} + +func TestTypeMetricPrefix(t *testing.T) { + t.Parallel() + suite.Run(t, &TypeMetricPrefixTestSuite{}) +} diff --git a/config/type_port.go b/config/type_port.go index 3c5ec15..ea02fbd 100644 --- a/config/type_port.go +++ b/config/type_port.go @@ -19,7 +19,7 @@ func (c *TypePort) UnmarshalJSON(data []byte) error { return fmt.Errorf("port number is not a number: %w", err) } - if intValue == 0 || intValue > 65536 { + if intValue == 0 || intValue >= 65536 { return fmt.Errorf("port number should be 0 < portNo < 65536: %d", intValue) } diff --git a/config/type_port_test.go b/config/type_port_test.go new file mode 100644 index 0000000..0bc4bfc --- /dev/null +++ b/config/type_port_test.go @@ -0,0 +1,111 @@ +package config_test + +import ( + "encoding/json" + "strconv" + "testing" + + "github.com/9seconds/mtg/v2/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +type typePortTestStruct struct { + Value config.TypePort `json:"value"` +} + +type TypePortTestSuite struct { + suite.Suite +} + +func (suite *TypePortTestSuite) TestUnmarshalFail() { + testData := []int{ + -1, + 1_000_000, + } + + for _, v := range testData { + data, err := json.Marshal(map[string]int{ + "value": v, + }) + suite.NoError(err) + + suite.T().Run(strconv.Itoa(v), func(t *testing.T) { + assert.Error(t, json.Unmarshal(data, &typePortTestStruct{})) + }) + } +} + +func (suite *TypePortTestSuite) TestUnmarshalOk() { + testData := []int{ + 1, + 1_000, + 65535, + } + + for _, v := range testData { + value := v + + data, err := json.Marshal(map[string]int{ + "value": v, + }) + suite.NoError(err) + + suite.T().Run(strconv.Itoa(v), func(t *testing.T) { + testStruct := &typePortTestStruct{} + + assert.NoError(t, json.Unmarshal(data, testStruct)) + assert.EqualValues(t, value, testStruct.Value.Value(0)) + }) + } +} + +func (suite *TypePortTestSuite) TestMarshalOk() { + testData := map[string]int{ + "1": 1, + "1000": 1000, + "65535": 65535, + } + + for k, v := range testData { + name := k + value := v + + data, err := json.Marshal(map[string]int{ + "value": value, + }) + suite.NoError(err) + + suite.T().Run(name, func(t *testing.T) { + testStruct := &typePortTestStruct{} + + assert.NoError(t, json.Unmarshal(data, testStruct)) + assert.Equal(t, name, testStruct.Value.String()) + + marshalled, err := testStruct.Value.MarshalJSON() + assert.NoError(t, err) + assert.Equal(t, name, string(marshalled)) + }) + } +} + +func (suite *TypePortTestSuite) TestValue() { + testStruct := &typePortTestStruct{} + + suite.EqualValues(0, testStruct.Value.Value(0)) + suite.EqualValues(1, testStruct.Value.Value(1)) + + data, err := json.Marshal(map[string]int{ + "value": 5, + }) + suite.NoError(err) + suite.NoError(json.Unmarshal(data, testStruct)) + + suite.EqualValues(5, testStruct.Value.Value(0)) + suite.EqualValues(5, testStruct.Value.Value(1)) +} + +func TestTypePort(t *testing.T) { + t.Parallel() + suite.Run(t, &TypePortTestSuite{}) +} diff --git a/config/type_url.go b/config/type_url.go index ec19012..9250492 100644 --- a/config/type_url.go +++ b/config/type_url.go @@ -10,7 +10,7 @@ type TypeURL struct { value *url.URL } -func (c *TypeURL) UnmarshalText(data []byte) error { +func (c *TypeURL) UnmarshalText(data []byte) error { // nolint: cyclop if len(data) == 0 { return nil }