Skip to content

Commit cbafb8f

Browse files
fix: ab test nullability keys (#776)
* update nullable / unullable parameters * add AddABTestRequest class with only the needed parameters Co-authored-by: Sylvain Bellone <[email protected]>
1 parent 1c0b75c commit cbafb8f

File tree

7 files changed

+117
-26
lines changed

7 files changed

+117
-26
lines changed

src/Algolia.Search.Test/EndToEnd/Analytics/AnalyticsAATest.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Algolia
2+
* Copyright (c) 2018-2021 Algolia
33
* http://www.algolia.com/
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -63,7 +63,7 @@ public async Task TestAATesting()
6363

6464
addOne.Wait();
6565

66-
var abTest = new ABTest
66+
var abTest = new AddABTestRequest
6767
{
6868
Name = testName,
6969
Variants = new List<Variant>
@@ -81,15 +81,16 @@ public async Task TestAATesting()
8181
};
8282

8383
AddABTestResponse addAbTest = await BaseTest.AnalyticsClient.AddABTestAsync(abTest);
84-
abTest.AbTestId = addAbTest.ABTestId;
8584
_index.WaitTask(addAbTest.TaskID);
8685

87-
ABTest abTestToCheck = await BaseTest.AnalyticsClient.GetABTestAsync(abTest.AbTestId.Value);
88-
Assert.IsTrue(TestHelper.AreObjectsEqual(abTestToCheck, abTest, "CreatedAt", "Status", "ClickCount",
89-
"ConversionCount"));
86+
ABTest abTestToCheck = await BaseTest.AnalyticsClient.GetABTestAsync(addAbTest.ABTestId);
87+
Assert.AreEqual(abTest.Name, abTestToCheck.Name);
88+
Assert.IsTrue(TestHelper.AreObjectsEqual(abTest.Variants.ElementAt(0), abTestToCheck.Variants.ElementAt(0), "ClickCount", "ConversionCount"));
89+
Assert.IsTrue(TestHelper.AreObjectsEqual(abTest.Variants.ElementAt(1), abTestToCheck.Variants.ElementAt(1), "ClickCount", "ConversionCount"));
90+
Assert.AreEqual(abTest.EndAt, abTestToCheck.EndAt);
9091
Assert.That(abTestToCheck.Status, Is.EqualTo("active"));
9192

92-
var deleteAbTest = await BaseTest.AnalyticsClient.DeleteABTestAsync(abTest.AbTestId.Value);
93+
var deleteAbTest = await BaseTest.AnalyticsClient.DeleteABTestAsync(addAbTest.ABTestId);
9394
_index.WaitTask(deleteAbTest.TaskID);
9495
}
9596

src/Algolia.Search.Test/EndToEnd/Analytics/AnalyticsAbTest.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Algolia
2+
* Copyright (c) 2018-2021 Algolia
33
* http://www.algolia.com/
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -66,7 +66,7 @@ public async Task TestAbTest()
6666
DateTime tomorrow = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, utcNow.Hour, utcNow.Minute, 0,
6767
utcNow.Kind);
6868

69-
var abTest = new ABTest
69+
var abTest = new AddABTestRequest
7070
{
7171
Name = testName,
7272
Variants = new List<Variant>
@@ -81,31 +81,38 @@ public async Task TestAbTest()
8181
addTwo.Wait();
8282

8383
AddABTestResponse addAbTest = await BaseTest.AnalyticsClient.AddABTestAsync(abTest);
84-
abTest.AbTestId = addAbTest.ABTestId;
8584
_index1.WaitTask(addAbTest.TaskID);
8685

87-
ABTest abTestToCheck = await BaseTest.AnalyticsClient.GetABTestAsync(abTest.AbTestId.Value);
88-
Assert.IsTrue(TestHelper.AreObjectsEqual(abTestToCheck, abTest, "CreatedAt", "Status", "ClickCount",
89-
"ConversionCount", "TrackedSearchCount"));
86+
ABTest abTestToCheck = await BaseTest.AnalyticsClient.GetABTestAsync(addAbTest.ABTestId);
87+
Assert.AreEqual(abTest.Name, abTestToCheck.Name);
88+
Assert.IsTrue(TestHelper.AreObjectsEqual(abTest.Variants.ElementAt(0), abTestToCheck.Variants.ElementAt(0), "ClickCount", "ConversionCount"));
89+
Assert.IsTrue(TestHelper.AreObjectsEqual(abTest.Variants.ElementAt(1), abTestToCheck.Variants.ElementAt(1), "ClickCount", "ConversionCount"));
90+
Assert.AreEqual(abTest.EndAt, abTestToCheck.EndAt);
9091
Assert.That(abTestToCheck.Status, Is.EqualTo("active"));
9192

9293
ABTestsResponse listAbTests = await BaseTest.AnalyticsClient.GetABTestsAsync();
93-
Assert.IsTrue(listAbTests.ABTests.Any(x => x.AbTestId == abTest.AbTestId));
94+
Assert.IsTrue(listAbTests.ABTests.Any(x => x.AbTestId == addAbTest.ABTestId));
95+
ABTest abTestFromList = listAbTests.ABTests.FirstOrDefault(x => x.AbTestId == addAbTest.ABTestId);
96+
Assert.AreEqual(abTestFromList.Name, abTestToCheck.Name);
9497
Assert.IsTrue(TestHelper.AreObjectsEqual(
95-
listAbTests.ABTests.FirstOrDefault(x => x.AbTestId == abTest.AbTestId), abTest, "CreatedAt", "Status",
96-
"ClickCount", "ConversionCount", "TrackedSearchCount"));
98+
abTestFromList.Variants.ElementAt(0), abTest.Variants.ElementAt(0),
99+
"ClickCount", "ConversionCount"));
100+
Assert.IsTrue(TestHelper.AreObjectsEqual(
101+
abTestFromList.Variants.ElementAt(1), abTest.Variants.ElementAt(1),
102+
"ClickCount", "ConversionCount"));
103+
Assert.AreEqual(abTestFromList.EndAt, abTestToCheck.EndAt);
97104

98-
await BaseTest.AnalyticsClient.StopABTestAsync(abTest.AbTestId.Value);
105+
await BaseTest.AnalyticsClient.StopABTestAsync(addAbTest.ABTestId);
99106

100-
ABTest stoppedAbTest = await BaseTest.AnalyticsClient.GetABTestAsync(abTest.AbTestId.Value);
107+
ABTest stoppedAbTest = await BaseTest.AnalyticsClient.GetABTestAsync(addAbTest.ABTestId);
101108
Assert.That(stoppedAbTest.Status, Is.EqualTo("stopped"));
102109

103-
DeleteABTestResponse deleteAbTest = await BaseTest.AnalyticsClient.DeleteABTestAsync(abTest.AbTestId.Value);
110+
DeleteABTestResponse deleteAbTest = await BaseTest.AnalyticsClient.DeleteABTestAsync(addAbTest.ABTestId);
104111
_index1.WaitTask(deleteAbTest.TaskID);
105112

106113
AlgoliaApiException ex =
107114
Assert.ThrowsAsync<AlgoliaApiException>(() =>
108-
BaseTest.AnalyticsClient.GetABTestAsync(abTest.AbTestId.Value));
115+
BaseTest.AnalyticsClient.GetABTestAsync(addAbTest.ABTestId));
109116
Assert.That(ex.HttpErrorCode, Is.EqualTo(404));
110117
}
111118

src/Algolia.Search/Clients/AnalyticsClient.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Algolia
2+
* Copyright (c) 2018-2021 Algolia
33
* http://www.algolia.com/
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -136,6 +136,19 @@ public async Task<AddABTestResponse> AddABTestAsync(ABTest aBTest, RequestOption
136136
.ConfigureAwait(false);
137137
}
138138

139+
/// <inheritdoc />
140+
public AddABTestResponse AddABTest(AddABTestRequest aBTestRequest, RequestOptions requestOptions = null) =>
141+
AsyncHelper.RunSync(() => AddABTestAsync(aBTestRequest, requestOptions));
142+
143+
/// <inheritdoc />
144+
public async Task<AddABTestResponse> AddABTestAsync(AddABTestRequest aBTestRequest, RequestOptions requestOptions = null,
145+
CancellationToken ct = default)
146+
{
147+
return await _transport.ExecuteRequestAsync<AddABTestResponse, AddABTestRequest>(HttpMethod.Post,
148+
"/2/abtests", CallType.Write, aBTestRequest, requestOptions, ct)
149+
.ConfigureAwait(false);
150+
}
151+
139152
/// <inheritdoc />
140153
public StopABTestResponse StopABTest(long abTestId, RequestOptions requestOptions = null) =>
141154
AsyncHelper.RunSync(() => StopABTestAsync(abTestId, requestOptions));

src/Algolia.Search/Clients/IAnalyticsClient.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Algolia
2+
* Copyright (c) 2021 Algolia
33
* http://www.algolia.com/
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -21,6 +21,7 @@
2121
* THE SOFTWARE.
2222
*/
2323

24+
using System;
2425
using System.Threading;
2526
using System.Threading.Tasks;
2627
using Algolia.Search.Http;
@@ -77,6 +78,7 @@ Task<ABTestsResponse> GetABTestsAsync(int offset = 0, int limit = 10, RequestOpt
7778
/// <param name="aBTest"></param>
7879
/// <param name="requestOptions">Add extra http header or query parameters to Algolia</param>
7980
/// <returns></returns>
81+
[Obsolete("Deprecated, use the version with an AddABTestRequest parameter instead.")]
8082
AddABTestResponse AddABTest(ABTest aBTest, RequestOptions requestOptions = null);
8183

8284
/// <summary>
@@ -86,9 +88,28 @@ Task<ABTestsResponse> GetABTestsAsync(int offset = 0, int limit = 10, RequestOpt
8688
/// <param name="requestOptions">Add extra http header or query parameters to Algolia</param>
8789
/// <param name="ct">Optional cancellation token</param>
8890
/// <returns></returns>
91+
[Obsolete("Deprecated, use the version with an AddABTestRequest parameter instead.")]
8992
Task<AddABTestResponse> AddABTestAsync(ABTest aBTest, RequestOptions requestOptions = null,
9093
CancellationToken ct = default);
9194

95+
/// <summary>
96+
/// Creates a new AB Test with the provided configuration.
97+
/// </summary>
98+
/// <param name="aBTestRequest"></param>
99+
/// <param name="requestOptions">Add extra http header or query parameters to Algolia</param>
100+
/// <returns></returns>
101+
AddABTestResponse AddABTest(AddABTestRequest aBTestRequest, RequestOptions requestOptions = null);
102+
103+
/// <summary>
104+
/// Creates a new AB Test with the provided configuration.
105+
/// </summary>
106+
/// <param name="aBTestRequest"></param>
107+
/// <param name="requestOptions">Add extra http header or query parameters to Algolia</param>
108+
/// <param name="ct">Optional cancellation token</param>
109+
/// <returns></returns>
110+
Task<AddABTestResponse> AddABTestAsync(AddABTestRequest aBTestRequest, RequestOptions requestOptions = null,
111+
CancellationToken ct = default);
112+
92113
/// <summary>
93114
/// Marks the A/B Test as stopped. At this point, the test is over and cannot be restarted.
94115
/// As a result, your application is back to normal: index A will perform as usual, receiving 100% of all search requests.

src/Algolia.Search/Models/Analytics/ABTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ public class ABTest
5050
/// <summary>
5151
/// End date for the AB Test
5252
/// </summary>
53-
public DateTime? EndAt { get; set; }
53+
public DateTime EndAt { get; set; }
5454

5555
/// <summary>
5656
/// Date of creation
5757
/// </summary>
58-
public DateTime? CreatedAt { get; set; }
58+
public DateTime CreatedAt { get; set; }
5959

6060
/// <summary>
6161
/// Ab test ID
6262
/// </summary>
6363
[JsonProperty(PropertyName = "abTestID")]
64-
public long? AbTestId { get; set; }
64+
public long AbTestId { get; set; }
6565

6666
/// <summary>
6767
/// ABTest significance based on click data. Should be > 0.95 to be considered significant (no matter which variant is winning).
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2021 Algolia
3+
* http://www.algolia.com/
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
using System;
25+
using System.Collections.Generic;
26+
27+
namespace Algolia.Search.Models.Analytics
28+
{
29+
/// <summary>
30+
/// https://www.algolia.com/doc/rest-api/ab-test/#add-ab-test
31+
/// </summary>
32+
public class AddABTestRequest
33+
{
34+
/// <summary>
35+
/// AB Test name.
36+
/// </summary>
37+
public string Name { get; set; }
38+
39+
/// <summary>
40+
/// List of variants for the ab test
41+
/// </summary>
42+
public IEnumerable<Variant> Variants { get; set; }
43+
44+
/// <summary>
45+
/// End date for the AB Test
46+
/// </summary>
47+
public DateTime EndAt { get; set; }
48+
}
49+
}

src/Algolia.Search/Models/Analytics/Variant.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class Variant
3838
/// <summary>
3939
/// Percentage of the traffic that should be going to the variant. The sum of the percentage should be equal to 100.
4040
/// </summary>
41-
public int? TrafficPercentage { get; set; }
41+
public int TrafficPercentage { get; set; }
4242

4343
/// <summary>
4444
/// Description of the variant. This is useful when seing the results in the dashboard or via the API.

0 commit comments

Comments
 (0)