forked from demo-apps/go-gin-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.article_test.go
55 lines (42 loc) · 1.26 KB
/
models.article_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// models.article_test.go
package main
import "testing"
// Test the function that fetches all articles
func TestGetAllArticles(t *testing.T) {
alist := getAllArticles()
// Check that the length of the list of articles returned is the
// same as the length of the global variable holding the list
if len(alist) != len(articleList) {
t.Fail()
}
// Check that each member is identical
for i, v := range alist {
if v.Content != articleList[i].Content ||
v.ID != articleList[i].ID ||
v.Title != articleList[i].Title {
t.Fail()
break
}
}
}
// Test the function that fetche an Article by its ID
func TestGetArticleByID(t *testing.T) {
a, err := getArticleByID(1)
if err != nil || a.ID != 1 || a.Title != "Article 1" || a.Content != "Article 1 body" {
t.Fail()
}
}
// Test the function that creates a new article
func TestCreateNewArticle(t *testing.T) {
// get the original count of articles
originalLength := len(getAllArticles())
// add another article
a, err := createNewArticle("New test title", "New test content")
// get the new count of articles
allArticles := getAllArticles()
newLength := len(allArticles)
if err != nil || newLength != originalLength+1 ||
a.Title != "New test title" || a.Content != "New test content" {
t.Fail()
}
}