def test_bulk_create_duplicated_key(self): inputs = [ dict(key="k1", value="v1"), dict(key="k1", value="v2", description="d2") ] s = InputSerializer(data=inputs, many=True) with self.assertRaises(ValidationError): s.is_valid(raise_exception=True)
def test_prevent_duplicate_key(self): data = { "key": "sample-key", "value": "sample-value", "description": "sample-desc", } Input.objects.create(**data) s = InputSerializer(data=data) with self.assertRaises(ValidationError): s.is_valid(raise_exception=True)
def test_empty_value(self): inputs = [ dict(key="k1", value=""), ] s = InputSerializer(data=inputs, many=True) self.assertTrue(s.is_valid(raise_exception=True)) s.save() ins = list(Input.objects.all()) self.assertEqual(len(ins), len(inputs)) for input in inputs: i = Input.objects.get(key=input["key"]) self.assertEqual(input["value"], i.value)
def test_valid_save(self): data = { "key": "sample-key", "value": "sample-value", "description": "sample-desc", } s = InputSerializer(data=data) self.assertTrue(s.is_valid(raise_exception=True)) s.save() i = Input.objects.get(key=data["key"]) for k, v in data.items(): self.assertEqual(getattr(i, k), v)
def test_bulk_create_duplicated_key_restore_state(self): Input.objects.create(key="ko", value="vo") inputs = [ dict(key="k1", value="v1"), dict(key="k1", value="v2", description="d2") ] s = InputSerializer(data=inputs, many=True) with self.assertRaises(ValidationError): s.is_valid(raise_exception=True) ins = list(Input.objects.all()) self.assertEqual(1, len(ins)) self.assertEqual(ins[0].key, "ko") self.assertEqual(ins[0].value, "vo")
def test_updating(self): orig = { "key": "sample-key", "value": "sample-value", "description": "sample-desc", } data = { "key": "sample-key", "value": "new-value", "description": "new-desc", } i = Input.objects.create(**orig) s = InputSerializer(i, data=data) self.assertTrue(s.is_valid(raise_exception=True)) s.save() i.refresh_from_db() for k, v in data.items(): self.assertEqual(getattr(i, k), v)
def test_valid_serialization(self): data = { "key": "sample-key", "value": "sample-value", "description": "sample-desc", } i = mock.MagicMock(**data) d = InputSerializer(i).data self.assertEqual(d, data)