public async Task TestAATesting()
        {
            var    now      = DateTime.UtcNow.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
            string testName = $"csharp-AA-{now}-{Environment.UserName}";

            var addOne = await _index.SaveObjectAsync(new AlgoliaStub { ObjectID = "one" });

            // Create tomorrow datetime without seconds/ms to avoid test to fail
            DateTime utcNow   = DateTime.UtcNow.AddDays(1);
            DateTime tomorrow = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, utcNow.Hour, utcNow.Minute, 0,
                                             utcNow.Kind);

            addOne.Wait();

            var abTest = new ABTest
            {
                Name     = testName,
                Variants = new List <Variant>
                {
                    new Variant {
                        Index = _indexName, TrafficPercentage = 90, Description = "a description"
                    },
                    new Variant
                    {
                        Index                  = _indexName,
                        TrafficPercentage      = 10,
                        Description            = "Variant number 2",
                        CustomSearchParameters = new Query {
                            IgnorePlurals = true
                        }
                    }
                },
                EndAt = tomorrow
            };

            AddABTestResponse addAbTest = await BaseTest.AnalyticsClient.AddABTestAsync(abTest);

            abTest.AbTestId = addAbTest.ABTestId;
            _index.WaitTask(addAbTest.TaskID);

            ABTest abTestToCheck = await BaseTest.AnalyticsClient.GetABTestAsync(abTest.AbTestId.Value);

            Assert.IsTrue(TestHelper.AreObjectsEqual(abTestToCheck, abTest, "CreatedAt", "Status", "ClickCount",
                                                     "ConversionCount"));
            Assert.That(abTestToCheck.Status, Is.EqualTo("active"));

            var deleteAbTest = await BaseTest.AnalyticsClient.DeleteABTestAsync(abTest.AbTestId.Value);

            _index.WaitTask(deleteAbTest.TaskID);
        }
Esempio n. 2
0
        public async Task TestAbTest()
        {
            var    now      = DateTime.UtcNow.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
            string testName = $"csharp-{now}-{Environment.UserName}";

            var abTests = await BaseTest.AnalyticsClient.GetABTestsAsync();

            if (abTests.ABTests != null)
            {
                var abTestsToDelete =
                    abTests.ABTests?.Where(x => x.Name.Contains("csharp-") && !x.Name.Contains($"csharp-{now}"));

                foreach (var item in abTestsToDelete)
                {
                    await BaseTest.AnalyticsClient.DeleteABTestAsync(item.AbTestId.Value);
                }
            }

            var addOne = await _index1.SaveObjectAsync(new AlgoliaStub { ObjectID = "one" });

            var addTwo = await _index2.SaveObjectAsync(new AlgoliaStub { ObjectID = "one" });

            // Create tomorrow datetime without seconds/ms to avoid test to fail
            DateTime utcNow   = DateTime.UtcNow.AddDays(1);
            DateTime tomorrow = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, utcNow.Hour, utcNow.Minute, 0,
                                             utcNow.Kind);

            var abTest = new ABTest
            {
                Name     = testName,
                Variants = new List <Variant>
                {
                    new Variant
                    {
                        Index = _index1Name, TrafficPercentage = 60, Description = "a description"
                    },
                    new Variant
                    {
                        Index = _index2Name, TrafficPercentage = 40, Description = string.Empty
                    }
                },
                EndAt = tomorrow
            };

            addOne.Wait();
            addTwo.Wait();

            AddABTestResponse addAbTest = await BaseTest.AnalyticsClient.AddABTestAsync(abTest);

            abTest.AbTestId = addAbTest.ABTestId;
            _index1.WaitTask(addAbTest.TaskID);

            ABTest abTestToCheck = await BaseTest.AnalyticsClient.GetABTestAsync(abTest.AbTestId.Value);

            Assert.IsTrue(TestHelper.AreObjectsEqual(abTestToCheck, abTest, "CreatedAt", "Status", "ClickCount", "ConversionCount"));
            Assert.IsFalse(abTestToCheck.Status.Equals("stopped"));

            ABTestsReponse listAbTests = await BaseTest.AnalyticsClient.GetABTestsAsync();

            Assert.IsTrue(listAbTests.ABTests.Any(x => x.AbTestId == abTest.AbTestId));
            Assert.IsTrue(TestHelper.AreObjectsEqual(
                              listAbTests.ABTests.FirstOrDefault(x => x.AbTestId == abTest.AbTestId), abTest, "CreatedAt", "Status", "ClickCount", "ConversionCount"));

            await BaseTest.AnalyticsClient.StopABTestAsync(abTest.AbTestId.Value);

            ABTest stoppedAbTest = await BaseTest.AnalyticsClient.GetABTestAsync(abTest.AbTestId.Value);

            Assert.IsTrue(stoppedAbTest.Status.Equals("stopped"));

            DeleteABTestResponse deleteAbTest = await BaseTest.AnalyticsClient.DeleteABTestAsync(abTest.AbTestId.Value);

            _index1.WaitTask(deleteAbTest.TaskID);

            AlgoliaApiException ex =
                Assert.ThrowsAsync <AlgoliaApiException>(() => BaseTest.AnalyticsClient.GetABTestAsync(abTest.AbTestId.Value));

            Assert.That(ex.HttpErrorCode == 404);
        }