Example #1
0
func TestCreateAndDeleteEvent(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		d := NewEventDriver(appCtx)
		t, err := d.Save(&event.Event{
			Title:     "Test Tour",
			Link:      "http://example.com",
			ImageLink: "http://example.com/foo.jpg",
		})
		assert.Nil(err, "TourDriver#Save should not return an error.")
		assert.Ok(t.Id != "", "TourDriver#Save should set Id for the given tour object.")

		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm the tour has been inserted within timeout window.")

		err = d.Delete(t.Id)
		assert.Nil(err, "TourDriver#Delete should not return an error.")

		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm the tour has been deleted within timeout window.")
	})
}
Example #2
0
func TestRecorderStart(t *testing.T) {
	assert := wcg.NewAssert(t)
	receiver := make(chan []Record)
	recorder := NewRecorder((<-chan []Record)(receiver))

	interval := time.Duration(30) * time.Millisecond
	wait := 10
	go recorder.Start(interval)

	now := time.Now()
	r1 := NewDummyRecord("r1") // cancel before starting
	r1.startAt = now
	r1.endAt = now.Add(time.Duration(5 * time.Second))
	receiver <- []Record{r1}
	err := util.WaitFor(func() bool {
		return len(recorder.controls) == 1
	}, wait)
	assert.Nil(err, "check r1 in controls.")
	ctrl := recorder.controls[r1.Key()]

	err = util.WaitFor(func() bool {
		return r1.done == true
	}, wait)
	assert.Nil(err, "check r1 has been done.")
	assert.EqInt(int(RSSucceeded), int(ctrl.state), "Record state should be RSSucceeded")
	recorder.Stop()
}
func TestChannel_AddAndDelCrawlerConfig(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := wcg.NewAssert(t)

		// prepare
		d := NewCrawlerConfigDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))
		d.Add(&tv.CrawlerConfig{
			Keyword:  "キーワード1",
			Category: "カテゴリー1",
			Scope:    1,
		})
		d.Add(&tv.CrawlerConfig{
			Keyword:  "キーワード2",
			Category: "カテゴリー2",
			Scope:    1,
		})

		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 2
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm CrawlerConfig entities has been stored within a timeout window.")

		d.Delete("キーワード1")
		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm CrawlerConfig entities has been deleted within a timeout window.")

		err = d.Delete("Not Exists")
		assert.Nil(err, "Delete should not return even trying to delete the unexising keyword.")
	})
}
Example #4
0
func TestRecorderStart_CancelWhileRecording(t *testing.T) {
	assert := wcg.NewAssert(t)
	receiver := make(chan []Record)
	recorder := NewRecorder((<-chan []Record)(receiver))
	interval := time.Duration(30) * time.Millisecond
	wait := 5

	go recorder.Start(interval)

	now := time.Now()
	r1 := NewDummyRecord("r1") // cancel before starting
	r1.startAt = now
	r1.endAt = now.Add(time.Duration(30 * time.Minute))

	receiver <- []Record{r1}
	err := util.WaitFor(func() bool {
		return len(recorder.controls) == 1
	}, wait)
	assert.Nil(err, "check r1 in controls.")
	assert.EqStr(r1.Key(), recorder.controls[r1.Key()].record.Key(), "check r1 in controls.")

	err = util.WaitFor(func() bool {
		return recorder.controls[r1.Key()].state == RSRecording
	}, wait)
	assert.Nil(err, "check r1 in RSRecording state.")

	receiver <- []Record{}
	err = util.WaitFor(func() bool {
		return len(recorder.controls) == 0
	}, wait)
	assert.Nil(err, "check r1 removed from controls.")

	recorder.Stop()
}
Example #5
0
func TestDelChannelApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		// prepare
		d := NewTvChannelDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))
		ent1 := &tv.TvChannel{"c1", "s1", "foo", "bar"}
		ent2 := &tv.TvChannel{"c2", "s2", "hoge", "piyo"}
		d.Put(d.NewKey(ent1.Key(), 0, nil), ent1)
		d.Put(d.NewKey(ent2.Key(), 0, nil), ent2)

		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 2
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm TvChannel entities has been stored within a timeout window.")
		p := app.Api.Path("/channels/c1/s1.json")
		req := ts.Delete(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)

		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "DELETE %s Confirm TvChannel entities has been deleted via API within a timeout window.", p)

		// Confirm cache invalidation
		mc := memcache.NewDriver(ts.Context, wcg.NewLogger(nil))
		assert.Ok(!mc.Exists(MC_KEY_CHANNELS), "DELETE %s should invalidate the cache", p)

	})
}
Example #6
0
func TestRecorderStart_CancelBeforeStart(t *testing.T) {
	assert := wcg.NewAssert(t)
	receiver := make(chan []Record)
	recorder := NewRecorder((<-chan []Record)(receiver))

	interval := time.Duration(30) * time.Millisecond
	wait := 10
	go recorder.Start(interval)

	now := time.Now()
	r1 := NewDummyRecord("r1") // cancel before starting
	r1.startAt = now.Add(time.Duration(30 * time.Minute))
	r1.endAt = now.Add(time.Duration(60 * time.Minute))
	// r2 := NewDummyRecord("r2") // cancel after starting
	// r3 := NewDummyRecord("r3") // succeeded
	// r4 := NewDummyRecord("r4") // failed

	receiver <- []Record{r1}
	err := util.WaitFor(func() bool {
		return len(recorder.controls) == 1
	}, wait)
	assert.Nil(err, "check r1 in controls.")
	assert.EqStr(r1.Key(), recorder.controls[r1.Key()].record.Key(), "check r1 in controls.")

	receiver <- []Record{}
	err = util.WaitFor(func() bool {
		return len(recorder.controls) == 0
	}, wait)
	assert.Nil(err, "check r1 removed from controls.")

	recorder.Stop()
}
Example #7
0
func Test_updateIndexes(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		d := NewAmebloEntryDriver(appCtx)
		amUrl := "http://ameblo.jp/foo/bar.html"
		key := d.NewKey(amUrl, 0, nil)

		t1 := time.Now()
		ent := &ameblo.AmebloEntry{
			Title:      "Test Title",
			Owner:      "Myblog",
			Url:        amUrl,
			UpdatedAt:  t1,
			CrawledAt:  t1,
			Content:    "crawled content",
			AmLikes:    5,
			AmComments: 10,
		}
		err := updateIndexes(appCtx, []*ameblo.AmebloEntry{ent})
		assert.Nil(err, "UpdateIndexes() should not return an error on creating an ameblo entry")

		err = util.WaitFor(func() bool {
			var ent ameblo.AmebloEntry
			d.Get(key, &ent)
			return ent.Url == amUrl
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm entry has been inserted within timeout window.")

		// test confirm not to update content and crawled at
		t2 := time.Now()
		newent := &ameblo.AmebloEntry{
			Title:      "(Updated) Test Title",
			Owner:      "(Updated) Myblog",
			Url:        "http://ameblo.jp/foo/bar.html",
			UpdatedAt:  t2,
			CrawledAt:  t2,
			Content:    "",
			AmLikes:    10,
			AmComments: 15,
		}
		err = updateIndexes(appCtx, []*ameblo.AmebloEntry{newent})
		assert.Nil(err, "UpdateIndexes() should not return an error on creating an ameblo entry")

		err = util.WaitFor(func() bool {
			var updatedent ameblo.AmebloEntry
			d.Get(key, &updatedent)
			return updatedent.AmLikes == 10 && updatedent.AmComments == 15
		}, util.DefaultWaitForTimeout)

		var updatedent ameblo.AmebloEntry
		d.Get(key, &updatedent)
		assert.Nil(err, "AmLikes and AmComments should be updated after UpdateIndexes() without timeout")
		assert.EqStr("crawled content", updatedent.Content, "Content should not be updated after UpdateIndexes()")
		assert.Ok(t1.Unix() == updatedent.CrawledAt.Unix(), "CrawledAt should not be updated UpdateIndexes")
	})
}
Example #8
0
func TestUpdatePost(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.App, ts)

		// TODO: fixture
		// prepare
		d := NewPostDriver(appCtx)
		post1, _ := d.Save(&blog.Post{
			Title:     "test post1",
			Content:   "test content",
			Tags:      []string{},
			IsDraft:   false,
			IsHidden:  false,
			PublishAt: time.Now(),
		})

		util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)

		// test
		p := app.Api.Path(fmt.Sprintf("/posts/%s.json", post1.Id))
		req := ts.PutForm(p, url.Values{
			"title":      []string{"updated: test post1"},
			"content":    []string{"updated: test content"},
			"publish_at": []string{"2015-01-01T12:04:05Z"}, // ISO8601 format
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)

		assert.Nil(util.WaitFor(func() bool {
			var p blog.Post
			key := d.NewKey(post1.Id, 0, nil)
			d.Get(key, &p)
			return "updated: test post1" == p.Title
		}, util.DefaultWaitForTimeout), "Confirm update")

		// case 404 not found
		p = app.Api.Path(fmt.Sprintf("/api/default/posts/%s.json", "not-found"))
		req = ts.PutForm(p, url.Values{
			"title":      []string{"updated: test post1"},
			"content":    []string{"updated: test content"},
			"publish_at": []string{"2015-01-01T12:04:05Z"}, // ISO8601 format
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(404, res)
	})
}
Example #9
0
func TestUpdateApiToken(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app, ts)

		// prepare
		d := lib.NewApiTokenDriver(appCtx)
		tok, _ := d.Issue("token1")
		d.Issue("token2")
		util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 2
		}, util.DefaultWaitForTimeout)

		var tokUrl = fmt.Sprintf("/api/admin/api_tokens/%s/", tok.Key())
		// Case 400
		req := ts.PutForm(tokUrl, url.Values{
			"desc":     []string{"updated"},
			"alert_on": []string{"-20"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(400, res)

		// Case 200
		req = ts.PutForm(tokUrl, url.Values{
			"desc":     []string{"updated"},
			"alert_on": []string{"30"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)

	})
}
Example #10
0
func TestRecorderUpcomming(t *testing.T) {
	assert := wcg.NewAssert(t)
	receiver := make(chan []Record)
	recorder := NewRecorder((<-chan []Record)(receiver))
	interval := time.Duration(30) * time.Millisecond
	wait := 10
	go recorder.Start(interval)

	r1 := NewDummyRecord("r1")
	r1.startAt = time.Now().Add(time.Duration(2 * time.Hour))
	r1.endAt = r1.startAt.Add(time.Duration(10 * time.Minute))
	r2 := NewDummyRecord("r2")
	r2.startAt = time.Now().Add(time.Duration(4 * time.Hour))
	r2.endAt = r2.startAt.Add(time.Duration(10 * time.Minute))
	r3 := NewDummyRecord("r3")
	r3.startAt = time.Now().Add(time.Duration(8 * time.Hour))
	r3.endAt = r3.startAt.Add(time.Duration(10 * time.Minute))

	receiver <- []Record{r1, r2, r3}
	err := util.WaitFor(func() bool {
		return len(recorder.controls) == 3
	}, wait)
	assert.Nil(err, "check all records in controls.")

	upcome := recorder.Upcomming()
	assert.Ok(r1.startAt.Equal(upcome), "Upcomming should be r2")
	recorder.Stop()
}
Example #11
0
func TestApiUpdateEvent(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiUpdateEvent.json", nil)
		d := NewEventDriver(appCtx)

		var e event.Event

		p := app.TestApp().Api.Path("/events/event1.json")
		req := ts.PutForm(p, url.Values{
			"title":      []string{"Updated Title"},
			"link":       []string{"http://www.up.example.com/"},
			"image_link": []string{"http://www.up.example.com/img.png"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(200, res)

		err := util.WaitFor(func() bool {
			d.Load("event1", &e)
			return e.Title == "Updated Title"
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm the show has been inserted within timeout window.")

		assert.EqStr("Updated Title", e.Title, "Title")
		assert.EqStr("http://www.up.example.com/", e.Link, "Link")
		assert.EqStr("http://www.up.example.com/img.png", e.ImageLink, "ImageLink")
	})
}
Example #12
0
func TestPt1Record_Start(t *testing.T) {
	assert := wcg.NewAssert(t)
	now := time.Now()

	tvrecord := tv.NewTvRecord(
		"Title",
		"Category",
		now,
		now.Add(time.Duration(5)*time.Second),
		"cid",
		"sid",
		"uid",
	)
	util.WithTempDir(func(dir string) {
		record := NewPt1Record(tvrecord, genPt1Config(dir))
		err := record.Start()
		assert.Nil(err, "Pt1Record started.")
		time.Sleep(1 * time.Second)
		assert.Ok(record.IsRunning(), "Pt1Record is running.")

		err = record.Stop()
		assert.Nil(err, "Stop record.")

		err = util.WaitFor(func() bool {
			return record.IsRunning() == false
		}, 10)
		assert.Nil(err, "Pt1Record has stopped.")

		content, _ := ioutil.ReadFile(record.filepath)
		lines := bytes.Split(content, []byte("\n"))
		assert.EqStr("OK", string(lines[len(lines)-1]), "Check the mock record content.")
	})
}
Example #13
0
func TestApiCreateEvent_201(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		d := NewEventDriver(appCtx)

		var respJson map[string]interface{}
		var e event.Event

		p := app.TestApp().Api.Path("/events/")
		req := ts.PostForm(p, url.Values{
			"title":      []string{"test tour title"},
			"link":       []string{"http://www.example.com/"},
			"image_link": []string{"http://www.example.com/img.png"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(201, res)

		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm the show has been inserted within timeout window.")

		res.Json(&respJson)
		d.Load(respJson["id"].(string), &e)
		assert.EqStr("test tour title", e.Title, "Title")
		assert.EqStr("http://www.example.com/", e.Link, "Link")
		assert.EqStr("http://www.example.com/img.png", e.ImageLink, "ImageLink")
	})
}
Example #14
0
func TestGetOverlaps(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		var found []*tv.TvRecord
		const timeForm = "2006/01/02 15:04:05"
		assert := wcg.NewAssert(t)
		d := NewTvRecordDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))

		base := genTestRecord()
		base.StartAt, _ = time.Parse(timeForm, "2014/01/01 12:00:00")
		base.EndAt, _ = time.Parse(timeForm, "2014/01/01 13:00:00")
		d.Save(base)
		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm TvRecord entities has been stored within a timeout window.")

		// case1: no overlaps
		r := genTestRecord()
		r.StartAt, _ = time.Parse(timeForm, "2014/01/01 10:50:00")
		r.EndAt, _ = time.Parse(timeForm, "2014/01/01 11:10:00")
		found, err = d.GetOverlaps(r)
		assert.Nil(err, "TvRecordDriver#GetOverlaps should not return an error (case1)")
		assert.EqInt(0, len(found), "TvRecordDriver#GetOverlaps should return empty array (case1)")

		// case 2: base.end < r.start
		r = genTestRecord()
		r.StartAt, _ = time.Parse(timeForm, "2014/01/01 13:50:00")
		r.EndAt, _ = time.Parse(timeForm, "2014/01/01 14:10:00")
		found, err = d.GetOverlaps(r)
		assert.Nil(err, "TvRecordDriver#GetOverlaps should not return an error (case2)")
		assert.EqInt(0, len(found), "TvRecordDriver#GetOverlaps should return empty array (case2)")

		// case 3: base.start < r.start < base.end
		r = genTestRecord()
		r.StartAt, _ = time.Parse(timeForm, "2014/01/01 11:50:00")
		r.EndAt, _ = time.Parse(timeForm, "2014/01/01 12:10:00")
		found, err = d.GetOverlaps(r)
		assert.Nil(err, "TvRecordDriver#GetOverlaps should not return an error (case3)")
		assert.EqInt(1, len(found), "TvRecordDriver#GetOverlaps should return an array with an entity (case3)")

		// case 4: r.start < base.end < r.end
		r = genTestRecord()
		r.StartAt, _ = time.Parse(timeForm, "2014/01/01 12:50:00")
		r.EndAt, _ = time.Parse(timeForm, "2014/01/01 13:10:00")
		found, err = d.GetOverlaps(r)
		assert.Nil(err, "TvRecordDriver#GetOverlaps should not return an error (case4)")
		assert.EqInt(1, len(found), "TvRecordDriver#GetOverlaps should return an array with an entity (case4)")

		// case 5: r.start < base.start < base.end < r.end
		r = genTestRecord()
		r.StartAt, _ = time.Parse(timeForm, "2014/01/01 12:10:00")
		r.EndAt, _ = time.Parse(timeForm, "2014/01/01 12:50:00")
		found, err = d.GetOverlaps(r)
		assert.Nil(err, "TvRecordDriver#GetOverlaps should not return an error (case4)")
		assert.EqInt(1, len(found), "TvRecordDriver#GetOverlaps should return an array with an entity (case4)")
	})
}
Example #15
0
func TestGetPost(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.App, ts)

		// TODO: fixture
		// prepare
		d := NewPostDriver(appCtx)
		post1, _ := d.Save(&blog.Post{
			Title:     "test post1",
			Content:   "test content",
			Tags:      []string{},
			IsDraft:   false,
			IsHidden:  false,
			PublishAt: time.Now(),
		})
		post2, _ := d.Save(&blog.Post{
			Title:     "test post2",
			Content:   "test content",
			Tags:      []string{},
			IsDraft:   true,
			IsHidden:  false,
			PublishAt: time.Now(),
		})

		util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)

		// test post1 : public post
		var got *blog.Post
		p := app.Api.Path(fmt.Sprintf("/posts/%s.json", post1.Id))
		req := ts.Get(p)
		res := req.RouteTo(app.Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
		assert.EqStr("test post1", got.Title, "post1.Title")

		// test post2 : draft post, only visible by admins.
		p = app.Api.Path(fmt.Sprintf("/posts/%s.json", post2.Id))
		req = ts.Get(p)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(404, res)

		req = ts.Get(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
		assert.EqStr("test post2", got.Title, "post2.Title")
	})
}
Example #16
0
func TestDeletePost(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.App, ts)

		// TODO: fixture
		// prepare
		d := NewPostDriver(appCtx)
		post1, _ := d.Save(&blog.Post{
			Title:     "test post1",
			Content:   "test content",
			Tags:      []string{},
			IsDraft:   false,
			IsHidden:  false,
			PublishAt: time.Now(),
		})

		util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)

		// test
		req := ts.Delete(fmt.Sprintf("/api/default/posts/%s.json", post1.Id))
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)

		assert.Nil(util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 0
		}, util.DefaultWaitForTimeout), "Confirm deletion")

		// case 200 not found even the post does not exist.
		req = ts.Delete(fmt.Sprintf("/api/default/posts/%s.json", "not-found"))
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
	})
}
Example #17
0
func TestDeleteRecordApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		d := NewRecordCacheDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))

		// create an empty cache
		var got []tv.TvRecord
		p := app.Api.Path("/records/")
		req := ts.Get(p)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
		res.Json(&got)
		assert.EqInt(0, len(got), "GET %s should return 2 TvRecord entities.")
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "GET %s should create RecordCacheTypeTvRecord cache", p)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "GET %s should create RecordCacheTypeIEpg cache", p)

		// prepare
		in_window := genTestRecord()
		in_window.StartAt = d.today.Add(1 * time.Hour)
		in_window.EndAt = d.today.Add(2 * time.Hour)
		d.TvRecord.Save(in_window)

		future := genTestRecord()
		future.StartAt = d.today.Add(RECORD_TIME_WINDOW + 1*time.Hour)
		future.EndAt = future.StartAt.Add(1 * time.Hour)
		d.TvRecord.Save(future)

		err := util.WaitFor(func() bool {
			records, _ := d.TvRecord.NewQuery().Count()
			return records == 2
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm TvRecord entities has been stored within a timeout window.")

		// case 1: future
		p = fmt.Sprintf("%s%s.json", app.Api.Path("/records/"), future.Key())
		req = ts.Delete(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "POST %s should not invalidate RecordCacheTypeTvRecord cache (future record)", p)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "GET %s should not invalidate RecordCacheTypeIEpg cache (future record)", p)

		// case 2: in recording window
		p = fmt.Sprintf("%s%s.json", app.Api.Path("/records/"), in_window.Key())
		req = ts.Delete(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
		assert.Ok(!d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "POST %s should invalidate RecordCacheTypeTvRecord cache (in_window record)", p)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "GET %s should not invalidate RecordCacheTypeIEpg cache (in_window record)", p)
	})
}
Example #18
0
func TestChannel_AddAndDelChannel(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := wcg.NewAssert(t)

		// prepare
		d := NewTvChannelDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))
		d.AddChannel("c1", "s1", "foo", "bar")
		d.AddChannel("c2", "s2", "foo", "bar")

		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 2
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm TvChannel entities has been stored within a timeout window.")

		d.DelChannel("c1", "s1")
		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm TvChannel entities has been deleted within a timeout window.")

		var list []*tv.TvChannel
		d.NewQuery().GetAll(&list)
		assert.EqStr("c2", list[0].Cid, "Confirm TvChannel.Cid")

		d.AddChannelList([]*tv.TvChannel{
			&tv.TvChannel{"c3", "s3", "aaa", "aaa"},
			&tv.TvChannel{"c4", "s4", "bbb", "bbb"},
			&tv.TvChannel{"c5", "s5", "ccc", "ccc"},
		})

		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 4
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm TvChannel entities has been stored by AddChannelList within a timeout window.")
	})
}
Example #19
0
func TestDelKeywordApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		// prepare
		d := NewCrawlerConfigDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))
		d.Add(&tv.CrawlerConfig{
			Keyword:  "モーニング娘。'15",
			Category: "モーニング娘",
			Scope:    tv.FEED_SCOPE_ALL,
		})
		d.Add(&tv.CrawlerConfig{
			Keyword:  "SPEED",
			Category: "SPEED",
			Scope:    tv.FEED_SCOPE_ALL,
		})

		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 2
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm CrawlerConfig entities has been stored within a timeout window.")

		p := app.Api.Path("/keywords/モーニング娘。'15.json")
		req := ts.Delete(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)

		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "DELETE %s Confirm CrawlerConfig entities has been deleted via API within a timeout window.", p)

		// Confirm cache invalidation
		mc := memcache.NewDriver(ts.Context, wcg.NewLogger(nil))
		assert.Ok(!mc.Exists(MC_KEY_KEYWORDS), "DELETE %s should invalidate the cache", p)
	})
}
func TestRecordCache_GetRecords_and_Invalidate(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := wcg.NewAssert(t)
		d := NewRecordCacheDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))

		// prepare
		in_window := genTestRecord()
		in_window.StartAt = d.today.Add(1 * time.Hour)
		in_window.EndAt = d.today.Add(2 * time.Hour)
		d.TvRecord.Save(in_window)

		future := genTestRecord()
		future.StartAt = d.today.Add(RECORD_TIME_WINDOW + 1*time.Hour)
		future.EndAt = future.StartAt.Add(1 * time.Hour)
		d.TvRecord.Save(future)

		past := genTestRecord()
		past.StartAt = d.today.Add(-RECORD_TIME_WINDOW - 1*time.Hour)
		past.EndAt = past.StartAt.Add(30 * time.Minute)
		d.TvRecord.Save(past)

		in_window_iepg := genTestIEpg()
		in_window.StartAt = d.today.Add(4 * time.Hour)
		in_window.EndAt = d.today.Add(5 * time.Hour)
		d.IEpg.Save(in_window_iepg)

		err := util.WaitFor(func() bool {
			records, _ := d.TvRecord.NewQuery().Count()
			iepgs, _ := d.IEpg.NewQuery().Count()
			return records+iepgs == 4
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm TvRecord/IEpg entities has been stored within a timeout window.")

		list, err := d.GetRecords(false)
		assert.Nil(err, "RecordCache#GetRecords should not return an error")
		assert.EqInt(2, len(list), "RecordCache#GetRecords should return 2 records in a time window")

		// cache check
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "RecordCache#GetRecords should create RecordCacheTypeTvRecord on memcache.")
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "RecordCache#GetRecords should create RecordCacheTypeIEpg on memcache.")

		// invalidate the keys
		assert.Ok(!d.Invalidate(future), "RecordCache#Invalidate should not return true if the passed record is out of window (future)")
		assert.Ok(!d.Invalidate(past), "RecordCache#Invalidate should not return true if the passed record is out of window (future)")
		assert.Ok(d.Invalidate(in_window), "RecordCache#Invalidate should return true if the passed record is in window")
		assert.Ok(!d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "RecordCache#GetRecords should invalidate RecordCacheTypeTvRecord on memcache.")

		assert.Ok(d.Invalidate(in_window_iepg), "RecordCache#Invalidate should return true if the passed record is in window (iepg)")
		assert.Ok(!d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "RecordCache#GetRecords should invalidate RecordCacheTypeIEpg on memcache.")
	})
}
Example #21
0
func TestIepgBulkUpdate(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := wcg.NewAssert(t)
		d := NewIEpgDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))
		iepg1 := genTestIEpg()
		iepg1.ProgramTitle = "new"
		iepg1.StartAt, _ = wcg.ParseDateTime("2014/11/10 12:00")
		iepg1.EndAt, _ = wcg.ParseDateTime("2014/11/10 12:30")
		_, err := d.BulkUpdate([]*tv.IEpg{iepg1})
		assert.Nil(err, "IEpgDriver#BulkUpdate should not return an error (insert).")

		// insert
		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm Iepg entities has been stored within a timeout window.")

		// update
		iepg1.ProgramTitle = "update"
		_, err = d.BulkUpdate([]*tv.IEpg{iepg1})
		assert.Nil(err, "IEpgDriver#BulkUpdate should not return an error (update).")
		err = util.WaitFor(func() bool {
			found, err := d.Load(iepg1.Id)
			return err == nil && found.ProgramTitle == "update"
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm Iepg entities has been updated within a timeout window.")

		// Optout
		iepg1.Optout = true
		d.SyncSave(iepg1)

		iepg1.Optout = false
		iepg1.ProgramTitle = "optout"
		keys, _ := d.BulkUpdate([]*tv.IEpg{iepg1})
		assert.EqInt(0, len(keys), "IEpgDriver#BulkUpdate should not update optout entity.")
	})
}
Example #22
0
func TestApiUser(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		app := NewApp("default", "TestApp")
		d := NewApiTokenDriver(NewAppContextFromTestServer(app, ts))
		token, err := d.Issue("New Api Token")
		util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)

		user, err := d.ApiUser(token.Token)
		assert.Nil(err, "ApiTokenDriver#ApiUser should not return an error")
		assert.EqStr(token.Token, user.(*ApiUser).token.Token, "ApiUser should return the valid user that has the token itself")
		assert.GtInt(int(token.LastAccess.Unix()), int(user.(*ApiUser).token.LastAccess.Unix()), "ApiUser should update LastAccess time on the token.")
	})
}
Example #23
0
func TestAmebloCrawlContents(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)

		// prepare
		amUrl := "http://ameblo.jp/eriko--imai/entry-11980960390.html"
		d := NewAmebloEntryDriver(appCtx)
		refd := NewAmebloRefDriver(appCtx)

		t1 := time.Now()
		ent := &ameblo.AmebloEntry{
			Title:      "切り替え〜る",
			Owner:      "今井絵理子",
			Url:        amUrl,
			UpdatedAt:  t1,
			CrawledAt:  time.Time{},
			Content:    "",
			AmLikes:    5,
			AmComments: 10,
		}
		err := updateIndexes(appCtx, []*ameblo.AmebloEntry{ent})
		assert.Nil(err, "UpdateIndexes() should not return an error on creating an ameblo entry")
		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm AmebloEntry has been indexed.")

		p := app.TestApp().Api.Path("/ameblo/contents/")
		req := ts.Get(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(200, res)

		// confirm the content updated.
		var ent1 ameblo.AmebloEntry
		err = d.Get(d.NewKey(amUrl, 0, nil), &ent1)
		assert.Ok(len(ent1.Content) > 0, "GET %s should update the content", p)

		// confirm the reference updated
		c, _ := refd.NewQuery().Count()
		assert.EqInt(1, c, "GET %s should update the content reference", p)
	})
}
Example #24
0
func TestApiCreateEventShow(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiCreateEventShow.json", nil)
		d := NewShowDriver(appCtx)

		var respJson map[string]interface{}
		var show event.Show
		p := app.TestApp().Api.Path("/events/event1/shows")
		req := ts.PostForm(p, url.Values{
			"open_at":    []string{"2015-01-02T06:00:00Z"},
			"start_at":   []string{"2015-01-02T07:00:00Z"},
			"latitude":   []string{"37.39"},
			"longitude":  []string{"140.38"},
			"venue_id":   []string{"153237058036933"},
			"venue_name": []string{"郡山市民文化センター"},
			"pia_link":   []string{"http://pia.jp/link"},
			"ya_keyword": []string{"郡山"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(201, res)

		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm the show has been inserted within timeout window.")

		res.Json(&respJson)
		d.Load(respJson["id"].(string), "event1", &show)
		open_at, _ := util.ParseDateTime("2015-01-02T06:00:00Z")
		start_at, _ := util.ParseDateTime("2015-01-02T07:00:00Z")
		assert.EqTime(open_at, show.OpenAt, "OpenAt")
		assert.EqTime(start_at, show.StartAt, "StartAt")
		assert.EqFloat64(37.39, show.Latitude, "Latitude")
		assert.EqFloat64(140.38, show.Longitude, "Longitude")
		assert.EqStr("153237058036933", show.VenueId, "VenueId")
		assert.EqStr("郡山市民文化センター", show.VenueName, "VenueName")
		assert.EqStr("http://pia.jp/link", show.PiaLink, "PiaLink")
		assert.EqStr("郡山", show.YAKeyword, "YAKeyword")
	})
}
Example #25
0
func TestListRecordApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		d := NewRecordCacheDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))

		// prepare
		in_window := genTestRecord()
		in_window.StartAt = d.today.Add(1 * time.Hour)
		in_window.EndAt = d.today.Add(2 * time.Hour)
		d.TvRecord.Save(in_window)

		future := genTestRecord()
		future.StartAt = d.today.Add(RECORD_TIME_WINDOW + 1*time.Hour)
		future.EndAt = future.StartAt.Add(1 * time.Hour)
		d.TvRecord.Save(future)

		past := genTestRecord()
		past.StartAt = d.today.Add(-RECORD_TIME_WINDOW - 1*time.Hour)
		past.EndAt = past.StartAt.Add(30 * time.Minute)
		d.TvRecord.Save(past)

		in_window_iepg := genTestIEpg()
		in_window.StartAt = d.today.Add(4 * time.Hour)
		in_window.EndAt = d.today.Add(5 * time.Hour)
		d.IEpg.Save(in_window_iepg)

		err := util.WaitFor(func() bool {
			records, _ := d.TvRecord.NewQuery().Count()
			iepgs, _ := d.IEpg.NewQuery().Count()
			return records+iepgs == 4
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm TvRecord/IEpg entities has been stored within a timeout window.")

		var got []tv.TvRecord
		p := app.Api.Path("/records/")
		req := ts.Get(p)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
		res.Json(&got)
		assert.EqInt(2, len(got), "GET %s should return 2 TvRecord entities.", p)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "GET %s should create RecordCacheTypeTvRecord cache", p)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "GET %s should create RecordCacheTypeIEpg cache", p)
	})
}
Example #26
0
func TestListChannelApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		p := app.Api.Path("/channels/")
		err := util.WaitFor(func() bool {
			var got []tv.TvChannel
			req := ts.Get(p)
			res := req.RouteTo(app.Routes())
			assert.HttpStatus(200, res)
			res.Json(&got)
			return len(got) > 0
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "GET %s should return the list of TvChannels within a timeout window.", p)

		// Confirm cache invalidation
		mc := memcache.NewDriver(ts.Context, wcg.NewLogger(nil))
		assert.Ok(mc.Exists(MC_KEY_CHANNELS), "GET %s should create the cache", p)
	})
}
Example #27
0
func TestTvRecord_GetRecords(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		const timeForm = "2006/01/02 15:04:05"
		assert := wcg.NewAssert(t)
		d := NewTvRecordDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))

		// prepare
		r0 := genTestRecord()
		r0.StartAt, _ = time.Parse(timeForm, "2014/01/01 12:00:00")
		r0.EndAt, _ = time.Parse(timeForm, "2014/01/01 13:00:00")
		d.Save(r0)

		r1 := genTestRecord()
		r1.StartAt, _ = time.Parse(timeForm, "2014/01/02 15:00:00")
		r1.EndAt, _ = time.Parse(timeForm, "2014/01/02 16:00:00")
		d.Save(r1)

		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 2
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm TvRecord entities has been stored within a timeout window")

		since, _ := time.Parse(timeForm, "2014/01/01 01:00:00")
		until, _ := time.Parse(timeForm, "2014/01/02 23:00:00")
		list, err := d.GetRecords(since, until)
		assert.Nil(err, "TvRecordDriver#GetRecords should not return an error")
		assert.EqInt(2, len(list), "TvRecordDriver#GetRecords should return 2 records")

		since, _ = time.Parse(timeForm, "2014/01/02 01:00:00")
		until, _ = time.Parse(timeForm, "2014/01/02 23:00:00")
		list, err = d.GetRecords(since, until)
		assert.Nil(err, "TvRecordDriver#GetRecords should not return an error")
		assert.EqInt(1, len(list), "TvRecordDriver#GetRecords should return 1 record")

		since, _ = time.Parse(timeForm, "2014/01/03 01:00:00")
		until, _ = time.Parse(timeForm, "2014/01/03 23:00:00")
		list, err = d.GetRecords(since, until)
		assert.Nil(err, "TvRecordDriver#GetRecords should not return an error")
		assert.EqInt(0, len(list), "TvRecordDriver#GetRecords should return 0 records")
	})
}
Example #28
0
func TestApiDeleteEventShow(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiDeleteEventShow.json", nil)
		d := NewShowDriver(appCtx)

		p := app.TestApp().Api.Path("/events/event1/shows/event1-show1.json")
		req := ts.Delete(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(200, res)

		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 0
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm the show has been inserted within timeout window.")
	})
}
Example #29
0
func TestIssueApiToken(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app, ts)
		req := ts.PostForm("/api/admin/api_tokens/", url.Values{
			"desc": []string{"new token"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(201, res)

		var got []*models.ApiToken
		d := lib.NewApiTokenDriver(appCtx)
		util.WaitFor(func() bool {
			d.NewQuery().GetAll(&got)
			return got != nil && len(got) == 1
		}, util.DefaultWaitForTimeout)
		assert.EqStr("new token", got[0].Description, "IssueApiToken Description")
	})
}
Example #30
0
func TestAmebloHistoryInsights(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)

		// prepare
		d := NewAmebloEntryDriver(appCtx)
		amUrl := "http://ameblo.jp/eriko--imai/entry-11980960390.html"
		t1 := time.Now()
		ent := &ameblo.AmebloEntry{
			Title:      "切り替え〜る",
			Owner:      "今井絵理子",
			Url:        amUrl,
			UpdatedAt:  t1,
			CrawledAt:  time.Time{},
			Content:    "",
			AmLikes:    5,
			AmComments: 10,
		}
		err := updateIndexes(appCtx, []*ameblo.AmebloEntry{ent})
		assert.Nil(err, "UpdateIndexes() should not return an error on creating an ameblo entry")

		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm entry has been inserted within timeout window.")

		var got amebloHistoryInsights
		p := app.TestApp().Api.Path("/ameblo/insights/今井絵理子/history.json")
		req := ts.Get(p)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(200, res)
		res.Json(&got)
		assert.EqInt(5, got.TotalLikes, "GET %s TotalLikes", p)
		assert.EqInt(10, got.TotalComments, "GET %s TotalComments", p)
		assert.EqInt(1, got.TotalPosts, "GET %s TotalPosts", p)
		assert.EqInt(1, len(got.History), "GET %s length of history", p)
		assert.EqStr(amUrl, got.History[0].Url, "GET %s hitory Url", p)
	})
}