Browse Source

closes T589 update tests to use dev client

this changes tests to only use the dev client as all code is shared
between that and the write.as client, just the host changes.

also updated post tests to round trip and test over sub tests
Rob Loranger 6 months ago
parent
commit
de8ce4dcd2
No account linked to committer's email
2 changed files with 83 additions and 102 deletions
  1. 27
    29
      collection_test.go
  2. 56
    73
      post_test.go

+ 27
- 29
collection_test.go View File

@@ -8,47 +8,45 @@ import (
8 8
 )
9 9
 
10 10
 func TestGetCollection(t *testing.T) {
11
-	wac := NewClient()
11
+	dwac := NewDevClient()
12 12
 
13
-	res, err := wac.GetCollection("blog")
13
+	res, err := dwac.GetCollection("tester")
14 14
 	if err != nil {
15 15
 		t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
16
-	} else {
17
-		t.Logf("Collection: %+v", res)
18
-		if res.Title != "write.as blog" {
19
-			t.Errorf("Unexpected fetch results: %+v\n", res)
20
-		}
16
+	}
17
+	if res == nil {
18
+		t.Error("Expected collection to not be nil")
21 19
 	}
22 20
 }
23 21
 
24 22
 func TestGetCollectionPosts(t *testing.T) {
25
-	wac := NewClient()
23
+	dwac := NewDevClient()
24
+	posts := []Post{}
26 25
 
27
-	res, err := wac.GetCollectionPosts("blog")
28
-	if err != nil {
29
-		t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
30
-	} else {
26
+	t.Run("Get all posts in collection", func(t *testing.T) {
27
+		res, err := dwac.GetCollectionPosts("tester")
28
+		if err != nil {
29
+			t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
30
+		}
31 31
 		if len(*res) == 0 {
32
-			t.Errorf("No posts returned!")
32
+			t.Error("Expected at least on post in collection")
33
+		}
34
+		posts = *res
35
+	})
36
+	t.Run("Get one post from collection", func(t *testing.T) {
37
+		res, err := dwac.GetCollectionPost("tester", posts[0].Slug)
38
+		if err != nil {
39
+			t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
33 40
 		}
34
-	}
35
-}
36
-
37
-func TestGetCollectionPost(t *testing.T) {
38
-	wac := NewClient()
39
-
40
-	res, err := wac.GetCollectionPost("blog", "extending-write-as")
41
-	if err != nil {
42
-		t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
43
-	}
44 41
 
45
-	if res == nil {
46
-		t.Errorf("No post returned!")
47
-	}
42
+		if res == nil {
43
+			t.Errorf("No post returned!")
44
+		}
48 45
 
49
-	if len(res.Content) == 0 {
50
-		t.Errorf("Post content is empty!")
51
-	}
46
+		if len(res.Content) == 0 {
47
+			t.Errorf("Post content is empty!")
48
+		}
49
+	})
52 50
 }
53 51
 
54 52
 func TestGetUserCollections(t *testing.T) {

+ 56
- 73
post_test.go View File

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