def test_get_projects_xml(self): response = requests.get('http://localhost:4567/projects', headers={'Accept': 'application/xml'}) # Compare response with expected xml project_xml = ET.fromstring(response.content) expected_project_xml_v1 = ET.fromstring(const.DEFAULT_PROJECT_XML_V1) expected_project_xml_v2 = ET.fromstring(const.DEFAULT_PROJECT_XML_V2) self.assertTrue(elements_equal(project_xml, expected_project_xml_v1) or elements_equal(project_xml, expected_project_xml_v2)) self.assertEqual(response.status_code, 200)
def test_post_projects_id_categories_xml(self): project_data = { "title": "School Work", "completed": False, "active": False, "description": "Work on assignments." } # Create a new project response = requests.post('http://localhost:4567/projects', json=project_data, headers={'Accept': 'application/xml'}) self.assertEqual(response.status_code, 201) project_xml = ET.fromstring(response.content) self.active_projects.append(project_xml.find("./id").text) # Check if what was created matches what was provided self.assertEqual( project_xml.find("./title").text, project_data["title"]) self.assertEqual( project_xml.find("./completed").text, str(project_data["completed"]).lower()) self.assertEqual( project_xml.find("./active").text, str(project_data["active"]).lower()) self.assertEqual( project_xml.find("./description").text, project_data["description"]) response = requests.get('http://localhost:4567/projects/' + project_xml.find("./id").text, headers={'Accept': 'application/xml'}) obtained_project = ET.fromstring(response.content)[0] # Check if created project matches what we have self.assertEqual(response.status_code, 200) self.assertTrue(elements_equal(project_xml, obtained_project)) # Create relationship between new project and default category 1 category_id = {"id": "1"} response = requests.post('http://localhost:4567/projects/' + project_xml.find("./id").text + '/categories', json=category_id, headers={'Accept': 'application/xml'}) self.assertEqual(response.status_code, 201) # Verify if relationship was properly applied response = requests.get('http://localhost:4567/projects/' + project_xml.find("./id").text + '/categories', headers={'Accept': 'application/xml'}) categories_xml = ET.fromstring(response.content) expected_category_xml = ET.fromstring(const.CATEGORIES_DEFAULT_XML_1) # Compare response with expected json self.assertEqual(response.status_code, 200) self.assertTrue(elements_equal(categories_xml, expected_category_xml))
def test_post_todos_id_categories_xml(self): # Create a new todo to establish a category relationship category_todo = { "title": "Todo to be associated to category", "doneStatus": False, "description": "" } response = requests.post('http://localhost:4567/todos', headers={'Accept': 'application/json'}, json=category_todo) actual_category_todo = response.json() # Check if what was created matches the title we want self.assertEqual(category_todo["title"], actual_category_todo["title"]) self.assertEqual(response.status_code, 201) self.active_todos.append(actual_category_todo["id"]) # Create relationship with default category 1 identify_json = {"id": "1"} response = requests.post('http://localhost:4567/todos/' + actual_category_todo["id"] + '/categories', json=identify_json) self.assertEqual(response.status_code, 201) # Check if category 1 is added to the todo (data received in XML) response = requests.get('http://localhost:4567/todos/' + actual_category_todo["id"] + '/categories', headers={'Accept': 'application/xml'}, json=identify_json) assigned_category = ET.fromstring(response.content) self.assertTrue( elements_equal(assigned_category, ET.fromstring(const.CATEGORIES_DEFAULT_XML_1))) self.assertEqual(response.status_code, 200)
def test_delete_projects_id_categories_id_xml(self): # Create relationship between default project 1 and default category 1 category_id = {"id": "1"} response = requests.post('http://localhost:4567/projects/1/categories', json=category_id, headers={'Accept': 'application/xml'}) self.assertEqual(response.status_code, 201) # Verify if relationship was properly applied response = requests.get('http://localhost:4567/projects/1/categories', headers={'Accept': 'application/xml'}) categories_xml = ET.fromstring(response.content) expected_category_xml = ET.fromstring(const.CATEGORIES_DEFAULT_XML_1) self.assertEqual(response.status_code, 200) self.assertTrue(elements_equal(categories_xml, expected_category_xml)) # Delete relationship between default project 1 and cateory 1 response = requests.delete( 'http://localhost:4567/projects/1/categories/1', headers={'Accept': 'application/xml'}) self.assertEqual(response.status_code, 200) # Verify that relationship was deleted properly response = requests.get('http://localhost:4567/projects/1/categories', headers={'Accept': 'application/xml'}) categories_xml = ET.fromstring(response.content) self.assertEqual(response.status_code, 200) self.assertIsNone(categories_xml.find("./categories"))
def test_post_project_id_xml(self): project_data = { "title": "School Work", "completed": False, "active": False, "description": "Work on assignments." } # Create a new project response = requests.post('http://localhost:4567/projects', json=project_data, headers={'Accept': 'application/xml'}) self.assertEqual(response.status_code, 201) project_xml = ET.fromstring(response.content) self.active_projects.append(project_xml.find("./id").text) # Check if what was created matches what was provided self.assertEqual( project_xml.find("./title").text, project_data["title"]) self.assertEqual( project_xml.find("./completed").text, str(project_data["completed"]).lower()) self.assertEqual( project_xml.find("./active").text, str(project_data["active"]).lower()) self.assertEqual( project_xml.find("./description").text, project_data["description"]) response = requests.get('http://localhost:4567/projects/' + project_xml.find("./id").text, headers={'Accept': 'application/xml'}) obtained_project = ET.fromstring(response.content)[0] # Check if created project matches what we have self.assertEqual(response.status_code, 200) self.assertTrue(elements_equal(project_xml, obtained_project)) # Amend project field with new data updated_field = {"active": True} response = requests.post('http://localhost:4567/projects/' + project_xml.find("./id").text, json=updated_field, headers={'Accept': 'application/xml'}) self.assertEqual(response.status_code, 200) updated_project_xml = ET.fromstring(response.content) # Check if update was done appropriately self.assertEqual( updated_project_xml.find("./id").text, project_xml.find("./id").text) self.assertEqual( updated_project_xml.find("./title").text, project_data["title"]) self.assertEqual( updated_project_xml.find("./completed").text, str(project_data["completed"]).lower()) self.assertEqual( updated_project_xml.find("./active").text, str(updated_field["active"]).lower()) self.assertEqual( updated_project_xml.find("./description").text, project_data["description"])
def test_get_todos_id_categories_xml(self): response = requests.get('http://localhost:4567/todos/1/categories', headers={'Accept': 'application/xml'}) categories_xml_1 = ET.fromstring(response.content) # Compare expected default categories xml with id 1 self.assertTrue( elements_equal(categories_xml_1, ET.fromstring(const.CATEGORIES_DEFAULT_XML_1))) self.assertEqual(response.status_code, 200)
def test_get_todos_xml(self): response = requests.get('http://localhost:4567/todos', headers={'Accept': 'application/xml'}) actual_todos_xml = ET.fromstring(response.content) actual_todos_xml[:] = sorted(actual_todos_xml, key=lambda val: val.find("./id").text ) # Sort to keep data consistent # Compare actual xml from expected xml expected_todos_xml = ET.fromstring(const.TODOS_DEFAULT_XML) self.assertTrue(elements_equal(actual_todos_xml, expected_todos_xml)) self.assertEqual(response.status_code, 200)
def test_delete_project_id_xml(self): project_data = { "title": "School Work", "completed": False, "active": False, "description": "Work on assignments." } # Create a new project response = requests.post('http://localhost:4567/projects', json=project_data, headers={'Accept': 'application/xml'}) self.assertEqual(response.status_code, 201) project_xml = ET.fromstring(response.content) self.active_projects.append(project_xml.find("./id").text) # Check if what was created matches what was provided self.assertEqual( project_xml.find("./title").text, project_data["title"]) self.assertEqual( project_xml.find("./completed").text, str(project_data["completed"]).lower()) self.assertEqual( project_xml.find("./active").text, str(project_data["active"]).lower()) self.assertEqual( project_xml.find("./description").text, project_data["description"]) response = requests.get('http://localhost:4567/projects/' + project_xml.find("./id").text, headers={'Accept': 'application/xml'}) obtained_project = ET.fromstring(response.content)[0] # Check if created project matches what we have self.assertEqual(response.status_code, 200) self.assertTrue(elements_equal(project_xml, obtained_project)) # Delete project response = requests.delete('http://localhost:4567/projects/' + project_xml.find("./id").text, headers={'Accept': 'application/xml'}) self.assertEqual(response.status_code, 200) # Check if project was deleted properly response = requests.get('http://localhost:4567/projects/' + project_xml.find("./id").text, headers={'Accept': 'application/xml'}) error_xml = ET.fromstring(response.content) self.assertEqual(response.status_code, 404) self.assertEqual( error_xml.find("./errorMessage").text, "Could not find an instance with projects/" + project_xml.find("./id").text)
def test_post_todos_xml(self): sample_todo = { "title": "Test Todo", "doneStatus": False, "description": "" } response = requests.post('http://localhost:4567/todos', headers={'Accept': 'application/xml'}, json=sample_todo) actual_sample_todo = ET.fromstring(response.content) # Check if what was created matches the title we want self.assertEqual(sample_todo["title"], actual_sample_todo.find("./title").text) self.assertEqual(response.status_code, 201) self.active_todos.append(actual_sample_todo.find("./id").text) response = requests.get('http://localhost:4567/todos/' + actual_sample_todo.find("./id").text, headers={'Accept': 'application/xml'}) obtained_todo = ET.fromstring(response.content)[0] # Check if created todo matches what we have self.assertTrue(elements_equal(actual_sample_todo, obtained_todo)) self.assertEqual(response.status_code, 200)
def test_delete_projects_id_tasks_id_xml(self): project_data = { "title": "School Work", "completed": False, "active": False, "description": "Work on assignments." } # Create a new project response = requests.post('http://localhost:4567/projects', json=project_data, headers={'Accept': 'application/xml'}) self.assertEqual(response.status_code, 201) project_xml = ET.fromstring(response.content) self.active_projects.append(project_xml.find("./id").text) # Check if what was created matches what was provided self.assertEqual( project_xml.find("./title").text, project_data["title"]) self.assertEqual( project_xml.find("./completed").text, str(project_data["completed"]).lower()) self.assertEqual( project_xml.find("./active").text, str(project_data["active"]).lower()) self.assertEqual( project_xml.find("./description").text, project_data["description"]) response = requests.get('http://localhost:4567/projects/' + project_xml.find("./id").text, headers={'Accept': 'application/xml'}) obtained_project = ET.fromstring(response.content)[0] # Check if created project matches what we have self.assertEqual(response.status_code, 200) self.assertTrue(elements_equal(project_xml, obtained_project)) # Create relationship between new project and default todo 1 todo_id = {"id": "1"} response = requests.post('http://localhost:4567/projects/' + project_xml.find("./id").text + '/tasks', json=todo_id, headers={'Accept': 'application/xml'}) self.assertEqual(response.status_code, 201) # Verify if relationship was properly applied response = requests.get('http://localhost:4567/projects/' + project_xml.find("./id").text + '/tasks', headers={'Accept': 'application/xml'}) todos_xml = ET.fromstring(response.content) self.assertEqual(response.status_code, 200) self.assertEqual(todos_xml.find("./todo/id").text, todo_id['id']) self.assertIn( project_xml.find("./id").text, [id.text for id in todos_xml.findall("./todo/tasksof/id")]) # Delete relationship between new project and todo 1 response = requests.delete('http://localhost:4567/projects/' + project_xml.find("./id").text + '/tasks/1', headers={'Accept': 'application/xml'}) self.assertEqual(response.status_code, 200) # Verify that relationship was deleted properly response = requests.get('http://localhost:4567/projects/' + project_xml.find("./id").text + '/tasks', headers={'Accept': 'application/xml'}) todos_xml = ET.fromstring(response.content) self.assertEqual(response.status_code, 200) self.assertIsNone(todos_xml.find("./todos"))