-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
142 lines (130 loc) · 3.89 KB
/
main_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/olherasymchuk/bookovyna/data"
"github.com/olherasymchuk/bookovyna/handlers"
"github.com/stretchr/testify/assert"
)
var api_ver string = "/api/1"
// TestMain sets up and tears down resources for all tests.
func TestMain(m *testing.M) {
data.ConnectDB()
// Run all tests
code := m.Run()
// Additional teardown code here
// Exit with the test result code
os.Exit(code)
}
func TestPostAuthors(t *testing.T) {
author := data.Author{
Name: "Григорій",
Surname: "Горинич",
}
jsonValue, _ := json.Marshal(author)
req, _ := http.NewRequest("POST", api_ver+"/authors", bytes.NewBuffer(jsonValue))
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
}
func TestGetAuthors(t *testing.T) {
req, _ := http.NewRequest("GET", api_ver+"/authors", nil)
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.NotEmpty(t, data.Authors)
}
func TestGetAuthorByID(t *testing.T) {
req, _ := http.NewRequest("GET", api_ver+"/authors/1", nil)
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.NotEmpty(t, data.Authors)
}
func TestGetAuthorByNotExistingID(t *testing.T) {
req, _ := http.NewRequest("GET", api_ver+"/authors/404", nil)
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
}
func TestPostPublishers(t *testing.T) {
publisher := data.Publisher{
Name: "Галичанська книга",
}
jsonValue, _ := json.Marshal(publisher)
req, _ := http.NewRequest("POST", api_ver+"/publishers", bytes.NewBuffer(jsonValue))
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
}
func TestGetPublishers(t *testing.T) {
req, _ := http.NewRequest("GET", api_ver+"/publishers", nil)
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.NotEmpty(t, data.Publishers)
}
func TestGetPublisherByID(t *testing.T) {
req, _ := http.NewRequest("GET", api_ver+"/publishers/1", nil)
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.NotEmpty(t, data.Publishers)
}
func TestGetPublisherByNotExistingID(t *testing.T) {
req, _ := http.NewRequest("GET", api_ver+"/publishers/404", nil)
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
}
func TestPostBooks(t *testing.T) {
book := data.Book{
Title: "Асканія-Нова. Історія заповідника",
Author_ID: 1,
Price: 380.00,
Publisher_ID: 1,
Published: 2024,
ISBN: "1111111111111",
}
jsonValue, _ := json.Marshal(book)
req, _ := http.NewRequest("POST", api_ver+"/books", bytes.NewBuffer(jsonValue))
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
}
func TestGetBooks(t *testing.T) {
req, _ := http.NewRequest("GET", api_ver+"/books", nil)
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.NotEmpty(t, data.Books)
}
func TestGetBookByID(t *testing.T) {
req, _ := http.NewRequest("GET", api_ver+"/books/1", nil)
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.NotEmpty(t, data.Books)
}
func TestGetBookByNotExistingID(t *testing.T) {
req, _ := http.NewRequest("GET", api_ver+"/books/404", nil)
w := httptest.NewRecorder()
r := handlers.SetupRouter()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
}