generated from Yandex-Practicum/go-musthave-metrics-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c96bbdd
commit 6df9ca8
Showing
5 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
package usecases | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/go-chi/chi/v5" | ||
common "github.com/matthiasBT/monitoring/internal/infra/entities" | ||
) | ||
|
||
func Test_handleInvalidMetric(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
err error | ||
wantCode int | ||
wantBody string | ||
}{ | ||
{ | ||
name: "invalid_type", | ||
err: common.ErrInvalidMetricType, | ||
wantCode: http.StatusBadRequest, | ||
wantBody: common.ErrInvalidMetricType.Error(), | ||
}, | ||
{ | ||
name: "no_name", | ||
err: common.ErrMissingMetricName, | ||
wantCode: http.StatusNotFound, | ||
wantBody: common.ErrMissingMetricName.Error(), | ||
}, | ||
{ | ||
name: "invalid_val", | ||
err: common.ErrInvalidMetricVal, | ||
wantCode: http.StatusBadRequest, | ||
wantBody: common.ErrInvalidMetricVal.Error(), | ||
}, | ||
{ | ||
name: "internal_server_error", | ||
err: errors.New("foobar"), | ||
wantCode: http.StatusInternalServerError, | ||
wantBody: "foobar", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
rr := httptest.NewRecorder() | ||
t.Run(tt.name, func(t *testing.T) { | ||
handleInvalidMetric(rr, tt.err) | ||
}) | ||
if tt.wantCode != rr.Code { | ||
t.Errorf("Code mismatch. got: %v, want: %v\n", rr.Code, tt.wantCode) | ||
} | ||
if !bytes.Equal([]byte(tt.wantBody), rr.Body.Bytes()) { | ||
t.Errorf("Body mismatch. got: %s, want: %s\n", rr.Body.Bytes(), tt.wantBody) | ||
} | ||
} | ||
} | ||
|
||
func Test_parseMetric(t *testing.T) { | ||
type args struct { | ||
url string | ||
body string | ||
asJSON bool | ||
withValue bool | ||
paramType string | ||
paramName string | ||
paramValue string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *common.Metrics | ||
}{ | ||
{ | ||
name: "test_valid_counter_json", args: args{ | ||
url: "/update/", | ||
body: `{"id": "foobar", "type": "counter", "delta": 123}`, | ||
asJSON: true, | ||
withValue: false, | ||
}, want: &common.Metrics{ | ||
ID: "foobar", | ||
MType: "counter", | ||
Delta: ptrint64(123), | ||
Value: nil, | ||
}, | ||
}, | ||
{ | ||
name: "test_valid_gauge_json", args: args{ | ||
url: "/update/", | ||
body: `{"id": "foobar", "type": "gauge", "value": 123.4}`, | ||
asJSON: true, | ||
withValue: false, | ||
}, want: &common.Metrics{ | ||
ID: "foobar", | ||
MType: "gauge", | ||
Delta: nil, | ||
Value: ptrfloat64(123.4), | ||
}, | ||
}, | ||
{ | ||
name: "test_invalid_counter_json", args: args{ | ||
url: "/update/", | ||
body: `"id": "foobar", "type": "counter", "delta": 123}`, | ||
asJSON: true, | ||
withValue: false, | ||
}, want: nil, | ||
}, | ||
{ | ||
name: "test_valid_counter_params_noval", args: args{ | ||
url: "/update/counter/foobar/123", | ||
body: "", | ||
asJSON: false, | ||
withValue: false, | ||
paramType: "counter", | ||
paramName: "foobar", | ||
paramValue: "123", | ||
}, want: &common.Metrics{ | ||
ID: "foobar", | ||
MType: "counter", | ||
Delta: nil, | ||
Value: nil, | ||
}, | ||
}, | ||
{ | ||
name: "test_valid_gauge_params_noval", args: args{ | ||
url: "/update/gauge/foobar/123", | ||
body: "", | ||
asJSON: false, | ||
withValue: false, | ||
paramType: "gauge", | ||
paramName: "foobar", | ||
paramValue: "123.4", | ||
}, want: &common.Metrics{ | ||
ID: "foobar", | ||
MType: "gauge", | ||
Delta: nil, | ||
Value: nil, | ||
}, | ||
}, | ||
{ | ||
name: "test_valid_counter_params_val", args: args{ | ||
url: "/update/counter/foobar/123", | ||
body: "", | ||
asJSON: false, | ||
withValue: true, | ||
paramType: "counter", | ||
paramName: "foobar", | ||
paramValue: "123", | ||
}, want: &common.Metrics{ | ||
ID: "foobar", | ||
MType: "counter", | ||
Delta: ptrint64(123), | ||
Value: nil, | ||
}, | ||
}, | ||
{ | ||
name: "test_valid_gauge_params_val", args: args{ | ||
url: "/update/gauge/foobar/123.4", | ||
body: "", | ||
asJSON: false, | ||
withValue: true, | ||
paramType: "gauge", | ||
paramName: "foobar", | ||
paramValue: "123.4", | ||
}, want: &common.Metrics{ | ||
ID: "foobar", | ||
MType: "gauge", | ||
Delta: nil, | ||
Value: ptrfloat64(123.4), | ||
}, | ||
}, | ||
{ | ||
name: "test_invalid_counter_params_val", args: args{ | ||
url: "/update/counter/foobar/123a", | ||
body: "", | ||
asJSON: false, | ||
withValue: true, | ||
paramType: "counter", | ||
paramName: "foobar", | ||
paramValue: "123a", | ||
}, want: nil, | ||
}, | ||
{ | ||
name: "test_invalid_gauge_params_val", args: args{ | ||
url: "/update/gauge/foobar/123.4a", | ||
body: "", | ||
asJSON: false, | ||
withValue: true, | ||
paramType: "gauge", | ||
paramName: "foobar", | ||
paramValue: "123.4a", | ||
}, want: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var req *http.Request | ||
var err error | ||
if tt.args.asJSON { | ||
body := []byte(tt.args.body) | ||
req, err = http.NewRequest("_", tt.args.url, bytes.NewBuffer(body)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} else { | ||
rctx := chi.NewRouteContext() | ||
rctx.URLParams.Add("type", tt.args.paramType) | ||
rctx.URLParams.Add("name", tt.args.paramName) | ||
rctx.URLParams.Add("value", tt.args.paramValue) | ||
req, err = http.NewRequest("_", tt.args.url, bytes.NewBuffer([]byte{})) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx)) | ||
} | ||
if got := parseMetric(req, tt.args.asJSON, tt.args.withValue); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("parseMetric() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_writeMetric(t *testing.T) { | ||
type args struct { | ||
w http.ResponseWriter | ||
asJSON bool | ||
metrics *common.Metrics | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := writeMetric(tt.args.w, tt.args.asJSON, tt.args.metrics); (err != nil) != tt.wantErr { | ||
t.Errorf("writeMetric() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |