Skip to content

Commit

Permalink
Merge pull request #2 from vseinstrumentiru/refactor
Browse files Browse the repository at this point in the history
refactoring + tests
  • Loading branch information
seacomandor authored Jul 18, 2019
2 parents 569be19 + 72621a5 commit efaa662
Show file tree
Hide file tree
Showing 26 changed files with 3,173 additions and 3,095 deletions.
20 changes: 20 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
run:
# timeout for analysis, e.g. 30s, 5m, default is 1m
deadline: 5m
linters:
enable-all: true
disable:
- scopelint
linters-settings:
misspell:
locale: US
issues:
max-same: 0
max-per-linter: 0
exclude:
# gosec: Duplicated errcheck checks
- G104
# gosec: TLS InsecureSkipVerify may be true
- G402
# gosec: weak random number generator in app is ok
- G404
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Roadmap
- [ ] getServiceCostInternational
- [ ] Web служба "Создание заказа"
- [X] createOrder
- [ ] getOrderStatus
- [x] getOrderStatus
- [ ] createAddress
- [ ] updateAddress
- [ ] getInvoiceFile
Expand Down Expand Up @@ -46,18 +46,13 @@ Roadmap
## Пример использования

```go
dpdClient := dpdSdk.NewDPDClient(
dpdSdk.DPDAuth{
ClientNumber: client number,
ClientKey: client key ,
},
dpdSdk.DPDUrls{
dpdClient := dpdSdk.NewDPDClient(clinetNumber, clientKey
ServiceUrls{
GeographyUrl: "http://wstest.dpd.ru/services/geography2",
OrderUrl: "http://wstest.dpd.ru/services/order",
OrderUrl: "http://wstest.dpd.ru/services/order2",
CalculatorUrl: "http://wstest.dpd.ru/services/calculator2",
TrackingUrl: "http://wstest.dpd.ru:80/services/tracing",
TrackingUrl: "http://wstest.dpd.ru/services/tracing",
},
"RU",
)


Expand Down
69 changes: 27 additions & 42 deletions calculateRequestBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,148 +2,133 @@ package dpd

import (
"time"

dpdSoap "github.com/vseinstrumentiru/dpd/soap"
)

//Request of calculate delivery cost
type CalculateRequest dpdSoap.ServiceCostRequest

//Address for calculate request
type CityRequest dpdSoap.CityRequest

//Dpd native, city identifier
func NewCity(cityId int64) *CityRequest {
//NewCity creates new pointer to CityRequest
//use by CalculateRequest
func NewCity(cityID int64) *CityRequest {
return &CityRequest{
CityId: &cityId,
CityID: &cityID,
}
}

//SetIndex set zip code of city
func (c *CityRequest) SetIndex(index string) *CityRequest {
c.Index = &index

return c
}

//SetCityName set city name for city request
func (c *CityRequest) SetCityName(name string) *CityRequest {
c.CityName = &name

return c
}

//SetRegionCode set region code for city request
func (c *CityRequest) SetRegionCode(code int) *CityRequest {
c.RegionCode = &code

return c
}

//Set country code according ISO 3166-1 alpha-2 standard
//
//https://www.iso.org/obp/ui/#search
//
//SetCountryCode the code must comply with ISO 3166-1 alpha-2
//If omitted, default RU
func (c *CityRequest) SetCountryCode(code string) *CityRequest {
c.CountryCode = &code

return c
}

//NewCalculateRequest creates new pointer to CalculateRequest with required parameters
func NewCalculateRequest(from, to *CityRequest, weight float64, selfPickup, selfDelivery bool) *CalculateRequest {
req := new(CalculateRequest)

dpdFrom := dpdSoap.CityRequest(*from)
dpdTo := dpdSoap.CityRequest(*to)

req.Pickup = &dpdFrom
req.Delivery = &dpdTo
req.Pickup = from
req.Delivery = to
req.Weight = &weight
req.SelfPickup = &selfPickup
req.SelfDelivery = &selfDelivery

return req
}

//Set pickup address
func (r *CalculateRequest) OverridePickup(city *CityRequest) *CalculateRequest {
dpdCityRequest := dpdSoap.CityRequest(*city)
r.Pickup = &dpdCityRequest
//OverrideFrom overrides pickup (city-sender) field in CalculateRequest struct
func (r *CalculateRequest) OverrideFrom(city *CityRequest) *CalculateRequest {
r.Pickup = city

return r
}

//Set delivery address
func (r *CalculateRequest) OverrideDelivery(city *CityRequest) *CalculateRequest {
delivery := dpdSoap.CityRequest(*city)
r.Delivery = &delivery
//OverrideTo overrides delivery (city-recipient) field in CalculateRequest struct
func (r *CalculateRequest) OverrideTo(city *CityRequest) *CalculateRequest {
r.Delivery = city

return r
}

//SetWeight set weight of parcel
func (r *CalculateRequest) SetWeight(weight float64) *CalculateRequest {
r.Weight = &weight

return r
}

//SetVolume set volume of parcel
func (r *CalculateRequest) SetVolume(volume float64) *CalculateRequest {
r.Volume = &volume

return r
}

//List of services codes.
//SetServiceCode set service codes - list of comma-separated values
//If set, DPD service will return cost only for given service codes
//code - list of codes, comma separated
func (r *CalculateRequest) SetServiceCode(code string) *CalculateRequest {
r.ServiceCode = &code

return r
}

//If not set DPD use current date by default
//SetPickupDate set date of parcel pickup& If not set DPD use current date by default
func (r *CalculateRequest) SetPickupDate(time time.Time) *CalculateRequest {
d := dpdSoap.Date(time.Format("2006-01-02"))
d := time.Format("2006-01-02")
r.PickupDate = &d

return r
}

//If specific service is set up for request, call of this function with any parameter
//SetMaxDays If specific service is set up for request, call of this function with any parameter
//has not affect result of request
func (r *CalculateRequest) SetMaxDays(days int) *CalculateRequest {
r.MaxDays = &days

return r
}

//If specific service is set up for request, call of this function with any parameter
//SetMaxCost If specific service is set up for request, call of this function with any parameter
//has not affect result of request
func (r *CalculateRequest) SetMaxCost(cost float64) *CalculateRequest {
r.MaxCost = &cost

return r
}

//SetDeclaredValue set cargo declared value
func (r *CalculateRequest) SetDeclaredValue(declaredValue float64) *CalculateRequest {
r.DeclaredValue = &declaredValue

return r
}

func (r *CalculateRequest) toDpdRequest() *dpdSoap.ServiceCostRequest {
dpdReq := dpdSoap.ServiceCostRequest(*r)

return &dpdReq
}

//Set client-side delivery to DPD terminal
//SetSelfPickup set client-side delivery to DPD terminal
func (r *CalculateRequest) SetSelfPickup(flag bool) *CalculateRequest {
r.SelfPickup = &flag

return r
}

//Set DPD-side delivery to their terminal and customer-side pickup on DPD terminal (o_O)
//SetSelfDelivery set DPD-side delivery to their terminal and customer-side pickup on DPD terminal
func (r *CalculateRequest) SetSelfDelivery(flag bool) *CalculateRequest {
r.SelfDelivery = &flag

Expand Down
64 changes: 22 additions & 42 deletions calculateRequestBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"strconv"
"testing"
"time"

dpdSoap "github.com/vseinstrumentiru/dpd/soap"
)

func TestNewCity(t *testing.T) {
type args struct {
cityId int64
cityID int64
}

var cityID int64 = 234
Expand All @@ -24,16 +22,17 @@ func TestNewCity(t *testing.T) {
{
"City " + strconv.Itoa(int(cityID)),
args{
cityId: cityID,
cityID: cityID,
},
&CityRequest{
CityId: &cityID,
CityID: &cityID,
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got := NewCity(tt.args.cityId); !reflect.DeepEqual(got, tt.want) {
if got := NewCity(tt.args.cityID); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewCity() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -65,6 +64,7 @@ func TestCityRequest_SetIndex(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got := tt.c.SetIndex(tt.args.index); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CityRequest.SetIndex() = %v, want %v", got, tt.want)
Expand Down Expand Up @@ -187,9 +187,6 @@ func TestNewCalculateRequest(t *testing.T) {
selfPickup := false
selfDelivery := false

dpdFrom := dpdSoap.CityRequest(*from)
dpdTo := dpdSoap.CityRequest(*to)

tests := []struct {
name string
args args
Expand All @@ -205,8 +202,8 @@ func TestNewCalculateRequest(t *testing.T) {
selfDelivery,
},
&CalculateRequest{
Pickup: &dpdFrom,
Delivery: &dpdTo,
Pickup: from,
Delivery: to,
Weight: &weight,
SelfPickup: &selfPickup,
SelfDelivery: &selfDelivery,
Expand All @@ -215,7 +212,13 @@ func TestNewCalculateRequest(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewCalculateRequest(tt.args.from, tt.args.to, tt.args.weight, tt.args.selfPickup, tt.args.selfDelivery); !reflect.DeepEqual(got, tt.want) {
if got := NewCalculateRequest(
tt.args.from,
tt.args.to,
tt.args.weight,
tt.args.selfPickup,
tt.args.selfDelivery,
); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewCalculateRequest() = %v, want %v", got, tt.want)
}
})
Expand All @@ -228,7 +231,6 @@ func TestCalculateRequest_SetPickup(t *testing.T) {
}

cityRequest := NewCity(123)
dpdReq := dpdSoap.CityRequest(*cityRequest)

tests := []struct {
name string
Expand All @@ -243,15 +245,15 @@ func TestCalculateRequest_SetPickup(t *testing.T) {
cityRequest,
},
&CalculateRequest{
Pickup: &dpdReq,
Pickup: cityRequest,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.OverridePickup(tt.args.city); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CalculateRequest.OverridePickup() = %v, want %v", got, tt.want)
if got := tt.r.OverrideFrom(tt.args.city); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CalculateRequest.OverrideFrom() = %v, want %v", got, tt.want)
}
})
}
Expand All @@ -263,7 +265,6 @@ func TestCalculateRequest_SetDelivery(t *testing.T) {
}

cityRequest := NewCity(123)
dpdReq := dpdSoap.CityRequest(*cityRequest)

tests := []struct {
name string
Expand All @@ -278,15 +279,15 @@ func TestCalculateRequest_SetDelivery(t *testing.T) {
cityRequest,
},
&CalculateRequest{
Delivery: &dpdReq,
Delivery: cityRequest,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.OverrideDelivery(tt.args.city); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CalculateRequest.OverrideDelivery() = %v, want %v", got, tt.want)
if got := tt.r.OverrideTo(tt.args.city); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CalculateRequest.OverrideTo() = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -397,7 +398,7 @@ func TestCalculateRequest_SetPickupDate(t *testing.T) {
}

currentTime := time.Now()
dpdDate := dpdSoap.Date(currentTime.Format("2006-01-02"))
dpdDate := currentTime.Format("2006-01-02")

tests := []struct {
name string
Expand Down Expand Up @@ -524,27 +525,6 @@ func TestCalculateRequest_SetDeclaredValue(t *testing.T) {
}
}

func TestCalculateRequest_toDpdRequest(t *testing.T) {
tests := []struct {
name string
r *CalculateRequest
want *dpdSoap.ServiceCostRequest
}{
{
"To dpd request",
&CalculateRequest{},
&dpdSoap.ServiceCostRequest{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.toDpdRequest(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CalculateRequest.toDpdRequest() = %v, want %v", got, tt.want)
}
})
}
}

func TestCalculateRequest_SetSelfPickup(t *testing.T) {
type args struct {
flag bool
Expand Down
Loading

0 comments on commit efaa662

Please sign in to comment.