Esempio n. 1
0
func TestScoredArticle(t *testing.T) {
	now := time.Now()
	u := createUser(data.User{Login: "******"})
	uf := createUserFeed(u, data.Feed{Link: "http://sugr.org/bg/sitemap.xml", Title: "article feed 1"})
	uf.AddArticles([]content.Article{
		createArticle(data.Article{Title: "article1", Date: now, Link: "http://sugr.org/bg/products/gearshift"}),
		createArticle(data.Article{Title: "article2", Date: now.Add(2 * time.Hour), Link: "http://sugr.org/bg/products/readeef"}),
		createArticle(data.Article{Title: "article3", Date: now.Add(-3 * time.Hour), Link: "http://sugr.org/bg/about/us"}),
	})
	tests.CheckBool(t, false, uf.HasErr(), uf.Err())

	ua := uf.AllArticles()
	tests.CheckBool(t, false, uf.HasErr(), uf.Err())
	tests.CheckInt64(t, 3, int64(len(ua)))
	id1, id3 := ua[0].Data().Id, ua[2].Data().Id

	asc1 := createArticleScores(data.ArticleScores{ArticleId: id1, Score1: 2, Score2: 2})
	asc2 := createArticleScores(data.ArticleScores{ArticleId: id3, Score1: 1, Score2: 3})

	sa := repo.ScoredArticle()
	sa.Data(data.Article{Id: id1})

	tests.CheckInt64(t, asc1.Calculate(), sa.Scores().Calculate())
	tests.CheckBool(t, false, sa.HasErr(), sa.Err())

	sa.Data(data.Article{Id: id3})
	tests.CheckInt64(t, asc2.Calculate(), sa.Scores().Calculate())
	tests.CheckBool(t, false, sa.HasErr(), sa.Err())
}
Esempio n. 2
0
func TestArticle(t *testing.T) {
	a := Article{}
	a.data.Title = "Title"
	a.data.Id = data.ArticleId(1)

	tests.CheckString(t, "Title (1)", a.String())

	d := a.Data()

	tests.CheckString(t, "Title", d.Title)

	d = a.Data(data.Article{Title: "New title", Description: "Desc"})

	tests.CheckString(t, "New title", d.Title)
	tests.CheckString(t, "Desc", d.Description)

	tests.CheckBool(t, false, a.Validate() == nil)

	d.Link = "http://sugr.org/en/"
	d.FeedId = 42
	a.Data(d)

	tests.CheckBool(t, true, a.Validate() == nil)

	ejson, eerr := json.Marshal(d)
	tests.CheckBool(t, true, eerr == nil)

	ajson, aerr := json.Marshal(a)
	tests.CheckBool(t, true, aerr == nil)

	tests.CheckBytes(t, ejson, ajson)
}
Esempio n. 3
0
func TestTag(t *testing.T) {
	tag := Tag{}
	tag.value = "Link"

	tests.CheckString(t, "Link", tag.String())

	d := tag.Value()

	tests.CheckString(t, "Link", string(d))

	d = tag.Value(data.TagValue(""))

	tests.CheckString(t, "", string(d))

	tests.CheckBool(t, false, tag.Validate() == nil)

	tag.value = "tag"
	tests.CheckBool(t, false, tag.Validate() == nil)

	ejson, eerr := json.Marshal(tag.value)
	tests.CheckBool(t, true, eerr == nil)

	ajson, aerr := json.Marshal(tag)
	tests.CheckBool(t, true, aerr == nil)

	tests.CheckBytes(t, ejson, ajson)
}
Esempio n. 4
0
func TestArticleScores(t *testing.T) {
	a := ArticleScores{}
	a.data.Score1 = 1
	a.data.Score2 = 2
	a.data.Score3 = 3
	a.data.Score4 = 4
	a.data.Score5 = 5

	d := a.Data()

	tests.CheckInt64(t, 2, a.data.Score2)
	d.Score2 = 10
	tests.CheckInt64(t, 2, a.data.Score2)

	d = a.Data(d)

	tests.CheckInt64(t, 10, d.Score2)

	tests.CheckBool(t, false, a.Validate() == nil)

	d.ArticleId = data.ArticleId(1)
	a.Data(d)

	tests.CheckBool(t, true, a.Validate() == nil)

	tests.CheckString(t, "Scores for article '1'", a.String())

}
Esempio n. 5
0
func TestDomain(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	}))
	defer ts.Close()

	d := repo.Domain(ts.URL)

	tests.CheckBool(t, false, d.HasErr(), d.Err())

	tests.CheckBool(t, false, d.SupportsHTTPS(), d.Err())
}
Esempio n. 6
0
func TestFeed(t *testing.T) {
	f := Feed{}
	f.data.Id = 1
	f.data.Title = "Title"

	tests.CheckString(t, "Title (1)", f.String())

	d := f.Data()

	tests.CheckString(t, "Title", d.Title)

	d = f.Data(data.Feed{Title: "New title", Description: "Desc"})

	tests.CheckString(t, "New title", d.Title)
	tests.CheckString(t, "Desc", d.Description)

	tests.CheckBool(t, false, f.Validate() == nil)

	f.data.Link = "foobar"
	tests.CheckBool(t, false, f.Validate() == nil)

	f.data.Link = "http://sugr.org"
	tests.CheckBool(t, true, f.Validate() == nil)

	tests.CheckInt64(t, 0, int64(len(f.ParsedArticles())))

	f.Refresh(parser.Feed{Title: "Title2", Articles: []parser.Article{parser.Article{Title: "Article title"}}})
	tests.CheckString(t, "Title2", f.data.Title)
	tests.CheckInt64(t, 1, int64(len(f.ParsedArticles())))

	tests.CheckString(t, "Article title", f.ParsedArticles()[0].Data().Title)

	ejson, eerr := json.Marshal(f.data)
	tests.CheckBool(t, true, eerr == nil)

	ajson, aerr := json.Marshal(f)
	tests.CheckBool(t, true, aerr == nil)

	tests.CheckBytes(t, ejson, ajson)
}
Esempio n. 7
0
func TestSubscription(t *testing.T) {
	s := Subscription{}
	s.data.Link = "Link"

	tests.CheckString(t, "Subscription for Link", s.String())

	d := s.Data()

	tests.CheckString(t, "Link", d.Link)

	d = s.Data(data.Subscription{Link: "New link"})

	tests.CheckString(t, "New link", d.Link)

	tests.CheckBool(t, false, s.Validate() == nil)

	s.data.Link = ""
	tests.CheckBool(t, false, s.Validate() == nil)

	s.data.Link = "http://sugr.org"
	tests.CheckBool(t, false, s.Validate() == nil)

	s.data.FeedId = 1
	tests.CheckBool(t, true, s.Validate() == nil)

	ejson, eerr := json.Marshal(s.data)
	tests.CheckBool(t, true, eerr == nil)

	ajson, aerr := json.Marshal(s)
	tests.CheckBool(t, true, aerr == nil)

	tests.CheckBytes(t, ejson, ajson)
}
Esempio n. 8
0
func TestErr(t *testing.T) {
	err := Error{}

	tests.CheckBool(t, false, err.HasErr())
	tests.CheckBool(t, true, err.Err() == nil)

	err1 := errors.New("test1")
	tests.CheckBool(t, true, err.Err(err1) == nil)

	tests.CheckBool(t, true, err.HasErr())
	tests.CheckBool(t, true, err.Err() == err1)

	tests.CheckBool(t, false, err.HasErr())
	tests.CheckBool(t, true, err.Err() == nil)
}
Esempio n. 9
0
func TestUser(t *testing.T) {
	u := User{}
	u.data.FirstName = "First"
	u.data.LastName = "Last"

	tests.CheckString(t, "First Last", u.String())

	u.data.Email = "*****@*****.**"
	tests.CheckString(t, "First Last <*****@*****.**>", u.String())

	d := u.Data()

	tests.CheckString(t, "*****@*****.**", d.Email)

	d = u.Data(data.User{Email: ""})

	tests.CheckString(t, "", d.Email)
	tests.CheckString(t, "", d.FirstName)

	tests.CheckBool(t, false, u.Validate() == nil)

	u.data.Email = "example"
	tests.CheckBool(t, false, u.Validate() == nil)

	u.data.Email = "*****@*****.**"
	tests.CheckBool(t, false, u.Validate() == nil)

	u.data.Login = data.Login("login")
	tests.CheckBool(t, true, u.Validate() == nil)

	ejson, eerr := json.Marshal(u.data)
	tests.CheckBool(t, true, eerr == nil)

	ajson, aerr := json.Marshal(u)
	tests.CheckBool(t, true, aerr == nil)

	tests.CheckBytes(t, ejson, ajson)

	tests.CheckBytes(t, []byte{}, u.data.MD5API)
	tests.CheckString(t, "", u.data.HashType)
	tests.CheckBytes(t, []byte{}, u.data.Hash)

	u.Password("pass", []byte("secret"))
	h := md5.Sum([]byte(fmt.Sprintf("%s:%s", "login", "pass")))
	tests.CheckBytes(t, h[:], u.data.MD5API)
	tests.CheckString(t, "sha1", u.data.HashType)
	tests.CheckBytes(t, u.generateHash("pass", []byte("secret")), u.data.Hash)
}
Esempio n. 10
0
func TestDomain(t *testing.T) {
	d := Domain{}

	tests.CheckBool(t, true, d.URL() == nil)

	urlString := "https://sugr.org"
	tests.CheckBool(t, true, d.URL(urlString) != nil)
	tests.CheckBool(t, false, d.HasErr())
	tests.CheckBool(t, true, d.CheckHTTPSSupport(), "The schema contains https")

	tests.CheckString(t, urlString, d.URL().String())

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	}))
	defer ts.Close()

	tests.CheckBool(t, true, d.URL(ts.URL+"/example") != nil)
	tests.CheckBool(t, false, d.HasErr())
	tests.CheckBool(t, false, d.CheckHTTPSSupport(), "Test server is not TLS")

	resp := make(chan bool)
	tlsts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if (r.URL.Path == "" || r.URL.Path == "/") && r.Method == "HEAD" {
			resp <- true
		} else {
			resp <- false
		}
	}))
	defer tlsts.Close()

	tlsts.TLS = new(tls.Config)
	tlsts.TLS.InsecureSkipVerify = true
	tlsts.StartTLS()

	timeoutClient = &http.Client{
		Transport: &http.Transport{
			Dial:            readeef.TimeoutDialer(1*time.Second, 1*time.Second),
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		},
	}

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		defer wg.Done()
		tests.CheckBool(t, true, <-resp, "The Server should've been queries using a HEAD method with no Path")
	}()

	u, err := url.Parse(tlsts.URL)
	tests.CheckBool(t, true, err == nil)
	u.Scheme = "http"
	u.Path = "example"
	tests.CheckBool(t, true, d.URL(u.String()) != nil)
	tests.CheckBool(t, false, d.HasErr())
	tests.CheckBool(t, true, d.CheckHTTPSSupport(), "Test server is TLS")

	wg.Wait()
}
Esempio n. 11
0
func TestUserArticle(t *testing.T) {
	now := time.Now()
	u := createUser(data.User{Login: "******"})
	uf := createUserFeed(u, data.Feed{Link: "http://sugr.org/bg/404", Title: "user article feed 1"})
	uf.AddArticles([]content.Article{
		createArticle(data.Article{Title: "article1", Date: now, Link: "http://sugr.org/bg/products/readeef"}),
	})
	tests.CheckBool(t, false, uf.HasErr(), uf.Err())

	articles := uf.AllArticles()
	tests.CheckBool(t, false, uf.HasErr(), uf.Err())
	tests.CheckInt64(t, 1, int64(len(articles)))

	id := articles[0].Data().Id
	tests.CheckBool(t, false, uf.HasErr(), uf.Err())

	a := u.ArticleById(id)
	tests.CheckBool(t, false, a.HasErr(), a.Err())
	tests.CheckBool(t, false, a.Data().Read)
	tests.CheckBool(t, false, a.Data().Favorite)

	a.Read(true)
	tests.CheckBool(t, false, a.HasErr(), a.Err())
	tests.CheckBool(t, true, a.Data().Read)
	tests.CheckBool(t, true, u.ArticleById(id).Data().Read)

	a.Read(false)
	tests.CheckBool(t, false, a.HasErr(), a.Err())
	tests.CheckBool(t, false, a.Data().Read)
	tests.CheckBool(t, false, u.ArticleById(id).Data().Read)

	a.Favorite(true)
	tests.CheckBool(t, false, a.HasErr(), a.Err())
	tests.CheckBool(t, true, a.Data().Favorite)
	tests.CheckBool(t, true, u.ArticleById(id).Data().Favorite)

	a.Favorite(false)
	tests.CheckBool(t, false, a.HasErr(), a.Err())
	tests.CheckBool(t, false, a.Data().Favorite)
	tests.CheckBool(t, false, u.ArticleById(id).Data().Favorite)
}
Esempio n. 12
0
func TestUserFeed(t *testing.T) {
	uf := repo.UserFeed(createUser(data.User{}))
	uf.Data(data.Feed{Link: "http://sugr.org"})

	tests.CheckBool(t, false, uf.Validate() == nil)

	u := createUser(data.User{Login: "******"})

	uf = repo.UserFeed(u)
	uf.Data(data.Feed{Link: "http://sugr.org", Title: "User feed 1"})

	tests.CheckBool(t, true, uf.Validate() == nil, uf.Validate())

	uf.Update()
	tests.CheckBool(t, false, uf.HasErr(), uf.Err())

	u.AddFeed(uf)

	id := uf.Data().Id

	uf2 := u.FeedById(id)
	tests.CheckBool(t, false, uf2.HasErr(), uf2.Err())
	tests.CheckString(t, uf.Data().Title, uf2.Data().Title)

	now := time.Now()

	uf.AddArticles([]content.Article{
		createArticle(data.Article{Title: "article1", Date: now, Link: "http://sugr.org/en/products/gearshift"}),
		createArticle(data.Article{Title: "article2", Date: now.Add(2 * time.Hour), Link: "http://sugr.org/en/products/readeef"}),
		createArticle(data.Article{Title: "article3", Date: now.Add(-3 * time.Hour), Link: "http://sugr.org/en/about/us"}),
	})
	tests.CheckBool(t, false, uf.HasErr(), uf.Err())

	uf.SortingById()
	ua := uf.Articles()
	tests.CheckBool(t, false, uf.HasErr(), uf.Err())

	tests.CheckInt64(t, 3, int64(len(ua)))

	var id1, id2, id3 data.ArticleId
	for i := range ua {
		d := ua[i].Data()
		switch d.Title {
		case "article1":
			id1 = d.Id
		case "article2":
			id2 = d.Id
		case "article3":
			id3 = d.Id
		default:
			tests.CheckBool(t, true, false, "Unknown article")
		}
	}

	tests.CheckInt64(t, int64(id1), int64(ua[0].Data().Id))
	tests.CheckString(t, "article2", ua[1].Data().Title)
	tests.CheckInt64(t, now.Add(-3*time.Hour).Unix(), ua[2].Data().Date.Unix())

	uf.SortingByDate()
	ua = uf.Articles()

	tests.CheckInt64(t, int64(id3), int64(ua[0].Data().Id))
	tests.CheckString(t, "article1", ua[1].Data().Title)
	tests.CheckInt64(t, now.Add(2*time.Hour).Unix(), ua[2].Data().Date.Unix())

	uf.Reverse()
	ua = uf.Articles()

	tests.CheckInt64(t, int64(id2), int64(ua[0].Data().Id))
	tests.CheckString(t, "article1", ua[1].Data().Title)
	tests.CheckInt64(t, now.Add(-3*time.Hour).Unix(), ua[2].Data().Date.Unix())

	ua[0].Read(true)

	uf.Reverse()
	uf.SortingById()

	ua = uf.UnreadArticles()
	tests.CheckBool(t, false, uf.HasErr(), uf.Err())
	tests.CheckInt64(t, 2, int64(len(ua)))

	tests.CheckInt64(t, int64(id1), int64(ua[0].Data().Id))
	tests.CheckString(t, "article3", ua[1].Data().Title)

	u.ArticleById(data.ArticleId(id2)).Read(false)

	ua = uf.UnreadArticles()
	tests.CheckInt64(t, 3, int64(len(ua)))

	uf.ReadBefore(now.Add(time.Minute), true)
	tests.CheckBool(t, false, uf.HasErr(), uf.Err())

	ua = uf.UnreadArticles()
	tests.CheckBool(t, false, uf.HasErr(), uf.Err())
	tests.CheckInt64(t, 1, int64(len(ua)))
	tests.CheckInt64(t, int64(id2), int64(ua[0].Data().Id))

	asc1 := createArticleScores(data.ArticleScores{ArticleId: id1, Score1: 2, Score2: 2})
	tests.CheckBool(t, false, asc1.HasErr(), asc1.Err())
	asc2 := createArticleScores(data.ArticleScores{ArticleId: id2, Score1: 1, Score2: 3})
	tests.CheckBool(t, false, asc2.HasErr(), asc2.Err())

	sa := uf.ScoredArticles(now.Add(-20*time.Hour), now.Add(20*time.Hour))

	tests.CheckBool(t, false, uf.HasErr(), uf.Err())
	tests.CheckInt64(t, 2, int64(len(sa)))

	for i := range sa {
		switch sa[i].Data().Id {
		case id1:
			tests.CheckInt64(t, asc1.Calculate(), sa[i].Data().Score)
		case id2:
			tests.CheckInt64(t, asc2.Calculate(), sa[i].Data().Score)
		}
	}

	uf.Detach()
	tests.CheckInt64(t, 0, int64(len(u.AllFeeds())))

	uf2 = u.FeedById(id)
	tests.CheckBool(t, true, uf2.Err() == content.ErrNoContent)

}
Esempio n. 13
0
func TestFeed(t *testing.T) {
	f := repo.Feed()
	f.Data(data.Feed{Title: "feed title", Link: "http://sugr.org/en/products/gearshift"})

	tests.CheckInt64(t, 0, int64(f.Data().Id))

	f.Update()

	tests.CheckBool(t, false, f.HasErr(), f.Err())
	tests.CheckBool(t, false, f.Data().Id == 0)
	tests.CheckInt64(t, 0, int64(len(f.NewArticles())))

	now := time.Now()

	f.AddArticles([]content.Article{
		createArticle(data.Article{Title: "article1", Date: now, Link: "http://sugr.org/en/products/gearshift"}),
		createArticle(data.Article{Title: "article2", Date: now.Add(2 * time.Hour), Link: "http://sugr.org/en/products/readeef"}),
		createArticle(data.Article{Title: "article3", Date: now.Add(-3 * time.Hour), Link: "http://sugr.org/en/about/us"}),
	})
	tests.CheckBool(t, false, f.HasErr(), f.Err())

	tests.CheckInt64(t, 3, int64(len(f.NewArticles())))

	f.AddArticles([]content.Article{
		createArticle(data.Article{Title: "article4", Date: now.Add(-10 * 24 * time.Hour), Link: "http://sugr.org/bg/"}),
	})
	tests.CheckBool(t, false, f.HasErr(), f.Err())

	tests.CheckInt64(t, 1, int64(len(f.NewArticles())))
	tests.CheckString(t, "article4", f.NewArticles()[0].Data().Title)

	a := f.AllArticles()

	tests.CheckBool(t, false, f.HasErr(), f.Err())
	tests.CheckInt64(t, 4, int64(len(a)))

	for i := range a {
		d := a[i].Data()
		switch d.Title {
		case "article1":
		case "article2":
		case "article3":
		case "article4":
		default:
			tests.CheckBool(t, false, true, "Unknown article")
		}
	}

	a = f.LatestArticles()
	tests.CheckBool(t, false, f.HasErr(), f.Err())
	tests.CheckInt64(t, 3, int64(len(a)))

	for i := range a {
		d := a[i].Data()
		switch d.Title {
		case "article1":
		case "article2":
		case "article3":
		default:
			tests.CheckBool(t, false, true, "Unknown article")
		}
	}

}
Esempio n. 14
0
func TestRepo(t *testing.T) {
	tests.CheckBool(t, false, repo.HasErr())
}
Esempio n. 15
0
func TestUser(t *testing.T) {
	u := repo.User()

	tests.CheckBool(t, false, u.HasErr(), u.Err())

	u.Update()
	tests.CheckBool(t, true, u.HasErr())

	err := u.Err()
	_, ok := err.(content.ValidationError)
	tests.CheckBool(t, true, ok, err)

	u.Data(data.User{Login: data.Login("login")})

	tests.CheckBool(t, false, u.HasErr(), u.Err())

	u.Update()
	tests.CheckBool(t, false, u.HasErr(), u.Err())

	u2 := repo.UserByLogin(data.Login("login"))
	tests.CheckBool(t, false, u2.HasErr(), u2.Err())
	tests.CheckString(t, "login", string(u2.Data().Login))

	u.Delete()
	tests.CheckBool(t, false, u.HasErr(), u.Err())

	u2 = repo.UserByLogin(data.Login("login"))
	tests.CheckBool(t, true, u2.HasErr())
	tests.CheckBool(t, true, u2.Err() == content.ErrNoContent)

	u = createUser(data.User{Login: data.Login("login")})

	now := time.Now()
	uf := createUserFeed(u, data.Feed{Link: "http://sugr.org/en/sitemap.xml", Title: "User feed 1"})
	uf.AddArticles([]content.Article{
		createArticle(data.Article{Title: "article1", Date: now, Link: "http://sugr.org/bg/products/gearshift"}),
		createArticle(data.Article{Title: "article2", Date: now.Add(2 * time.Hour), Link: "http://sugr.org/bg/products/readeef"}),
		createArticle(data.Article{Title: "article3", Date: now.Add(-3 * time.Hour), Link: "http://sugr.org/bg/about/us"}),
	})

	u.AddFeed(uf)

	var id1, id2, id3 data.ArticleId

	for _, a := range uf.AllArticles() {
		d := a.Data()
		switch d.Title {
		case "article1":
			id1 = d.Id
		case "article2":
			id2 = d.Id
		case "article3":
			id3 = d.Id
		default:
			tests.CheckBool(t, true, false, "Unknown article")
		}
	}

	tests.CheckBool(t, false, uf.HasErr(), uf.Err())
	tests.CheckInt64(t, 1, int64(len(u.AllFeeds())))
	tests.CheckString(t, "http://sugr.org/en/sitemap.xml", u.AllFeeds()[0].Data().Link)
	tests.CheckString(t, "User feed 1", u.AllFeeds()[0].Data().Title)

	a := u.ArticleById(10000000)
	tests.CheckBool(t, true, a.Err() == content.ErrNoContent)

	a = u.ArticleById(id1)
	tests.CheckBool(t, false, a.HasErr(), a.Err())

	tests.CheckString(t, "article1", a.Data().Title)

	a2 := u.ArticlesById([]data.ArticleId{100000000, id1, id2})
	tests.CheckBool(t, false, u.HasErr(), u.Err())

	tests.CheckInt64(t, 2, int64(len(a2)))

	for i := range a2 {
		d := a2[i].Data()
		switch d.Title {
		case "article1":
		case "article2":
		default:
			tests.CheckBool(t, false, true, "Unknown article")
		}
	}

	u.SortingById()
	ua := u.Articles()
	tests.CheckBool(t, false, u.HasErr(), u.Err())

	tests.CheckInt64(t, 3, int64(len(ua)))

	tests.CheckInt64(t, int64(id1), int64(ua[0].Data().Id))
	tests.CheckString(t, "article2", ua[1].Data().Title)
	tests.CheckInt64(t, now.Add(-3*time.Hour).Unix(), ua[2].Data().Date.Unix())

	u.SortingByDate()
	ua = u.Articles()

	tests.CheckInt64(t, int64(id3), int64(ua[0].Data().Id))
	tests.CheckString(t, "article1", ua[1].Data().Title)
	tests.CheckInt64(t, now.Add(2*time.Hour).Unix(), ua[2].Data().Date.Unix())

	u.Reverse()
	ua = u.Articles()

	tests.CheckInt64(t, int64(id2), int64(ua[0].Data().Id))
	tests.CheckString(t, "article1", ua[1].Data().Title)
	tests.CheckInt64(t, now.Add(-3*time.Hour).Unix(), ua[2].Data().Date.Unix())

	ua[1].Read(false)
	ua[2].Read(false)

	u.Reverse()
	u.SortingById()

	ua = u.Articles(data.ArticleQueryOptions{UnreadOnly: true})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 2, int64(len(ua)))

	tests.CheckInt64(t, int64(id1), int64(ua[0].Data().Id))
	tests.CheckString(t, "article3", ua[1].Data().Title)

	u.ArticleById(id2).Read(false)

	ua = u.Articles(data.ArticleQueryOptions{UnreadOnly: true})
	tests.CheckInt64(t, 3, int64(len(ua)))

	u.ReadState(true, data.ArticleUpdateStateOptions{BeforeDate: now.Add(time.Minute)})
	tests.CheckBool(t, false, u.HasErr(), u.Err())

	ua = u.Articles(data.ArticleQueryOptions{UnreadOnly: true})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 1, int64(len(ua)))
	tests.CheckInt64(t, int64(id2), int64(ua[0].Data().Id))

	u.ArticleById(id1).Read(false)

	u.ReadState(true, data.ArticleUpdateStateOptions{AfterDate: now.Add(time.Minute)})
	tests.CheckBool(t, false, u.HasErr(), u.Err())

	ua = u.Articles(data.ArticleQueryOptions{UnreadOnly: true})
	tests.CheckInt64(t, 1, int64(len(ua)))
	tests.CheckInt64(t, int64(id1), int64(ua[0].Data().Id))

	u.ArticleById(id1).Favorite(true)
	u.ArticleById(id3).Favorite(true)

	uIds := u.Ids(data.ArticleIdQueryOptions{UnreadOnly: true})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 1, int64(len(uIds)))
	tests.CheckInt64(t, int64(id1), int64(uIds[0]))

	fIds := u.Ids(data.ArticleIdQueryOptions{FavoriteOnly: true})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 2, int64(len(fIds)))

	for i := range fIds {
		switch fIds[i] {
		case id1:
		case id3:
		default:
			tests.CheckBool(t, false, true, "Unknown article id")
		}
	}

	tests.CheckInt64(t, 3, u.Count())
	tests.CheckBool(t, false, u.HasErr(), u.Err())

	ua = u.Articles(data.ArticleQueryOptions{FavoriteOnly: true})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 2, int64(len(ua)))

	for i := range ua {
		d := ua[i].Data()
		switch d.Id {
		case id1:
		case id3:
		default:
			tests.CheckBool(t, false, true, "Unknown article id")
		}
	}

	u.SortingById()
	ua = u.Articles(data.ArticleQueryOptions{AfterId: id1})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 2, int64(len(ua)))

	for i := range ua {
		d := ua[i].Data()
		switch d.Id {
		case id2:
		case id3:
		default:
			tests.CheckBool(t, false, true, "Unknown article id")
		}
	}

	u.Reverse()
	ua = u.Articles(data.ArticleQueryOptions{BeforeId: id2})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 1, int64(len(ua)))
	tests.CheckString(t, "article1", ua[0].Data().Title)

	asc1 := createArticleScores(data.ArticleScores{ArticleId: id1, Score1: 2, Score2: 2})
	asc2 := createArticleScores(data.ArticleScores{ArticleId: id2, Score1: 1, Score2: 3})

	sa := u.Articles(data.ArticleQueryOptions{AfterDate: now.Add(-20 * time.Hour), BeforeDate: now.Add(20 * time.Hour), IncludeScores: true, HighScoredFirst: true})

	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 2, int64(len(sa)))

	for i := range sa {
		switch sa[i].Data().Id {
		case 1:
			tests.CheckInt64(t, asc1.Calculate(), sa[i].Data().Score)
		case 2:
			tests.CheckInt64(t, asc2.Calculate(), sa[i].Data().Score)
		}
	}

	ua = u.Articles()
	ua[0].Read(true)
	ua[1].Read(true)
	ua[2].Read(false)
	ua[0].Favorite(true)
	ua[1].Favorite(false)
	ua[2].Favorite(true)

	count := u.Count()
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 3, count)

	count = u.Count(data.ArticleCountOptions{UnreadOnly: true})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 1, count)

	count = u.Count(data.ArticleCountOptions{FavoriteOnly: true})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 2, count)

	count = u.Count(data.ArticleCountOptions{FavoriteOnly: true, UnreadOnly: true})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 1, count)

	count = u.Count(data.ArticleCountOptions{BeforeId: id2})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 1, count)

	count = u.Count(data.ArticleCountOptions{AfterId: id1})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 2, count)

	count = u.Count(data.ArticleCountOptions{BeforeId: id3, AfterId: id1})
	tests.CheckBool(t, false, u.HasErr(), u.Err())
	tests.CheckInt64(t, 1, count)
}
Esempio n. 16
0
func TestTag(t *testing.T) {
	u := createUser(data.User{Login: "******"})

	tag := repo.Tag(u)
	tests.CheckBool(t, false, tag.HasErr(), tag.Err())

	tf := createTaggedFeed(u, data.Feed{Link: "http://sugr.org/"})

	tests.CheckInt64(t, 0, int64(len(tf.Tags())))
	tests.CheckInt64(t, 1, int64(len(tf.Tags([]content.Tag{tag}))))

	tf.UpdateTags()
	tests.CheckBool(t, true, tf.HasErr())
	_, ok := tf.Err().(content.ValidationError)
	tests.CheckBool(t, true, ok)

	tag.Data(data.Tag{Value: "tag1"})
	tests.CheckString(t, "tag1", tag.String())

	tf.Tags([]content.Tag{tag})
	tf.UpdateTags()
	tests.CheckBool(t, false, tf.HasErr(), tf.Err())

	tf2 := createTaggedFeed(u, data.Feed{Link: "http://sugr.org/products/readeef"})

	tag2 := repo.Tag(u)
	tag2.Data(data.Tag{Value: "tag2"})

	tag3 := repo.Tag(u)
	tag3.Data(data.Tag{Value: "tag3"})

	tests.CheckInt64(t, 2, int64(len(tf2.Tags([]content.Tag{tag2, tag3}))))
	tf2.UpdateTags()
	tests.CheckBool(t, false, tf2.HasErr(), tf2.Err())

	tags := u.Tags()
	tests.CheckBool(t, false, u.HasErr(), u.Err())

	tests.CheckInt64(t, 3, int64(len(tags)))

	feeds := u.AllTaggedFeeds()
	tests.CheckBool(t, false, u.HasErr(), u.Err())

	var fId1 data.FeedId
	for i := range feeds {
		tags := feeds[i].Tags()
		d := feeds[i].Data()
		switch d.Link {
		case "http://sugr.org/":
			fId1 = d.Id
			tests.CheckInt64(t, 1, int64(len(tags)))
		case "http://sugr.org/products/readeef":
			tests.CheckInt64(t, 2, int64(len(tags)))
		default:
			tests.CheckBool(t, false, true, "Unknown feed")
		}
	}

	tf.Tags([]content.Tag{tag, tag3})
	tf.UpdateTags()
	tests.CheckBool(t, false, tf.HasErr(), tf.Err())

	feeds = tag.AllFeeds()
	tests.CheckBool(t, false, tag.HasErr(), tag.Err())

	tests.CheckInt64(t, 1, int64(len(feeds)))
	tests.CheckInt64(t, int64(fId1), int64(feeds[0].Data().Id))

	feeds = tag3.AllFeeds()
	tests.CheckBool(t, false, tag.HasErr(), tag.Err())

	tests.CheckInt64(t, 2, int64(len(feeds)))

	now := time.Now()

	tf.AddArticles([]content.Article{
		createArticle(data.Article{Title: "article1", Date: now, Link: "http://1.example.com"}),
		createArticle(data.Article{Title: "article2", Link: "http://sugr.org", Date: now.Add(3 * time.Hour)}),
	})
	tf2.AddArticles([]content.Article{createArticle(data.Article{Title: "article3", Date: now.Add(-2 * time.Hour), Link: "http://sugr.org/products/readeef"})})

	tag3.SortingById()
	ua := tag3.Articles()
	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())
	tests.CheckInt64(t, 3, int64(len(ua)))

	var id1, id2, id3 data.ArticleId
	for i := range ua {
		d := ua[i].Data()
		switch d.Title {
		case "article1":
			id1 = d.Id
		case "article2":
			id2 = d.Id
		case "article3":
			id3 = d.Id
		default:
			tests.CheckBool(t, false, true, "Unknown article")
		}
	}

	tests.CheckInt64(t, int64(id1), int64(ua[0].Data().Id))
	tests.CheckString(t, "article2", ua[1].Data().Title)
	tests.CheckInt64(t, now.Add(-2*time.Hour).Unix(), ua[2].Data().Date.Unix())

	tag3.SortingByDate()
	ua = tag3.Articles()

	tests.CheckInt64(t, int64(id3), int64(ua[0].Data().Id))
	tests.CheckString(t, "article1", ua[1].Data().Title)
	tests.CheckInt64(t, now.Add(3*time.Hour).Unix(), ua[2].Data().Date.Unix())

	tag3.Reverse()
	ua = tag3.Articles()

	tests.CheckInt64(t, int64(id2), int64(ua[0].Data().Id))
	tests.CheckString(t, "article1", ua[1].Data().Title)
	tests.CheckInt64(t, now.Add(-2*time.Hour).Unix(), ua[2].Data().Date.Unix())

	ua[1].Read(false)
	ua[2].Read(false)

	tag3.Reverse()
	tag3.SortingById()

	ua = tag3.Articles(data.ArticleQueryOptions{UnreadOnly: true})
	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())
	tests.CheckInt64(t, 2, int64(len(ua)))

	tests.CheckInt64(t, int64(id1), int64(ua[0].Data().Id))
	tests.CheckString(t, "article3", ua[1].Data().Title)

	u.ArticleById(id2).Read(false)

	ua = tag3.Articles(data.ArticleQueryOptions{UnreadOnly: true})
	tests.CheckInt64(t, 3, int64(len(ua)))

	tag3.ReadState(true, data.ArticleUpdateStateOptions{BeforeDate: now.Add(time.Minute)})
	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())

	ua = tag3.Articles(data.ArticleQueryOptions{UnreadOnly: true})
	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())
	tests.CheckInt64(t, 1, int64(len(ua)))
	tests.CheckInt64(t, int64(id2), int64(ua[0].Data().Id))

	asc1 := createArticleScores(data.ArticleScores{ArticleId: id1, Score1: 2, Score2: 2})
	asc2 := createArticleScores(data.ArticleScores{ArticleId: id2, Score1: 1, Score2: 3})

	sa := tag3.Articles(data.ArticleQueryOptions{AfterDate: now.Add(-20 * time.Hour), BeforeDate: now.Add(20 * time.Hour), IncludeScores: true, HighScoredFirst: true})

	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())
	tests.CheckInt64(t, 2, int64(len(sa)))

	for i := range sa {
		switch sa[i].Data().Id {
		case 1:
			tests.CheckInt64(t, asc1.Calculate(), sa[i].Data().Score)
		case 2:
			tests.CheckInt64(t, asc2.Calculate(), sa[i].Data().Score)
		}
	}

	ua = tag3.Articles()
	ua[0].Read(true)
	ua[1].Read(true)
	ua[2].Read(false)
	ua[0].Favorite(true)
	ua[1].Favorite(false)
	ua[2].Favorite(true)

	count := tag3.Count()
	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())
	tests.CheckInt64(t, 3, count)

	count = tag3.Count(data.ArticleCountOptions{UnreadOnly: true})
	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())
	tests.CheckInt64(t, 1, count)

	count = tag3.Count(data.ArticleCountOptions{FavoriteOnly: true})
	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())
	tests.CheckInt64(t, 2, count)

	count = tag3.Count(data.ArticleCountOptions{FavoriteOnly: true, UnreadOnly: true})
	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())
	tests.CheckInt64(t, 1, count)

	count = tag3.Count(data.ArticleCountOptions{BeforeId: id2})
	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())
	tests.CheckInt64(t, 1, count)

	count = tag3.Count(data.ArticleCountOptions{AfterId: id1})
	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())
	tests.CheckInt64(t, 2, count)

	count = tag3.Count(data.ArticleCountOptions{BeforeId: id3, AfterId: id1})
	tests.CheckBool(t, false, tag3.HasErr(), tag3.Err())
	tests.CheckInt64(t, 1, count)
}