Repository: algolia/algoliasearch-client-go
Pull request #555 (2019-10-09 14:19:10)

Fix aa testing test

aseure: @Ant-hem Because the two PRs were stacked, I had to re-push this one, could you re-approve (nothing changed), sorry about the noise.

Patch:
diff --git a/algolia/internal/gen/options.go b/algolia/internal/gen/options.go
index 24dc71df..06d42d35 100644
--- a/algolia/internal/gen/options.go
+++ b/algolia/internal/gen/options.go
@@ -139,7 +139,7 @@ var options = []Option{
 
 	// Secured API key
 	{"restrictIndices", Other, []string{}, ""},
-	{"restrictSources", Other, []string{}, ""},
+	{"restrictSources", Other, "", ""},
 	{"validUntil", Other, nil, ""},
 	{"referers", Other, []string{}, ""},
 
diff --git a/algolia/opt/restrict_sources.go b/algolia/opt/restrict_sources.go
index c1ea2558..ebcc4fa4 100644
--- a/algolia/opt/restrict_sources.go
+++ b/algolia/opt/restrict_sources.go
@@ -2,26 +2,23 @@
 
 package opt
 
-import (
-	"encoding/json"
-	"reflect"
-)
+import "encoding/json"
 
 // RestrictSourcesOption is a wrapper for an RestrictSources option parameter. It holds
 // the actual value of the option that can be accessed by calling Get.
 type RestrictSourcesOption struct {
-	value []string
+	value string
 }
 
 // RestrictSources wraps the given value into a RestrictSourcesOption.
-func RestrictSources(v ...string) *RestrictSourcesOption {
+func RestrictSources(v string) *RestrictSourcesOption {
 	return &RestrictSourcesOption{v}
 }
 
 // Get retrieves the actual value of the option parameter.
-func (o *RestrictSourcesOption) Get() []string {
+func (o *RestrictSourcesOption) Get() string {
 	if o == nil {
-		return []string{}
+		return ""
 	}
 	return o.value
 }
@@ -36,7 +33,7 @@ func (o RestrictSourcesOption) MarshalJSON() ([]byte, error) {
 // RestrictSourcesOption.
 func (o *RestrictSourcesOption) UnmarshalJSON(data []byte) error {
 	if string(data) == "null" {
-		o.value = []string{}
+		o.value = ""
 		return nil
 	}
 	return json.Unmarshal(data, &o.value)
@@ -47,12 +44,12 @@ func (o *RestrictSourcesOption) UnmarshalJSON(data []byte) error {
 // value of the option.
 func (o *RestrictSourcesOption) Equal(o2 *RestrictSourcesOption) bool {
 	if o == nil {
-		return o2 == nil || reflect.DeepEqual(o2.value, []string{})
+		return o2 == nil || o2.value == ""
 	}
 	if o2 == nil {
-		return o == nil || reflect.DeepEqual(o.value, []string{})
+		return o == nil || o.value == ""
 	}
-	return reflect.DeepEqual(o.value, o2.value)
+	return o.value == o2.value
 }
 
 // RestrictSourcesEqual returns true if the two options are equal.
diff --git a/cts/analytics/utils.go b/cts/analytics/utils.go
new file mode 100644
index 00000000..8bfc4551
--- /dev/null
+++ b/cts/analytics/utils.go
@@ -0,0 +1,40 @@
+package analytics
+
+import (
+	"testing"
+
+	"github.com/algolia/algoliasearch-client-go/v3/algolia/analytics"
+	"github.com/stretchr/testify/require"
+)
+
+func checkABTestsAreEqual(t *testing.T, a analytics.ABTest, b analytics.ABTestResponse) {
+	require.Equal(t, a.Name, b.Name)
+	require.Equal(t, a.EndAt.Unix(), b.EndAt.Unix())
+	require.Equal(t, len(a.Variants), len(b.Variants))
+
+	var responseVariants []analytics.Variant
+	for _, v := range b.Variants {
+		responseVariants = append(responseVariants, analytics.Variant{
+			Index:                  v.Index,
+			TrafficPercentage:      v.TrafficPercentage,
+			Description:            v.Description,
+			CustomSearchParameters: v.CustomSearchParameters,
+		})
+	}
+
+	require.Equal(t, len(a.Variants), len(responseVariants))
+
+	found := 0
+	for _, v1 := range a.Variants {
+		for _, v2 := range responseVariants {
+			if v1.Index == v2.Index &&
+				v1.TrafficPercentage == v2.TrafficPercentage &&
+				v1.Description == v2.Description &&
+				v1.CustomSearchParameters.Equal(v2.CustomSearchParameters) {
+				found++
+				break
+			}
+		}
+	}
+	require.Equal(t, len(a.Variants), found)
+}


Test patch:
diff --git a/algolia/analytics/responses_ab_testing.go b/algolia/analytics/responses_ab_testing.go
index 82cb7c0b..3f0c27a4 100644
--- a/algolia/analytics/responses_ab_testing.go
+++ b/algolia/analytics/responses_ab_testing.go
@@ -1,6 +1,10 @@
 package analytics
 
-import "time"
+import (
+	"time"
+
+	"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
+)
 
 // ABTestTaskRes represents a generic task submitted to the Analytics API for an
 // AB test.
@@ -41,15 +45,16 @@ type ABTestResponse struct {
 // VariantResponse represents an AB test's variant as returned by the Analytics
 // API when retrieved as part of a response.
 type VariantResponse struct {
-	AverageClickPosition int     `json:"averageClickPosition"`
-	ClickCount           int     `json:"clickCount"`
-	ClickThroughRate     float64 `json:"clickThroughRate"`
-	ConversionCount      int     `json:"conversionCount"`
-	ConversionRate       float64 `json:"conversionRate"`
-	Description          string  `json:"description"`
-	Index                string  `json:"index"`
-	NoResultCount        int     `json:"noResultCount"`
-	SearchCount          int     `json:"searchCount"`
-	TrafficPercentage    int     `json:"trafficPercentage"`
-	UserCount            int     `json:"userCount"`
+	AverageClickPosition   int                 `json:"averageClickPosition"`
+	ClickCount             int                 `json:"clickCount"`
+	ClickThroughRate       float64             `json:"clickThroughRate"`
+	ConversionCount        int                 `json:"conversionCount"`
+	ConversionRate         float64             `json:"conversionRate"`
+	Description            string              `json:"description"`
+	Index                  string              `json:"index"`
+	NoResultCount          int                 `json:"noResultCount"`
+	SearchCount            int                 `json:"searchCount"`
+	TrafficPercentage      int                 `json:"trafficPercentage"`
+	UserCount              int                 `json:"userCount"`
+	CustomSearchParameters *search.QueryParams `json:"customSearchParameters"`
 }
diff --git a/algolia/internal/opt/restrict_sources_test.go b/algolia/internal/opt/restrict_sources_test.go
index de7753f4..2a5eeef6 100644
--- a/algolia/internal/opt/restrict_sources_test.go
+++ b/algolia/internal/opt/restrict_sources_test.go
@@ -17,15 +17,15 @@ func TestRestrictSources(t *testing.T) {
 	}{
 		{
 			opts:     []interface{}{nil},
-			expected: opt.RestrictSources([]string{}...),
+			expected: opt.RestrictSources(""),
 		},
 		{
-			opts:     []interface{}{opt.RestrictSources("value1")},
-			expected: opt.RestrictSources("value1"),
+			opts:     []interface{}{opt.RestrictSources("")},
+			expected: opt.RestrictSources(""),
 		},
 		{
-			opts:     []interface{}{opt.RestrictSources("value1", "value2", "value3")},
-			expected: opt.RestrictSources("value1", "value2", "value3"),
+			opts:     []interface{}{opt.RestrictSources("content of the string value")},
+			expected: opt.RestrictSources("content of the string value"),
 		},
 	} {
 		var (
@@ -36,6 +36,6 @@ func TestRestrictSources(t *testing.T) {
 		require.NoError(t, err)
 		err = json.Unmarshal(data, &out)
 		require.NoError(t, err)
-		require.ElementsMatch(t, c.expected.Get(), out.Get())
+		require.Equal(t, *c.expected, out)
 	}
 }
diff --git a/cts/analytics/aa_testing_test.go b/cts/analytics/aa_testing_test.go
new file mode 100644
index 00000000..732e504d
--- /dev/null
+++ b/cts/analytics/aa_testing_test.go
@@ -0,0 +1,82 @@
+package analytics
+
+import (
+	"strings"
+	"testing"
+	"time"
+
+	"github.com/algolia/algoliasearch-client-go/v3/algolia/analytics"
+	"github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
+	"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
+	"github.com/algolia/algoliasearch-client-go/v3/cts"
+	"github.com/stretchr/testify/require"
+)
+
+func TestAATesting(t *testing.T) {
+	t.Parallel()
+	_, index, indexName := cts.InitSearchClient1AndIndex(t)
+	analyticsClient := cts.InitAnalyticsClient1(t)
+
+	// Remove old AB tests
+	{
+		var toRemove []int
+		today := cts.TodayDate()
+
+		res, err := analyticsClient.GetABTests()
+		require.NoError(t, err)
+		for _, abTest := range res.ABTests {
+			if strings.HasPrefix("go-", abTest.Name) &&
+				!strings.HasPrefix("go-"+today, abTest.Name) {
+				toRemove = append(toRemove, abTest.ABTestID)
+			}
+		}
+
+		for _, id := range toRemove {
+			_, _ = analyticsClient.DeleteABTest(id)
+		}
+	}
+
+	// Add a dummy object to the index
+	{
+		res, err := index.SaveObject(map[string]string{"objectID": "one"})
+		require.NoError(t, err)
+		require.NoError(t, res.Wait())
+	}
+
+	var abTestID int
+	now := time.Now()
+
+	abTest := analytics.ABTest{
+		Name: cts.GenerateCanonicalPrefixName(),
+		Variants: []analytics.Variant{
+			{
+				Index:             indexName,
+				TrafficPercentage: 90,
+			},
+			{
+				Index:             indexName,
+				TrafficPercentage: 10,
+				CustomSearchParameters: &search.QueryParams{
+					IgnorePlurals: opt.IgnorePlurals(true),
+				},
+			},
+		},
+		EndAt: now.Truncate(time.Hour).Add(24 * time.Hour),
+	}
+
+	// Create the AB test
+	{
+		res, err := analyticsClient.AddABTest(abTest)
+		require.NoError(t, err)
+		require.NoError(t, res.Wait())
+		abTestID = res.ABTestID
+	}
+
+	// Retrieve the AB test and check it corresponds to the original one
+	{
+		res, err := analyticsClient.GetABTest(abTestID)
+		require.NoError(t, err)
+		checkABTestsAreEqual(t, abTest, res)
+		require.NotEqual(t, res.Status, "stopped")
+	}
+}
diff --git a/cts/analytics/ab_testing_test.go b/cts/analytics/ab_testing_test.go
index 695a2aff..7c0c5885 100644
--- a/cts/analytics/ab_testing_test.go
+++ b/cts/analytics/ab_testing_test.go
@@ -125,19 +125,3 @@ func TestABTesting(t *testing.T) {
 		require.Error(t, err)
 	}
 }
-
-func checkABTestsAreEqual(t *testing.T, a analytics.ABTest, b analytics.ABTestResponse) {
-	require.Equal(t, a.Name, b.Name)
-	require.Equal(t, a.EndAt.Unix(), b.EndAt.Unix())
-	require.Equal(t, len(a.Variants), len(b.Variants))
-
-	var responseVariants []analytics.Variant
-	for _, v := range b.Variants {
-		responseVariants = append(responseVariants, analytics.Variant{
-			Index:             v.Index,
-			TrafficPercentage: v.TrafficPercentage,
-			Description:       v.Description,
-		})
-	}
-	require.ElementsMatch(t, a.Variants, responseVariants)
-}