Exemple #1
0
func TestApiCreateEvent_400(t *testing.T) {
	var genParams = func(omitName string) url.Values {
		params := url.Values{
			"title":      []string{"test tour title"},
			"link":       []string{"http://www.example.com/"},
			"image_link": []string{"http://www.example.com/img.png"},
		}
		params.Del(omitName)
		return params
	}

	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiCreateEventShow.json", nil)
		p := app.TestApp().Api.Path("/events/")
		var testOmitParams = []string{
			"title", "link", "image_link",
		}
		for _, paramName := range testOmitParams {
			req := ts.PostForm(p, genParams(paramName))
			lib.SetApiTokenForTest(req, lib.Admin)
			res := req.RouteTo(app.TestApp().Routes())
			assert.HttpStatus(400, res)
		}
	})
}
Exemple #2
0
func TestDeleteAmebloContents(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)

		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")

		p := app.TestApp().Api.Path("/ameblo/contents/今井絵理子.json")
		req := ts.Delete(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(ent1.CrawledAt.Equal(time.Time{}), "DELETE %s should delete the cralwing timestamp.", p)
	})
}
Exemple #3
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")
	})
}
func TestApiCreateEventShow_400(t *testing.T) {
	var genParams = func(omitName string) url.Values {
		params := 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{"郡山"},
		}
		params.Del(omitName)
		return params
	}

	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiCreateEventShow.json", nil)
		p := app.TestApp().Api.Path("/events/event1/shows")
		var testOmitParams = []string{
			"open_at", "start_at", "latitude", "longitude", "venue_id", "venue_name", // "pia_link", "ya_keyword",
		}
		for _, paramName := range testOmitParams {
			req := ts.PostForm(p, genParams(paramName))
			lib.SetApiTokenForTest(req, lib.Admin)
			res := req.RouteTo(app.TestApp().Routes())
			assert.HttpStatus(400, res)
		}
	})
}
Exemple #5
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")
	})
}
func TestApiCreateEventShow_404(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiCreateEventShow.json", nil)
		p := app.TestApp().Api.Path("/events/notexit/shows")
		req := ts.PostForm(p, url.Values{})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(404, res)
	})
}
Exemple #7
0
func TestAmebloIndexApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		var got map[string][]string
		p := app.TestApp().Api.Path("/ameblo/indexes/")
		req := ts.Get(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
		assert.EqInt(2, len(got), "GET %s should return the list of crawled results.", p)
		assert.EqInt(20, len(got["今井絵理子"]), "GET %s return 20 entries for '今井絵理子'.", p)
	})
}
func TestApiListEventShow(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiListEventShow.json", nil)
		var respJson []event.Show
		p := app.TestApp().Api.Path("/events/event1/shows")
		req := ts.Get(p)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(200, res)
		res.Json(&respJson)
		assert.EqInt(1, len(respJson), "shows should return 1 element")
		assert.EqStr("event1-show1", respJson[0].Id, "Id")
		assert.EqStr("event1", respJson[0].EventId, "EventId")
	})
}
Exemple #9
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.")
	})
}
Exemple #10
0
func TestAmebloIndexAllApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		var got []string
		p := app.TestApp().Api.Path("/ameblo/indexes/今井絵理子.json")
		req := ts.Get(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
		assert.Ok(
			len(got) > 20,
			"GET %s should return more thant 20 entries for '今井絵理子' but got %d.",
			p, len(got))
	})
}
Exemple #11
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)
	})
}
Exemple #12
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")
	})
}
Exemple #13
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")
	})
}
Exemple #14
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.")
	})
}
Exemple #15
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)
	})
}
Exemple #16
0
func TestNewEventShowList(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := wcg.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		test.DatastoreFixture(ts.Context, "./fixtures/TestNewEventShowList.json", nil)

		var showList []event.Show
		d := NewShowDriver(appCtx)
		d.NewQuery().GetAll(&showList)
		assert.EqInt(2, len(showList), "2 Show entities must be prepared.")
		list, _ := NewEventShowList(appCtx, showList)

		// showList[1] is orphan Show entity so that only 1 entity is returned.
		assert.EqInt(1, len(list), "1 eventShow must be returned.")
	})
}
Exemple #17
0
func Test_generateYACrawlerKeywords(t *testing.T) {
	// set base time to skip
	now = func() time.Time {
		return time.Date(2015, 01, 01, 15, 0, 0, 0, time.UTC)
	}

	test.RunTestServer(func(ts *test.TestServer) {
		assert := wcg.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		test.DatastoreFixture(ts.Context, "./fixtures/Test_generateYACrawlerKeywords.json", nil)

		keywords, err := generateYACrawlerKeywords(appCtx)
		assert.Nil(err, "generateYACCrawlerKeywords should not return an error")
		assert.EqInt(2, len(keywords), "generateYACCrawlerKeywords should return 1 keyword object.")

		// no updates should happen
		keywords, err = generateYACrawlerKeywords(appCtx)
		assert.Nil(err, "generateYACCrawlerKeywords should not return an error")
		assert.EqInt(0, len(keywords), "generateYACCrawlerKeywords should return 1 keyword object.")
	})
}
Exemple #18
0
func init() {
	setupApi(app.TestApp())
}
Exemple #19
0
func init() {
	setupEventShowApi(app.TestApp())
}
Exemple #20
0
func Test_updateContents(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)

		var dummyMember = &ameblo.Member{}
		dummyMember.Name = "dummy"
		dummyMember.Nicknames = []string{"Updated"}

		amUrl := "http://ameblo.jp/foo/bar.html"
		d := NewAmebloEntryDriver(appCtx)
		refd := NewAmebloRefDriver(appCtx)

		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 := updateContents(
			appCtx,
			[]*ameblo.AmebloEntry{ent},
			[]*ameblo.Member{},
		)
		assert.Nil(err, "UpdateContents() 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.")
		c, _ := refd.NewQuery().Count()
		assert.EqInt(0, c, "AmebloRef should not be inserted by UpdateContents if it is empty.")

		// test confirm to update contents and refs
		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:    "(Updated) crawled content",
			AmLikes:    10,
			AmComments: 15,
		}
		err = updateContents(
			appCtx,
			[]*ameblo.AmebloEntry{newent},
			[]*ameblo.Member{dummyMember},
		)
		assert.Nil(err, "UpdateContents() 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.Content == "(Updated) crawled content"
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Content should be updated after UpdateContents() within timeout window.")

		err = util.WaitFor(func() bool {
			c, _ = refd.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "AmebloRef should be inserted after UpdateContents() within timeout window.")

		// test confirm to update contents and remove refs
		err = updateContents(
			appCtx,
			[]*ameblo.AmebloEntry{newent},
			[]*ameblo.Member{},
		)

		err = util.WaitFor(func() bool {
			c, _ = refd.NewQuery().Count()
			return c == 0
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "AmebloRef should be removed after UpdateContents() within timeout window.")

	})
}