예제 #1
0
    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)
예제 #2
0
    def test_serializer_saves_multipref_experiment_design(self):
        data = {
            "is_multi_pref": True,
            "variants": [self.control_variant, self.branch1]
        }

        serializer = ExperimentDesignMultiPrefSerializer(
            instance=self.experiment,
            data=data,
            context={"request": self.request})

        self.assertTrue(serializer.is_valid())
        self.assertEqual(self.experiment.changes.count(), 0)

        experiment = serializer.save()
        self.assertTrue(experiment.variants.all().count(), 1)
        self.assertEqual(experiment.changes.count(), 1)

        control = ExperimentVariant.objects.get(experiment=experiment,
                                                is_control=True)
        branch1 = ExperimentVariant.objects.get(experiment=experiment,
                                                is_control=False)

        self.assertEqual(control.preferences.all().count(), 2)
        self.assertEqual(branch1.preferences.all().count(), 4)
예제 #3
0
    def test_serailizer_reject_duplicate_pref_name_in_branch(self):
        self.pref1["pref_name"] = self.pref2["pref_name"]
        data = {"variants": [self.control_variant]}

        serializer = ExperimentDesignMultiPrefSerializer(
            instance=self.experiment, data=data)
        self.assertFalse(serializer.is_valid())
        error_string = str(serializer.errors)
        self.assertIn("Pref name per Branch needs to be unique", error_string)
예제 #4
0
    def test_serailizer_reject_mismatch_type_value_json_string(self):
        self.pref1["pref_type"] = Experiment.PREF_TYPE_JSON_STR
        self.pref1["pref_value"] = "some random string"
        data = {"variants": [self.control_variant]}

        serializer = ExperimentDesignMultiPrefSerializer(
            instance=self.experiment, data=data)
        self.assertFalse(serializer.is_valid())
        error_string = str(serializer.errors)
        self.assertIn("The pref value must be valid JSON", error_string)
예제 #5
0
    def test_serializer_rejects_missing_preference_fields(self):
        self.pref1.pop("pref_name")

        data = {"variants": [self.control_variant]}

        serializer = ExperimentDesignMultiPrefSerializer(
            instance=self.experiment, data=data)

        self.assertFalse(serializer.is_valid())

        error_string = str(serializer.errors)

        self.assertIn("This field is required", error_string)
예제 #6
0
    def test_serializer_outputs_dummy_variants_when_no_variants(self):
        experiment = ExperimentFactory.create(
            type=ExperimentConstants.TYPE_PREF, is_multi_pref=True)

        serializer = ExperimentDesignMultiPrefSerializer(experiment)

        self.assertEqual(
            serializer.data,
            {
                "is_multi_pref":
                True,
                "variants": [
                    {
                        "description": None,
                        "is_control": True,
                        "name": None,
                        "ratio": 50,
                        "preferences": [{}],
                    },
                    {
                        "description": None,
                        "is_control": False,
                        "name": None,
                        "ratio": 50,
                        "preferences": [{}],
                    },
                ],
            },
        )
예제 #7
0
    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,
                }],
            },
        )
예제 #8
0
    def test_get_design_multi_pref_returns_design_info(self):

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

        self.assertEqual(response.status_code, 200)
        json_data = json.loads(response.content)
        serialized_experiment = ExperimentDesignMultiPrefSerializer(self.experiment).data
        self.assertEqual(serialized_experiment, json_data)