Esempio n. 1
0
func Test_LoadArticleIndex(t *testing.T) {
	a := assert.New(t)

	datastore.DeleteArtilcles("*****@*****.**")

	article := datastore.ParseEntry(fakeFileMetaData(), fakeFileContentWithNoMetadata())
	article.Path = "p1"
	article.Permalink = "a1"
	article.CreatedAt = "2014-12-01"
	article.GenerateID("*****@*****.**")
	article.Save()
	anotherArticle := datastore.ParseEntry(fakeFileMetaData(), fakeFileContentWithNoMetadata())
	article.Path = "p2"
	anotherArticle.Permalink = "a2"
	anotherArticle.Title = "t2"
	anotherArticle.CreatedAt = "2014-12-02"
	anotherArticle.GenerateID("*****@*****.**")
	anotherArticle.Save()

	datastore.ArticlesReindex("*****@*****.**")
	index := datastore.LoadArticleIndex("*****@*****.**")

	a.Contains(index[0].Permalink, "a2")
	a.Contains(index[0].ID, anotherArticle.ID)
	a.Contains(index[0].Title, "t2")
	a.Contains(index[0].Summary, "this is a pragraph")
	a.Contains(index[0].CreatedAt, "2014-12-02")
}
Esempio n. 2
0
func Test_DeleteArticles(t *testing.T) {
	a := assert.New(t)
	article := datastore.ParseEntry(fakeFileMetaData(), fakeFileContentWithNoMetadata())
	article.GenerateID("*****@*****.**")
	article.Save()
	datastore.DeleteArtilcles("*****@*****.**")
	a.Equal(len(datastore.LoadArticleIndex("*****@*****.**")), 0)
}
Esempio n. 3
0
func processUserDelta(email string) {
	articlesCursor, _ := datastore.GetCurrentCursor(email, "/published")
	at, err := datastore.LoadUserToken(email)
	if err != nil {
		log.Fatal(err)
		return
	}
	dbc := dropbox.NewClient(at, config.AppToken)

	//process articles
	d, _ := dbc.GetDelta("/published", articlesCursor)

	for _, v := range d.Deleted {
		a, err := datastore.LoadArticle(email + ":article:" + v)
		if err == nil {
			a.Delete()
			log.Printf("deleted: %s", v)
		}
	}

	wait.Wait(len(d.Updated), func(index int) {
		entry, _ := dbc.GetMetadata(d.Updated[index], true)
		file, _ := dbc.GetFile(d.Updated[index])
		content, _ := ioutil.ReadAll(file)
		article := datastore.ParseEntry(*entry, content)
		article.GenerateID(email)
		article.Save()
		log.Printf("updated: %s", article.Path)
	})

	datastore.ArticlesReindex(email)
	datastore.SaveCurrentCursor(email, "/published", d.Cursor)

	//process images
	imageCursor, _ := datastore.GetCurrentCursor(email, "/images")
	d, err = dbc.GetDelta("/images", imageCursor)
	for _, v := range d.Deleted {
		err := os.Remove("./public" + v)
		if err != nil {
			log.Println(err)
		}
		log.Printf("deleted: %s", v)
	}

	wait.Wait(len(d.Updated), func(index int) {
		file, _ := dbc.GetFile(d.Updated[index])
		content, _ := ioutil.ReadAll(file)
		imgPath, _ := filepath.Abs("./public" + d.Updated[index])
		os.MkdirAll(filepath.Dir(imgPath), 0755)
		err = ioutil.WriteFile("./public"+d.Updated[index], content, 0644)
		if err != nil {
			log.Println(err)
		}
		log.Printf("updated: %s", d.Updated[index])
	})
	datastore.SaveCurrentCursor(email, "/images", d.Cursor)

}
Esempio n. 4
0
func Test_ParseArticle_WithNoMetadata(t *testing.T) {
	a := assert.New(t)
	article := datastore.ParseEntry(fakeFileMetaData(), fakeFileContentWithNoMetadata())
	a.Contains(article.Content, "my first article</h1>")
	a.Equal(article.Title, "this is my first article")
	a.Equal(article.Permalink, "this-is-my-first-article")
	a.Equal(article.CreatedAt, "2011-07-19")
	a.Equal(article.FileMetadata, fakeFileMetaData())
	article.GenerateID("*****@*****.**")
	a.Equal(article.TimeStamp, "1311033600")
}
Esempio n. 5
0
func Test_Datastore_Bucket_Delete(t *testing.T) {
	a := assert.New(t)
	article := datastore.ParseEntry(fakeFileMetaData(), fakeFileContent())
	article.GenerateID("*****@*****.**")
	article.Save()

	datastore.Delete([]byte("UserArticles"), article.ID)

	_, err := datastore.LoadArticle(article.ID)
	a.NotEqual(err, nil)
}
Esempio n. 6
0
func Test_Article_Save(t *testing.T) {
	a := assert.New(t)
	func() { //I am a horrible person
		article := datastore.ParseEntry(fakeFileMetaData(), fakeFileContent())
		article.GenerateID("*****@*****.**")
		article.Save()
	}()
	article, _ := datastore.LoadArticle("[email protected]:article:/published/this_is_my-first article.md")
	a.Equal(article.Title, "this is my first article")
	a.Equal(article.Permalink, "this-is-my-first-article")
	a.Equal(article.CreatedAt, "2015-10-10")
	a.Equal(article.FileMetadata, fakeFileMetaData())
}
Esempio n. 7
0
func Test_ParseArticle(t *testing.T) {
	a := assert.New(t)
	article := datastore.ParseEntry(fakeFileMetaData(), fakeFileContent())
	a.Contains(article.Content, "my first article</h1>")
	a.NotContains(article.Content, "../images/foo.jpg")
	a.Equal(article.Title, "this is my first article")
	a.Equal(article.Permalink, "this-is-my-first-article")
	a.Equal(article.CreatedAt, "2015-10-10")
	a.Equal(article.Summary, "this is a pragraph")
	a.Equal(article.FileMetadata, fakeFileMetaData())
	article.GenerateID("*****@*****.**")
	a.Equal(article.ID, "[email protected]:article:/published/this_is_my-first article.md")
	a.Equal(article.TimeStamp, "1444435200")
}
Esempio n. 8
0
func Test_LoadArticle(t *testing.T) {
	a := assert.New(t)
	article := datastore.ParseEntry(fakeFileMetaData(), fakeFileContent())
	article.GenerateID("*****@*****.**")
	article.Save()

	//load article by ID (key)
	loadedArticle, _ := datastore.LoadArticle(article.ID)
	a.Equal(article, loadedArticle)

	//test article not found
	_, err := datastore.LoadArticle("foo")
	a.NotEqual(err, nil)
}