def test_variantpref_pref_value_returns_str_when_type_is_str(self): vp = VariantPreferencesFactory.create( pref_type=Experiment.PREF_TYPE_STR, pref_value="it's another string") value = PrefValueField(type_field="pref_type", value_field="pref_value").to_representation(vp) self.assertEqual(value, "it's another string")
def setUp(self): self.user_email = "*****@*****.**" self.experiment = ExperimentFactory.create(type="pref") self.variant = ExperimentVariantFactory.create( experiment=self.experiment, is_control=True) self.preference = VariantPreferencesFactory.create( variant=self.variant)
def test_serializer_outputs_expected_schema_for_multipref(self): experiment = ExperimentFactory.create( name="public name", firefox_min_version="70.0", locales=[LocaleFactory.create()], countries=[CountryFactory.create()], public_description="this is my public description!", recipe_slug="some-random-slug", platforms=[ Experiment.PLATFORM_WINDOWS, Experiment.PLATFORM_MAC, Experiment.PLATFORM_LINUX, ], is_multi_pref=True, ) variant = ExperimentVariant( slug="slug-value", ratio=25, experiment=experiment, is_control=True ) variant.save() pref = VariantPreferencesFactory.create(variant=variant) expected_comment = expected_comment = f"{experiment.client_matching}\n" serializer = ExperimentRecipeSerializer(experiment) self.assertEqual(serializer.data["action_name"], "multi-preference-experiment") self.assertEqual(serializer.data["name"], experiment.name) self.assertEqual(serializer.data["comment"], expected_comment) self.assertEqual( serializer.data["filter_object"], [ FilterObjectBucketSampleSerializer(experiment).data, FilterObjectChannelSerializer(experiment).data, FilterObjectVersionsSerializer(experiment).data, FilterObjectLocaleSerializer(experiment).data, FilterObjectCountrySerializer(experiment).data, ], ) expected_data = { "slug": "some-random-slug", "experimentDocumentUrl": experiment.experiment_url, "userFacingName": "public name", "userFacingDescription": "this is my public description!", "branches": [ { "preferences": { pref.pref_name: { "preferenceBranchType": pref.pref_branch, "preferenceType": pref.pref_type, "preferenceValue": pref.pref_value, } }, "ratio": 25, "slug": "slug-value", } ], } self.assertDictEqual(serializer.data["arguments"], expected_data)
def test_serializer_outputs_expected_schema(self): variant = ExperimentVariantFactory.create() vp = VariantPreferencesFactory.create(variant=variant) serializer = ExperimentDesignBranchMultiPrefSerializer(variant) self.assertEqual( serializer.data, { "id": variant.id, "description": variant.description, "ratio": variant.ratio, "is_control": False, "name": variant.name, "preferences": [{ "id": vp.id, "pref_name": vp.pref_name, "pref_value": vp.pref_value, "pref_branch": vp.pref_branch, "pref_type": vp.pref_type, }], }, )
def test_serializer_updates_existing_variant_pref(self): variant = ExperimentVariantFactory.create(experiment=self.experiment) variant_pref = VariantPreferencesFactory.create(variant=variant) self.pref1["id"] = variant_pref.id self.control_variant["id"] = variant.id self.control_variant["ratio"] = 100 self.assertEqual(variant.preferences.all().count(), 1) self.assertTrue(variant.preferences.get(id=variant_pref.id)) data = {"is_multi_pref": True, "variants": [self.control_variant]} serializer = ExperimentDesignMultiPrefSerializer( instance=self.experiment, data=data, context={"request": self.request}) self.assertTrue(serializer.is_valid()) experiment = serializer.save() variant = ExperimentVariant.objects.get(id=variant.id) self.assertEqual(variant.preferences.all().count(), 2) self.assertEqual( variant.preferences.filter(id=variant_pref.id).count(), 1) self.assertEqual(experiment.changes.count(), 1)
def test_serializer_outputs_expected_schema(self): experiment = ExperimentFactory.create() variant = ExperimentVariantFactory.create(experiment=experiment, is_control=True) vp = VariantPreferencesFactory.create(variant=variant) serializer = ExperimentDesignMultiPrefSerializer(experiment) serializer.data["variants"][0]["preferences"][0].pop("id") self.assertCountEqual( serializer.data["variants"][0], { "id": vp.id, "description": variant.description, "is_control": variant.is_control, "name": variant.name, "ratio": variant.ratio, "preferences": [{ "pref_name": vp.pref_name, "pref_value": vp.pref_value, "pref_branch": vp.pref_branch, "pref_type": vp.pref_type, }], }, )
def test_variantpref_pref_value_returns_int_when_type_is_int(self): vp = VariantPreferencesFactory.create( pref_type=Experiment.PREF_TYPE_INT, pref_value="22" ) value = PrefValueField( type_field="pref_type", value_field="pref_value" ).to_representation(vp) self.assertEqual(value, 22)
def test_serializer_outputs_expected_schema(self): variant = ExperimentVariantFactory.create() vp = VariantPreferencesFactory.create(variant=variant) serializer = ExperimentDesignBranchVariantPreferencesSerializer(vp) self.assertEqual( serializer.data, { "id": vp.id, "pref_name": vp.pref_name, "pref_branch": vp.pref_branch, "pref_type": vp.pref_type, "pref_value": vp.pref_value, }, )
def test_seriailzer_outputs_expected_schema_for_multi_pref_variant(self): experiment = ExperimentFactory.create( pref_type=Experiment.PREF_TYPE_JSON_STR, is_multi_pref=True ) variant = ExperimentVariantFactory.create(experiment=experiment) preference = VariantPreferencesFactory.create(variant=variant) serializer = ExperimentRecipeMultiPrefVariantSerializer(variant) self.assertEqual(serializer.data["ratio"], variant.ratio) self.assertEqual(serializer.data["slug"], variant.slug) serialized_preferences = serializer.data["preferences"] self.assertDictEqual( serialized_preferences[preference.pref_name], { "preferenceBranchType": preference.pref_branch, "preferenceType": PrefTypeField().to_representation(preference.pref_type), "preferenceValue": preference.pref_value, }, ) self.assertEqual(serializer.data["ratio"], variant.ratio) self.assertEqual(serializer.data["slug"], variant.slug)
def test_serializer_outputs_expected_schema_for_multi_pref_format(self): experiment = ExperimentFactory.create(normandy_slug="normandy-slug", firefox_min_version="55.0", is_multi_pref=True) variant = ExperimentVariantFactory.create(slug="control", ratio=25, experiment=experiment) preference = VariantPreferencesFactory.create(variant=variant) serializer = ExperimentRecipeMultiPrefVariantSerializer(variant) self.assertEqual(serializer.data["ratio"], 25) self.assertEqual(serializer.data["slug"], "control") serialized_preferences = serializer.data["preferences"] self.assertDictEqual( serialized_preferences[preference.pref_name], { "preferenceBranchType": preference.pref_branch, "preferenceType": preference.pref_type, "preferenceValue": preference.pref_value, }, )
def test_generate_change_log_gives_correct_output(self): experiment = ExperimentFactory.create_with_status( target_status=Experiment.STATUS_REVIEW, num_variants=0, short_description="description", qa_status="pretty good", firefox_min_version="55.0", ) variant1 = ExperimentVariantFactory.create( experiment=experiment, ratio=75, description="variant1 description", name="variant1", slug="variant1-slug", ) variant1.save() old_serialized_val = ChangeLogSerializer(experiment).data experiment.short_description = "changing the description" experiment.qa_status = "good" experiment.firefox_min_version = "56.0" variant2 = ExperimentVariantFactory.create( experiment=experiment, ratio=25, description="variant2 description", name="variant2", slug="variant2-slug", ) variant2.save() VariantPreferencesFactory.create( variant=variant2, pref_name="p1", pref_type=Experiment.PREF_TYPE_INT, pref_branch=Experiment.PREF_BRANCH_DEFAULT, pref_value="5", ) experiment.save() new_serialized_val = ChangeLogSerializer(experiment).data changed_variant_pref = { "pref_name": "p1", "pref_type": "integer", "pref_branch": "default", "pref_value": "5", } changed_data = { "short_description": "changing the description", "qa_status": "good", "firefox_min_version": "56.0", "variants": [{ "ratio": 25, "description": "variant2 description", "name": "variant2", "slug": "variant2-slug", "preferences": [changed_variant_pref], }], } user = UserFactory.create() generate_change_log(old_serialized_val, new_serialized_val, experiment, changed_data, user) changed_value = experiment.changes.latest().changed_values expected_changed_value = { "firefox_min_version": { "display_name": "Firefox Min Version", "new_value": "56.0", "old_value": "55.0", }, "qa_status": { "display_name": "Qa Status", "new_value": "good", "old_value": "pretty good", }, "short_description": { "display_name": "Short Description", "new_value": "changing the description", "old_value": "description", }, "variants": { "display_name": "Branches", "new_value": [ { "ratio": 25, "description": "variant2 description", "name": "variant2", "slug": "variant2-slug", }, { "ratio": 75, "description": "variant1 description", "name": "variant1", "slug": "variant1-slug", }, ], "old_value": [{ "ratio": 75, "description": "variant1 description", "name": "variant1", "slug": "variant1-slug", }], }, } self.assertEqual( expected_changed_value["firefox_min_version"], changed_value["firefox_min_version"], ) self.assertEqual(expected_changed_value["qa_status"], changed_value["qa_status"]) self.assertEqual( expected_changed_value["short_description"], changed_value["short_description"], ) self.assertCountEqual(expected_changed_value["variants"], changed_value["variants"])