category_test.py 5.0 KB

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