func TestSetAllDefaultsShouldIgnoreNotDefaults(t *testing.T) { assert := assert.New(t) type S struct { A string `default:"i am a string"` B string C int `default:"1"` } s := S{ A: "original", B: "non default", C: 42, } assert.Equal(s.A, "original") assert.Equal(s.B, "non default") assert.Equal(s.C, 42) err := SetAllDefaults(&s) assert.Nil(err) assert.Equal(s.A, "i am a string") assert.Equal(s.B, "non default") assert.Equal(s.C, 1) }
func TestDefaultShouldDefaultMultipleFields(t *testing.T) { assert := assert.New(t) type S struct { A string `default:"i am a string"` B string C int `default:"1"` } s := S{} assert.Equal(s.A, "") assert.Equal(s.B, "") assert.Equal(s.C, 0) v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, "i am a string") assert.Equal(s.A, v) _, err = SetDefault(&s, "B") assert.NotNil(err) assert.Equal(s.B, "") v, err = SetDefault(&s, "C") assert.Nil(err) assert.Equal(s.C, 1) assert.Equal(s.C, v) }
func TestScheduleShouldStartAndStop(t *testing.T) { defer glog.Flush() assert := assert.New(t) unit := 10 * time.Millisecond calledCount := 0 testF := func(start time.Time, dur time.Time) { remainder := start.Nanosecond() % int(unit) assert.Equal(0, remainder, "Start not truncated to 10ms") calledCount++ } s := schedule.Schedule{ Callback: testF, Period: unit, } assert.NotNil(s) sleepCount := 3 start := time.Now() err := s.Start() assert.Nil(err) time.Sleep(unit * time.Duration(sleepCount)) s.Stop() elapsed := time.Since(start) assert.True(calledCount >= sleepCount && calledCount <= int(elapsed/unit)) }
func TestInfluxDBConfShouldDefaultEmpty(t *testing.T) { assert := assert.New(t) conf := influxdb.InfluxDBConf{} conf.Default() assert.Equal("localhost", conf.Host) assert.Equal(uint(8083), conf.Port) }
func TestDynamicConfiguratonValidateShouldFailNilConf(t *testing.T) { assert := assert.New(t) dc := config.DynamicConfiguration{ Type: "test", Conf: nil, } err := dc.Validate() assert.NotNil(err) }
func TestRiemannConfShouldDefault(t *testing.T) { assert := assert.New(t) rc := riemann.RiemannConf{} rc.Default() assert.Equal("localhost", rc.Host) assert.Equal(uint(5555), rc.Port) }
func TestDynamicConfiguratonDefaultShouldIgnoreNilConf(t *testing.T) { assert := assert.New(t) dc := config.DynamicConfiguration{ Type: "test", Conf: nil, } dc.Default() //No panics means pass assert.True(true) }
func TestCalcDShouldBe0(t *testing.T) { assert := assert.New(t) data := make([]float64, 10) for i := range data { data[i] = float64(i+1) / float64(len(data)) } expectedD := 0.0 d := calcD(data, data) assert.InDelta(expectedD, d, 1e-5) }
func TestDefaultShouldDefaultErrorOnMissingTag(t *testing.T) { assert := assert.New(t) type S struct { A string } s := S{} _, err := SetDefault(&s, "A") assert.NotNil(err) }
func TestDefaultShouldDefaultErrorOnMissingField(t *testing.T) { assert := assert.New(t) type S struct { A string `default:"a"` } s := S{} _, err := SetDefault(&s, "B") assert.NotNil(err) }
func TestSetAllDefaultsShouldErrorOnNonPtr(t *testing.T) { assert := assert.New(t) type S struct { A string `default:"i am a string"` } s := S{} assert.Equal(s.A, "") err := SetAllDefaults(s) assert.NotNil(err) }
func TestDefaultShouldNotDefaultInvalidFloat(t *testing.T) { assert := assert.New(t) type S struct { A float64 `default:"1.0a"` } s := S{} assert.Equal(s.A, 0.0) _, err := SetDefault(&s, "A") assert.NotNil(err) }
func TestDefaultShouldNotDefaultInvalidBool(t *testing.T) { assert := assert.New(t) type S struct { A bool `default:"invalid"` } s := S{} assert.Equal(s.A, false) _, err := SetDefault(&s, "A") assert.NotNil(err) }
func TestDefaultShouldNotDefaultInvalidUint(t *testing.T) { assert := assert.New(t) type S struct { A uint `default:"-1"` } s := S{} assert.Equal(s.A, uint(0)) _, err := SetDefault(&s, "A") assert.NotNil(err) }
func TestInfluxDBConfShouldDefaultNonEmpty(t *testing.T) { assert := assert.New(t) conf := influxdb.InfluxDBConf{ Port: 65536, Database: "morgoth", } conf.Default() assert.Equal("localhost", conf.Host) assert.Equal(uint(8083), conf.Port) assert.Equal("morgoth", conf.Database) }
func TestInfluxDBConfDefaultShouldIgnoreValidFields(t *testing.T) { assert := assert.New(t) conf := influxdb.InfluxDBConf{ Host: "influx", Port: 42, Database: "morgoth", } conf.Default() assert.Equal("influx", conf.Host) assert.Equal(uint(42), conf.Port) assert.Equal("morgoth", conf.Database) }
func TestHasDefaultShouldWorkWithoutPtr(t *testing.T) { assert := assert.New(t) type S struct { A int `default:"1"` } s := S{} assert.Equal(s.A, 0) def, err := HasDefault(s, "A") assert.Nil(err) assert.True(def) }
func TestDefaultShouldDefaultString(t *testing.T) { assert := assert.New(t) type S struct { A string `default:"1.0"` } s := S{} assert.Equal(s.A, "") v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, "1.0") assert.Equal(s.A, v) }
func TestRiemannConfShouldValidate(t *testing.T) { assert := assert.New(t) rc := riemann.RiemannConf{ Host: "example.com", Port: 42, } err := rc.Validate() assert.Nil(err) assert.Equal("example.com", rc.Host) assert.Equal(uint(42), rc.Port) }
func TestDefaultShouldDefaultFloat(t *testing.T) { assert := assert.New(t) type S struct { A float64 `default:"1.0"` } s := S{} assert.Equal(s.A, 0.0) v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, 1.0) assert.Equal(s.A, v) }
func TestDefaultShouldDefaultInt(t *testing.T) { assert := assert.New(t) type S struct { A int `default:"-1"` } s := S{} assert.Equal(s.A, 0) v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, -1) assert.Equal(s.A, v) }
func TestDefaultShouldDefaultBool(t *testing.T) { assert := assert.New(t) type S struct { A bool `default:"true"` } s := S{} assert.Equal(s.A, false) v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, true) assert.Equal(s.A, v) }
func TestDefaultShouldDefaultUint(t *testing.T) { assert := assert.New(t) type S struct { A uint `default:"1"` } s := S{} assert.Equal(s.A, uint(0)) v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, uint(1)) assert.Equal(s.A, v) }
func TestDynamicType(t *testing.T) { assert := assert.New(t) registery := config.NewRegistry() tf := testFactory{} registery.RegisterFactory("jim", &tf) ts := testStruct{assert, registery} var data = ` jim: a: 1 b: 2 c: 4 ` err := yaml.Unmarshal([]byte(data), &ts) assert.Nil(err) }
func TestLossyCounterShouldCountAllItems(t *testing.T) { assert := assert.New(t) lc := NewLossyCounter(0.05, 0.01) fp1 := &fp{1} fp2 := &fp{2} assert.NotEqual(fp1, fp2) assert.Equal(1, lc.Count(fp1)) assert.Equal(2, lc.Count(fp1)) assert.Equal(1, lc.Count(fp2)) assert.Equal(2, lc.Count(fp2)) assert.Equal(3, lc.Count(fp1)) assert.Equal(4, lc.Count(fp1)) }
func TestDynamicConfiguratonShouldValidate(t *testing.T) { assert := assert.New(t) mockConf := new(mocks.Configuration) dc := config.DynamicConfiguration{ Type: "test", Conf: mockConf, } mockConf.On("Validate").Return(nil) err := dc.Validate() assert.Nil(err) mockConf.Mock.AssertExpectations(t) }
func TestRiemannConfShouldParse(t *testing.T) { assert := assert.New(t) var data string = `--- host: riemann port: 43 ` rc := riemann.RiemannConf{} err := yaml.Unmarshal([]byte(data), &rc) assert.Nil(err) assert.Equal("riemann", rc.Host) assert.Equal(uint(43), rc.Port) }
func TestScheduleShouldNotDoubleStart(t *testing.T) { defer glog.Flush() assert := assert.New(t) s := schedule.Schedule{ Callback: func(start time.Time, dur time.Time) {}, Period: time.Millisecond, } assert.NotNil(s) err := s.Start() assert.Nil(err) err = s.Start() assert.NotNil(err) s.Stop() }
func TestSetAllDefaultsShouldDefaultAll(t *testing.T) { assert := assert.New(t) type S struct { A string `default:"i am a string"` B string C int `default:"1"` } s := S{} assert.Equal(s.A, "") assert.Equal(s.B, "") assert.Equal(s.C, 0) err := SetAllDefaults(&s) assert.Nil(err) assert.Equal(s.A, "i am a string") assert.Equal(s.B, "") assert.Equal(s.C, 1) }
func TestCalcDShouldBe1(t *testing.T) { assert := assert.New(t) data1 := make([]float64, 10) for i := range data1 { data1[i] = 0.0 } data2 := make([]float64, 10) for i := range data2 { data2[i] = float64(i+1) / float64(len(data2)) } expectedD := 1.0 d := calcD(data1, data2) assert.InDelta(expectedD, d, 1e-5) d = calcD(data2, data1) assert.InDelta(expectedD, d, 1e-5) }