def test_project_invalid_id_post(): # Given project = { 'title': 'Project title 1', 'completed': False, 'active': True, } project_change = {'description': 'This is a description'} res_specific_project = create_project(project) res_specific_project = create_project(project) specific_non_existing_id = int(res_specific_project['id']) + 1 specific_project_id_url = url + str(specific_non_existing_id) # When res = requests.put(specific_project_id_url, headers=headers, data=json.dumps(project_change)) res_body = res.json() # Then print_response(res) assert res.status_code == 404
def test_get_non_empty_response_xml_projects(): # Given headers = {'Content-Type': 'application/json', 'Accept': 'application/xml'} # Given project1 = { 'title': 'Project title 1', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc' } project2 = { 'title': 'Project title 2', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim xyz' } create_project(project1) create_project(project2) # 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_valid_id_get_response(): # Given project = { 'title': 'Project title 1', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc' } res_specific_project = create_project(project) specific_id = res_specific_project['id'] specific_project_id_url = url + specific_id # When res = requests.get(specific_project_id_url, headers=headers) res_body = res.json() # Then print_response(res) assert res.status_code == 200 assert len(res_body['projects']) == 1 # Get id from specific project response res_project = [ project for project in res_body['projects'] if project['title'] == project['title'] ][0] assert res_project['id'] == res_specific_project['id']
def test_get_project_with_tasks(): # Given headers = {'Content-Type': 'application/json'} todo = {'title': 'todo title', 'description': 'description of todo'} todo_id = create_todo(todo)['id'] project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc', 'tasks': [{ 'id': todo_id }] } project_id = create_project(project)['id'] # When res = requests.get('http://localhost:4567/projects/' + project_id, headers=headers) res_body = res.json() print_response(res) # Then assert res.status_code == 200 assert res_body['projects'][0]['tasks'][0]['id'] == todo_id assert len(res_body['projects'][0]['tasks'][0]) == 1
def test_project_valid_id_post_xml(): # Given headers = {'Content-Type': 'application/json', 'Accept': 'application/xml'} project = { 'title': 'Project title 1', 'completed': False, 'active': True, } project_change = {'description': 'This is a description'} res_specific_project = create_project(project) specific_id = res_specific_project['id'] specific_project_id_url = url + specific_id # When res = requests.post(specific_project_id_url, headers=headers, data=json.dumps(project_change)) # Then res_xml = xml.dom.minidom.parseString(res.content) print(res_xml.toprettyxml()) assert res.status_code == 200
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_project_id_tasks_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 project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc' } todo_to_add = {'ID': str(invalid_todo_id)} project_id = create_project(project)['id'] # When res = requests.post(url + project_id + '/tasks', 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_get_todos_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'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description', 'tasksof': [ { 'id': project_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_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'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description' } todo_id = create_todo(todo)['id'] body = { 'idd': project_id } # When res = requests.post(url(todo_id), headers=headers, data=json.dumps(body)) # Then print_response(res) assert res.status_code == 400
def test_project_valid_id_put(): # Given project = { 'title': 'Project title 1', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc' } project_change = {'title': 'Project changed title'} res_specific_project = create_project(project) specific_id = res_specific_project['id'] specific_project_id_url = url + specific_id # When res = requests.put(specific_project_id_url, headers=headers, data=json.dumps(project_change)) res_body = res.json() # Then print_response(res) assert res.status_code == 200 assert res_body['title'] == project_change['title']
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_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_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_post_project_id_tasks_id_not_allowed(): # Given headers = {'Content-Type': 'application/json'} todo = {'title': 'todo title', 'description': 'description of todo'} todo_id = create_todo(todo)['id'] project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc' } project_id = create_project(project)['id'] # When res = requests.post(url + project_id + '/tasks/' + todo_id, headers=headers) # Then print_response(res) assert res.status_code == 405
def test_project_id_tasks_head_OK(): # Given headers = {'Content-Type': 'application/json'} todo = {'title': 'todo title', 'description': 'description of todo'} todo_id = create_todo(todo)['id'] project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc' } todo_to_add = {'ID': todo_id} project_id = create_project(project)['id'] # When res = requests.head(url + project_id + '/tasks', headers=headers, data=json.dumps(todo_to_add)) # Then print_response(res) assert res.status_code == 200
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_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_todos_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'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description', 'tasksof': [ { 'id': project_id } ] } todo_id = create_todo(todo)['id'] # When res = requests.delete(url(todo_id), headers=headers) # Then print_response(res) assert res.status_code == 405
def test_get_non_empty_response_projects(): # Given project1 = { 'title': 'Project title 1', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc' } project2 = { 'title': 'Project title 2', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim xyz' } create_project(project1) create_project(project2) # When res = requests.get(url, headers=headers) res_body = res.json() # Then print_response(res) assert res.status_code == 200 assert len(res_body['projects']) == 2 # Get todos from list since there is no guarantee on ordering in the response res_project1 = [ project for project in res_body['projects'] if project['title'] == project1['title'] ][0] res_project2 = [ project for project in res_body['projects'] if project['title'] == project2['title'] ][0] assert res_project1['title'] == project1['title'] assert res_project1['description'] == project1['description'] assert res_project2['title'] == project2['title'] assert res_project2['description'] == project2['description']
def test_delete_todo_project(): # Given headers = {'Content-Type': 'application/json' } project = { 'title': 'Office Work', 'completed': False, 'active': False, 'description': 'a description' } project_id = create_project(project)['id'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description', 'tasksof': [ { 'id': project_id } ] } todo_id = create_todo(todo)['id'] create_todo_project_relation(todo_id, project_id) # When res = requests.delete(url(todo_id, project_id), headers=headers) # Then print_response(res) assert res.status_code == 200 # Fetch todo and project to assert that the project 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('tasksof') is None updated_project = requests.get(project_url + todo_id, headers=headers) updated_project_body = updated_project.json() print('Project:') print_response(updated_project) assert updated_project_body['projects'][0].get('tasks') is None
def test_modify_project(num): t_start = datetime.utcnow().timestamp() reset_system() init_existing_projects(num=num) headers = {'Content-Type': 'application/json'} # Given project = { 'title': 'Project title 1', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc' } project_change = {'title': 'Project changed title'} res_specific_project = create_project(project) specific_id = res_specific_project['id'] specific_project_id_url = url + '/' + specific_id t_start_call = datetime.utcnow().timestamp() # When res = requests.put(specific_project_id_url, headers=headers, data=json.dumps(project_change)) t_end_call = datetime.utcnow().timestamp() res_body = res.json() # Then print_response(res) assert res.status_code == 200 assert res_body['title'] == project_change['title'] t_end = datetime.utcnow().timestamp() t1 = t_end - t_start t2 = t_end_call - t_start_call f = open("perf_logs/modify_project_performance_test.csv", "a") f.write( str(datetime.now().isoformat()) + ',' + str(num) + ',' + str(t1) + ',' + str(t2) + '\n') f.close()
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 step_impl(context): body = dict() if 'listTitle' in context: body['title'] = context.listTitle if 'description' in context: body['description'] = context.description if 'id' in context: body['id'] = context.id context.response = create_project(body)
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_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
def test_post_project_categories_valid(): # Given headers = {'Content-Type': 'application/json'} category = { 'title': 'category title', 'description': 'description of category' } category_id = str(create_category(category)['id']) project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc' } project_id = create_project(project)['id'] category_to_add = {'id': category_id} # When res = requests.post(url + project_id + '/categories', headers=headers, data=json.dumps(category_to_add)) # Then print_response(res) assert res.status_code == 201 # Fetch project and category to ensure relation was created 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 not 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 not None
def test_post_todos_projects(): # Given headers = {'Content-Type': 'application/json' } project = { 'title': 'Office Work', 'completed': False, 'active': False, 'description': 'a description' } project_id = create_project(project)['id'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description' } todo_id = create_todo(todo)['id'] body = { 'id': project_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 project to assert tasksof relationship was created 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]['tasksof'][0]['id'] == project_id updated_project = requests.get(projects_url + project_id, headers=headers) updated_project_body = updated_project.json() print_response(updated_project) assert updated_project_body['projects'][0]['tasks'][0]['id'] == todo_id
def test_delete_project_id_tasks_id_allowed(): # Given headers = {'Content-Type': 'application/json'} todo = {'title': 'todo title', 'description': 'description of todo'} todo_id = create_todo(todo)['id'] project = { 'title': 'Project title', 'completed': False, 'active': True, 'description': 'agna aliqua. Ut enim abc', 'tasks': [{ 'id': todo_id }] } project_id = create_project(project)['id'] create_todo_project_relation(todo_id, project_id) # When res = requests.delete(url + project_id + '/tasks/' + todo_id, headers=headers) # Then print_response(res) assert res.status_code == 200 # Confirm it was succesfully deleted from both project and todo res = requests.get('http://localhost:4567/projects/' + project_id, headers=headers) res_body = res.json() print_response(res) assert res_body['projects'][0].get('tasks') is None res = requests.get('http://localhost:4567/todos/' + todo_id, headers=headers) res_body = res.json() print_response(res) assert res_body['todos'][0].get('tasksof') is None
def step_impl(context): for todo in context.table: todo_table = todo.as_dict() todo_dict = {'title': todo_table['taskTitle']} if 'doneStatus' in todo_table: todo_dict['doneStatus'] = todo_table['doneStatus'] == 'True' if 'description' in todo_table: todo_dict['description'] = todo_table['description'] todo_id = create_todo(todo_dict)['id'] if 'priorityLevel' in todo_table: category_id = get_categories({'title': todo_table['priorityLevel']}).json()['categories'][0]['id'] create_todo_category_relation(todo_id, category_id) if 'project' in todo_table: project_id = create_project({'title': todo_table['project']})['id'] create_todo_project_relation(todo_id, project_id)
def test_post_todo_valid_body_with_project_relationship(): # Given headers = {'Content-Type': 'application/json' } project = { 'title': 'Office Work', 'completed': False, 'active': False, 'description': 'a description' } project_id = create_project(project)['id'] todo = { 'title': 'Task title 1', 'doneStatus': False, 'description': 'this is a description', 'tasksof': [ { 'id': project_id } ] } # When res = requests.post(url, headers=headers, data=json.dumps(todo)) res_body = res.json() # Then print_response(res) assert res.status_code == 201 assert res_body['title'] == todo['title'] assert res_body['doneStatus'] == str(todo['doneStatus']).lower() assert res_body['description'] == todo['description'] # Assert todo was added to project updated_project = requests.get(base_url + 'projects/' + project_id, headers=headers) updated_project_body = updated_project.json() print_response(updated_project) assert updated_project_body['projects'][0]['tasks'][0]['id'] == res_body['id']