def test_get_non_empty_response_xml_categories(): headers = {'Content-Type': 'application/json', 'Accept': 'application/xml'} # Given category1 = { 'title': 'Category title 1', 'description': 'this is a description' } category2 = { 'title': 'Category title 2', 'description': 'this is another description' } create_category(category1) create_category(category2) # When res = requests.get(url, headers=headers) # Then res_xml = xml.dom.minidom.parseString(res.content) print(res_xml.toprettyxml()) assert res.status_code == 200
def test_delete_project_id_categories_id_no_relationship(): # Given headers = {'Content-Type': 'application/json'} category1 = { 'title': 'category title1', 'description': 'description of category' } category2 = { 'title': 'category title2', 'description': 'description of category' } category_id1 = create_category(category1)['id'] category_id2 = create_category(category2)['id'] project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc', 'categories': [{ 'id': category_id1 }] } project_id = create_project(project)['id'] # When res = requests.delete(url + project_id + '/categories/' + category_id2, headers=headers) res_body = res.json() # Then assert res.status_code == 404 assert res_body['errorMessages'][ 0] == 'Could not find any instances with projects/' + project_id + '/categories/' + category_id2 res = requests.get('http://localhost:4567/projects/' + project_id, headers=headers) res_body = res.json() print_response(res) # assert relationship wasn't deleted assert len(res_body['projects'][0]) == 6 assert res_body['projects'][0]['categories'][0]['id'] == category_id1
def test_post_project_with_invalid_categories(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] invalid_category_id = int(category_id) + 1 project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc' } category_to_add = {'ID': invalid_category_id} project_id = create_project(project)['id'] # When res = requests.post(url + project_id + '/categories', headers=headers, data=json.dumps(category_to_add)) res_body = res.json() # Then print_response(res) assert res.status_code == 404 assert res_body['errorMessages'][ 0] == 'Could not find thing matching value for ID'
def test_get_category_with_todos_xml(): # Given headers = {'Content-Type': 'application/json', 'Accept': 'application/xml'} todo = {'title': 'todo title', 'description': 'description of todo'} todo_id = create_todo(todo)['id'] category = { 'title': 'category title', 'description': 'a description', 'todos': [{ 'id': todo_id }] } category_id = create_category(category)['id'] # When res = requests.get(url + category_id + '/todos', headers=headers) # Then res_xml = xml.dom.minidom.parseString(res.content) print(res_xml.toprettyxml()) assert res.status_code == 200
def test_get_category_with_todos(): # Given headers = {'Content-Type': 'application/json'} todo = {'title': 'todo title', 'description': 'description of todo'} todo_id = create_todo(todo)['id'] category = { 'title': 'category title', 'description': 'a description', 'todos': [{ 'id': todo_id }] } category_id = create_category(category)['id'] # When res = requests.get('http://localhost:4567/categories/' + category_id, headers=headers) res_body = res.json() print_response(res) # Then assert res.status_code == 200 assert res_body['categories'][0]['todos'][0]['id'] == todo_id assert len(res_body['categories'][0]['todos'][0]) == 1
def test_post_categories_projects_category_project_does_not_exist(): # Given headers = {'Content-Type': 'application/json'} invalid_id = '999' category = { 'title': 'Category title 1', 'description': 'this is a description' } category_id = create_category(category)['id'] body = {'id': invalid_id} # When res = requests.post(url(category_id), headers=headers, data=json.dumps(body)) # Then print_response(res) assert res.status_code == 404
def test_delete_category_project_category_does_not_exist(): # Given headers = {'Content-Type': 'application/json'} project = { 'title': 'Office Work', 'completed': False, 'active': False, 'description': 'a description' } project_id = create_project(project)['id'] category = {'title': 'todo title'} category_id = create_category(category)['id'] create_category_project_relation(category_id, project_id) invalid_id = '999' # When res = requests.delete(url(invalid_id, project_id), headers=headers) # Then print_response(res) assert res.status_code == 400
def test_post_todo_category_not_allowed(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description', 'categories': [{ 'id': category_id }] } todo_id = create_todo(todo)['id'] # When res = requests.post(url(todo_id, category_id), headers=headers) # Then print_response(res) assert res.status_code == 405
def test_post_category_valid_body_xml_categories_id(): # Given headers = {'Content-Type': 'application/json', 'Accept': 'application/xml'} category = { 'title': 'Category title 1', 'description': 'this is a description' } category_id = create_category(category)['id'] edited_category = { 'title': 'A different title', 'description': 'a different description' } # When res = requests.post(url + category_id, headers=headers, data=json.dumps(edited_category)) # Then res_xml = xml.dom.minidom.parseString(res.content) print(res_xml.toprettyxml()) assert res.status_code == 200
def test_project_id_category_patch_not_allowed(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc' } category_to_add = {'ID': category_id} project_id = create_project(project)['id'] # When res = requests.patch(url + project_id + '/categories', headers=headers, data=json.dumps(category_to_add)) # Then print_response(res) assert res.status_code == 405
def test_get_project_with_categories(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc', 'categories': [{ 'id': category_id }] } project_id = create_project(project)['id'] # When res = requests.get('http://localhost:4567/projects/' + project_id, headers=headers) res_body = res.json() # Then print_response(res) assert res.status_code == 200 assert res_body['projects'][0]['categories'][0]['id'] == category_id assert len(res_body['projects'][0]['categories'][0]) == 1
def test_get_todos_categories_xml(): # Given headers = {'Content-Type': 'application/json', 'Accept': 'application/xml'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description', 'categories': [{ 'id': category_id }] } todo_id = create_todo(todo)['id'] # When res = requests.get(url(todo_id), headers=headers) # Then res_xml = xml.dom.minidom.parseString(res.content) print(res_xml.toprettyxml()) assert res.status_code == 200
def test_post_todos_categories_todo_does_not_exist(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] invalid_id = '999' body = {'id': category_id} # When res = requests.post(url(invalid_id), headers=headers, data=json.dumps(body)) # Then print_response(res) assert res.status_code == 404
def test_get_todos_categories(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description', 'categories': [{ 'id': category_id }] } todo_id = create_todo(todo)['id'] # When res = requests.get(url(todo_id), headers=headers) res_body = res.json() # Then print_response(res) assert res.status_code == 200 assert res_body['categories'][0]['id'] == category_id assert res_body['categories'][0]['title'] == category['title'] assert res_body['categories'][0]['description'] == category['description']
def test_post_category_invalid_attribute(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'Category title 1', 'description': 'this is a description' } category_id = create_category(category)['id'] edited_category = {'invalid attr': 'test'} # When res = requests.put(url + category_id, headers=headers, data=json.dumps(edited_category)) res_body = res.json() # Then print_response(res) assert res.status_code == 400 assert res_body['errorMessages'][0] == 'Could not find field: invalid attr'
def test_delete_todo_category_todo_does_not_exist(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] todo = {'title': 'todo title'} todo_id = create_todo(todo)['id'] create_todo_category_relation(todo_id, category_id) invalid_id = '999' # When res = requests.delete(url(invalid_id, category_id), headers=headers) # Then print_response(res) assert res.status_code == 400
def test_put_category_valid_body(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'Category title 1', 'description': 'this is a description' } category_id = create_category(category)['id'] edited_category = { 'title': 'A different title', 'description': 'a different description' } # When res = requests.put(url + category_id, headers=headers, data=json.dumps(edited_category)) res_body = res.json() # Then print_response(res) assert res.status_code == 200 assert res_body['id'] == category_id assert res_body['title'] == edited_category['title'] assert res_body['description'] == edited_category['description']
def test_get_categories_projects_xml(): # Given headers = {'Content-Type': 'application/json', 'Accept': 'application/xml'} project = { 'title': 'Office Work', 'completed': False, 'active': False, 'description': 'a description' } project_id = create_project(project)['id'] category = { 'title': 'Category title 1', 'description': 'this is a description', 'projects': [{ 'id': project_id }] } category_id = create_category(category)['id'] # When res = requests.get(url(category_id), headers=headers) # Then res_xml = xml.dom.minidom.parseString(res.content) print(res_xml.toprettyxml()) assert res.status_code == 200
def test_post_category_id_todos(): # Given headers = {'Content-Type': 'application/json'} todo = {'title': 'todo title', 'description': 'description of todo'} todo_id = create_todo(todo)['id'] category = {'title': 'category title', 'description': 'a description'} todo_to_add = {'ID': todo_id} category_id = create_category(category)['id'] # When res = requests.post(url + category_id + '/todos', headers=headers, data=json.dumps(todo_to_add)) # Then assert res.status_code == 201 # When res = requests.get('http://localhost:4567/categories/' + category_id, headers=headers) res_body = res.json() # Then, assert a relationship was made print_response(res) assert res_body['categories'][0]['todos'][0]['id'] == todo_id assert len(res_body['categories'][0]['todos'][0]) == 1
def test_delete_categories_projects_not_allowed(): # Given headers = {'Content-Type': 'application/json'} project = { 'title': 'Office Work', 'completed': False, 'active': False, 'description': 'a description' } project_id = create_project(project)['id'] category = { 'title': 'Category title 1', 'description': 'this is a description', 'projects': [{ 'id': project_id }] } category_id = create_category(category)['id'] # When res = requests.delete(url(category_id), headers=headers) # Then print_response(res) assert res.status_code == 405
def test_post_category_id_todos_invalid_todo_id(): # Given headers = {'Content-Type': 'application/json'} todo = {'title': 'todo title', 'description': 'description of todo'} todo_id = create_todo(todo)['id'] invalid_todo_id = int(todo_id) + 1 category = {'title': 'category title', 'description': 'a description'} todo_to_add = {'ID': str(invalid_todo_id)} category_id = create_category(category)['id'] # When res = requests.post(url + category_id + '/todos', headers=headers, data=json.dumps(todo_to_add)) res_body = res.json() # Then print_response(res) assert res.status_code == 404 assert res_body['errorMessages'][ 0] == 'Could not find thing matching value for ID'
def test_post_categories_projects_invalid_body(): # Given headers = {'Content-Type': 'application/json'} project = { 'title': 'Office Work', 'completed': False, 'active': False, 'description': 'a description' } project_id = create_project(project)['id'] category = { 'title': 'Category title 1', 'description': 'this is a description' } category_id = create_category(category)['id'] body = {'idd': project_id} # When res = requests.post(url(category_id), headers=headers, data=json.dumps(body)) # Then print_response(res) assert res.status_code == 400
def test_get_project_with_categories_xml(): # Given headers = {'Content-Type': 'application/json', 'Accept': 'application/xml'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc', 'categories': [{ 'id': category_id }] } project_id = create_project(project)['id'] # When res = requests.get(url + project_id + '/categories', headers=headers) # Then res_xml = xml.dom.minidom.parseString(res.content) print(res_xml.toprettyxml()) assert res.status_code == 200
def test_delete_todo_category_relationship_does_not_exist(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } other_category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] other_category_id = create_category(other_category)['id'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description', 'categories': [{ 'id': category_id }] } todo_id = create_todo(todo)['id'] # When res = requests.delete(url(todo_id, other_category_id), headers=headers) # Then print_response(res) assert res.status_code == 404 # Fetch todo to assert that the catgeory relationship was not deleted updated_todo = requests.get(base_url + todo_id, headers=headers) updated_todo_body = updated_todo.json() print_response(updated_todo) assert updated_todo_body['todos'][0]['categories'][0]['id'] == category_id
def test_get_non_empty_response_categories(): # Given headers = {'Content-Type': 'application/json'} category1 = { 'title': 'Category title 1', 'description': 'this is a description' } category2 = { 'title': 'Category title 2', 'description': 'this is another description' } create_category(category1) create_category(category2) # When res = requests.get(url, headers=headers) res_body = res.json() # Then print_response(res) assert res.status_code == 200 assert len(res_body['categories']) == 2 # Get categories from list since there is no guarantee on ordering in the response res_category1 = [ category for category in res_body['categories'] if category['title'] == category1['title'] ][0] res_category2 = [ category for category in res_body['categories'] if category['title'] == category2['title'] ][0] assert res_category1['title'] == category1['title'] assert res_category1['description'] == category1['description'] assert res_category2['title'] == category2['title'] assert res_category2['description'] == category2['description']
def test_modify_category(num): t_start = datetime.utcnow().timestamp() reset_system() init_existing_categories(num=num) # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'Category title 1', 'description': 'this is a description' } category_id = create_category(category)['id'] edited_category = { 'title': 'A different title', 'description': 'a different description' } t_start_call = datetime.utcnow().timestamp() # When res = requests.put(url + '/' + category_id, headers=headers, data=json.dumps(edited_category)) t_end_call = datetime.utcnow().timestamp() res_body = res.json() # Then print_response(res) assert res.status_code == 200 assert res_body['id'] == category_id assert res_body['title'] == edited_category['title'] assert res_body['description'] == edited_category['description'] t_end = datetime.utcnow().timestamp() t1 = t_end - t_start t2 = t_end_call - t_start_call f = open("perf_logs/modify_category_performance_test.csv", "a") f.write( str(datetime.now().isoformat()) + ',' + str(num) + ',' + str(t1) + ',' + str(t2) + '\n') f.close()
def test_delete_todo_category(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description', 'categories': [{ 'id': category_id }] } todo_id = create_todo(todo)['id'] create_todo_category_relation(todo_id, category_id) # When res = requests.delete(url(todo_id, category_id), headers=headers) # Then print_response(res) assert res.status_code == 200 # Fetch todo and category to assert that the catgeory relationship was deleted updated_todo = requests.get(base_url + todo_id, headers=headers) updated_todo_body = updated_todo.json() print('Todo:') print_response(updated_todo) assert updated_todo_body['todos'][0].get('categories') is None updated_category = requests.get(category_url + category_id, headers=headers) updated_category_body = updated_category.json() print('Category:') print_response(updated_category) assert updated_category_body['categories'][0].get('todos') is None
def test_delete_category_project(): # Given headers = {'Content-Type': 'application/json'} project = { 'title': 'Office Work', 'completed': False, 'active': False, 'description': 'a description' } project_id = create_project(project)['id'] category = { 'title': 'Category title 1', 'description': 'this is a description', 'projects': [{ 'id': project_id }] } category_id = create_category(category)['id'] create_category_project_relation(category_id, project_id) # When res = requests.delete(url(category_id, project_id), headers=headers) # Then print_response(res) assert res.status_code == 200 # Fetch category and project to assert that the project relationship was deleted updated_category = requests.get(base_url + category_id, headers=headers) updated_category_body = updated_category.json() print('Category:') print_response(updated_category) assert updated_category_body['categories'][0].get('projects') is None updated_project = requests.get(project_url + category_id, headers=headers) updated_project_body = updated_project.json() print('Project:') print_response(updated_project) assert updated_project_body['projects'][0].get('categories') is None
def test_post_todos_categories(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description' } todo_id = create_todo(todo)['id'] body = {'id': category_id} # When res = requests.post(url(todo_id), headers=headers, data=json.dumps(body)) # Then print_response(res) assert res.status_code == 201 # Fetch todo and category to assert that the catgeory relationship was created updated_todo = requests.get(base_url + todo_id, headers=headers) updated_todo_body = updated_todo.json() print('Todo:') print_response(updated_todo) assert updated_todo_body['todos'][0]['categories'][0]['id'] == category_id updated_category = requests.get(category_url + category_id, headers=headers) updated_category_body = updated_category.json() print('Category:') print_response(updated_category) assert updated_category_body['categories'][0]['todos'][0][ 'id'] == category_id
def test_delete_project_id_categories_id_allowed(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } category_id = create_category(category)['id'] project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc', 'categories': [{ 'id': category_id }] } project_id = create_project(project)['id'] create_category_project_relation(category_id, project_id) # When res = requests.delete(url + project_id + '/categories/' + category_id, headers=headers) # Then assert res.status_code == 200 # Check that relationship was deleted from both project and category res = requests.get('http://localhost:4567/projects/' + project_id, headers=headers) res_body = res.json() print_response(res) assert res_body['projects'][0].get('categories') is None res = requests.get('http://localhost:4567/categories/' + category_id, headers=headers) res_body = res.json() print_response(res) assert res_body['categories'][0].get('projects') is None