Go client for the Write.as API https://developers.write.as

post_test.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package writeas
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestPostRoundTrip(t *testing.T) {
  7. var id, token string
  8. dwac := NewClient()
  9. t.Run("Create post", func(t *testing.T) {
  10. p, err := dwac.CreatePost(&PostParams{
  11. Title: "Title!",
  12. Content: "This is a post.",
  13. Font: "sans",
  14. })
  15. if err != nil {
  16. t.Errorf("Post create failed: %v", err)
  17. return
  18. }
  19. t.Logf("Post created: %+v", p)
  20. id, token = p.ID, p.Token
  21. })
  22. t.Run("Get post", func(t *testing.T) {
  23. res, err := dwac.GetPost(id)
  24. if err != nil {
  25. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  26. } else {
  27. t.Logf("Post: %+v", res)
  28. if res.Content != "This is a post." {
  29. t.Errorf("Unexpected fetch results: %+v\n", res)
  30. }
  31. }
  32. })
  33. t.Run("Update post", func(t *testing.T) {
  34. p, err := dwac.UpdatePost(id, token, &PostParams{
  35. Content: "Now it's been updated!",
  36. })
  37. if err != nil {
  38. t.Errorf("Post update failed: %v", err)
  39. return
  40. }
  41. t.Logf("Post updated: %+v", p)
  42. })
  43. t.Run("Delete post", func(t *testing.T) {
  44. err := dwac.DeletePost(id, token)
  45. if err != nil {
  46. t.Errorf("Post delete failed: %v", err)
  47. return
  48. }
  49. t.Logf("Post deleted!")
  50. })
  51. }
  52. func TestPinUnPin(t *testing.T) {
  53. dwac := NewDevClient()
  54. _, err := dwac.LogIn("demo", "demo")
  55. if err != nil {
  56. t.Fatalf("Unable to log in: %v", err)
  57. }
  58. defer dwac.LogOut()
  59. t.Run("Pin post", func(t *testing.T) {
  60. err := dwac.PinPost("tester", &PinnedPostParams{ID: "olx6uk7064heqltf"})
  61. if err != nil {
  62. t.Fatalf("Pin failed: %v", err)
  63. }
  64. })
  65. t.Run("Unpin post", func(t *testing.T) {
  66. err := dwac.UnpinPost("tester", &PinnedPostParams{ID: "olx6uk7064heqltf"})
  67. if err != nil {
  68. t.Fatalf("Unpin failed: %v", err)
  69. }
  70. })
  71. }
  72. func ExampleClient_CreatePost() {
  73. dwac := NewDevClient()
  74. // Publish a post
  75. p, err := dwac.CreatePost(&PostParams{
  76. Title: "Title!",
  77. Content: "This is a post.",
  78. Font: "sans",
  79. })
  80. if err != nil {
  81. fmt.Printf("Unable to create: %v", err)
  82. return
  83. }
  84. fmt.Printf("%s", p.Content)
  85. // Output: This is a post.
  86. }