generated from Yandex-Practicum/go-musthave-metrics-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
118 lines (107 loc) · 3.35 KB
/
example_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
package monitoring_test
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
common "github.com/matthiasBT/monitoring/internal/infra/entities"
"github.com/matthiasBT/monitoring/internal/infra/logging"
"github.com/matthiasBT/monitoring/internal/server/adapters"
"github.com/matthiasBT/monitoring/internal/server/usecases"
"github.com/sirupsen/logrus"
)
func Example() {
logger := logging.SetupLogger()
logger.SetLevel(logrus.FatalLevel) // to avoid printing unnecessary logs
storage := adapters.NewMemStorage(nil, nil, logger, nil)
controller := usecases.NewBaseController(logger, storage, "/")
ping(controller)
updateCounter(100500, controller)
updateGauge(5.5, controller)
getCounter(controller)
getGauge(controller)
updateCounter(11, controller)
updateGauge(1.5, controller)
getCounter(controller)
getGauge(controller)
// Output:
// 200
// {id:FooBar, type:counter, delta:100500}
// {id:BarFoo, type:gauge, value:5.5}
// {id:FooBar, type:counter, delta:100511}
// {id:BarFoo, type:gauge, value:1.5}
}
func updateCounter(value int64, controller *usecases.BaseController) {
w := httptest.NewRecorder()
newCounter := common.Metrics{
ID: "FooBar",
MType: "counter",
Delta: &value,
Value: nil,
}
body, _ := json.Marshal(newCounter)
addCounterReq := httptest.NewRequest(http.MethodPost, "/update/", bytes.NewReader(body))
addCounterReq.Header.Set("Content-Type", "application/json")
controller.UpdateMetric(w, addCounterReq)
}
func updateGauge(value float64, controller *usecases.BaseController) {
w := httptest.NewRecorder()
newGauge := common.Metrics{
ID: "BarFoo",
MType: "gauge",
Delta: nil,
Value: &value,
}
body, _ := json.Marshal(newGauge)
addGaugeReq := httptest.NewRequest(http.MethodPost, "/update/", bytes.NewReader(body))
addGaugeReq.Header.Set("Content-Type", "application/json")
controller.UpdateMetric(w, addGaugeReq)
}
func getCounter(controller *usecases.BaseController) {
w := httptest.NewRecorder()
newCounter := common.Metrics{
ID: "FooBar",
MType: "counter",
Delta: nil,
Value: nil,
}
body, _ := json.Marshal(newCounter)
getCounterReq := httptest.NewRequest(http.MethodGet, "/value/", bytes.NewReader(body))
getCounterReq.Header.Set("Content-Type", "application/json")
controller.GetMetric(w, getCounterReq)
printSorted(w.Body.Bytes())
}
func getGauge(controller *usecases.BaseController) {
w := httptest.NewRecorder()
newGauge := common.Metrics{
ID: "BarFoo",
MType: "gauge",
Delta: nil,
Value: nil,
}
body, _ := json.Marshal(newGauge)
getGaugeReq := httptest.NewRequest(http.MethodGet, "/value/", bytes.NewReader(body))
getGaugeReq.Header.Set("Content-Type", "application/json")
controller.GetMetric(w, getGaugeReq)
printSorted(w.Body.Bytes())
}
func printSorted(body []byte) {
var result common.Metrics
json.Unmarshal(body, &result)
if result.MType == common.TypeGauge {
fmt.Printf("{%s:%v, %s:%v, %s:%v}\n", "id", result.ID, "type", result.MType, "value", *result.Value)
} else {
fmt.Printf("{%s:%v, %s:%v, %s:%v}\n", "id", result.ID, "type", result.MType, "delta", *result.Delta)
}
}
func ping(controller *usecases.BaseController) {
w := httptest.NewRecorder()
getGaugeReq := httptest.NewRequest(http.MethodGet, "/ping", nil)
controller.Ping(w, getGaugeReq)
resp := w.Result()
if resp.Body != nil {
defer resp.Body.Close()
}
fmt.Println(resp.StatusCode)
}