Skip to content

Commit 855b52a

Browse files
author
Gal Topper
authored
Minor refactoring and unit test. (#46)
* Minor refactoring and unit test. * go fmt * Shorthand declaration syntax. * Added build tags. * Fix testify usage. * Separated test suites in the same package. * Fix test suite stuff. * Test bad time input also.
1 parent 0045f0f commit 855b52a

File tree

3 files changed

+85
-17
lines changed

3 files changed

+85
-17
lines changed

pkg/tsdbctl/add.go

+5-13
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/v3io/v3io-tsdb/pkg/tsdb"
2929
"github.com/v3io/v3io-tsdb/pkg/utils"
3030
"io"
31-
"math"
3231
"os"
3332
"sort"
3433
"strconv"
@@ -268,20 +267,13 @@ func strToTV(tarr, varr string) ([]int64, []float64, error) {
268267
return nil, nil, errors.New("time and value arrays must have the same number of elements")
269268
}
270269

271-
tarray := []int64{}
272-
varray := []float64{}
270+
var tarray []int64
271+
var varray []float64
273272

274273
for i := 0; i < len(vlist); i++ {
275-
276-
var v float64
277-
var err error
278-
if vlist[i] == "NaN" {
279-
v = math.NaN()
280-
} else {
281-
v, err = strconv.ParseFloat(vlist[i], 64)
282-
if err != nil {
283-
return nil, nil, errors.Wrap(err, "not a valid float value")
284-
}
274+
v, err := strconv.ParseFloat(vlist[i], 64)
275+
if err != nil {
276+
return nil, nil, errors.WithStack(err)
285277
}
286278

287279
varray = append(varray, v)

pkg/tsdbctl/add_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// +build unit
2+
3+
/*
4+
Copyright 2018 Iguazio Systems Ltd.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License") with
7+
an addition restriction as set forth herein. You may not use this
8+
file except in compliance with the License. You may obtain a copy of
9+
the License at http://www.apache.org/licenses/LICENSE-2.0.
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
implied. See the License for the specific language governing
15+
permissions and limitations under the License.
16+
17+
In addition, you may not use the software for any purposes that are
18+
illegal under applicable law, and the grant of the foregoing license
19+
under the Apache 2.0 license is conditioned upon your compliance with
20+
such restriction.
21+
*/
22+
23+
package tsdbctl
24+
25+
import (
26+
"github.com/stretchr/testify/suite"
27+
"math"
28+
"testing"
29+
)
30+
31+
type testAddSuite struct {
32+
suite.Suite
33+
}
34+
35+
func (suite *testAddSuite) TestStrToTV() {
36+
37+
ts, vs, err := strToTV("1533814796000,1533894796000", "10.1,202")
38+
39+
suite.Require().Nil(err)
40+
suite.Require().Equal(ts, []int64{1533814796000, 1533894796000})
41+
suite.Require().Equal(vs, []float64{10.1, 202})
42+
}
43+
44+
func (suite *testAddSuite) TestStrToTVSpecialValues() {
45+
46+
ts, vs, err := strToTV("1533814796000,1533894796000,1533899796000", "NaN,Inf,-Inf")
47+
48+
suite.Require().Nil(err)
49+
suite.Require().Equal(ts, []int64{1533814796000, 1533894796000, 1533899796000})
50+
suite.Require().True(math.IsNaN(vs[0])) // NaN != NaN, so we have to check this explicitly
51+
suite.Require().Equal(vs[1:], []float64{math.Inf(1), math.Inf(-1)})
52+
}
53+
54+
func (suite *testAddSuite) TestStrToTVInvalidValue() {
55+
56+
ts, vs, err := strToTV("1533814796000,1533894796000,1533899796000", "1.2,5,z")
57+
58+
suite.Require().Nil(ts)
59+
suite.Require().Nil(vs)
60+
suite.Require().Error(err)
61+
}
62+
63+
func (suite *testAddSuite) TestStrToTVInvalidTime() {
64+
65+
ts, vs, err := strToTV("1533814796000,1533894796000,abc", "1.2,5,5.1")
66+
67+
suite.Require().Nil(ts)
68+
suite.Require().Nil(vs)
69+
suite.Require().Error(err)
70+
}
71+
72+
func TestAddSuite(t *testing.T) {
73+
suite.Run(t, new(testAddSuite))
74+
}

pkg/tsdbctl/tsdbctl_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build unit
2+
13
/*
24
Copyright 2018 Iguazio Systems Ltd.
35
@@ -26,11 +28,11 @@ import (
2628
"testing"
2729
)
2830

29-
type testSuite struct {
31+
type testTsdbctlSuite struct {
3032
suite.Suite
3133
}
3234

33-
func (suite *testSuite) TestPopulateConfigWithTenant() {
35+
func (suite *testTsdbctlSuite) TestPopulateConfigWithTenant() {
3436
rc := RootCommandeer{v3ioPath: "Vel@Odar:p455w0rd@localhost:80123/123"}
3537
cfg := &config.V3ioConfig{
3638
Path: "/x/y/z",
@@ -55,6 +57,6 @@ func (suite *testSuite) TestPopulateConfigWithTenant() {
5557
suite.Require().Equal(expectedRc, rc)
5658
}
5759

58-
func TestBuilderSuite(t *testing.T) {
59-
suite.Run(t, new(testSuite))
60+
func TestTsdbctlSuite(t *testing.T) {
61+
suite.Run(t, new(testTsdbctlSuite))
6062
}

0 commit comments

Comments
 (0)