def test_put_one(self): """PUT update one field without affecting others.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target( user=self.user, target_type=TargetType.standard, location=l, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Test target') t.save() data = {'shape': 'circle'} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() t.location.refresh_from_db() self.assertEqual(self.user, t.user) self.assertEqual(TargetType.standard, t.target_type) self.assertEqual(38, t.location.latitude) self.assertEqual(-76, t.location.longitude) self.assertEqual(Orientation.s, t.orientation) self.assertEqual(Shape.circle, t.shape) self.assertEqual(Color.white, t.background_color) self.assertEqual('ABC', t.alphanumeric) self.assertEqual(Color.black, t.alphanumeric_color) self.assertEqual('Test target', t.description)
def test_put_one(self): """PUT update one field without affecting others.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target(user=self.user, target_type=TargetType.standard, location=l, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Test target') t.save() data = {'shape': 'circle'} response = self.client.put( targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() t.location.refresh_from_db() self.assertEqual(self.user, t.user) self.assertEqual(TargetType.standard, t.target_type) self.assertEqual(38, t.location.latitude) self.assertEqual(-76, t.location.longitude) self.assertEqual(Orientation.s, t.orientation) self.assertEqual(Shape.circle, t.shape) self.assertEqual(Color.white, t.background_color) self.assertEqual('ABC', t.alphanumeric) self.assertEqual(Color.black, t.alphanumeric_color) self.assertEqual('Test target', t.description)
def test_put_change_autonomous(self): """Change autonomous with PUT""" t = Target(user=self.user, target_type=TargetType.standard) t.save() data = {'autonomous': True} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual(True, t.autonomous)
def test_put_location(self): """PUT new location""" t = Target(user=self.user, target_type=TargetType.standard) t.save() data = {'latitude': 38, 'longitude': -76} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual(38, t.location.latitude) self.assertEqual(-76, t.location.longitude)
def test_put_change_autonomous(self): """Change autonomous with PUT""" t = Target(user=self.user, target_type=TargetType.standard) t.save() data = {'autonomous': True} response = self.client.put( targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual(True, t.autonomous)
def test_put_clear_shape(self): """PUT clear a field with None.""" t = Target(user=self.user, target_type=TargetType.standard, shape=Shape.square) t.save() data = {'shape': None} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual(None, t.shape)
def test_put_clear_location(self): """PUT clear location by clearing lat and lon.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target(user=self.user, target_type=TargetType.standard, location=l) t.save() data = {'latitude': None, 'longitude': None} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual(None, t.location)
def test_put_append(self): """PUT sets a new field that wasn't set before.""" t = Target(user=self.user, target_type=TargetType.standard) t.save() data = {'description': 'Hello'} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual('Hello', t.description) # Response also matches self.assertEqual(t.json(), json.loads(response.content))
def test_put_review(self): """Test PUT review is saved.""" target = Target(user=self.team, target_type=TargetType.qrc) target.save() response = self.client.put( targets_review_id_url(args=[target.pk]), data=json.dumps({'thumbnail_approved': False})) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertIn('id', data) self.assertEqual(target.pk, data['id']) self.assertIn('thumbnail_approved', data) self.assertFalse(data['thumbnail_approved']) target.refresh_from_db() self.assertFalse(target.thumbnail_approved)
def test_put_update_location(self): """PUT updating location only requires one of lat/lon.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target(user=self.user, target_type=TargetType.standard, location=l) t.save() data = {'latitude': 39} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() t.location.refresh_from_db() self.assertEqual(39, t.location.latitude) self.assertEqual(-76, t.location.longitude)
def test_delete_thumb(self): """Test DELETEing a target with thumbnail.""" t = Target(user=self.user, target_type=TargetType.standard) t.save() pk = t.pk with open(test_image("A.jpg")) as f: response = self.client.post(targets_id_image_url(args=[pk]), data=f.read(), content_type="image/jpeg") self.assertEqual(200, response.status_code) t.refresh_from_db() thumb = t.thumbnail.name self.assertTrue(os.path.exists(absolute_media_path(thumb))) response = self.client.delete(targets_id_url(args=[pk])) self.assertEqual(200, response.status_code) self.assertFalse(os.path.exists(absolute_media_path(thumb)))
def test_delete_thumb(self): """Test DELETEing a target with thumbnail.""" t = Target(user=self.user, target_type=TargetType.standard) t.save() pk = t.pk with open(test_image("A.jpg")) as f: response = self.client.post( targets_id_image_url(args=[pk]), data=f.read(), content_type="image/jpeg") self.assertEqual(200, response.status_code) t.refresh_from_db() thumb = t.thumbnail.name self.assertTrue(os.path.exists(absolute_media_path(thumb))) response = self.client.delete(targets_id_url(args=[pk])) self.assertEqual(200, response.status_code) self.assertFalse(os.path.exists(absolute_media_path(thumb)))