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

collection_test.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package writeas
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "time"
  7. )
  8. func TestGetCollection(t *testing.T) {
  9. wac := NewClient()
  10. res, err := wac.GetCollection("blog")
  11. if err != nil {
  12. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  13. } else {
  14. t.Logf("Collection: %+v", res)
  15. if res.Title != "write.as" {
  16. t.Errorf("Unexpected fetch results: %+v\n", res)
  17. }
  18. }
  19. }
  20. func TestGetCollectionPosts(t *testing.T) {
  21. wac := NewClient()
  22. res, err := wac.GetCollectionPosts("blog")
  23. if err != nil {
  24. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  25. } else {
  26. if len(*res) == 0 {
  27. t.Errorf("No posts returned!")
  28. }
  29. }
  30. }
  31. func TestGetUserCollections(t *testing.T) {
  32. wac := NewDevClient()
  33. _, err := wac.LogIn("demo", "demo")
  34. if err != nil {
  35. t.Fatalf("Unable to log in: %v", err)
  36. }
  37. defer wac.LogOut()
  38. res, err := wac.GetUserCollections()
  39. if err != nil {
  40. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  41. } else {
  42. t.Logf("User collections: %+v", res)
  43. if len(*res) == 0 {
  44. t.Errorf("No collections returned!")
  45. }
  46. }
  47. }
  48. func TestCreateAndDeleteCollection(t *testing.T) {
  49. wac := NewDevClient()
  50. _, err := wac.LogIn("demo", "demo")
  51. if err != nil {
  52. t.Fatalf("Unable to log in: %v", err)
  53. }
  54. defer wac.LogOut()
  55. now := time.Now().Unix()
  56. alias := fmt.Sprintf("test-collection-%v", now)
  57. c, err := wac.CreateCollection(&CollectionParams{
  58. Alias: alias,
  59. Title: fmt.Sprintf("Test Collection %v", now),
  60. })
  61. if err != nil {
  62. t.Fatalf("Unable to create collection %q: %v", alias, err)
  63. }
  64. if err := wac.DeleteCollection(c.Alias); err != nil {
  65. t.Fatalf("Unable to delete collection %q: %v", alias, err)
  66. }
  67. }
  68. func TestDeleteCollectionUnauthenticated(t *testing.T) {
  69. wac := NewDevClient()
  70. now := time.Now().Unix()
  71. alias := fmt.Sprintf("test-collection-does-not-exist-%v", now)
  72. err := wac.DeleteCollection(alias)
  73. if err == nil {
  74. t.Fatalf("Should not be able to delete collection %q unauthenticated.", alias)
  75. }
  76. if !strings.Contains(err.Error(), "Not authenticated") {
  77. t.Fatalf("Error message should be more informative: %v", err)
  78. }
  79. }
  80. func ExampleClient_GetCollection() {
  81. c := NewClient()
  82. coll, err := c.GetCollection("blog")
  83. if err != nil {
  84. fmt.Printf("%v", err)
  85. return
  86. }
  87. fmt.Printf("%s", coll.Title)
  88. // Output: write.as
  89. }