Example #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)
Example #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)
Example #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)
Example #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)
Example #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)