def test_update_spot_with_items(self): """Creates a spot and then updates it with items.""" spot_name = self.random_name() capacity = 20 spot_json = utils_test.get_spot(spot_name, capacity) for i in range(0, 10): spot_json['items'].append(utils_test.get_item()) response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) try: updated_items = Item.objects.filter(spot=self.spot) except Exception as ex: self.fail("Items shoud exist but do not! :" + str(ex)) self.assertEqual(len(updated_items), 10) for i in range(0, len(updated_items)): self.assertEqual(updated_items[i].json_data_structure()['name'], spot_json['items'][i]['name']) self.assertEqual( updated_items[i].json_data_structure()['category'], spot_json['items'][i]['category']) self.assertEqual( updated_items[i].json_data_structure()['subcategory'], spot_json['items'][i]['subcategory'])
def test_remove_item(self): """Tests to ensure that not including an item in PUT will remove it""" spot_name = self.random_name() capacity = 20 spot_json = utils_test.get_spot(spot_name, capacity) for i in range(0, 10): spot_json['items'].append(utils_test.get_item()) response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) del spot_json['items'][9] response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) try: updated_items = Item.objects.filter(spot=self.spot) except Exception as ex: self.fail("Items shoud exist but do not! :" + str(ex)) self.assertEqual(len(updated_items), 9)
def test_no_duplicate_extended_info(self): new_name = "testing PUT name: {0}".format(random.random()) new_capacity = 30 spot_json = utils_test.get_spot(new_name, new_capacity) spot_json['extended_info']["has_a_funky_beat"] = "true" self.put_spot(self.url, spot_json) self.put_spot(self.url, spot_json) self.assertEqual( len(self.spot.spotextendedinfo_set.filter(key="has_a_funky_beat")), 1) spot_json['extended_info']['has_a_funky_beat'] = "of_course" self.put_spot(self.url, spot_json) self.assertEqual( len(self.spot.spotextendedinfo_set.filter(key="has_a_funky_beat")), 1) self.assertEqual( self.spot.spotextendedinfo_set.get(key="has_a_funky_beat").value, 'of_course')
def test_valid_json(self): c = self.client new_name = self.random_name() new_capacity = 10 spot_data = utils_test.get_spot(new_name, new_capacity) response = self.post_spot(spot_data) self.assertEquals(response.status_code, 201, "Spot did not get created") self.assertIn("Location", response, "Did not find Location header in response") self.spot = Spot.objects.get(name=new_name) self.assertEqual(response["Location"], 'http://testserver' + self.spot.rest_url()) get_response = c.get(response["Location"]) self.assertEqual(get_response.status_code, 200) spot_json = json.loads(get_response.content) self.assertEqual(spot_json["name"], new_name, "Did not find correct name in response") self.assertEqual(spot_json["capacity"], new_capacity, "Did not find correct capacity in response")
def test_remove_item_extended_info(self): """Tests to ensure that not including an item EI in PUT will delete""" ei = { "make_model": "itemmodel", "customer_type": "UW Student", "auto_item_status": "active" } spot_json = utils_test.get_spot(self.random_name(), 20) spot_json['items'].append(utils_test.get_item()) for key, value in ei.iteritems(): spot_json['items'][0]['extended_info'][key] = value response = self.put_spot(self.url, spot_json) spot_json = json.loads(response.content) self.assertEqual(response.status_code, 200) del spot_json['items'][0]['extended_info']['make_model'] response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) get_response = self.client.get(self.url) updated_spot_json = json.loads(get_response.content) self.assertNotIn('make_model', updated_spot_json['items'][0]['extended_info'])
def test_create_spot_with_items(self): """ Tests the creation of a spot with correct items data. """ c = self.client new_name = self.random_name() new_capacity = 10 spot_json = utils_test.get_spot(new_name, new_capacity) spot_json['items'].append(utils_test.get_item()) response = self.post_spot(spot_json) # assert that the spot was created self.assertEqual(response.status_code, 201) # try to retrieve the spot and ensure that items are there get_response = c.get(response["Location"]) json_response = json.loads(get_response.content) self.assertEqual(len(json_response["items"]), 1) original_item = spot_json['items'][0] item = json_response["items"][0] self.assertEqual(item["name"], original_item['name']) self.assertEqual(item["category"], original_item['category']) self.assertEqual(item["subcategory"], original_item['subcategory'])
def test_update_spot_items(self): """ Tests that when a spot item is updated that changes persist and that the id remains the same. """ spot_json = utils_test.get_spot(self.random_name(), 20) spot_json['items'].append(utils_test.get_item()) response = self.put_spot(self.url, spot_json) get_response = self.client.get(self.url) returned_spot_json = json.loads(get_response.content) returned_spot_json['items'][0]['name'] = 'Test!' response = self.put_spot(self.url, returned_spot_json) get_response = self.client.get(self.url) updated_spot_json = json.loads(get_response.content) self.assertEqual(updated_spot_json['items'][0]['id'], returned_spot_json['items'][0]['id'], "IDs do not match for updated spot!") self.assertEqual(updated_spot_json['items'][0]['name'], returned_spot_json['items'][0]['name'], "Spot name failed to update!")
def test_remove_item_extended_info(self): """Tests to ensure that not including an item EI in PUT will delete""" ei = {"make_model": "itemmodel", "customer_type": "UW Student", "auto_item_status": "active"} spot_json = utils_test.get_spot(self.random_name(), 20) spot_json["items"].append(utils_test.get_item()) for key, value in ei.iteritems(): spot_json["items"][0]["extended_info"][key] = value response = self.put_spot(self.url, spot_json) spot_json = json.loads(response.content) self.assertEqual(response.status_code, 200) del spot_json["items"][0]["extended_info"]["make_model"] response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) get_response = self.client.get(self.url) updated_spot_json = json.loads(get_response.content) self.assertNotIn("make_model", updated_spot_json["items"][0]["extended_info"])
def test_update_spot_items(self): """ Tests that when a spot item is updated that changes persist and that the id remains the same. """ spot_json = utils_test.get_spot(self.random_name(), 20) spot_json["items"].append(utils_test.get_item()) response = self.put_spot(self.url, spot_json) get_response = self.client.get(self.url) returned_spot_json = json.loads(get_response.content) returned_spot_json["items"][0]["name"] = "Test!" response = self.put_spot(self.url, returned_spot_json) get_response = self.client.get(self.url) updated_spot_json = json.loads(get_response.content) self.assertEqual( updated_spot_json["items"][0]["id"], returned_spot_json["items"][0]["id"], "IDs do not match for updated spot!", ) self.assertEqual( updated_spot_json["items"][0]["name"], returned_spot_json["items"][0]["name"], "Spot name failed to update!" )
def test_update_spot_with_missing_data(self): """ Tests to make sure that updating a spot without all required fields fails, giving a 400. """ spot_name = self.random_name() capacity = 20 spot_json = utils_test.get_spot(spot_name, capacity) spot_json["items"].append(utils_test.get_item()) new_spots = (copy.deepcopy(spot_json), copy.deepcopy(spot_json), copy.deepcopy(spot_json)) messages = ( "Item name validation failed to raise an error", "Item category validation failed to raise an error", "Item subcategory validation failed to raise an error", ) # delete required fields del new_spots[0]["items"][0]["name"] del new_spots[1]["items"][0]["category"] del new_spots[2]["items"][0]["subcategory"] for spot, message in zip(new_spots, messages): response = self.put_spot(self.url, spot) self.assertEqual(response.status_code, 400, message)
def test_remove_item(self): """Tests to ensure that not including an item in PUT will remove it""" spot_name = self.random_name() capacity = 20 spot_json = utils_test.get_spot(spot_name, capacity) for i in range(0, 10): spot_json["items"].append(utils_test.get_item()) response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) del spot_json["items"][9] response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) try: updated_items = Item.objects.filter(spot=self.spot) except Exception as ex: self.fail("Items shoud exist but do not! :" + str(ex)) self.assertEqual(len(updated_items), 9)
def test_update_spot_with_items(self): """Creates a spot and then updates it with items.""" spot_name = self.random_name() capacity = 20 spot_json = utils_test.get_spot(spot_name, capacity) for i in range(0, 10): spot_json["items"].append(utils_test.get_item()) response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) try: updated_items = Item.objects.filter(spot=self.spot) except Exception as ex: self.fail("Items shoud exist but do not! :" + str(ex)) self.assertEqual(len(updated_items), 10) for i in range(0, len(updated_items)): self.assertEqual(updated_items[i].json_data_structure()["name"], spot_json["items"][i]["name"]) self.assertEqual(updated_items[i].json_data_structure()["category"], spot_json["items"][i]["category"]) self.assertEqual( updated_items[i].json_data_structure()["subcategory"], spot_json["items"][i]["subcategory"] )
def test_create_spot_with_items(self): """ Tests the creation of a spot with correct items data. """ c = self.client new_name = self.random_name() new_capacity = 10 spot_json = utils_test.get_spot(new_name, new_capacity) spot_json["items"].append(utils_test.get_item()) response = self.post_spot(spot_json) # assert that the spot was created self.assertEqual(response.status_code, 201) # try to retrieve the spot and ensure that items are there get_response = c.get(response["Location"]) json_response = json.loads(get_response.content) self.assertEqual(len(json_response["items"]), 1) original_item = spot_json["items"][0] item = json_response["items"][0] self.assertEqual(item["name"], original_item["name"]) self.assertEqual(item["category"], original_item["category"]) self.assertEqual(item["subcategory"], original_item["subcategory"])
def test_update_spot_with_missing_data(self): """ Tests to make sure that updating a spot without all required fields fails, giving a 400. """ spot_name = self.random_name() capacity = 20 spot_json = utils_test.get_spot(spot_name, capacity) spot_json['items'].append(utils_test.get_item()) new_spots = (copy.deepcopy(spot_json), copy.deepcopy(spot_json), copy.deepcopy(spot_json)) messages = ('Item name validation failed to raise an error', 'Item category validation failed to raise an error', 'Item subcategory validation failed to raise an error') # delete required fields del new_spots[0]['items'][0]['name'] del new_spots[1]['items'][0]['category'] del new_spots[2]['items'][0]['subcategory'] for spot, message in zip(new_spots, messages): response = self.put_spot(self.url, spot) self.assertEqual(response.status_code, 400, message)
def test_update_item_extended_info(self): """Test that item EI updates correctly.""" ei = {"make_model": "itemmodel", "customer_type": "UW Student", "auto_item_status": "active"} spot_json = utils_test.get_spot(self.random_name(), 20) spot_json["items"].append(utils_test.get_item()) for key, value in ei.iteritems(): spot_json["items"][0]["extended_info"][key] = value response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) get_response = self.client.get(self.url) spot_json = json.loads(get_response.content) self.assertEqual(len(spot_json["items"]), 1) self.assertIn("extended_info", spot_json["items"][0], "No extended_info in the item!") # assert that values were correct for key, value in ei.iteritems(): self.assertTrue(key in spot_json["items"][0]["extended_info"], key + " not in item extended info!") self.assertEqual( spot_json["items"][0]["extended_info"][key], value, key + " was not " + value + ", was :" + spot_json["items"][0]["extended_info"][key], ) spot_json["items"][0]["extended_info"]["make_model"] = "new_model" ei["make_model"] = "new_model" response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) get_response = self.client.get(self.url) spot_json = json.loads(get_response.content) self.assertEqual(len(spot_json["items"]), 1) self.assertIn("extended_info", spot_json["items"][0], "No extended_info in the item!") # assert that values were correct for key, value in ei.iteritems(): self.assertTrue(key in spot_json["items"][0]["extended_info"], key + " not in item extended info!") self.assertEqual( spot_json["items"][0]["extended_info"][key], value, key + " was not " + value + ", was :" + spot_json["items"][0]["extended_info"][key], )
def test_valid_json_valid_etag(self): new_name = "testing PUT name: {0}".format(random.random()) new_capacity = 20 spot_json = utils_test.get_spot(new_name, new_capacity) response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) updated_spot = Spot.objects.get(pk=self.spot.pk) self.assertEqual(updated_spot.name, new_name) self.assertEqual(updated_spot.capacity, new_capacity)
def test_valid_json_outdated_etag(self): new_name = "testing PUT name: {0}".format(random.random()) new_capacity = 30 etag = self.get_etag(self.url) spot_json = utils_test.get_spot(new_name, new_capacity) intermediate_spot = Spot.objects.get(pk=self.spot.pk) intermediate_spot.name = "This interferes w/ the PUT" intermediate_spot.save() response = self.client.put(self.url, spot_json, content_type="application/json", If_Match=etag) self.assertEqual(response.status_code, 409, "An outdated etag leads to a conflict") updated_spot = Spot.objects.get(pk=self.spot.pk) self.assertEqual(updated_spot.name, intermediate_spot.name) self.assertEqual(updated_spot.capacity, intermediate_spot.capacity)
def test_item_extended_info(self): """ Tests to ensure that the item extended info is being saved on creation. """ # todo : change to dict ei_keys = ("make_model", "customer_type", "auto_item_status") ei_values = ("itemmodel", "UW Student", "active") spot_json = utils_test.get_spot(self.random_name(), 20) spot_json['items'].append(utils_test.get_item()) for key, value in zip(ei_keys, ei_values): spot_json['items'][0]['extended_info'][key] = value response = self.client.post('/api/v1/spot', json.dumps(spot_json), content_type="application/json") self.assertEqual(response.status_code, 201) get_response = self.client.get(response['Location']) self.assertEqual(get_response.status_code, 200) json_response = json.loads(get_response.content) self.assertEqual(len(json_response["items"]), 1) self.assertIn('extended_info', spot_json['items'][0], "No extended_info in the item!") # assert that values were correct for key, value in zip(ei_keys, ei_values): self.assertIn(key, json_response["items"][0]["extended_info"], key + " not in item extended info!") self.assertEqual( json_response["items"][0]["extended_info"][key], value, key + " was not " + value + ", was :" + json_response["items"][0]["extended_info"][key])
def test_no_duplicate_extended_info(self): new_name = "testing PUT name: {0}".format(random.random()) new_capacity = 30 spot_json = utils_test.get_spot(new_name, new_capacity) spot_json["extended_info"]["has_a_funky_beat"] = "true" self.put_spot(self.url, spot_json) self.put_spot(self.url, spot_json) self.assertEqual(len(self.spot.spotextendedinfo_set.filter(key="has_a_funky_beat")), 1) spot_json["extended_info"]["has_a_funky_beat"] = "of_course" self.put_spot(self.url, spot_json) self.assertEqual(len(self.spot.spotextendedinfo_set.filter(key="has_a_funky_beat")), 1) self.assertEqual(self.spot.spotextendedinfo_set.get(key="has_a_funky_beat").value, "of_course")
def test_valid_json(self): c = self.client new_name = self.random_name() new_capacity = 10 spot_data = utils_test.get_spot(new_name, new_capacity) response = self.post_spot(spot_data) self.assertEquals(response.status_code, 201, "Spot did not get created") self.assertIn("Location", response, "Did not find Location header in response") self.spot = Spot.objects.get(name=new_name) self.assertEqual(response["Location"], "http://testserver" + self.spot.rest_url()) get_response = c.get(response["Location"]) self.assertEqual(get_response.status_code, 200) spot_json = json.loads(get_response.content) self.assertEqual(spot_json["name"], new_name, "Did not find correct name in response") self.assertEqual(spot_json["capacity"], new_capacity, "Did not find correct capacity in response")
def test_item_extended_info(self): """ Tests to ensure that the item extended info is being saved on creation. """ # todo : change to dict ei_keys = ("make_model", "customer_type", "auto_item_status") ei_values = ("itemmodel", "UW Student", "active") spot_json = utils_test.get_spot(self.random_name(), 20) spot_json["items"].append(utils_test.get_item()) for key, value in zip(ei_keys, ei_values): spot_json["items"][0]["extended_info"][key] = value response = self.client.post("/api/v1/spot", json.dumps(spot_json), content_type="application/json") self.assertEqual(response.status_code, 201) get_response = self.client.get(response["Location"]) self.assertEqual(get_response.status_code, 200) json_response = json.loads(get_response.content) self.assertEqual(len(json_response["items"]), 1) self.assertIn("extended_info", spot_json["items"][0], "No extended_info in the item!") # assert that values were correct for key, value in zip(ei_keys, ei_values): self.assertIn(key, json_response["items"][0]["extended_info"], key + " not in item extended info!") self.assertEqual( json_response["items"][0]["extended_info"][key], value, key + " was not " + value + ", was :" + json_response["items"][0]["extended_info"][key], )
def test_update_item_extended_info(self): """Test that item EI updates correctly.""" ei = { "make_model": "itemmodel", "customer_type": "UW Student", "auto_item_status": "active" } spot_json = utils_test.get_spot(self.random_name(), 20) spot_json['items'].append(utils_test.get_item()) for key, value in ei.iteritems(): spot_json['items'][0]['extended_info'][key] = value response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) get_response = self.client.get(self.url) spot_json = json.loads(get_response.content) self.assertEqual(len(spot_json["items"]), 1) self.assertIn('extended_info', spot_json['items'][0], "No extended_info in the item!") # assert that values were correct for key, value in ei.iteritems(): self.assertTrue(key in spot_json["items"][0]["extended_info"], key + " not in item extended info!") self.assertEqual( spot_json["items"][0]["extended_info"][key], value, key + " was not " + value + ", was :" + spot_json["items"][0]["extended_info"][key]) spot_json['items'][0]['extended_info']['make_model'] = 'new_model' ei['make_model'] = 'new_model' response = self.put_spot(self.url, spot_json) self.assertEqual(response.status_code, 200) get_response = self.client.get(self.url) spot_json = json.loads(get_response.content) self.assertEqual(len(spot_json["items"]), 1) self.assertIn('extended_info', spot_json['items'][0], "No extended_info in the item!") # assert that values were correct for key, value in ei.iteritems(): self.assertTrue(key in spot_json["items"][0]["extended_info"], key + " not in item extended info!") self.assertEqual( spot_json["items"][0]["extended_info"][key], value, key + " was not " + value + ", was :" + spot_json["items"][0]["extended_info"][key])