def test_serializer_outputs_expected_schema(self): country1 = CountryFactory.create(code="ab") country2 = CountryFactory.create(code="cd") experiment = ExperimentFactory.create(countries=[country1, country2]) serializer = FilterObjectCountrySerializer(experiment) self.assertEqual(serializer.data["type"], "country") self.assertEqual(set(serializer.data["countries"]), set(["ab", "cd"]))
def test_serializer_outputs_expected_schema(self): country = CountryFactory.create() serializer = CountrySerializer(country) self.assertEqual(serializer.data, { "code": country.code, "name": country.name })
def test_serializer_outputs_expected_schema_for_addon_experiment(self): experiment = ExperimentFactory.create_with_status( Experiment.STATUS_SHIP, firefox_min_version="63.0", type=Experiment.TYPE_ADDON, locales=[LocaleFactory.create()], countries=[CountryFactory.create()], platforms=[Experiment.PLATFORM_WINDOWS], windows_versions=[Experiment.VERSION_WINDOWS_8], ) serializer = ExperimentRecipeSerializer(experiment) self.assertEqual(serializer.data["action_name"], "opt-out-study") self.assertEqual(serializer.data["name"], experiment.name) expected_comment = (f"{experiment.client_matching}\n" f"Platform: ['All Windows']\n" f"Windows Versions: ['Windows 8']\n") 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, ], ) self.assertDictEqual( serializer.data["arguments"], ExperimentRecipeAddonArgumentsSerializer(experiment).data, ) self.assertEqual(serializer.data["experimenter_slug"], experiment.slug)
def test_serializer_outputs_expected_schema_for_pref_experiment(self): experiment = ExperimentFactory.create_with_status( Experiment.STATUS_SHIP, firefox_min_version="65.0", type=Experiment.TYPE_PREF, locales=[LocaleFactory.create()], countries=[CountryFactory.create()], platforms=[Experiment.PLATFORM_MAC], profile_age="Existing Profiles Only", ) serializer = ExperimentRecipeSerializer(experiment) self.assertEqual(serializer.data["action_name"], "preference-experiment") self.assertEqual(serializer.data["name"], experiment.name) expected_comment = (f"{experiment.client_matching}\n" f"Platform: ['All Mac']\n" "Profile Age: Existing Profiles Only") 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, ], ) self.assertDictEqual( serializer.data["arguments"], ExperimentRecipePrefArgumentsSerializer(experiment).data, ) self.assertEqual(serializer.data["experimenter_slug"], experiment.slug)
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_for_message(self): experiment = ExperimentFactory.create( name="public name", type=Experiment.TYPE_MESSAGE, firefox_min_version="70.0", locales=[LocaleFactory.create()], countries=[CountryFactory.create()], public_description="this is my public description!", normandy_slug="some-random-slug", platforms=[Experiment.PLATFORM_WINDOWS], ) variant = ExperimentVariant(slug="slug-value", ratio=25, experiment=experiment, is_control=True) variant.save() expected_comment = expected_comment = ( f"{experiment.client_matching}\n" f"Platform: ['All Windows']\n") serializer = ExperimentRecipeSerializer(experiment) self.assertEqual(serializer.data["action_name"], "messaging-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": [{ "groups": [], "value": {}, "ratio": 25, "slug": "slug-value" }], } self.assertDictEqual(serializer.data["arguments"], expected_data)
def test_serializer_outputs_expected_multipref_schema_for_singularpref(self): experiment = ExperimentFactory.create( name="public name", pref_type=Experiment.PREF_TYPE_INT, pref_branch=Experiment.PREF_BRANCH_DEFAULT, 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], ) variant = ExperimentVariant( slug="slug-value", ratio=25, experiment=experiment, value=5 ) variant.save() expected_comment = expected_comment = ( f"{experiment.client_matching}\n" f"Platform: ['All Windows']\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": { experiment.pref_name: { "preferenceBranchType": "default", "preferenceType": Experiment.PREF_TYPE_INT, "preferenceValue": 5, } }, "ratio": 25, "slug": "slug-value", } ], } self.assertDictEqual(serializer.data["arguments"], expected_data)
def test_targeting_with_countries(self): country_ca = CountryFactory.create(code="CA") country_us = CountryFactory.create(code="US") experiment = NimbusExperimentFactory.create_with_lifecycle( NimbusExperimentFactory.Lifecycles.LAUNCH_APPROVE_APPROVE, application=NimbusExperiment.Application.DESKTOP, firefox_min_version=NimbusExperiment.Version.NO_VERSION, targeting_config_slug=NimbusExperiment.TargetingConfig.NO_TARGETING, channel=NimbusExperiment.Channel.NO_CHANNEL, locales=[], countries=[country_ca, country_us], ) self.assertEqual( experiment.targeting, ( "'app.shield.optoutstudies.enabled'|preferenceValue " "&& region in ['CA', 'US']" ), )
def test_update_experiment_audience(self): user_email = "*****@*****.**" country = CountryFactory.create() locale = LocaleFactory.create() experiment = NimbusExperimentFactory.create( status=NimbusExperiment.Status.DRAFT, channel=NimbusExperiment.Channel.NO_CHANNEL, application=NimbusConstants.Application.DESKTOP, firefox_min_version=NimbusExperiment.Version.NO_VERSION, population_percent=0.0, proposed_duration=0, proposed_enrollment=0, targeting_config_slug=NimbusExperiment.TargetingConfig.NO_TARGETING, total_enrolled_clients=0, ) self.query( UPDATE_EXPERIMENT_MUTATION, variables={ "input": { "id": experiment.id, "channel": NimbusConstants.Channel.BETA.name, "firefoxMinVersion": NimbusConstants.Version.FIREFOX_83.name, "populationPercent": "10", "proposedDuration": 42, "proposedEnrollment": 120, "targetingConfigSlug": ( NimbusConstants.TargetingConfig.ALL_ENGLISH.name ), "totalEnrolledClients": 100, "changelogMessage": "test changelog message", "countries": [country.id], "locales": [locale.id], } }, headers={settings.OPENIDC_EMAIL_HEADER: user_email}, ) experiment = NimbusExperiment.objects.get(id=experiment.id) self.assertEqual(experiment.channel, NimbusConstants.Channel.BETA.value) self.assertEqual( experiment.firefox_min_version, NimbusConstants.Version.FIREFOX_83.value ) self.assertEqual(experiment.population_percent, 10.0) self.assertEqual(experiment.proposed_duration, 42) self.assertEqual(experiment.proposed_enrollment, 120) self.assertEqual( experiment.targeting_config_slug, NimbusConstants.TargetingConfig.ALL_ENGLISH.value, ) self.assertEqual(experiment.total_enrolled_clients, 100) self.assertEqual(list(experiment.countries.all()), [country]) self.assertEqual(list(experiment.locales.all()), [locale])
def test_serializer_outputs_expect_schema_for_branched_addon(self): experiment = ExperimentFactory.create( countries=[CountryFactory.create()], firefox_min_version="70.0", locales=[LocaleFactory.create()], name="public name", normandy_slug="some-random-slug", platforms=[Experiment.PLATFORM_LINUX], public_description="this is my public description!", type=Experiment.TYPE_ADDON, ) variant = ExperimentVariant(slug="slug-value", ratio=25, experiment=experiment) variant.save() serializer = ExperimentRecipeSerializer(experiment) self.assertEqual(serializer.data["action_name"], "branched-addon-study") self.assertEqual(serializer.data["name"], experiment.name) expected_comment = f"{experiment.client_matching}\n" f"Platform: ['All Linux']\n" 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, ], ) self.assertDictEqual( serializer.data["arguments"], { "branches": [{ "ratio": 25, "slug": "slug-value", "extensionApiId": None }], "slug": "some-random-slug", "userFacingDescription": "this is my public description!", "userFacingName": "public name", }, )
def test_serializer_outputs_expected_schema_for_windows_versions(self): experiment = ExperimentFactory.create_with_status( Experiment.STATUS_SHIP, firefox_min_version="65.0", type=Experiment.TYPE_PREF, locales=[LocaleFactory.create()], countries=[CountryFactory.create()], windows_versions=[Experiment.VERSION_WINDOWS_8], ) serializer = ExperimentRecipeSerializer(experiment) self.assertEqual( serializer.data["filter_object"], [ FilterObjectBucketSampleSerializer(experiment).data, FilterObjectChannelSerializer(experiment).data, FilterObjectVersionsSerializer(experiment).data, FilterObjectLocaleSerializer(experiment).data, FilterObjectCountrySerializer(experiment).data, ], )
def test_message_experiment_sets_default_locales_countries(self): [ LocaleFactory.create(code=l) for l in Experiment.MESSAGE_DEFAULT_LOCALES ] [ CountryFactory.create(code=c) for c in Experiment.MESSAGE_DEFAULT_COUNTRIES ] self.data["type"] = Experiment.TYPE_MESSAGE form = ExperimentOverviewForm(request=self.request, data=self.data) self.assertTrue(form.is_valid()) experiment = form.save() self.assertEqual( set(experiment.locales.values_list("code", flat=True)), set(Experiment.MESSAGE_DEFAULT_LOCALES), ) self.assertEqual( set(experiment.countries.values_list("code", flat=True)), set(Experiment.MESSAGE_DEFAULT_COUNTRIES), )