Exemple #1
0
    def test_serializer_rejects_inconsistent_pref_type_int(self):
        experiment = ExperimentFactory.create(
            type=ExperimentConstants.TYPE_PREF)
        self.variant_1["value"] = "value 1"
        self.variant_2["value"] = "value 2"

        data = {
            "is_multi_pref": False,
            "pref_type": "integer",
            "pref_key": "name",
            "pref_branch": "default",
            "variants": [self.variant_1, self.variant_2],
        }
        serializer = ExperimentDesignPrefSerializer(instance=experiment,
                                                    data=data)

        self.assertFalse(serializer.is_valid())
        self.assertIn("variants", serializer.errors)
Exemple #2
0
    def test_serializer_accepts_int_branch_values(self):
        experiment = ExperimentFactory.create(
            type=ExperimentConstants.TYPE_PREF)

        self.variant_1["value"] = 50
        self.variant_2["value"] = 40

        data = {
            "is_multi_pref": False,
            "pref_type": "integer",
            "pref_key": "name",
            "pref_branch": "default",
            "variants": [self.variant_1, self.variant_2],
        }
        serializer = ExperimentDesignPrefSerializer(instance=experiment,
                                                    data=data)

        self.assertTrue(serializer.is_valid())
Exemple #3
0
    def test_serializer_rejects_no_type_choice(self):
        experiment = ExperimentFactory.create(
            type=ExperimentConstants.TYPE_PREF)

        data = {
            "is_multi_pref": False,
            "pref_type": "Firefox Pref Type",
            "pref_key": "name",
            "pref_branch": "default",
            "variants": [self.variant_1, self.variant_2],
        }
        serializer = ExperimentDesignPrefSerializer(instance=experiment,
                                                    data=data)

        self.assertFalse(serializer.is_valid())
        self.assertEqual(set(serializer.errors), set(["pref_type"]))
        experiment = Experiment.objects.get(id=experiment.id)
        self.assertEqual(experiment.changes.count(), 0)
Exemple #4
0
    def test_serializer_rejects_inconsistent_pref_type_bool(self):
        experiment = ExperimentFactory.create(
            type=ExperimentConstants.TYPE_PREF)

        self.variant_1["value"] = "value 1"

        data = {
            "is_multi_pref": False,
            "pref_type": "boolean",
            "pref_key": "name",
            "pref_branch": "default",
            "variants": [self.variant_1, self.variant_2],
        }
        serializer = ExperimentDesignPrefSerializer(instance=experiment,
                                                    data=data)

        self.assertFalse(serializer.is_valid())
        self.assertIn("variants", serializer.errors)
        experiment = Experiment.objects.get(id=experiment.id)
        self.assertEqual(experiment.changes.count(), 0)
Exemple #5
0
    def test_serializer_saves_pref_experiment_design(self):
        experiment = ExperimentFactory.create(
            type=ExperimentConstants.TYPE_PREF, pref_key="first pref name")

        data = {
            "is_multi_pref": False,
            "pref_type": "boolean",
            "pref_key": "second name",
            "pref_branch": "default",
            "variants": [self.variant_1, self.variant_2],
        }

        serializer = ExperimentDesignPrefSerializer(
            instance=experiment, data=data, context={"request": self.request})
        self.assertTrue(serializer.is_valid())
        self.assertEqual(experiment.changes.count(), 0)

        experiment = serializer.save()

        self.assertEqual(experiment.pref_key, "second name")
        self.assertEqual(experiment.changes.count(), 1)
Exemple #6
0
    def test_get_design_pref_returns_design_info(self):
        user_email = "*****@*****.**"
        experiment = ExperimentFactory.create_with_variants(type="pref")

        response = self.client.get(
            reverse("experiments-design-pref", kwargs={"slug": experiment.slug}),
            **{settings.OPENIDC_EMAIL_HEADER: user_email},
        )

        self.assertEqual(response.status_code, 200)
        json_data = json.loads(response.content)
        serialized_experiment = ExperimentDesignPrefSerializer(experiment).data
        self.assertEqual(serialized_experiment, json_data)
Exemple #7
0
    def test_serializer_outputs_expected_schema(self):
        experiment = ExperimentFactory.create_with_variants(
            type=ExperimentConstants.TYPE_PREF)

        serializer = ExperimentDesignPrefSerializer(experiment)

        self.assertCountEqual(
            serializer.data,
            {
                "is_multi_pref":
                False,
                "pref_key":
                experiment.pref_key,
                "pref_type":
                experiment.pref_type,
                "pref_branch":
                experiment.pref_branch,
                "variants": [
                    ExperimentVariantSerializer(variant).data
                    for variant in experiment.variants.all()
                ],
            },
        )