import pytest import requests import json from constants import WEBSITE_PREFIX OK = 200 CREATED = 201 NOT_FOUND = 404 CONFLICT = 409 def test_categories_and_materials(): resp = create_category('root') root_id = json.loads(resp.text)['id'] assert(resp.status_code == OK) resp = create_category('child') child_id = json.loads(resp.text)['id'] resp = update_category(child_id, { 'parentId': root_id }) assert(resp.status_code == OK) resp = get_category(child_id) assert(json.loads(resp.text)[0]['parentId'] == root_id) resp = get_category(root_id) assert(len(json.loads(resp.text)) == 2) assert(json.loads(resp.text)[0]['id'] == root_id) assert(json.loads(resp.text)[1]['id'] == child_id) resp = show_root_categories() root_categories = json.loads(resp.text) assert(len(root_categories) == 1) assert(root_categories[0]['id'] == root_id) material = create_material({ 'name': '学习资料1', 'description': '好看的资料', 'type': 'ARTICLE', 'contents': '十年高考三年模拟', 'links': ['a', 'b', 'c'] }) materialId = json.loads(material.text)['id'] assert(resp.status_code == OK) resp = add_material_to_category(child_id, json.loads(material.text)) assert(resp.status_code == OK) resp = materials_in_category(child_id) assert(resp.status_code == OK) resp = json.loads(resp.text) assert(len(resp) == 1) resp = remove_material_from_category(child_id, json.loads(material.text)) resp = json.loads(materials_in_category(child_id).text) assert(len(resp) == 0) resp = update_material(materialId, { 'name': '学习资料1更改版' }) assert(resp.status_code == OK) delete_material(materialId) resp = get_material(materialId) assert(resp.status_code == NOT_FOUND) delete_category(root_id) resp = show_root_categories() root_categories = json.loads(resp.text) assert(len(root_categories) == 0) delete_category(child_id) def create_material(data): url = WEBSITE_PREFIX + '/admin/materials' return requests.post(url, json=data) def delete_material(materialId): url = WEBSITE_PREFIX + '/admin/materials/' + materialId return requests.delete(url) def get_material(materialId): url = WEBSITE_PREFIX + '/materials/' + materialId return requests.get(url) def like_material(materialId, user): url = WEBSITE_PREFIX + '/materials/' + materialId + '/like' return requests.post(url, json=user) def update_material(materialId, udpatedMaterial): url = WEBSITE_PREFIX + '/admin/materials/' + materialId return requests.put(url, json=udpatedMaterial) def show_root_categories(): url = WEBSITE_PREFIX + '/categories' return requests.get(url) def show_root_question_categories(): url = WEBSITE_PREFIX + '/q-categories' return requests.get(url) def create_category(name): url = WEBSITE_PREFIX + '/admin/materialCategories' data = { 'name': name } return requests.post(url, json=data) def create_question_category(name): url = WEBSITE_PREFIX + '/admin/qCategories' data = { 'name': name } return requests.post(url, json=data) def delete_category(categoryId): url = WEBSITE_PREFIX + '/admin/categories/' + categoryId return requests.delete(url) def delete_question_category(categoryId): url = WEBSITE_PREFIX + '/admin/q-categories/' + categoryId return requests.delete(url) def get_category(categoryId): url = WEBSITE_PREFIX + '/categories/' + categoryId return requests.get(url) def get_question_category(categoryId): url = WEBSITE_PREFIX + '/q-categories/' + categoryId return requests.get(url) def update_category(categoryId, updatedData): url = WEBSITE_PREFIX + '/admin/categories/' + categoryId return requests.put(url, json=updatedData) def update_question_category(categoryId, updatedData): url = WEBSITE_PREFIX + '/admin/q-categories/' + categoryId return requests.put(url, json=updatedData) def materials_in_category(categoryId): url = WEBSITE_PREFIX + '/categories/' + categoryId + '/materials' return requests.get(url) def questions_in_category(categoryId): url = WEBSITE_PREFIX + '/q-categories/' + categoryId + '/questions' return requests.get(url) def add_material_to_category(categoryId, material): url = WEBSITE_PREFIX + '/categories/' + categoryId + '/materials' return requests.post(url, json=material) def add_question_to_question_category(categoryId, question): url = WEBSITE_PREFIX + '/q-categories/' + categoryId + '/questions' return requests.post(url, json=question) def remove_material_from_category(categoryId, material): url = WEBSITE_PREFIX + '/categories/' + categoryId + '/materials' return requests.delete(url, json=material) def remove_question_from_question_category(categoryId, question): url = WEBSITE_PREFIX + '/q-categories/' + categoryId + '/questions' return requests.delete(url, json=question)