Context is needed to limit time of execution of a method. Previously, context was passed to structure and it was stored inside structure which is a bad practice. Now we need to pass context to function, which is a best practice
157 lines
3.6 KiB
Go
157 lines
3.6 KiB
Go
package ozon
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
|
|
core "github.com/diphantxm/ozon-api-client"
|
|
)
|
|
|
|
func TestCreateDeliveryPolygon(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
statusCode int
|
|
headers map[string]string
|
|
params *CreateDeliveryPolygonParams
|
|
response string
|
|
}{
|
|
// Test Ok
|
|
{
|
|
http.StatusOK,
|
|
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
|
|
&CreateDeliveryPolygonParams{
|
|
Coordinates: "[[[30.149574279785153,59.86550435303646],[30.21205902099609,59.846884387977326],[30.255661010742184,59.86240174913176],[30.149574279785153,59.86550435303646]]]",
|
|
},
|
|
`{
|
|
"polygonId": "1323"
|
|
}`,
|
|
},
|
|
// Test No Client-Id or Api-Key
|
|
{
|
|
http.StatusUnauthorized,
|
|
map[string]string{},
|
|
&CreateDeliveryPolygonParams{},
|
|
`{
|
|
"code": 16,
|
|
"message": "Client-Id and Api-Key headers are required"
|
|
}`,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
|
resp, err := c.Polygons().CreateDelivery(ctx, test.params)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if resp.StatusCode != test.statusCode {
|
|
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLinkDeliveryMethodToPolygon(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
statusCode int
|
|
headers map[string]string
|
|
params *LinkDeliveryMethodToPolygonParams
|
|
response string
|
|
}{
|
|
// Test Ok
|
|
{
|
|
http.StatusOK,
|
|
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
|
|
&LinkDeliveryMethodToPolygonParams{
|
|
DeliveryMethodId: 0,
|
|
Polygons: []LinkDeliveryMethodToPolygonPolygon{
|
|
{
|
|
PolygonId: 1323,
|
|
Time: 30,
|
|
},
|
|
},
|
|
WarehouseLocation: LinkDeliveryMethodToPolygonWarehouse{
|
|
Latitude: "58.52391272075821",
|
|
Longitude: "31.236791610717773",
|
|
},
|
|
},
|
|
`{}`,
|
|
},
|
|
// Test No Client-Id or Api-Key
|
|
{
|
|
http.StatusUnauthorized,
|
|
map[string]string{},
|
|
&LinkDeliveryMethodToPolygonParams{},
|
|
`{
|
|
"code": 16,
|
|
"message": "Client-Id and Api-Key headers are required"
|
|
}`,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
|
resp, err := c.Polygons().Link(ctx, test.params)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if resp.StatusCode != test.statusCode {
|
|
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDeletePolygon(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
statusCode int
|
|
headers map[string]string
|
|
params *DeletePolygonParams
|
|
response string
|
|
}{
|
|
// Test Ok
|
|
{
|
|
http.StatusOK,
|
|
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
|
|
&DeletePolygonParams{
|
|
PolygonIds: []int64{1323},
|
|
},
|
|
`{}`,
|
|
},
|
|
// Test No Client-Id or Api-Key
|
|
{
|
|
http.StatusUnauthorized,
|
|
map[string]string{},
|
|
&DeletePolygonParams{},
|
|
`{
|
|
"code": 16,
|
|
"message": "Client-Id and Api-Key headers are required"
|
|
}`,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
|
resp, err := c.Polygons().Delete(ctx, test.params)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if resp.StatusCode != test.statusCode {
|
|
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
|
|
}
|
|
}
|
|
}
|