Skip to content

Commit 5a948b0

Browse files
authored
Merge pull request #28 from disco07/main2
testing
2 parents e4c8e27 + 2b56247 commit 5a948b0

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

.idea/workspace.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main_test.go

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"net/http"
67
"net/http/httptest"
78
"testing"
9+
"time"
810
)
911

1012
func TestFindAllPost(t *testing.T) {
13+
app := newApp()
14+
15+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
16+
defer cancel()
17+
posts, err := app.findAllPost(ctx)
18+
19+
if err != nil {
20+
t.Error("Get All Posts failed.")
21+
}
22+
23+
if len(posts) == 0 {
24+
t.Error("Posts did not return any values.")
25+
}
1126

1227
}
1328

@@ -16,14 +31,7 @@ func TestGetPosts(t *testing.T) {
1631
// pass 'nil' as the third parameter.
1732
req := httptest.NewRequest("GET", "/posts", nil)
1833

19-
var cfg = config{
20-
port: 8000,
21-
dsn: "postgres://app:app@localhost/app?sslmode=disable",
22-
}
23-
24-
db, err := open(cfg)
25-
26-
app := apps{db}
34+
app := newApp()
2735

2836
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
2937
rr := httptest.NewRecorder()
@@ -40,7 +48,7 @@ func TestGetPosts(t *testing.T) {
4048
}
4149

4250
var posts []post
43-
err = json.NewDecoder(rr.Body).Decode(&posts)
51+
err := json.NewDecoder(rr.Body).Decode(&posts)
4452
if err != nil {
4553
t.Error(err.Error())
4654
t.Error("Error retreiving list of posts.")
@@ -50,3 +58,15 @@ func TestGetPosts(t *testing.T) {
5058
t.Error("Error retreiving list of posts.")
5159
}
5260
}
61+
62+
func newApp() apps {
63+
cfg := config{
64+
port: 8000,
65+
dsn: "postgres://app:app@localhost/app?sslmode=disable",
66+
}
67+
68+
db, _ := open(cfg)
69+
return apps{
70+
DB: db,
71+
}
72+
}

0 commit comments

Comments
 (0)