Skip to content

Commit

Permalink
Fixed clean API endpoint. Added cleaner live API tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
turboezh committed Mar 31, 2024
1 parent 22d1552 commit b46c317
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
10 changes: 9 additions & 1 deletion dadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ const (
EndpointURL = "https://dadata.ru/api/v2/"
// EndpointURLSuggest is a suggestion API endpoint.
EndpointURLSuggest = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/"
// EndpointURLClean is a cleaner API endpoint.
EndpointURLClean = "https://cleaner.dadata.ru/api/v1/"
)

var (
endpointURL *url.URL
endpointURLSuggest *url.URL
endpointURLClean *url.URL
)

func init() {
Expand All @@ -34,12 +37,17 @@ func init() {
if err != nil {
panic(err)
}

endpointURLClean, err = url.Parse(EndpointURLClean)
if err != nil {
panic(err)
}
}

// NewCleanApi provides "clean" API.
func NewCleanApi(opts ...client.Option) *clean.Api {
return &clean.Api{
Client: client.NewClient(endpointURL, opts...),
Client: client.NewClient(endpointURLClean, opts...),
}
}

Expand Down
76 changes: 74 additions & 2 deletions dadata_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Live API tests.
// To run API test pass `DADATA_API_KEY` and `DADATA_SECRET_KEY` environment variables with your values.
package dadata

import (
Expand All @@ -15,6 +17,10 @@ type (
ApiSuggestIntegrationTest struct {
suite.Suite
}

ApiCleanIntegrationTest struct {
suite.Suite
}
)

func (s *ApiSuggestIntegrationTest) SetupSuite() {
Expand All @@ -23,6 +29,20 @@ func (s *ApiSuggestIntegrationTest) SetupSuite() {
}
}

func (s *ApiCleanIntegrationTest) SetupSuite() {
if _, ok := os.LookupEnv("DADATA_API_KEY"); !ok {
s.Suite.T().Skip("no api keys in env")
}
}

func TestSuggestApiIntegration(t *testing.T) {
suite.Run(t, &ApiSuggestIntegrationTest{})
}

func TestCleanApiIntegration(t *testing.T) {
suite.Run(t, &ApiCleanIntegrationTest{})
}

func (s *ApiSuggestIntegrationTest) TestAddress() {
api := NewSuggestApi()
params := suggest.RequestParams{
Expand Down Expand Up @@ -158,8 +178,60 @@ func (s *ApiSuggestIntegrationTest) TestFMSUnit() {
s.NotEmpty(res)
}

func TestSuggestApiIntegration(t *testing.T) {
suite.Run(t, &ApiSuggestIntegrationTest{})
func (s *ApiCleanIntegrationTest) TestAddress() {
api := NewCleanApi()
res, err := api.Address(context.Background(), "мск сухонска 11/-89")
s.NoError(err)
s.NotEmpty(res)
s.Len(res, 1)
}

func (s *ApiCleanIntegrationTest) TestPhone() {
api := NewCleanApi()
res, err := api.Phone(context.Background(), "+79851234567")
s.NoError(err)
s.NotEmpty(res)
s.Len(res, 1)
}

func (s *ApiCleanIntegrationTest) TestName() {
api := NewCleanApi()
res, err := api.Name(context.Background(), "Срегей владимерович иванов")
s.NoError(err)
s.NotEmpty(res)
s.Len(res, 1)
}

func (s *ApiCleanIntegrationTest) TestEmail() {
api := NewCleanApi()
res, err := api.Email(context.Background(), "serega@yandex/ru")
s.NoError(err)
s.NotEmpty(res)
s.Len(res, 1)
}

func (s *ApiCleanIntegrationTest) TestBirthday() {
api := NewCleanApi()
res, err := api.Birthday(context.Background(), "12 12 12")
s.NoError(err)
s.NotEmpty(res)
s.Len(res, 1)
}

func (s *ApiCleanIntegrationTest) TestVehicle() {
api := NewCleanApi()
res, err := api.Vehicle(context.Background(), "форд фокус")
s.NoError(err)
s.NotEmpty(res)
s.Len(res, 1)
}

func (s *ApiCleanIntegrationTest) TestPassport() {
api := NewCleanApi()
res, err := api.Passport(context.Background(), "4509 235857")
s.NoError(err)
s.NotEmpty(res)
s.Len(res, 1)
}

func ExampleNewSuggestApi() {
Expand Down

0 comments on commit b46c317

Please sign in to comment.