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 TestMapCreation(t *testing.T) { o := New(nil) assert.Nil(t, o) o = New("Tyler") assert.Nil(t, o) unconvertable := &Unconvertable{name: "Tyler"} o = New(unconvertable) assert.Nil(t, o) convertable := &Convertable{name: "Tyler"} o = New(convertable) if assert.NotNil(t, convertable) { assert.Equal(t, "Tyler", o["name"], "Tyler") } o = MSI() if assert.NotNil(t, o) { assert.NotNil(t, o) } o = MSI("name", "Tyler") if assert.NotNil(t, o) { if assert.NotNil(t, o) { assert.Equal(t, o["name"], "Tyler") } } }
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 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 TestMapFromJSONWithError(t *testing.T) { var m Map assert.Panics(t, func() { m = MustFromJSON(`"name":"Mat"}`) }) assert.Nil(t, m) }
func TestAccessorsAccessGetFromArrayWithInt(t *testing.T) { current := []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}} one := access(current, 0, nil, false, false) two := access(current, 1, nil, false, false) three := access(current, 2, nil, false, false) assert.Equal(t, "Tyler", one.(map[string]interface{})["first"]) assert.Equal(t, "Capitol", two.(map[string]interface{})["first"]) assert.Nil(t, three) }
func TestAccessorsAccessGetInsideArray(t *testing.T) { current := map[string]interface{}{"names": []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}} assert.Equal(t, "Tyler", access(current, "names[0].first", nil, false, true)) assert.Equal(t, "Bunnell", access(current, "names[0].last", nil, false, true)) assert.Equal(t, "Capitol", access(current, "names[1].first", nil, false, true)) assert.Equal(t, "Bollocks", access(current, "names[1].last", nil, false, true)) assert.Panics(t, func() { access(current, "names[2]", nil, false, true) }) assert.Nil(t, access(current, "names[2]", nil, false, false)) }
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 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 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 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 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 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 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 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 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 TestScheduleQueryShouldAddTimeWhereCondition(t *testing.T) { assert := assert.New(t) type testCase struct { quertStr string start time.Time stop time.Time expectedStr string } testCases := []testCase{ testCase{ quertStr: `SELECT value FROM kbps`, start: time.Unix(1433378783, 0), stop: time.Unix(1433379783, 0), expectedStr: `SELECT value FROM kbps WHERE time > '2015-06-04 00:46:23' AND time < '2015-06-04 01:03:03'`, }, testCase{ quertStr: `SELECT value FROM kbps WHERE datacenter = nyc`, start: time.Unix(1433378783, 0), stop: time.Unix(1433379783, 0), expectedStr: `SELECT value FROM kbps WHERE datacenter = nyc AND time > '2015-06-04 00:46:23' AND time < '2015-06-04 01:03:03'`, }, } test := func(tc testCase) { q, err := influxdb.NewQueryBuilder(tc.quertStr, 0) assert.Nil(err) assert.Equal( tc.expectedStr, q.GetForTimeRange(tc.start, tc.stop).Command, ) } for _, tc := range testCases { test(tc) } }
func TestInfluxDBConfShouldParse(t *testing.T) { assert := assert.New(t) ic := influxdb.InfluxDBConf{} var data string = `--- host: influx1.example.com port: 4242 user: morgoth password: mysecret database: morgothdb ` err := yaml.Unmarshal([]byte(data), &ic) assert.Nil(err) assert.Equal("influx1.example.com", ic.Host) assert.Equal(uint(4242), ic.Port) assert.Equal("morgoth", ic.User) assert.Equal("mysecret", ic.Password) assert.Equal("morgothdb", ic.Database) }
func TestInfluxDBShouldBeRegistered(t *testing.T) { assert := assert.New(t) _, err := morgoth.EngineRegistery.GetFactory("influxdb") assert.Nil(err) }