def test_form_data_reaching_executor(self): d = {"celery_task_dispatched": False, "cleaned_data": None} def apply_sync(*args, **kwargs): d["celery_task_dispatched"] = True return celery_service_task.apply(*args, **kwargs) def process(_self): d["cleaned_data"] = _self.cleaned_data with patch( "service_objects.celery_services.celery_service_task.apply_async", apply_sync, ): with patch("tests.test_services.FooModelService.process", process): FooModelService.execute(self.initial_data) self.assertTrue(d["celery_task_dispatched"]) self.assertEqual(d["cleaned_data"], self.initial_data)
def test_cleaned_data_pickling(self): service = FooModelService(self.initial_data) service.is_valid() # Check deflated ModelField format deflated_models = self.initial_data.copy() deflated_models["foo"] = (CustomFooModel, self.foo.pk) self.assertEqual(CeleryService._deflate_models(service.cleaned_data), deflated_models) # Check deflate/inflate invariant self.assertEqual( CeleryService._inflate_models( CeleryService._deflate_models(service.cleaned_data)), service.cleaned_data, ) # Check deflated data picklable invariant deflated_data = CeleryService._deflate_models(service.cleaned_data) self.assertEqual(pickle.loads(pickle.dumps(deflated_data)), deflated_data)
def test_auto_fields(self): class FooModelService(ModelService): class Meta: model = FooModel fields = '__all__' def process(self): pass f = FooModelService() field_names = list(six.iterkeys(f.fields)) self.assertEqual(1, len(field_names)) self.assertEqual('one', field_names[0])
def test_extra_fields(self): class FooModelService(ModelService): two = forms.CharField() class Meta: model = FooModel fields = '__all__' def process(self): pass f = FooModelService() field_names = list(six.iterkeys(f.fields)) self.assertEqual(2, len(field_names)) self.assertEqual('one', field_names[0]) self.assertEqual('two', field_names[1])