Example #1
0
func Test_API_Task_Sync(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context, "hplink")

	httptest.StartMockServer(func(mock *httptest.MockServer) {
		mock.Routes().GET("/", middleware.ServeFile("./fixtures/mocks/helloproject.html"))
		mock.Routes().GET("/c-ute/", middleware.ServeFile("./fixtures/mocks/c-ute/index.html"))
		mock.Routes().GET("/c-ute/*", middleware.StaticFile("", "fixtures/mocks/c-ute/"))

		crawler.MockExternalResource(map[string]string{
			"http://www.helloproject.com/artist/":                             fmt.Sprintf("%s", mock.BaseURL()),
			"http://www.helloproject.com/artist/c-ute/":                       fmt.Sprintf("%sc-ute/", mock.BaseURL()),
			"http://www.helloproject.com/artist/c-ute/profile/maimi_yajima/":  fmt.Sprintf("%sc-ute/maimi_yajima.html", mock.BaseURL()),
			"http://www.helloproject.com/artist/c-ute/profile/saki_nakajima/": fmt.Sprintf("%sc-ute/saki_nkajima.html", mock.BaseURL()),
			"http://www.helloproject.com/artist/c-ute/profile/airi_suzuki/":   fmt.Sprintf("%sc-ute/airi_suzuki.html", mock.BaseURL()),
			"http://www.helloproject.com/artist/c-ute/profile/chisato_okai/":  fmt.Sprintf("%sc-ute/chisato_okai.html", mock.BaseURL()),
			"http://www.helloproject.com/artist/c-ute/profile/mai_hagiwara/":  fmt.Sprintf("%sc-ute/mai_hagiwara.html", mock.BaseURL()),
		}, func() {
			runner := testhelper.NewAsyncTaskTestRunner(t, instance.Routes(), ts)
			runner.OnMonitor(func(testreq *httptest.TestRequest, f func()) {
				testhelper.TemporaryAllow(testreq, "hplink.admin", f)
			})
			runner.OnTrigger(func(testreq *httptest.TestRequest, f func()) {
				testhelper.TemporaryAllow(testreq, "hplink.admin", f)
			})
			runner.Run("/api/hplink/tasks/profile/", url.Values{})

			p := entities.Artist.Query().Order("Index").MustExecute(ts.GET("/").Request)
			artistList := p.Data.([]hplink.Artist)
			assert.EqInt(1, len(artistList))
			assert.EqInt(0, artistList[0].Index)
			assert.EqStr("c-ute", artistList[0].Key)
			assert.EqStr("℃-ute", artistList[0].Name)

			p = entities.Member.Query().Order("Index").Filter("ArtistKey=", "c-ute").MustExecute(ts.GET("/").Request)
			memberList := p.Data.([]hplink.Member)
			assert.EqInt(5, len(memberList))
			assert.EqInt(0, memberList[0].Index)
			assert.EqStr("c-ute", memberList[0].ArtistKey)
			assert.EqStr("c-ute.maimi_yajima", memberList[0].Key)
			assert.EqStr("矢島舞美", memberList[0].Name)

			entities.FillMemberPublicProfiles(ts.GET("/").Request, memberList)
			assert.NotNil(memberList[0].PublicProfile)
			assert.EqStr("c-ute.maimi_yajima", memberList[0].PublicProfile.Key)
			assert.EqStr("埼玉県", memberList[0].PublicProfile.Hometown)

			_, settings := entities.CrawlerSettings.Get().Key("http://ameblo.jp/c-ute-official").MustOne(ts.GET("/").Request)
			assert.NotNil(settings)
			assert.EqStr("c-ute", settings.(*hplink.CrawlerSettings).ArtistKey)
			assert.EqInt(int(hplink.CrawlerSettingsTypeAmeblo), int(settings.(*hplink.CrawlerSettings).Type))
		})
	})
}
func Test_API_IEPGKeyword_Crawl_Preview(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetMemcache(ts.Context))
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGKeyword_Crawl.json", nil, "intern.pt"), "fixture")

	httptest.StartMockServer(func(mock *httptest.MockServer) {
		// preparation for dummy entry
		mock.Routes().GET("/*", middleware.StaticFile("", "./fixtures/mocks/"))

		// Preview Ttest
		req := ts.POSTForm("/api/intern/pt/iepg/keywords/preview/", url.Values{
			"keyword": []string{"テスト"},
			"scope":   []string{"0"},
		})

		var feedIDs []string
		req.Request.SetLocal("__test_iepg_url_resolver", &iepgURLResolver{
			getFeedURL: func(keyword string, scope pt.IEPGCrawlerScope) string {
				assert.EqStr("テスト", keyword, "keyword")
				assert.EqInt(int(pt.IEPGCrawlerScopeAll), int(scope), "scope")
				return fmt.Sprintf(
					"%s/feed/feed.rss",
					mock.BaseURL(),
				)
			},
			getDataURL: func(id string) string {
				feedIDs = append(feedIDs, id)
				return fmt.Sprintf(
					"%s/data/%s.txt",
					mock.BaseURL(),
					id,
				)
			},
		})

		helper.TemporaryAllow(req, "family", func() {
			res := req.RouteTo(instance.Routes())
			assert.HTTPStatus(200, res)
			assert.EqInt(4, len(feedIDs), "collected Feed IDs")
			assert.EqStr("succ", feedIDs[0], "feedIDs[0]")
			assert.EqStr("opted-out", feedIDs[1], "feedIDs[1]")
			assert.EqStr("excluded", feedIDs[2], "feedIDs[2]")
			assert.EqStr("multiple-categories", feedIDs[3], "feedIDs[3]")
			var got map[string][]pt.IEPG
			assert.JSONResponse(&got, res)
			assert.EqInt(4, len(got["hits"]), "len(got[\"hits\"])")

			var hits = got["hits"]
			var succ = hits[0]
			var optout = hits[1]
			var excluded = hits[2]
			var multiplecategories = hits[3]

			// assertion for succ
			assert.EqStr("DFS00430", succ.StationID, "succ.StationID")
			assert.EqStr("テレビ東京", succ.StationName, "succ.StationName")
			assert.EqStr(
				"The\u3000Girls\u3000Live\u3000▽道重さゆみ卒業ライブに密着▽LoVendoЯスタジオライブ",
				succ.ProgramTitle,
				"succ.ProgramTitle",
			)
			assert.EqInt(2014, succ.StartAt.Year(), "succ.StartAt.Year()")
			assert.EqInt(12, int(succ.StartAt.Month()), "succ.StartAt.Month()")
			assert.EqInt(5, succ.StartAt.Day(), "succ.StartAt.Day()")
			assert.EqInt(23, succ.StartAt.Hour(), "succ.StartAt.Hour()")
			assert.EqInt(0, succ.StartAt.Minute(), "succ.StartAt.Minute()")
			assert.EqInt(2014, succ.EndAt.Year(), "succ.EndAt.Year()")
			assert.EqInt(12, int(succ.EndAt.Month()), "succ.EndAt.Month()")
			assert.EqInt(6, succ.EndAt.Day(), "succ.EndAt.Day()")
			assert.EqInt(1, succ.EndAt.Hour(), "succ.EndAt.Hour()")
			assert.EqInt(30, succ.EndAt.Minute(), "succ.EndAt.Minute()")
			assert.EqStr("23", succ.CID, "succ.CID")
			assert.EqStr("hd", succ.SID, "succ.SID")
			assert.Not(succ.OptIn, "succ.OptIn")
			assert.Not(succ.OptOut, "succ.OptOut")
			assert.EqInt(0, len(succ.ExcludedBy), "len(succ.ExcludedBy)")
			assert.EqInt(0, len(succ.CrawledBy), "len(succ.CrawledBy)")

			// assertion for optout
			assert.Not(optout.OptIn, "OptIn")
			assert.OK(optout.OptOut, "OptOut")
			assert.EqInt(0, len(optout.ExcludedBy), "len(ExcludedBy)")

			// assertion for excluded
			assert.Not(excluded.OptIn, "excluded.OptIn")
			assert.Not(excluded.OptOut, "excluded.OptOut")
			assert.EqInt(1, len(excluded.ExcludedBy), "len(excluded.ExcludedBy)")
			assert.EqStr("crawl-test-exclusion", excluded.ExcludedBy[0], "excluded.ExcludedBy[0]")

			// assertion for multiple categories.
			assert.EqInt(1, len(multiplecategories.CrawledBy), "len(multiplecategories.CrawledBy)")
			assert.EqStr("another-crawl-keyword", multiplecategories.CrawledBy[0], "multiplecategories.CrawledBy[0]")

		})
	})
}