-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
85 lines (68 loc) · 2.14 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
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/palash0216/Non-Boarders/controllers"
"github.com/stretchr/testify/assert"
)
func TestStCreate(t *testing.T) {
// Initialize a test router
router := gin.New()
router.POST("/student", controllers.StCreate)
// Create a test request with a sample JSON payload for success
successJSON := `{
"Name": "Rahul Sharma",
"Enroll": "102V202",
"Place": "Raogarh",
"In": "8:55 AM",
"Out": "4:55 PM"}`
reqSuccess, err := http.NewRequest("POST", "/student", bytes.NewBufferString(successJSON))
if err != nil {
t.Fatal(err)
}
// Perform the request
wSuccess := httptest.NewRecorder()
router.ServeHTTP(wSuccess, reqSuccess)
// Assert the HTTP status code for success
assert.Equal(t, http.StatusOK, wSuccess.Code)
// Assert the response body for success
// Add your specific assertions here based on the expected response for success
// Create a test request with a sample JSON payload for an error
errorJSON := `{
"Name": "Rahul Sharma",
"Enroll": "102V202",
"Place": "Raogarh",
"In": "8:55 AM",
"Out": "4:55 PM"}`
reqError, err := http.NewRequest("POST", "/student", bytes.NewBufferString(errorJSON))
if err != nil {
t.Fatal(err)
}
// Perform the request
wError := httptest.NewRecorder()
router.ServeHTTP(wError, reqError)
// Assert the HTTP status code for error
assert.Equal(t, http.StatusBadRequest, wError.Code)
// Assert the response body for error
// Add your specific assertions here based on the expected response for error
}
// func TestStudentIndex(t *testing.T) {
// // Initialize a test router
// router := gin.New()
// router.GET("/student", controllers.StudentIndex)
// // Create a test request
// req, err := http.NewRequest("GET", "/student", nil)
// if err != nil {
// t.Fatal(err)
// }
// // Perform the request
// w := httptest.NewRecorder()
// router.ServeHTTP(w, req)
// // Assert the HTTP status code
// assert.Equal(t, http.StatusOK, w.Code)
// // Add more assertions as needed based on your specific logic
// }
// Similarly, create tests for StudentShow, StUpdate, and StDelete functions...