def test_serializer_rejects_too_long_pref_branch(self): data = { "pref_type": "json string", "pref_key": "name", "pref_branch": "default" * 100, "variants": [self.variant_1, self.variant_2], } serializer = ExperimentDesignPrefSerializer(data=data) self.assertFalse(serializer.is_valid()) self.assertIn("pref_branch", serializer.errors)
def test_serializer_rejects_no_branch_choice(self): experiment = ExperimentFactory.create( type=ExperimentConstants.TYPE_PREF) data = { "is_multi_pref": False, "pref_type": "boolean", "pref_key": "name", "pref_branch": "Firefox Pref Branch", "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_branch"]))
def test_serializer_accepts_pref_type_json_value(self): experiment = ExperimentFactory.create( type=ExperimentConstants.TYPE_PREF) self.variant_1["value"] = "{}" self.variant_2["value"] = '{"variant":[1,2,3,4]}' data = { "is_multi_pref": False, "pref_type": "json string", "pref_branch": "default", "pref_key": "name", "variants": [self.variant_1, self.variant_2], } serializer = ExperimentDesignPrefSerializer(instance=experiment, data=data) self.assertTrue(serializer.is_valid())
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)
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())
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)
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)
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)