Fixed clean API endpoint. Added cleaner live API tests.

This commit is contained in:
Alexander Zhuravlev 2024-04-01 00:02:32 +03:00
parent 22d155285b
commit b46c317596
2 changed files with 83 additions and 3 deletions

View File

@ -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() {
@ -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...),
}
}

View File

@ -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 (
@ -15,6 +17,10 @@ type (
ApiSuggestIntegrationTest struct {
suite.Suite
}
ApiCleanIntegrationTest struct {
suite.Suite
}
)
func (s *ApiSuggestIntegrationTest) SetupSuite() {
@ -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{
@ -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() {