func TestFileIndex(t *testing.T) { Convey("Indexes", t, func() { Convey("Newer", func() { f := defaultGenerator.NewFake() t := time.Now() f.LastUsage = time.Now().Unix() keyOlder := f.indexKey() f.LastUsage = time.Now().Unix() + 50 keyNewer := f.indexKey() // keyOlder should be less than keyNewer So(bytes.Compare(keyOlder[:], keyNewer[:]), ShouldEqual, -1) Convey("Extract ID", func() { hash := getIDFromIndexKey(f.indexKey()) So(bytes.Compare(hash, f.ByteID()), ShouldEqual, 0) }) Convey("Start/end", func() { f.LastUsage = t.Unix() start := getIndexStart(t) end := getIndexEnd(t) So(bytes.Compare(start, f.indexKey()), ShouldEqual, -1) So(bytes.Compare(f.indexKey(), end), ShouldEqual, -1) }) }) Convey("Start/end", func() { t := time.Now() start := getIndexStart(t) end := getIndexEnd(t) log.Println(start, end) So(bytes.Compare(start, end), ShouldEqual, -1) }) }) }
func TestModelPageToml(t *testing.T) { Convey("ParseTomlFrontMatter", t, func() { p, err := NewPageOfMarkdown("testdata/page/page_toml.md", "page/page_toml", nil) if err != nil { So(err, ShouldNotBeNil) return } So(p.Title, ShouldEqual, "Welcome") t, _ := time.Parse("2006-01-02 15:04:05", "2016-03-25 12:20:20") So(p.Date, ShouldEqual, "2016-03-25 12:20:20") So(p.Created().Unix(), ShouldEqual, t.Unix()) So(p.Updated().Format("2006-01-02"), ShouldEqual, "2016-03-26") So(p.IsUpdated(), ShouldEqual, true) So(p.Content(), ShouldHaveLength, 1768) So(p.Draft, ShouldEqual, false) So(p.URL(), ShouldEqual, "/page/page_toml.html") So(p.SourceURL(), ShouldEqual, "testdata/page/page_toml.md") So(p.Meta, ShouldContainKey, "key") Convey("PageSetURL", func() { p.SetURL("/welcome.html") So(p.URL(), ShouldEqual, "/welcome.html") p.SetDestURL("dest/welcome.html") So(p.DestURL(), ShouldEqual, "dest/welcome.html") }) }) }
func TestSubscriber(t *testing.T) { Convey("When creating a subscriber", t, func() { email := "*****@*****.**" lastPayed := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) nextPayment := time.Date(2009, time.December, 10, 23, 0, 0, 0, time.UTC) s, _ := NewSubscriber(email, lastPayed, nextPayment, PaymentStatusDelayed) dbCfg := DBConfig() db := postgres.GetDatabase(dbCfg) err := s.Create(db) Convey("We can create a subscriber successfully", func() { So(err, ShouldEqual, nil) }) Convey("We can retrieve a saved subscriber", func() { var newSub Subscriber GetSubscriber(email, &newSub, db) So(newSub.LastPayed.Unix(), ShouldEqual, s.LastPayed.Unix()) }) Convey("We can update an already saved subscriber", func() { var newSub Subscriber t := time.Date(2015, time.November, 10, 23, 0, 0, 0, time.UTC) s.LastPayed = t s.NextPayment = t.AddDate(0, 1, 0) s.Save(db) GetSubscriber(email, &newSub, db) So(newSub.LastPayed.Unix(), ShouldEqual, t.Unix()) }) db.Exec("TRUNCATE TABLE subscribers") }) }
func TestPassengerFindDriver(t *testing.T) { buff.WriteString("\n\n\n========================================================\n") buff.WriteString("Test PassengerFindDriver begin\n\n\n") // uid int64, startTime int64, sLat float64, // sLng float64, eLat float64, eLng float64, sCity string, eCity string buff.WriteString("\n***********************缺失参数寻找**********************\n") buff.WriteString("########缺失uid#########") str := "/route/dadd?startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, unixTime, csLat, csLng, ceLat, ceLng, "003", "002") writeString(s, err) buff.WriteString("########缺失startTime#########") str := "/route/dadd?uid=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[11], csLat, csLng, ceLat, ceLng, "003", "002") writeString(s, err) buff.WriteString("########缺失sLat#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[11], unixTime, csLng, ceLat, ceLng, "003", "002") writeString(s, err) buff.WriteString("########缺失sLng#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[11], unixTime, csLat, ceLat, ceLng, "003", "002") writeString(s, err) buff.WriteString("########缺失eLat#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[11], unixTime, csLat, csLng, ceLng, "003", "002") writeString(s, err) buff.WriteString("########缺失eLng#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[11], unixTime, csLat, csLng, ceLat, "003", "002") writeString(s, err) buff.WriteString("########缺失sCity#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[11], unixTime, csLat, csLng, ceLat, ceLng, "002") writeString(s, err) buff.WriteString("########缺失eCity#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&distance=%v" s, err := ts.G(str, uidSlice[11], unixTime, csLat, csLng, ceLat, ceLng, "003") writeString(s, err) buff.WriteString("\n***********************错误参数格式寻找****************\n") str := "/route/dadd?uid=%v&startTime=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v" s, err := ts.G(str, "userid", "unixTime", "csLat", "csLng", "ceLat", "ceLng", "003", "002") writeString(s, err) buff.WriteString("\n***********************正常参数寻找****************\n") unixTime := t.Unix() str := "/route/dadd?uid=%v&startTime=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v" for i := 10; i < 20; i++ { s, err := ts.G(str, uidSlice[i], unixTime, csLat, csLng, ceLat, ceLng, "001", "002") writeString(s, err) routes := []bean.FindDriverRoute{} util.J2Ss(s, routes) if len(routes) > 0 { tmpFinds = append(tmpFinds, tmpFind{routes[0].Drid, routes[0].Duid}) } else { tmpFinds = append(tmpFinds, tmpFind{-1, -1}) } } }
func TestValidate(t *testing.T) { now := time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC) before, after := now.Add(-time.Minute), now.Add(time.Minute) leeway := 10 * time.Second exp := func(t time.Time) jwt.Claims { return jwt.Claims{"exp": t.Unix()} } nbf := func(t time.Time) jwt.Claims { return jwt.Claims{"nbf": t.Unix()} } var tests = []struct { desc string c jwt.Claims now time.Time expLeeway time.Duration nbfLeeway time.Duration err error }{ // test for nbf < now <= exp {desc: "exp == nil && nbf == nil", c: jwt.Claims{}, now: now, err: nil}, {desc: "now > exp", now: now, c: exp(before), err: jwt.ErrTokenIsExpired}, {desc: "now = exp", now: now, c: exp(now), err: nil}, {desc: "now < exp", now: now, c: exp(after), err: nil}, {desc: "nbf < now", c: nbf(before), now: now, err: nil}, {desc: "nbf = now", c: nbf(now), now: now, err: jwt.ErrTokenNotYetValid}, {desc: "nbf > now", c: nbf(after), now: now, err: jwt.ErrTokenNotYetValid}, // test for nbf-x < now <= exp+y {desc: "now < exp+x", now: now.Add(leeway - time.Second), expLeeway: leeway, c: exp(now), err: nil}, {desc: "now = exp+x", now: now.Add(leeway), expLeeway: leeway, c: exp(now), err: nil}, {desc: "now > exp+x", now: now.Add(leeway + time.Second), expLeeway: leeway, c: exp(now), err: jwt.ErrTokenIsExpired}, {desc: "nbf-x > now", c: nbf(now), nbfLeeway: leeway, now: now.Add(-leeway + time.Second), err: nil}, {desc: "nbf-x = now", c: nbf(now), nbfLeeway: leeway, now: now.Add(-leeway), err: jwt.ErrTokenNotYetValid}, {desc: "nbf-x < now", c: nbf(now), nbfLeeway: leeway, now: now.Add(-leeway - time.Second), err: jwt.ErrTokenNotYetValid}, } for i, tt := range tests { if got, want := tt.c.Validate(tt.now, tt.expLeeway, tt.nbfLeeway), tt.err; got != want { t.Errorf("%d - %q: got %v want %v", i, tt.desc, got, want) } } }
func TestNanosecondsToUTCAndBack(t *testing.T) { f := func(nsec int64) bool { t := Unix(0, nsec).UTC() ns := t.Unix()*1e9 + int64(t.Nanosecond()) return ns == nsec } f32 := func(nsec int32) bool { return f(int64(nsec)) } cfg := &quick.Config{MaxCount: 10000} // Try a small date first, then the large ones. (The span is only a few hundred years // for nanoseconds in an int64.) if err := quick.Check(f32, cfg); err != nil { t.Fatal(err) } if err := quick.Check(f, cfg); err != nil { t.Fatal(err) } }
func TestModelPostToml(t *testing.T) { Convey("ParseTomlFrontMatter", t, func() { p, err := NewPostOfMarkdown("testdata/post/post_toml.md", nil) if err != nil { So(err, ShouldNotBeNil) return } So(p.Title, ShouldEqual, "Welcome") So(p.Tags[0].Name, ShouldEqual, "pugo") t, _ := time.Parse("2006-01-02 15:04:05", "2016-03-25 12:20:20") So(p.Date, ShouldEqual, "2016-03-25 12:20:20") So(p.Created().Unix(), ShouldEqual, t.Unix()) So(p.Updated().Format("2006-01-02"), ShouldEqual, "2016-03-26") So(p.IsUpdated(), ShouldEqual, true) So(p.Content(), ShouldHaveLength, 1768) So(p.Brief(), ShouldHaveLength, 1043) So(p.Draft, ShouldEqual, false) So(p.URL(), ShouldEqual, "/2016/3/25/welcome.html") So(p.SourceURL(), ShouldEqual, "testdata/post/post_toml.md") Convey("PostSetURL", func() { p.SetURL("/welcome.html") So(p.URL(), ShouldEqual, "/welcome.html") p.SetDestURL("dest/welcome.html") So(p.DestURL(), ShouldEqual, "dest/welcome.html") }) Convey("PostSetPlaceholder", func() { p.SetPlaceholder(r, hr) So(p.Thumb, ShouldEqual, "/media/golang.png") }) }) }
func TestDriverAddRoute(t *testing.T) { buff.WriteString("\n\n\n========================================================\n") buff.WriteString("Test DriverAddRoute begin\n\n\n") buff.WriteString("\n***********************缺失参数加入**********************\n") buff.WriteString("########缺失uid#########") str := "/route/dadd?startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, unixTime, "sPlace18", "ePlace18", csLat, csLng, ceLat, ceLng, "003", "002", 8.2) writeString(s, err) buff.WriteString("########缺失startTime#########") str := "/route/dadd?uid=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[8], "sPlace28", "ePlace28", csLat, csLng, ceLat, ceLng, "003", "002", 8.2) writeString(s, err) buff.WriteString("########缺失sPlace#########") str := "/route/dadd?uid=%v&startTime=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[8], unixTime, "ePlace38", csLat, csLng, ceLat, ceLng, "003", "002", 8.2) writeString(s, err) buff.WriteString("########缺失ePlace#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[8], unixTime, "sPlace38", csLat, csLng, ceLat, ceLng, "003", "002", 8.2) writeString(s, err) buff.WriteString("########缺失sLat#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[8], unixTime, "sPlace48", "ePlace48", csLng, ceLat, ceLng, "003", "002", 8.2) writeString(s, err) buff.WriteString("########缺失sLng#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[8], unixTime, "sPlace58", "ePlace58", csLat, ceLat, ceLng, "003", "002", 8.2) writeString(s, err) buff.WriteString("########缺失eLat#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[8], unixTime, "sPlace68", "ePlace68", csLat, csLng, ceLng, "003", "002", 8.2) writeString(s, err) buff.WriteString("########缺失eLng#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[8], unixTime, "sPlace78", "ePlace78", csLat, csLng, ceLat, "003", "002", 8.2) writeString(s, err) buff.WriteString("########缺失sCity#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&eCity=%v&distance=%v" s, err := ts.G(str, uidSlice[8], unixTime, "sPlace88", "ePlace88", csLat, csLng, ceLat, ceLng, "002", 8.2) writeString(s, err) buff.WriteString("########缺失eCity#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&distance=%v" s, err := ts.G(str, uidSlice[8], unixTime, "sPlace98", "ePlace98", csLat, csLng, ceLat, ceLng, "003", 8.2) writeString(s, err) buff.WriteString("########缺失distance#########") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v" s, err := ts.G(str, uidSlice[8], unixTime, "sPlace08", "ePlace08", csLat, csLng, ceLat, ceLng, "003", "002") writeString(s, err) buff.WriteString("\n***********************错误参数格式加入****************\n") str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" s, err := ts.G(str, "userid", "unixTime", "sPlace8", "ePlace8", "csLat", "csLng", "ceLat", "ceLng", "003", "002", "8.2") writeString(s, err) buff.WriteString("\n***********************正常参数加入****************\n") unixTime := t.Unix() str := "/route/dadd?uid=%v&startTime=%v&sPlace=%v&ePlace=%v&sLat=%v&sLng=%v&eLat=%v&eLng=%v&sCity=%v&eCity=%v&distance=%v" for i := 0; i < 8; i++ { f := rand.Float64() * 0.0015 s, err := ts.G(str, uidSlice[i], unixTime, "sPlace"+strconv.Itoa(i), "ePlace"+strconv.Itoa(i), csLat+f, csLng+f/2, ceLat+f, ceLng+f/2, "001", "002", 8.2) writeString(s, err) } //起点城市不一致 s, err := ts.G(str, uidSlice[8], unixTime, "sPlace8", "ePlace8", csLat, csLng, ceLat, ceLng, "003", "002", 8.2) writeString(s, err) //终点城市不一致 s, err := ts.G(str, uidSlice[9], unixTime, "sPlace9", "ePlace9", csLat, csLng, ceLat, ceLng, "001", "003", 8.2) writeString(s, err) }