category_test.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import pytest
  2. import requests
  3. import json
  4. from constants import WEBSITE_PREFIX
  5. OK = 200
  6. CREATED = 201
  7. NOT_FOUND = 404
  8. CONFLICT = 409
  9. def test_categories_and_materials():
  10. resp = create_category('root')
  11. root_id = json.loads(resp.text)['id']
  12. assert(resp.status_code == OK)
  13. resp = create_category('child')
  14. child_id = json.loads(resp.text)['id']
  15. resp = update_category(child_id, {
  16. 'parentId': root_id
  17. })
  18. assert(resp.status_code == OK)
  19. resp = get_category(child_id)
  20. assert(json.loads(resp.text)[0]['parentId'] == root_id)
  21. resp = get_category(root_id)
  22. assert(len(json.loads(resp.text)) == 2)
  23. assert(json.loads(resp.text)[0]['id'] == root_id)
  24. assert(json.loads(resp.text)[1]['id'] == child_id)
  25. resp = show_root_categories()
  26. root_categories = json.loads(resp.text)
  27. assert(len(root_categories) == 1)
  28. assert(root_categories[0]['id'] == root_id)
  29. material = create_material({
  30. 'name': '学习资料1',
  31. 'description': '好看的资料',
  32. 'type': 'ARTICLE',
  33. 'contents': '十年高考三年模拟',
  34. 'links': ['a', 'b', 'c']
  35. })
  36. materialId = json.loads(material.text)['id']
  37. assert(resp.status_code == OK)
  38. resp = add_material_to_category(child_id, json.loads(material.text))
  39. assert(resp.status_code == OK)
  40. resp = materials_in_category(child_id)
  41. assert(resp.status_code == OK)
  42. resp = json.loads(resp.text)
  43. assert(len(resp) == 1)
  44. resp = remove_material_from_category(child_id, json.loads(material.text))
  45. resp = json.loads(materials_in_category(child_id).text)
  46. assert(len(resp) == 0)
  47. resp = update_material(materialId, {
  48. 'name': '学习资料1更改版'
  49. })
  50. assert(resp.status_code == OK)
  51. delete_material(materialId)
  52. resp = get_material(materialId)
  53. assert(resp.status_code == NOT_FOUND)
  54. delete_category(root_id)
  55. resp = show_root_categories()
  56. root_categories = json.loads(resp.text)
  57. assert(len(root_categories) == 0)
  58. delete_category(child_id)
  59. def create_material(data):
  60. url = WEBSITE_PREFIX + '/admin/materials'
  61. return requests.post(url, json=data)
  62. def delete_material(materialId):
  63. url = WEBSITE_PREFIX + '/admin/materials/' + materialId
  64. return requests.delete(url)
  65. def get_material(materialId):
  66. url = WEBSITE_PREFIX + '/materials/' + materialId
  67. return requests.get(url)
  68. def like_material(materialId, user):
  69. url = WEBSITE_PREFIX + '/materials/' + materialId + '/like'
  70. return requests.post(url, json=user)
  71. def update_material(materialId, udpatedMaterial):
  72. url = WEBSITE_PREFIX + '/admin/materials/' + materialId
  73. return requests.put(url, json=udpatedMaterial)
  74. def show_root_categories():
  75. url = WEBSITE_PREFIX + '/categories'
  76. return requests.get(url)
  77. def show_root_question_categories():
  78. url = WEBSITE_PREFIX + '/q-categories'
  79. return requests.get(url)
  80. def create_category(name):
  81. url = WEBSITE_PREFIX + '/admin/materialCategories'
  82. data = {
  83. 'name': name
  84. }
  85. return requests.post(url, json=data)
  86. def create_question_category(name):
  87. url = WEBSITE_PREFIX + '/admin/qCategories'
  88. data = {
  89. 'name': name
  90. }
  91. return requests.post(url, json=data)
  92. def delete_category(categoryId):
  93. url = WEBSITE_PREFIX + '/admin/categories/' + categoryId
  94. return requests.delete(url)
  95. def delete_question_category(categoryId):
  96. url = WEBSITE_PREFIX + '/admin/q-categories/' + categoryId
  97. return requests.delete(url)
  98. def get_category(categoryId):
  99. url = WEBSITE_PREFIX + '/categories/' + categoryId
  100. return requests.get(url)
  101. def get_question_category(categoryId):
  102. url = WEBSITE_PREFIX + '/q-categories/' + categoryId
  103. return requests.get(url)
  104. def update_category(categoryId, updatedData):
  105. url = WEBSITE_PREFIX + '/admin/categories/' + categoryId
  106. return requests.put(url, json=updatedData)
  107. def update_question_category(categoryId, updatedData):
  108. url = WEBSITE_PREFIX + '/admin/q-categories/' + categoryId
  109. return requests.put(url, json=updatedData)
  110. def materials_in_category(categoryId):
  111. url = WEBSITE_PREFIX + '/categories/' + categoryId + '/materials'
  112. return requests.get(url)
  113. def questions_in_category(categoryId):
  114. url = WEBSITE_PREFIX + '/q-categories/' + categoryId + '/questions'
  115. return requests.get(url)
  116. def add_material_to_category(categoryId, material):
  117. url = WEBSITE_PREFIX + '/categories/' + categoryId + '/materials'
  118. return requests.post(url, json=material)
  119. def add_question_to_question_category(categoryId, question):
  120. url = WEBSITE_PREFIX + '/q-categories/' + categoryId + '/questions'
  121. return requests.post(url, json=question)
  122. def remove_material_from_category(categoryId, material):
  123. url = WEBSITE_PREFIX + '/categories/' + categoryId + '/materials'
  124. return requests.delete(url, json=material)
  125. def remove_question_from_question_category(categoryId, question):
  126. url = WEBSITE_PREFIX + '/q-categories/' + categoryId + '/questions'
  127. return requests.delete(url, json=question)