def test_update_label_wrong_params(self): label_data = { 'label_meta': self.label_meta, 'shape': self.shape, 'class_id': 'eye', 'surface': 'BOL' } sample_serializer = SampleSerializer(data={ 'label': label_data, 'image_name': self.image_name }) sample_serializer.is_valid() sample = sample_serializer.save() label_data.update({'class_id': None, 'surface': False}) resp = self.client.put( f'/api/label/{sample.label.id}/', data=encode_multipart('BoUnDaRyStRiNg', {'label': json.dumps(label_data)}), content_type='multipart/form-data; boundary=BoUnDaRyStRiNg') label_data.pop('label_meta') label_data.pop('shape') label_data.update({'id': DummyValue()}) self.assertEqual(resp.status_code, 400) expected_data = { 'class_id': ['This field may not be null.'], 'surface': ['Not a valid string.'] } self.assertEqual(expected_data, json.loads(resp.content))
def test_get_all_labels(self): label_data = { 'label_meta': self.label_meta, 'shape': self.shape, 'class_id': 'tooth', 'surface': 'BOL' } sample_serializer = SampleSerializer(data={ 'label': label_data, 'image_name': self.image_name }) sample_serializer.is_valid() sample_serializer.save() resp = self.client.get(f'/api/labels/') self.assertEqual(resp.status_code, 200) label_data.update({'id': DummyValue()}) label_data.pop('label_meta') label_data.pop('shape') self.assertEqual([label_data], json.loads(resp.content))
def post(self, request, *args, **kwargs): image = request.data.get('image') if image is None: return Response({'image': ['Must not be empty.']}, status=status.HTTP_400_BAD_REQUEST) data = {'image_name': image.name} label = request.data.get('label') if label: data.update({'label': json.loads(label)}) sample_serializer = SampleSerializer(data=data) if not sample_serializer.is_valid(): return Response(sample_serializer.errors, status=status.HTTP_400_BAD_REQUEST) with transaction.atomic(): sample = sample_serializer.save() default_storage.save(name=image.name, content=image) return Response(sample.id, content_type='application/json')
def test_get_internal_label(self): label_data = { 'label_meta': self.label_meta, 'shape': self.shape, 'class_id': 'tooth', 'surface': 'BOL' } sample_serializer = SampleSerializer(data={ 'label': label_data, 'image_name': self.image_name }) sample_serializer.is_valid() sample = sample_serializer.save() resp = self.client.get(f'/api/label/{sample.label.id}/', data={'format': 'internal'}) self.assertEqual(resp.status_code, 200) self.label_meta.update({'id': DummyValue()}) self.shape.update({'id': DummyValue()}) label_data.update({'id': DummyValue()}) self.assertEqual(label_data, json.loads(resp.content))
def test_update_label(self): label_data = { 'label_meta': self.label_meta, 'shape': self.shape, 'class_id': 'eye', 'surface': 'BOL' } sample_serializer = SampleSerializer(data={ 'label': label_data, 'image_name': self.image_name }) sample_serializer.is_valid() sample = sample_serializer.save() label_data.update({'surface': 'BO'}) resp = self.client.put( f'/api/label/{sample.label.id}/', data=encode_multipart('BoUnDaRyStRiNg', {'label': json.dumps(label_data)}), content_type='multipart/form-data; boundary=BoUnDaRyStRiNg') label_data.pop('label_meta') label_data.pop('shape') label_data.update({'id': DummyValue()}) self.assertEqual(resp.status_code, 200) self.assertEqual(label_data, json.loads(resp.content))