Exemplo n.º 1
0
        public void TestGetVariationUserWithSetForcedVariation()
        {
            var experimentKey       = "test_experiment";
            var pausedExperimentKey = "paused_experiment";
            var userId = "test_user";
            var expectedForcedVariationKey = "variation";
            var optlyObject = new Optimizely(TestData.Datafile, new ValidEventDispatcher(), LoggerMock.Object);

            var userAttributes = new UserAttributes
            {
                { "device_type", "iPhone" },
                { "location", "San Francisco" }
            };

            optlyObject.Activate(experimentKey, userId, userAttributes);

            // confirm normal bucketing occurs before setting the forced variation
            var actualVariation = optlyObject.GetVariation(experimentKey, userId, userAttributes);

            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyControl, actualVariation));

            // test valid experiment
            Assert.IsTrue(optlyObject.SetForcedVariation(experimentKey, userId, expectedForcedVariationKey), string.Format(@"Set variation to ""{0}"" failed.", expectedForcedVariationKey));

            var actualForcedVariation = optlyObject.GetVariation(experimentKey, userId, userAttributes);

            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyVariation, actualForcedVariation));

            // clear forced variation and confirm that normal bucketing occurs
            Assert.IsTrue(optlyObject.SetForcedVariation(experimentKey, userId, null));

            actualVariation = optlyObject.GetVariation(experimentKey, userId, userAttributes);
            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyControl, actualVariation));

            // check that a paused experiment returns null
            Assert.IsTrue(optlyObject.SetForcedVariation(pausedExperimentKey, userId, expectedForcedVariationKey), string.Format(@"Set variation to ""{0}"" failed.", expectedForcedVariationKey));
            actualForcedVariation = optlyObject.GetVariation(pausedExperimentKey, userId, userAttributes);

            Assert.IsNull(actualForcedVariation);
        }
Exemplo n.º 2
0
        public void TestGetVariationWithBucketingId()
        {
            var pausedExperimentKey = "paused_experiment";
            var userId = "test_user";
            var testUserIdWhitelisted    = "user1";
            var experimentKey            = "test_experiment";
            var testBucketingIdControl   = "testBucketingIdControl!"; // generates bucketing number 3741
            var testBucketingIdVariation = "123456789";               // generates bucketing number 4567
            var variationKeyControl      = "control";

            var testUserAttributes = new UserAttributes
            {
                { "device_type", "iPhone" },
                { "company", "Optimizely" },
                { "location", "San Francisco" }
            };

            var userAttributesWithBucketingId = new UserAttributes
            {
                { "device_type", "iPhone" },
                { "company", "Optimizely" },
                { "location", "San Francisco" },
                { ControlAttributes.BUCKETING_ID_ATTRIBUTE, testBucketingIdVariation }
            };

            var userAttributesWithInvalidBucketingId = new UserAttributes
            {
                { "device_type", "iPhone" },
                { "company", "Optimizely" },
                { "location", "San Francisco" },
                { ControlAttributes.BUCKETING_ID_ATTRIBUTE, 1.59 }
            };

            var invalidUserAttributesWithBucketingId = new UserAttributes
            {
                { "company", "Optimizely" },
                { ControlAttributes.BUCKETING_ID_ATTRIBUTE, testBucketingIdControl }
            };

            var optlyObject = new Optimizely(TestData.Datafile, new ValidEventDispatcher(), LoggerMock.Object);

            // confirm normal bucketing occurs before setting the bucketing ID
            var actualVariation = optlyObject.GetVariation(experimentKey, userId, testUserAttributes);

            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyControl, actualVariation));

            // confirm valid bucketing with bucketing ID set in attributes
            actualVariation = optlyObject.GetVariation(experimentKey, userId, userAttributesWithBucketingId);
            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyVariation, actualVariation));
            LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, $"BucketingId is valid: \"{testBucketingIdVariation}\""));

            // check audience with invalid bucketing Id
            actualVariation = optlyObject.GetVariation(experimentKey, userId, userAttributesWithInvalidBucketingId);
            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyControl, actualVariation));
            LoggerMock.Verify(l => l.Log(LogLevel.WARN, "BucketingID attribute is not a string. Defaulted to userId"));

            // check invalid audience with bucketing ID
            actualVariation = optlyObject.GetVariation(experimentKey, userId, invalidUserAttributesWithBucketingId);
            Assert.Null(actualVariation);

            // check null audience with bucketing Id
            actualVariation = optlyObject.GetVariation(experimentKey, userId, null);
            Assert.Null(actualVariation);

            // test that an experiment that's not running returns a null variation
            actualVariation = optlyObject.GetVariation(pausedExperimentKey, userId, userAttributesWithBucketingId);
            Assert.Null(actualVariation);

            // check forced variation
            Assert.IsTrue(optlyObject.SetForcedVariation(experimentKey, userId, variationKeyControl), string.Format("Set variation to \"{0}\" failed.", variationKeyControl));
            actualVariation = optlyObject.GetVariation(experimentKey, userId, userAttributesWithBucketingId);
            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyControl, actualVariation));

            // check whitelisted variation
            actualVariation = optlyObject.GetVariation(experimentKey, testUserIdWhitelisted, userAttributesWithBucketingId);
            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyControl, actualVariation));

            var         bucketerMock      = new Mock <Bucketer>(LoggerMock.Object);
            var         decision          = new Decision("7722370027");
            UserProfile storedUserProfile = new UserProfile(userId, new Dictionary <string, Decision>
            {
                { "7716830082", decision }
            });

            UserProfileServiceMock.Setup(up => up.Lookup(userId)).Returns(storedUserProfile.ToMap());
            DecisionService decisionService = new DecisionService(bucketerMock.Object, ErrorHandlerMock.Object, ProjectConfig, UserProfileServiceMock.Object, LoggerMock.Object);

            actualVariation = optlyObject.GetVariation(experimentKey, userId, userAttributesWithBucketingId);
            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyControl, actualVariation), string.Format("Variation \"{0}\" does not match expected user profile variation \"{1}\".", actualVariation?.Key, variationKeyControl));
        }