156 lines
2.9 KiB
Go
156 lines
2.9 KiB
Go
package repository_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/repository"
|
|
)
|
|
|
|
const (
|
|
articleFakeDotCom = "www.fake.com"
|
|
)
|
|
|
|
func TestCreateArticle(t *testing.T) {
|
|
t.Log(time.Time{})
|
|
db, err := setupInMemoryDb()
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
defer db.Close()
|
|
|
|
r := repository.NewArticleRepository(db)
|
|
created, err := r.Create(1, "", "unit test", articleFakeDotCom, "", "testing", "", "", time.Now(), false)
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
|
|
if created != 1 {
|
|
t.Log("failed to create the record")
|
|
t.FailNow()
|
|
}
|
|
}
|
|
|
|
func TestArticleByUrl(t *testing.T) {
|
|
db, err := setupInMemoryDb()
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
defer db.Close()
|
|
r := repository.NewArticleRepository(db)
|
|
|
|
err = insertFakeArticles(r, "u1", 0)
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
|
|
article, err := r.GetByUrl(articleFakeDotCom)
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
|
|
if article.Url != "www.fake.com" {
|
|
t.Log("failed to find the requested record")
|
|
t.FailNow()
|
|
}
|
|
}
|
|
|
|
func TestPullingMultipleArticlesWithLimit(t *testing.T) {
|
|
db, err := setupInMemoryDb()
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
defer db.Close()
|
|
r := repository.NewArticleRepository(db)
|
|
insertFakeArticles(r, "u1", 0)
|
|
insertFakeArticles(r, "u2", 0)
|
|
insertFakeArticles(r, "u3", 0)
|
|
insertFakeArticles(r, "u4", 0)
|
|
|
|
items, err := r.ListTop(3)
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
|
|
if len(items) != 3 {
|
|
t.Log("expected 3 rows back")
|
|
t.FailNow()
|
|
}
|
|
}
|
|
|
|
func TestPullingMultipleArticlesWithPaging(t *testing.T) {
|
|
db, err := setupInMemoryDb()
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
defer db.Close()
|
|
r := repository.NewArticleRepository(db)
|
|
insertFakeArticles(r, "u1", 0)
|
|
insertFakeArticles(r, "u2", 0)
|
|
insertFakeArticles(r, "u3", 0)
|
|
insertFakeArticles(r, "u4", 0)
|
|
|
|
items, err := r.ListByPage(2, 1)
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
|
|
if items[0].Title != "u2" {
|
|
t.Log("pulled the wrong record with paging")
|
|
t.FailNow()
|
|
}
|
|
}
|
|
|
|
func TestPullingByPublishDate(t *testing.T) {
|
|
db, err := setupInMemoryDb()
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
defer db.Close()
|
|
r := repository.NewArticleRepository(db)
|
|
|
|
today := time.Now()
|
|
|
|
insertFakeArticles(r, "u1", 0)
|
|
insertFakeArticles(r, "u1", -1)
|
|
insertFakeArticles(r, "u1", -2)
|
|
|
|
items, err := r.ListByPublishDate(0, 2, repository.ArticleOrderByPublishDateDesc)
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
|
|
if len(items) != 2 {
|
|
t.Log("expected two items back")
|
|
t.FailNow()
|
|
}
|
|
pubDate := items[1].PubDate.Day()
|
|
expectedDay := today.Day() - 1
|
|
if pubDate != expectedDay {
|
|
t.Log("expected a record that was 2 days old")
|
|
t.FailNow()
|
|
}
|
|
}
|
|
|
|
//func TestArticleBySource
|
|
|
|
func insertFakeArticles(r repository.ArticleRepository, title string, daysOld int) error {
|
|
pubDate := time.Now().AddDate(0,0, daysOld)
|
|
_, err := r.Create(1, "", title, articleFakeDotCom, "", "testing", "", "", pubDate, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|