func TestPostEncodeWithNewPost(t *testing.T) { post := model.NewPost("Example title", &model.Time{time.Date(2016, 2, 2, 6, 30, 46, 0, time.UTC)}, nil) post.ID = "4bd431809afb1bb99e4f" post.URL = "https://qiita.com/yaotti/items/4bd431809afb1bb99e4f" buf := bytes.NewBuffer([]byte{}) err := post.Encode(buf) if err != nil { t.Fatal(err) } actual := string(buf.Bytes()) expected := `<!-- id: 4bd431809afb1bb99e4f url: https://qiita.com/yaotti/items/4bd431809afb1bb99e4f created_at: 2016-02-02T15:30:46+09:00 updated_at: 2016-02-02T15:30:46+09:00 private: false coediting: false tags: [] team: null --> # Example title ` if expected != actual { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }
func TestTagsMarshalYAMLWithTagsVersions(t *testing.T) { tags := model.Tags{ model.Tag{ Name: "Go", Versions: []string{ "1.5.3", "1.6.0", }, }, model.Tag{ Name: "Ruby", Versions: []string{ "2.3.0", }, }, } b, err := yaml.Marshal(tags) if err != nil { t.Fatal(err) } actual := string(b) expected := `- Go: - 1.5.3 - 1.6.0 - Ruby: - 2.3.0 ` if actual != expected { t.Errorf("wrong string:\n%s", testutil.Diff(expected, actual)) } }
func TestFetchPostWithID(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() mux := http.NewServeMux() handleItem(mux) serverMine := httptest.NewServer(mux) defer serverMine.Close() err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX") if err != nil { log.Fatal(err) } client := api.NewClient(func(subDomain, path string) (url string) { switch subDomain { case "": url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path) default: log.Fatalf("wrong sub domain \"%s\"", subDomain) } return }, inf) testutil.ShouldExistFile(t, 0) errBuf := bytes.NewBuffer([]byte{}) app := command.New(inf, client, os.Stdout, errBuf) app.Run([]string{"qiitactl", "fetch", "post", "-i", "4bd431809afb1bb99e4f"}) e := errBuf.Bytes() if len(e) != 0 { t.Fatal(string(e)) } testutil.ShouldExistFile(t, 1) b, err := ioutil.ReadFile("mine/2000/01/01/Example Title.md") if err != nil { t.Fatal(err) } actual := string(b) expected := `<!-- id: 4bd431809afb1bb99e4f url: https://qiita.com/yaotti/items/4bd431809afb1bb99e4f created_at: 2000-01-01T09:00:00+09:00 updated_at: 2000-01-01T09:00:00+09:00 private: false coediting: false tags: - Ruby: - 0.0.1 team: null --> # Example Title ## Example body` if actual != expected { t.Errorf("wrong body:\n%s", testutil.Diff(expected, actual)) } }
func TestTagsMarshalYAMLWithEmptyTags(t *testing.T) { tags := model.Tags{} b, err := yaml.Marshal(tags) if err != nil { t.Fatal(err) } actual := string(b) expected := `[] ` if actual != expected { t.Errorf("wrong string:\n%s", testutil.Diff(expected, actual)) } }
func TestTagsMarshalYAMLWithATagNoVersion(t *testing.T) { tags := model.Tags{ model.Tag{ Name: "Go", }, } b, err := yaml.Marshal(tags) if err != nil { t.Fatal(err) } actual := string(b) expected := `- Go ` if actual != expected { t.Errorf("wrong string:\n%s", testutil.Diff(expected, actual)) } }
func TestTagMarshalJSONWithNoVersion(t *testing.T) { tag := model.Tag{ Name: "Go", } b, err := json.MarshalIndent(tag, "", " ") if err != nil { t.Fatal(err) } actual := string(b) expected := `{ "name": "Go", "versions": [] }` if actual != expected { t.Errorf("wrong JSON:\n%s", testutil.Diff(expected, actual)) } }
func TestPostSaveWithPath(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() testutil.ShouldExistFile(t, 0) post := model.NewPost("Example Title", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, nil) post.ID = "abcdefghijklmnopqrst" post.Path = "foo/bar.md" err := post.Save(nil) if err != nil { t.Fatal(err) } testutil.ShouldExistFile(t, 1) func() { a, err := ioutil.ReadFile("foo/bar.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: abcdefghijklmnopqrst url: "" created_at: 2015-11-28T22:02:37+09:00 updated_at: 2015-11-28T22:02:37+09:00 private: false coediting: false tags: [] team: null --> # Example Title ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() }
func TestInvalidError(t *testing.T) { func() { err := model.InvalidError{} if err.Error() != "Valid" { t.Errorf("wrong message: %s", err) } }() func() { err := model.InvalidError{} err["foo"] = model.InvalidStatus{ Name: "foo", Required: true, } actual := err.Error() expected := `A field is invalid: - foo - shouldn't be empty` if actual != expected { t.Errorf("wrong message:\n%s", testutil.Diff(expected, actual)) } }() }
func TestMetaEncode(t *testing.T) { at := model.Time{Time: time.Date(2011, 2, 3, 4, 5, 6, 0, time.UTC)} tag := model.Tag{ Name: "Go", } tags := model.Tags{tag} meta := model.Meta{ ID: "4bd431809afb1bb99e4f", URL: "https://qiita.com/yaotti/items/4bd431809afb1bb99e4f", CreatedAt: at, UpdatedAt: at, Private: true, Coediting: true, Tags: tags, Team: &model.Team{ Active: true, ID: "increments", Name: "Increments Inc.", }, } expected := `id: 4bd431809afb1bb99e4f url: https://qiita.com/yaotti/items/4bd431809afb1bb99e4f created_at: 2011-02-03T13:05:06+09:00 updated_at: 2011-02-03T13:05:06+09:00 private: true coediting: true tags: - Go team: active: true id: increments name: Increments Inc.` actual := meta.Encode() if actual != expected { t.Errorf("wrong string:\n%s", testutil.Diff(expected, actual)) } }
func TestPostsSave(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() post0 := model.NewPost("Example Title 0", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, nil) post1 := model.NewPost("Example Title 1", &model.Time{time.Date(2016, 2, 1, 5, 21, 49, 0, time.UTC)}, nil) posts := model.Posts{post0, post1} err := posts.Save() if err != nil { t.Fatal(err) } func() { a, err := ioutil.ReadFile("mine/2015/11/28/Example Title 0.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: "" url: "" created_at: 2015-11-28T22:02:37+09:00 updated_at: 2015-11-28T22:02:37+09:00 private: false coediting: false tags: [] team: null --> # Example Title 0 ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() func() { a, err := ioutil.ReadFile("mine/2016/02/01/Example Title 1.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: "" url: "" created_at: 2016-02-01T14:21:49+09:00 updated_at: 2016-02-01T14:21:49+09:00 private: false coediting: false tags: [] team: null --> # Example Title 1 ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() }
func TestPostSaveWithInvalidMarkdown(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() err := os.MkdirAll("foo", 0755) if err != nil { t.Fatal(err) } err = ioutil.WriteFile("foo/wrong-format.md", []byte{}, 0644) if err != nil { t.Fatal(err) } err = ioutil.WriteFile("foo/no-id.md", []byte(`<!-- id: "" url: "" created_at: 2015-11-28T22:02:37+09:00 updated_at: 2015-11-28T22:02:37+09:00 private: false coediting: false tags: [] team: null --> # Example Title `), 0644) if err != nil { t.Fatal(err) } if err != nil { t.Fatal(err) } testutil.ShouldExistFile(t, 2) func() { post := model.NewPost("Example Title", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, nil) post.ID = "abcdefghijklmnopqrst" post.Path = "foo/bar.md" err := post.Save(nil) if err != nil { t.Fatal(err) } }() testutil.ShouldExistFile(t, 3) func() { a, err := ioutil.ReadFile("foo/bar.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: abcdefghijklmnopqrst url: "" created_at: 2015-11-28T22:02:37+09:00 updated_at: 2015-11-28T22:02:37+09:00 private: false coediting: false tags: [] team: null --> # Example Title ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() }
func TestPostSaveWithTeam(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() testutil.ShouldExistFile(t, 0) post := model.NewPost("Example Title", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, &model.Team{Active: true, ID: "increments", Name: "Increments Inc."}) post.ID = "abcdefghijklmnopqrst" err := post.Save(nil) if err != nil { t.Fatal(err) } testutil.ShouldExistFile(t, 1) func() { a, err := ioutil.ReadFile("increments/2015/11/28/Example Title.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: abcdefghijklmnopqrst url: "" created_at: 2015-11-28T22:02:37+09:00 updated_at: 2015-11-28T22:02:37+09:00 private: false coediting: false tags: [] team: active: true id: increments name: Increments Inc. --> # Example Title ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() post.Title = "Example Edited Title" post.CreatedAt = model.Time{time.Date(2015, 12, 28, 13, 2, 37, 0, time.UTC)} post.UpdatedAt = post.CreatedAt err = post.Save(nil) if err != nil { t.Fatal(err) } testutil.ShouldExistFile(t, 1) func() { _, err := os.Stat("increments/2015/12/28/Example Edited Title.md") if err == nil { t.Errorf("filename based on edited post shouldn't exist: %s", "mine/2015/12/28/Example Edited Title.md") } }() func() { a, err := ioutil.ReadFile("increments/2015/11/28/Example Title.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: abcdefghijklmnopqrst url: "" created_at: 2015-12-28T22:02:37+09:00 updated_at: 2015-12-28T22:02:37+09:00 private: false coediting: false tags: [] team: active: true id: increments name: Increments Inc. --> # Example Edited Title ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() }