func Test_API_Task_List(t *testing.T) { assert := gaetest.NewAssert(t) assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_Task_List.json", nil), "fixture") now, _ := wcg.ParseDateTime("2016-01-10T00:00:00Z") lib.TemporarySetNow(now, func() { req := ts.GET("/api/admin/tasks/") res := req.RouteTo(instance.Routes()) assert.HTTPStatus(200, res) var got entities.Pagination assert.JSONResponse(&got, res) assert.EqInt(0, got.Length()) }) now, _ = wcg.ParseDateTime("2016-01-09T00:00:00Z") lib.TemporarySetNow(now, func() { req := ts.GET("/api/admin/tasks/") res := req.RouteTo(instance.Routes()) assert.HTTPStatus(200, res) var got entities.Pagination assert.JSONResponse(&got, res) assert.EqInt(1, got.Length()) }) }
func Test_parseUserFeed(t *testing.T) { assert := wcg.NewAssert(t) f, err := os.Open("./fixtures/Test_parseUserFeed.xml") assert.Nil(err, "Open fixture file") defer f.Close() albums, err := parseUserFeed(f) assert.Nil(err, "parseAlbums should return no error.") assert.EqInt(2, len(albums), "len(albums)") assert.EqStr("1000000487409559", albums[0].ID, "albums[0].ID") assert.EqStr("自動バックアップ", albums[0].Title, "albums[0].Title") assert.EqStr("109784491874587409559", albums[0].AuthorID, "albums[0].AuthorID") assert.EqStr("Yohei Sasaki", albums[0].AuthorName, "albums[0].AuthorName") publishedAt, _ := wcg.ParseDateTime("2015-11-29T11:03:42.000Z") updatedAt, _ := wcg.ParseDateTime("2015-11-29T15:32:17.582Z") assert.EqTime(publishedAt, *albums[0].PublishedAt, "albums[0].PublishedAt") assert.EqTime(updatedAt, *albums[0].UpdatedAt, "albums[0].UpdatedAt") assert.EqStr("6173825975323480657", albums[1].ID, "albums[0].ID") assert.EqStr("2015/07/21", albums[1].Title, "albums[0].Title") assert.EqStr("109784491874587409559", albums[1].AuthorID, "albums[0].AuthorID") assert.EqStr("Yohei Sasaki", albums[1].AuthorName, "albums[0].AuthorName") publishedAt, _ = wcg.ParseDateTime("2015-07-21T05:17:55.000Z") updatedAt, _ = wcg.ParseDateTime("2015-07-21T05:38:48.016Z") assert.EqTime(publishedAt, *albums[1].PublishedAt, "albums[0].PublishedAt") assert.EqTime(updatedAt, *albums[1].UpdatedAt, "albums[0].UpdatedAt") }
func Test_Agent_Recorder_Cancel(t *testing.T) { var agent *Agent var r Recorder assert := wcg.NewAssert(t) now, _ := wcg.ParseDateTime("2016-05-15T16:05:00Z") httptest.StartMockServer(func(mock *httptest.MockServer) { mock.Routes().GET("/api/intern/pt/iepg/records/", middleware.ServeFile("./fixtures/mocks/agent_tests.json")) agent = NewAgent(mock.BaseURL(), "test-token", nil) agent.recorderFactory = func(iepg *pt.IEPG) Recorder { return NewMockRecorder(iepg) } lib.TemporarySetNow(now, func() { agent.RunOnce() time.Sleep(100 * time.Millisecond) r = agent.recorders["200171201605160105"] assert.EqInt(1, len(agent.recorders), "agent should have a recorder thread.") assert.EqInt(int(pt.IEPGProcessing), int(r.GetStats().Status), "the recorder status should be pt.IEPGProcessing.") }) }) httptest.StartMockServer(func(mock *httptest.MockServer) { // preparation for dummy entry mock.Routes().GET("/api/intern/pt/iepg/records/", middleware.ServeFile("./fixtures/mocks/agent_tests_no_data.json")) agent.subscriber = NewSubscriber(mock.BaseURL(), "test-token") lib.TemporarySetNow(now, func() { agent.RunOnce() time.Sleep(100 * time.Millisecond) assert.EqInt(0, len(agent.recorders), "agent should have no recorder thread.") assert.EqInt(int(r.GetStats().Status), int(pt.IEPGCanceled), "the recorder status should be pt.IEPGPRocessing.") }) }) }
func Test_API_CronStatsCleanup(t *testing.T) { assert := gaetest.NewAssert(t) assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_SystemStats_Query.json", nil, "intern.home"), "fixture") sk, _ := Server.Get().Key("dummy").MustOne(ts.GET("/").Request) c := SystemStats.Query().Ancestor(sk).MustCount(ts.GET("/").Request) assert.EqInt(2, c) tempnow, _ := wcg.ParseDateTime("2014-12-01T11:00:00Z") lib.TemporarySetNow(tempnow, func() { app := NewApp() req := ts.GET("/cron/intern/home/stats/cleanup/") res := req.RouteTo(app.Routes()) assert.HTTPStatus(200, res) // confirm nothing deleted c = SystemStats.Query().Ancestor(sk).MustCount(ts.GET("/").Request) assert.EqInt(2, c) }) app := NewApp() req := ts.GET("/cron/intern/home/stats/cleanup/") res := req.RouteTo(app.Routes()) assert.HTTPStatus(200, res) // everything deleted since it's old c = SystemStats.Query().Ancestor(sk).MustCount(ts.GET("/").Request) assert.EqInt(0, c) }
// GetValue returns a form value in FormValidationTarget func (ovt *FormValidationTarget) GetValue(field string) interface{} { values := ovt.values[field] typed := ovt.fieldTypes[field] if values != nil { if len(values) > 0 { switch typed { case fieldTypeBool: return values[0] == "true" || values[0] == "1" case fieldTypeInt: v, _ := strconv.Atoi(values[0]) return v case fieldTypeDate: v, e := wcg.ParseDate(values[0]) if e != nil { return nil } return v case fieldTypeDateTime: v, e := wcg.ParseDateTime(values[0]) if e != nil { return nil } return v default: // string return values[0] } } } return nil }
func Test_API_SystemStats_Query(t *testing.T) { assert := gaetest.NewAssert(t) assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_SystemStats_Query.json", nil, "intern.home"), "fixture") app := NewApp() req := ts.GET("/api/intern/home/stats/servers/dummy/system/") res := req.RouteTo(app.Routes()) assert.HTTPStatus(200, res) var got []home.SystemStats assert.JSONResponse(&got, res) assert.EqInt(0, len(got), string(res.Body)) req = ts.GET("/api/intern/home/stats/servers/dummy/system/?until=2014-12-01T11:00:00Z") res = req.RouteTo(app.Routes()) assert.HTTPStatus(200, res) assert.JSONResponse(&got, res) assert.EqInt(2, len(got), string(res.Body)) tempnow, _ := wcg.ParseDateTime("2014-12-01T11:00:00Z") lib.TemporarySetNow(tempnow, func() { req := ts.GET("/api/intern/home/stats/servers/dummy/system/") res := req.RouteTo(app.Routes()) assert.HTTPStatus(200, res) var got []home.SystemStats assert.JSONResponse(&got, res) assert.EqInt(2, len(got), string(res.Body)) }) }
func Test_Subscribe(t *testing.T) { assert := wcg.NewAssert(t) httptest.StartMockServer(func(mock *httptest.MockServer) { // preparation for dummy entry mock.Routes().GET("/api/intern/pt/iepg/records/", middleware.ServeFile("./fixtures/mocks/subscriber_tests.json")) s := NewSubscriber(mock.BaseURL(), "test-token") t, _ := wcg.ParseDateTime("2016-05-12T16:00:00Z") lib.TemporarySetNow(t, func() { list, err := s.Subscribe() assert.Nil(err, "error should be nil") assert.EqInt(1, len(list), "Subscribe should return the list of *pt.IEPG") }) t, _ = wcg.ParseDateTime("2016-05-13T18:00:00Z") lib.TemporarySetNow(t, func() { list, err := s.Subscribe() assert.Nil(err, "error should be nil") assert.EqInt(0, len(list), "Subscribe should not return any IEPG") }) }) }
func Test_API_IEPGExclusion_RunTask(t *testing.T) { assert := gaetest.NewAssert(t) assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGExclusion_RunTask.json", nil, "intern.pt")) now, _ := wcg.ParseDateTime("2016-01-01T00:00:00Z") lib.TemporarySetNow(now, func() { runner := helper.NewAsyncTaskTestRunner(t, instance.Routes(), ts) runner.OnTrigger(func(req *httptest.TestRequest, trigger func()) { helper.TemporaryAllow(req, "family", trigger) }) runner.OnMonitor(func(req *httptest.TestRequest, monitor func()) { helper.TemporaryAllow(req, "family", monitor) }) runner.Run("/api/intern/pt/iepg/exclusions/task/", url.Values{}) req := ts.GET("/dummy") _, flagged := IEPG.Get().Key("test1").MustOne(req.Request) assert.OK(len(flagged.(*pt.IEPG).ExcludedBy) > 0) }) }
func Test_Agent_Recorder_Completed(t *testing.T) { assert := wcg.NewAssert(t) httptest.StartMockServer(func(mock *httptest.MockServer) { // preparation for dummy entry mock.Routes().GET("/api/intern/pt/iepg/records/", middleware.ServeFile("./fixtures/mocks/agent_tests.json")) agent := NewAgent(mock.BaseURL(), "test-token", nil) agent.recorderFactory = func(iepg *pt.IEPG) Recorder { return NewMockRecorder(iepg) } now, _ := wcg.ParseDateTime("2016-05-15T16:05:00Z") lib.TemporarySetNow(now, func() { agent.RunOnce() // started r := agent.recorders["200171201605160105"] agent.RunOnce() // notify update agent.RunOnce() // notify update agent.RunOnce() // notify update agent.RunOnce() // notify update and confirmed to be completed. assert.EqInt(0, len(agent.recorders), "agent should have no recorder thread.") assert.EqInt(int(r.GetStats().Status), int(pt.IEPGCompleted), "the recorder status should be pt.IEPGCompleted.") }) }) }
func convertJsonValueToProperties(k string, v interface{}) []datastore.Property { var propertyList []datastore.Property var value = reflect.ValueOf(v) switch value.Kind() { case reflect.String: p := datastore.Property{Name: k} s := v.(string) if strings.HasPrefix(s, "[]") { p.Value = []byte(strings.TrimPrefix(s, "[]")) p.NoIndex = true propertyList = append(propertyList, p) } else { if dt, err := wcg.ParseDateTime(fmt.Sprintf("%s", v)); err == nil { p.Value = dt propertyList = append(propertyList, p) } else if d, err := wcg.ParseDate(fmt.Sprintf("%s", v)); err == nil { p.Value = d propertyList = append(propertyList, p) } else { p.Value = s propertyList = append(propertyList, p) } } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: // reach here from FixtureFromMap since it can contain non floating number. var vv int64 switch v.(type) { case int: vv = int64(v.(int)) case int8: vv = int64(v.(int8)) case int16: vv = int64(v.(int16)) case int32: vv = int64(v.(int32)) case int64: vv = v.(int64) } propertyList = append(propertyList, datastore.Property{ Name: k, Value: vv, }) case reflect.Float32, reflect.Float64: str := []byte(fmt.Sprintf("%f", v)) if _floatRe.Match(str) { // should be int. propertyList = append(propertyList, datastore.Property{ Name: k, Value: int64(v.(float64)), }) } else { propertyList = append(propertyList, datastore.Property{ Name: k, Value: v, }) } case reflect.Bool: propertyList = append(propertyList, datastore.Property{ Name: k, Value: v, }) case reflect.Map: for k1, v1 := range v.(map[string]interface{}) { if !strings.HasPrefix(k1, "_") { for _, val := range convertJsonValueToProperties(k1, v1) { propertyList = append(propertyList, datastore.Property{ Name: fmt.Sprintf("%s.%s", k, val.Name), Value: val.Value, Multiple: val.Multiple, }) } } } propertyList = append(propertyList) case reflect.Slice: for i := 0; i < value.Len(); i++ { propertyList = append(propertyList, datastore.Property{ Name: k, Value: value.Index(i).Interface(), Multiple: true, }) } default: break } return propertyList }
// time reflect.TypeOf(time.Duration(0)): func(s string) (reflect.Value, error) { v, err := time.ParseDuration(s) return reflect.ValueOf(v), err }, reflect.TypeOf(time.Now()): func(s string) (reflect.Value, error) { switch s { case _ValueMacroTimeNow: return reflect.ValueOf(lib.Now()), nil case _ValueMacroTimeToday: return reflect.ValueOf(lib.Today()), nil case "": return reflect.ValueOf(time.Time{}), nil default: if t, err := wcg.ParseDateTime(s); err == nil { return reflect.ValueOf(t), err } else if t, err := wcg.ParseDate(s); err == nil { return reflect.ValueOf(t), err } else { // as JSON representation var t time.Time err := json.Unmarshal([]byte(fmt.Sprintf("\"%s\"", s)), &t) return reflect.ValueOf(t), err } } }, // String reflect.TypeOf(""): func(s string) (reflect.Value, error) { return reflect.ValueOf(s), nil