本文整理匯總了Golang中github.com/minodisk/qiitactl/model.NewPost函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewPost函數的具體用法?Golang NewPost怎麽用?Golang NewPost使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewPost函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestPostSaveDuplicationWithID
func TestPostSaveDuplicationWithID(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
testutil.ShouldExistFile(t, 0)
func() {
post := model.NewPost("Example Title", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, nil)
post.ID = "abcdefghijklmnopqrst"
err := post.Save(nil)
if err != nil {
t.Fatal(err)
}
}()
func() {
post := model.NewPost("Example Title", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, nil)
post.ID = "abcdefghijklmnopqrst"
err := post.Save(nil)
if err != nil {
t.Fatal(err)
}
}()
testutil.ShouldExistFile(t, 1)
}
示例2: TestPostCreateWithWrongResponseBody
func TestPostCreateWithWrongResponseBody(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
mux.HandleFunc("/api/v2/items", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Non JSON format")
})
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
post := model.NewPost("Example Title", nil, nil)
err = post.Create(client, model.CreationOptions{})
if err == nil {
t.Fatal("error should occur")
}
}
示例3: TestPostDeleteWithNoServer
func TestPostDeleteWithNoServer(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
testutil.ShouldExistFile(t, 0)
post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil)
post.ID = "abcdefghijklmnopqrst"
err = post.Delete(client)
if err == nil {
t.Fatal("error should occur")
}
testutil.ShouldExistFile(t, 0)
}
示例4: TestPostEncodeWithNewPost
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))
}
}
示例5: TestPostCreateWithNoServer
func TestPostCreateWithNoServer(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
testutil.ShouldExistFile(t, 0)
post := model.NewPost("Example Title", nil, nil)
err = post.Create(client, model.CreationOptions{})
if err == nil {
t.Fatal("error should occur")
}
testutil.ShouldExistFile(t, 0)
}
示例6: TestPostDeleteWithWrongResponseBody
func TestPostDeleteWithWrongResponseBody(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
mux.HandleFunc("/api/v2/items/abcdefghijklmnopqrst", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Non JSON format")
})
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
post := model.NewPost("Example Title", nil, nil)
post.ID = "abcdefghijklmnopqrst"
err = post.Delete(client)
// Don't parse response body from DELETE method
if err != nil {
t.Fatal(err)
}
}
示例7: TestNewPost
func TestNewPost(t *testing.T) {
post := model.NewPost("Example Title", nil, nil)
if post.Title != "Example Title" {
t.Errorf("wrong Title: %s", post.Title)
}
if !post.CreatedAt.Equal(post.UpdatedAt.Time) {
t.Errorf("wrong CreatedAt: %s", post.CreatedAt)
}
}
示例8: BenchmarkPostsSave
func BenchmarkPostsSave(b *testing.B) {
testutil.CleanUp()
defer testutil.CleanUp()
var posts model.Posts
for i := 0; i < b.N; i++ {
posts = append(posts, model.NewPost(fmt.Sprintf("Example Title %d", i), nil, nil))
}
b.ResetTimer()
err := posts.Save()
if err != nil {
b.Fatal(err)
}
}
示例9: Run
// GenerateFile generates markdown file at current working directory.
func (r GenerateFileRunner) Run(c api.Client, o GlobalOptions, w io.Writer) (err error) {
var team *model.Team
if *r.Team != "" {
team = &model.Team{
ID: *r.Team,
}
}
post := model.NewPost(*r.Title, nil, team)
err = post.Save(nil)
if err != nil {
return
}
_, err = fmt.Fprintf(w, "%s\n", post.Path)
return
}
示例10: TestPostSaveWithPath
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))
}
}()
}
示例11: TestPostUpdateWithEmptyID
func TestPostUpdateWithEmptyID(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
log.Fatal(err)
}
client := api.NewClient(nil, inf)
testutil.ShouldExistFile(t, 0)
post := model.NewPost("Example Title", nil, nil)
err = post.Update(client)
err, ok := err.(model.EmptyIDError)
if !ok {
t.Fatal("empty ID error should occur")
}
testutil.ShouldExistFile(t, 0)
}
示例12: TestPostsSave
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))
}
}()
}
示例13: TestPostUpdateInTeam
func TestPostUpdateInTeam(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
mux.HandleFunc("/api/v2/items/abcdefghijklmnopqrst", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PATCH" {
w.WriteHeader(405)
b, _ := json.Marshal(api.ResponseError{"method_not_allowed", "Method Not Allowed"})
w.Write(b)
return
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
testutil.ResponseError(w, 500, err)
return
}
if string(b) == "" {
testutil.ResponseAPIError(w, 500, api.ResponseError{
Type: "fatal",
Message: "empty body",
})
return
}
var post model.Post
err = json.Unmarshal(b, &post)
if err != nil {
testutil.ResponseError(w, 500, err)
return
}
post.UpdatedAt = model.Time{Time: time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)}
b, err = json.Marshal(post)
if err != nil {
testutil.ResponseError(w, 500, err)
return
}
_, err = w.Write(b)
if err != nil {
testutil.ResponseError(w, 500, err)
return
}
})
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
if subDomain != "increments" {
t.Fatalf("wrong sub domain: %s", subDomain)
return
}
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
testutil.ShouldExistFile(t, 0)
post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, &model.Team{Active: true, ID: "increments", Name: "Increments Inc."})
post.ID = "abcdefghijklmnopqrst"
prevPath := post.Path
if err != nil {
t.Fatal(err)
}
err = post.Update(client)
if err != nil {
t.Fatal(err)
}
postPath := 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)
}
testutil.ShouldExistFile(t, 0)
}
示例14: TestPostCreateWithTweetAndGist
func TestPostCreateWithTweetAndGist(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
mux.HandleFunc("/api/v2/items", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(405)
b, _ := json.Marshal(api.ResponseError{"method_not_allowed", "Method Not Allowed"})
w.Write(b)
return
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
testutil.ResponseError(w, 500, err)
return
}
if string(b) == "" {
testutil.ResponseAPIError(w, 500, api.ResponseError{
Type: "fatal",
Message: "empty body",
})
return
}
var post model.CreationPost
err = json.Unmarshal(b, &post)
if err != nil {
testutil.ResponseError(w, 500, err)
return
}
if !post.Tweet || !post.Gist {
testutil.ResponseError(w, 500, errors.New("tweet and gist should be true"))
return
}
post.CreatedAt = model.Time{Time: time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)}
post.UpdatedAt = post.CreatedAt
b, err = json.Marshal(post)
if err != nil {
testutil.ResponseError(w, 500, err)
return
}
_, err = w.Write(b)
if err != nil {
testutil.ResponseError(w, 500, err)
return
}
})
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
testutil.ShouldExistFile(t, 0)
post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil)
prevPath := post.Path
if err != nil {
t.Fatal(err)
}
err = post.Create(client, model.CreationOptions{true, true})
if err != nil {
t.Fatal(err)
}
postPath := 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)
}
if !post.UpdatedAt.Equal(time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)) {
t.Errorf("wrong UpdatedAt: %s", post.UpdatedAt)
}
testutil.ShouldExistFile(t, 0)
}
示例15: TestPostValidateWithTeam
func TestPostValidateWithTeam(t *testing.T) {
func() {
post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil)
post.Body = "Example Body"
post.Team = &model.Team{
Active: true,
ID: "increments",
Name: "Increments Inc.",
}
err := post.Validate()
if err != nil {
t.Error("should be valid")
}
}()
func() {
post := model.NewPost("", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil)
post.Body = "Example Body"
post.Team = &model.Team{
Active: true,
ID: "increments",
Name: "Increments Inc.",
}
err := post.Validate()
if err == nil {
t.Error("should be invalid")
}
for name, status := range err {
switch name {
case "title":
if !status.Required {
t.Errorf("%s should be required", name)
}
default:
t.Errorf("%s should be valid: %s", name, status)
}
}
}()
func() {
post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil)
post.Team = &model.Team{
Active: true,
ID: "increments",
Name: "Increments Inc.",
}
err := post.Validate()
if err == nil {
t.Error("should be invalid")
}
for name, status := range err {
switch name {
case "body":
if !status.Required {
t.Errorf("%s should be required", name)
}
default:
t.Errorf("%s should be valid: %s", name, status)
}
}
}()
func() {
post := model.NewPost("", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil)
post.Team = &model.Team{
Active: true,
ID: "increments",
Name: "Increments Inc.",
}
err := post.Validate()
if err == nil {
t.Error("should be invalid")
}
for name, status := range err {
switch name {
case "title":
if !status.Required {
t.Errorf("%s should be required", name)
}
case "body":
if !status.Required {
t.Errorf("%s should be required", name)
}
default:
t.Errorf("%s should be valid: %s", name, status)
}
}
}()
}