From b46c317596a27c7233169746d645857841f9c0ef Mon Sep 17 00:00:00 2001 From: Alexander Zhuravlev Date: Mon, 1 Apr 2024 00:02:32 +0300 Subject: [PATCH] Fixed clean API endpoint. Added cleaner live API tests. --- dadata.go | 10 ++++++- dadata_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/dadata.go b/dadata.go index f7005b5..266595e 100644 --- a/dadata.go +++ b/dadata.go @@ -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...), } } diff --git a/dadata_test.go b/dadata_test.go index 06a26c9..23e07e6 100644 --- a/dadata_test.go +++ b/dadata_test.go @@ -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() {