Skip to content

Commit

Permalink
Add test for #16
Browse files Browse the repository at this point in the history
  • Loading branch information
minodisk committed Feb 5, 2016
1 parent 2fb8cbc commit fd04a4a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 11 deletions.
21 changes: 19 additions & 2 deletions command/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -607,8 +608,8 @@ func TestCreatePost(t *testing.T) {
t.Fatal(err)
}
err = ioutil.WriteFile("mine/2000/01/01-example-title.md", []byte(`<!--
id: 4bd431809afb1bb99e4f
url: https://qiita.com/yaotti/items/4bd431809afb1bb99e4f
id: ""
url: ""
created_at: 2000-01-01T09:00:00+09:00
updated_at: 2000-01-01T09:00:00+09:00
private: false
Expand Down Expand Up @@ -636,6 +637,14 @@ tags:
t.Fatal(string(e))
}

matches, err := filepath.Glob("*/*/*/*.md")
if err != nil {
t.Fatal(err)
}
if len(matches) > 1 {
t.Fatal("shouldn't make a new file")
}

b, err := ioutil.ReadFile("mine/2000/01/01-example-title.md")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -700,6 +709,14 @@ tags:
t.Fatal(string(e))
}

matches, err := filepath.Glob("*/*/*/*.md")
if err != nil {
t.Fatal(err)
}
if len(matches) > 1 {
t.Fatal("shouldn't make a new file")
}

b, err := ioutil.ReadFile("mine/2000/01/01-example-title.md")
if err != nil {
t.Fatal(err)
Expand Down
25 changes: 16 additions & 9 deletions model/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,9 @@ func (post *Post) Delete(client api.Client) (err error) {
}

func (post Post) Save() (err error) {
var path string
if post.ID != "" {
path, err = post.findPath()
if err != nil {
return err
}
}
if path == "" {
path = post.createPath()
path, err := post.Path()
if err != nil {
return
}

dir := filepath.Dir(path)
Expand All @@ -193,6 +187,19 @@ func (post Post) Save() (err error) {
return
}

func (post Post) Path() (path string, err error) {
if post.ID != "" {
path, err = post.findPath()
if err != nil {
return
}
}
if path == "" {
path = post.createPath()
}
return
}

func (post Post) findPath() (path string, err error) {
dir, err := os.Getwd()
if err != nil {
Expand Down
56 changes: 56 additions & 0 deletions model/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,25 @@ func TestPostCreate(t *testing.T) {
}

post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil)

prevPath, err := post.Path()
if err != nil {
t.Fatal(err)
}

err = post.Create(client, model.CreationOptions{})
if err != nil {
t.Fatal(err)
}

postPath, err := post.Path()
if err != nil {
t.Fatal(err)
}
if postPath != prevPath {
t.Errorf("wrong path: expected %s, but actual %s", prevPath, postPath)
}

if !post.CreatedAt.Equal(time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)) {
t.Errorf("wrong CreatedAt: %s", post.CreatedAt)
}
Expand Down Expand Up @@ -177,11 +191,25 @@ func TestPostCreateWithTweetAndGist(t *testing.T) {
}

post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil)

prevPath, err := post.Path()
if err != nil {
t.Fatal(err)
}

err = post.Create(client, model.CreationOptions{true, true})
if err != nil {
t.Fatal(err)
}

postPath, err := post.Path()
if err != nil {
t.Fatal(err)
}
if postPath != prevPath {
t.Errorf("wrong path: expected %s, but actual %s", prevPath, postPath)
}

if !post.CreatedAt.Equal(time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)) {
t.Errorf("wrong CreatedAt: %s", post.CreatedAt)
}
Expand Down Expand Up @@ -516,11 +544,25 @@ func TestPostUpdate(t *testing.T) {

post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil)
post.ID = "abcdefghijklmnopqrst"

prevPath, err := post.Path()
if err != nil {
t.Fatal(err)
}

err = post.Update(client)
if err != nil {
t.Fatal(err)
}

postPath, err := post.Path()
if err != nil {
t.Fatal(err)
}
if postPath != prevPath {
t.Errorf("wrong path: expected %s, but actual %s", prevPath, postPath)
}

if !post.UpdatedAt.Equal(time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)) {
t.Errorf("wrong UpdatedAt: %s", post.UpdatedAt)
}
Expand Down Expand Up @@ -613,10 +655,24 @@ func TestPostDelete(t *testing.T) {

post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil)
post.ID = "abcdefghijklmnopqrst"

prevPath, err := post.Path()
if err != nil {
t.Fatal(err)
}

err = post.Delete(client)
if err != nil {
t.Fatal(err)
}

postPath, err := post.Path()
if err != nil {
t.Fatal(err)
}
if postPath != prevPath {
t.Errorf("wrong path: expected %s, but actual %s", prevPath, postPath)
}
}

func TestPostSave(t *testing.T) {
Expand Down

0 comments on commit fd04a4a

Please sign in to comment.