You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

952 lines
50 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import os
  2. from rest_framework import status
  3. from rest_framework.reverse import reverse
  4. from rest_framework.test import APITestCase
  5. from model_mommy import mommy
  6. from ..models import User, SequenceAnnotation, Document
  7. from ..models import DOCUMENT_CLASSIFICATION, SEQUENCE_LABELING, SEQ2SEQ
  8. from ..utils import PlainTextParser, CoNLLParser, JSONParser, CSVParser
  9. from ..exceptions import FileParseException
  10. DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
  11. class TestProjectListAPI(APITestCase):
  12. @classmethod
  13. def setUpTestData(cls):
  14. cls.main_project_member_name = 'project_member_name'
  15. cls.main_project_member_pass = 'project_member_pass'
  16. cls.sub_project_member_name = 'sub_project_member_name'
  17. cls.sub_project_member_pass = 'sub_project_member_pass'
  18. cls.super_user_name = 'super_user_name'
  19. cls.super_user_pass = 'super_user_pass'
  20. main_project_member = User.objects.create_user(username=cls.main_project_member_name,
  21. password=cls.main_project_member_pass)
  22. sub_project_member = User.objects.create_user(username=cls.sub_project_member_name,
  23. password=cls.sub_project_member_pass)
  24. # Todo: change super_user to project_admin.
  25. super_user = User.objects.create_superuser(username=cls.super_user_name,
  26. password=cls.super_user_pass,
  27. email='fizz@buzz.com')
  28. cls.main_project = mommy.make('server.TextClassificationProject', users=[main_project_member])
  29. cls.sub_project = mommy.make('server.TextClassificationProject', users=[sub_project_member])
  30. cls.url = reverse(viewname='project_list')
  31. cls.data = {'name': 'example', 'project_type': 'DocumentClassification',
  32. 'description': 'example', 'guideline': 'example',
  33. 'resourcetype': 'TextClassificationProject'}
  34. cls.num_project = main_project_member.projects.count()
  35. def test_returns_main_project_to_main_project_member(self):
  36. self.client.login(username=self.main_project_member_name,
  37. password=self.main_project_member_pass)
  38. response = self.client.get(self.url, format='json')
  39. project = response.data[0]
  40. num_project = len(response.data)
  41. self.assertEqual(num_project, self.num_project)
  42. self.assertEqual(project['id'], self.main_project.id)
  43. def test_do_not_return_main_project_to_sub_project_member(self):
  44. self.client.login(username=self.sub_project_member_name,
  45. password=self.sub_project_member_pass)
  46. response = self.client.get(self.url, format='json')
  47. project = response.data[0]
  48. num_project = len(response.data)
  49. self.assertEqual(num_project, self.num_project)
  50. self.assertNotEqual(project['id'], self.main_project.id)
  51. def test_allows_superuser_to_create_project(self):
  52. self.client.login(username=self.super_user_name,
  53. password=self.super_user_pass)
  54. response = self.client.post(self.url, format='json', data=self.data)
  55. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  56. def test_disallows_project_member_to_create_project(self):
  57. self.client.login(username=self.main_project_member_name,
  58. password=self.main_project_member_pass)
  59. response = self.client.post(self.url, format='json', data=self.data)
  60. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  61. class TestProjectDetailAPI(APITestCase):
  62. @classmethod
  63. def setUpTestData(cls):
  64. cls.project_member_name = 'project_member_name'
  65. cls.project_member_pass = 'project_member_pass'
  66. cls.non_project_member_name = 'non_project_member_name'
  67. cls.non_project_member_pass = 'non_project_member_pass'
  68. cls.super_user_name = 'super_user_name'
  69. cls.super_user_pass = 'super_user_pass'
  70. cls.project_member = User.objects.create_user(username=cls.project_member_name,
  71. password=cls.project_member_pass)
  72. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  73. password=cls.non_project_member_pass)
  74. # Todo: change super_user to project_admin.
  75. super_user = User.objects.create_superuser(username=cls.super_user_name,
  76. password=cls.super_user_pass,
  77. email='fizz@buzz.com')
  78. cls.main_project = mommy.make('server.TextClassificationProject', users=[cls.project_member, super_user])
  79. sub_project = mommy.make('server.TextClassificationProject', users=[non_project_member])
  80. cls.url = reverse(viewname='project_detail', args=[cls.main_project.id])
  81. cls.data = {'description': 'lorem'}
  82. def test_returns_main_project_detail_to_main_project_member(self):
  83. self.client.login(username=self.project_member_name,
  84. password=self.project_member_pass)
  85. response = self.client.get(self.url, format='json')
  86. self.assertEqual(response.data['id'], self.main_project.id)
  87. def test_do_not_return_main_project_to_sub_project_member(self):
  88. self.client.login(username=self.non_project_member_name,
  89. password=self.non_project_member_pass)
  90. response = self.client.get(self.url, format='json')
  91. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  92. def test_allows_superuser_to_update_project(self):
  93. self.client.login(username=self.super_user_name,
  94. password=self.super_user_pass)
  95. response = self.client.patch(self.url, format='json', data=self.data)
  96. self.assertEqual(response.data['description'], self.data['description'])
  97. def test_disallows_project_member_to_update_project(self):
  98. self.client.login(username=self.project_member_name,
  99. password=self.project_member_pass)
  100. response = self.client.patch(self.url, format='json', data=self.data)
  101. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  102. def test_allows_superuser_to_delete_project(self):
  103. self.client.login(username=self.super_user_name,
  104. password=self.super_user_pass)
  105. response = self.client.delete(self.url, format='json')
  106. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  107. def test_disallows_project_member_to_delete_project(self):
  108. self.client.login(username=self.project_member_name,
  109. password=self.project_member_pass)
  110. response = self.client.delete(self.url, format='json')
  111. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  112. class TestLabelListAPI(APITestCase):
  113. @classmethod
  114. def setUpTestData(cls):
  115. cls.project_member_name = 'project_member_name'
  116. cls.project_member_pass = 'project_member_pass'
  117. cls.non_project_member_name = 'non_project_member_name'
  118. cls.non_project_member_pass = 'non_project_member_pass'
  119. cls.super_user_name = 'super_user_name'
  120. cls.super_user_pass = 'super_user_pass'
  121. project_member = User.objects.create_user(username=cls.project_member_name,
  122. password=cls.project_member_pass)
  123. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  124. password=cls.non_project_member_pass)
  125. # Todo: change super_user to project_admin.
  126. super_user = User.objects.create_superuser(username=cls.super_user_name,
  127. password=cls.super_user_pass,
  128. email='fizz@buzz.com')
  129. cls.main_project = mommy.make('server.Project', users=[project_member, super_user])
  130. cls.main_project_label = mommy.make('server.Label', project=cls.main_project)
  131. sub_project = mommy.make('server.Project', users=[non_project_member])
  132. other_project = mommy.make('server.Project', users=[super_user])
  133. mommy.make('server.Label', project=sub_project)
  134. cls.url = reverse(viewname='label_list', args=[cls.main_project.id])
  135. cls.other_url = reverse(viewname='label_list', args=[other_project.id])
  136. cls.data = {'text': 'example'}
  137. def test_returns_labels_to_project_member(self):
  138. self.client.login(username=self.project_member_name,
  139. password=self.project_member_pass)
  140. response = self.client.get(self.url, format='json')
  141. self.assertEqual(response.status_code, status.HTTP_200_OK)
  142. def test_do_not_return_labels_to_non_project_member(self):
  143. self.client.login(username=self.non_project_member_name,
  144. password=self.non_project_member_pass)
  145. response = self.client.get(self.url, format='json')
  146. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  147. def test_do_not_return_labels_of_other_projects(self):
  148. self.client.login(username=self.project_member_name,
  149. password=self.project_member_pass)
  150. response = self.client.get(self.url, format='json')
  151. label = response.data[0]
  152. num_labels = len(response.data)
  153. self.assertEqual(num_labels, len(self.main_project.labels.all()))
  154. self.assertEqual(label['id'], self.main_project_label.id)
  155. def test_allows_superuser_to_create_label(self):
  156. self.client.login(username=self.super_user_name,
  157. password=self.super_user_pass)
  158. response = self.client.post(self.url, format='json', data=self.data)
  159. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  160. def test_can_create_multiple_labels_without_shortcut_key(self):
  161. self.client.login(username=self.super_user_name,
  162. password=self.super_user_pass)
  163. labels = [
  164. {'text': 'Ruby', 'prefix_key': None, 'suffix_key': None},
  165. {'text': 'PHP', 'prefix_key': None, 'suffix_key': None}
  166. ]
  167. for label in labels:
  168. response = self.client.post(self.url, format='json', data=label)
  169. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  170. def test_can_create_same_label_in_multiple_projects(self):
  171. self.client.login(username=self.super_user_name,
  172. password=self.super_user_pass)
  173. label = {'text': 'LOC', 'prefix_key': None, 'suffix_key': 'l'}
  174. response = self.client.post(self.url, format='json', data=label)
  175. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  176. response = self.client.post(self.other_url, format='json', data=label)
  177. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  178. def test_disallows_project_member_to_create_label(self):
  179. self.client.login(username=self.project_member_name,
  180. password=self.project_member_pass)
  181. response = self.client.post(self.url, format='json', data=self.data)
  182. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  183. class TestLabelDetailAPI(APITestCase):
  184. @classmethod
  185. def setUpTestData(cls):
  186. cls.project_member_name = 'project_member_name'
  187. cls.project_member_pass = 'project_member_pass'
  188. cls.non_project_member_name = 'non_project_member_name'
  189. cls.non_project_member_pass = 'non_project_member_pass'
  190. cls.super_user_name = 'super_user_name'
  191. cls.super_user_pass = 'super_user_pass'
  192. project_member = User.objects.create_user(username=cls.project_member_name,
  193. password=cls.project_member_pass)
  194. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  195. password=cls.non_project_member_pass)
  196. # Todo: change super_user to project_admin.
  197. super_user = User.objects.create_superuser(username=cls.super_user_name,
  198. password=cls.super_user_pass,
  199. email='fizz@buzz.com')
  200. project = mommy.make('server.Project', users=[project_member, super_user])
  201. cls.label = mommy.make('server.Label', project=project)
  202. cls.url = reverse(viewname='label_detail', args=[project.id, cls.label.id])
  203. cls.data = {'text': 'example'}
  204. def test_returns_label_to_project_member(self):
  205. self.client.login(username=self.project_member_name,
  206. password=self.project_member_pass)
  207. response = self.client.get(self.url, format='json')
  208. self.assertEqual(response.data['id'], self.label.id)
  209. def test_do_not_return_label_to_non_project_member(self):
  210. self.client.login(username=self.non_project_member_name,
  211. password=self.non_project_member_pass)
  212. response = self.client.get(self.url, format='json')
  213. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  214. def test_allows_superuser_to_update_label(self):
  215. self.client.login(username=self.super_user_name,
  216. password=self.super_user_pass)
  217. response = self.client.patch(self.url, format='json', data=self.data)
  218. self.assertEqual(response.data['text'], self.data['text'])
  219. def test_disallows_project_member_to_update_label(self):
  220. self.client.login(username=self.project_member_name,
  221. password=self.project_member_pass)
  222. response = self.client.patch(self.url, format='json', data=self.data)
  223. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  224. def test_allows_superuser_to_delete_label(self):
  225. self.client.login(username=self.super_user_name,
  226. password=self.super_user_pass)
  227. response = self.client.delete(self.url, format='json')
  228. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  229. def test_disallows_project_member_to_delete_label(self):
  230. self.client.login(username=self.project_member_name,
  231. password=self.project_member_pass)
  232. response = self.client.delete(self.url, format='json')
  233. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  234. class TestDocumentListAPI(APITestCase):
  235. @classmethod
  236. def setUpTestData(cls):
  237. cls.project_member_name = 'project_member_name'
  238. cls.project_member_pass = 'project_member_pass'
  239. cls.non_project_member_name = 'non_project_member_name'
  240. cls.non_project_member_pass = 'non_project_member_pass'
  241. cls.super_user_name = 'super_user_name'
  242. cls.super_user_pass = 'super_user_pass'
  243. project_member = User.objects.create_user(username=cls.project_member_name,
  244. password=cls.project_member_pass)
  245. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  246. password=cls.non_project_member_pass)
  247. # Todo: change super_user to project_admin.
  248. super_user = User.objects.create_superuser(username=cls.super_user_name,
  249. password=cls.super_user_pass,
  250. email='fizz@buzz.com')
  251. cls.main_project = mommy.make('server.TextClassificationProject', users=[project_member, super_user])
  252. mommy.make('server.Document', project=cls.main_project)
  253. sub_project = mommy.make('server.TextClassificationProject', users=[non_project_member])
  254. mommy.make('server.Document', project=sub_project)
  255. cls.url = reverse(viewname='doc_list', args=[cls.main_project.id])
  256. cls.data = {'text': 'example'}
  257. def test_returns_docs_to_project_member(self):
  258. self.client.login(username=self.project_member_name,
  259. password=self.project_member_pass)
  260. response = self.client.get(self.url, format='json')
  261. self.assertEqual(response.status_code, status.HTTP_200_OK)
  262. def test_do_not_return_docs_to_non_project_member(self):
  263. self.client.login(username=self.non_project_member_name,
  264. password=self.non_project_member_pass)
  265. response = self.client.get(self.url, format='json')
  266. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  267. def test_do_not_return_docs_of_other_projects(self):
  268. self.client.login(username=self.project_member_name,
  269. password=self.project_member_pass)
  270. response = self.client.get(self.url, format='json')
  271. self.assertEqual(response.data['count'], self.main_project.documents.count())
  272. def test_allows_superuser_to_create_doc(self):
  273. self.client.login(username=self.super_user_name,
  274. password=self.super_user_pass)
  275. response = self.client.post(self.url, format='json', data=self.data)
  276. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  277. def test_disallows_project_member_to_create_doc(self):
  278. self.client.login(username=self.project_member_name,
  279. password=self.project_member_pass)
  280. response = self.client.post(self.url, format='json', data=self.data)
  281. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  282. class TestDocumentDetailAPI(APITestCase):
  283. @classmethod
  284. def setUpTestData(cls):
  285. cls.project_member_name = 'project_member_name'
  286. cls.project_member_pass = 'project_member_pass'
  287. cls.non_project_member_name = 'non_project_member_name'
  288. cls.non_project_member_pass = 'non_project_member_pass'
  289. cls.super_user_name = 'super_user_name'
  290. cls.super_user_pass = 'super_user_pass'
  291. project_member = User.objects.create_user(username=cls.project_member_name,
  292. password=cls.project_member_pass)
  293. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  294. password=cls.non_project_member_pass)
  295. # Todo: change super_user to project_admin.
  296. super_user = User.objects.create_superuser(username=cls.super_user_name,
  297. password=cls.super_user_pass,
  298. email='fizz@buzz.com')
  299. project = mommy.make('server.TextClassificationProject', users=[project_member, super_user])
  300. cls.doc = mommy.make('server.Document', project=project)
  301. cls.url = reverse(viewname='doc_detail', args=[project.id, cls.doc.id])
  302. cls.data = {'text': 'example'}
  303. def test_returns_doc_to_project_member(self):
  304. self.client.login(username=self.project_member_name,
  305. password=self.project_member_pass)
  306. response = self.client.get(self.url, format='json')
  307. self.assertEqual(response.data['id'], self.doc.id)
  308. def test_do_not_return_doc_to_non_project_member(self):
  309. self.client.login(username=self.non_project_member_name,
  310. password=self.non_project_member_pass)
  311. response = self.client.get(self.url, format='json')
  312. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  313. def test_allows_superuser_to_update_doc(self):
  314. self.client.login(username=self.super_user_name,
  315. password=self.super_user_pass)
  316. response = self.client.patch(self.url, format='json', data=self.data)
  317. self.assertEqual(response.data['text'], self.data['text'])
  318. def test_disallows_project_member_to_update_doc(self):
  319. self.client.login(username=self.project_member_name,
  320. password=self.project_member_pass)
  321. response = self.client.patch(self.url, format='json', data=self.data)
  322. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  323. def test_allows_superuser_to_delete_doc(self):
  324. self.client.login(username=self.super_user_name,
  325. password=self.super_user_pass)
  326. response = self.client.delete(self.url, format='json')
  327. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  328. def test_disallows_project_member_to_delete_doc(self):
  329. self.client.login(username=self.project_member_name,
  330. password=self.project_member_pass)
  331. response = self.client.delete(self.url, format='json')
  332. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  333. class TestAnnotationListAPI(APITestCase):
  334. @classmethod
  335. def setUpTestData(cls):
  336. cls.project_member_name = 'project_member_name'
  337. cls.project_member_pass = 'project_member_pass'
  338. cls.another_project_member_name = 'another_project_member_name'
  339. cls.another_project_member_pass = 'another_project_member_pass'
  340. cls.non_project_member_name = 'non_project_member_name'
  341. cls.non_project_member_pass = 'non_project_member_pass'
  342. project_member = User.objects.create_user(username=cls.project_member_name,
  343. password=cls.project_member_pass)
  344. another_project_member = User.objects.create_user(username=cls.another_project_member_name,
  345. password=cls.another_project_member_pass)
  346. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  347. password=cls.non_project_member_pass)
  348. main_project = mommy.make('server.SequenceLabelingProject', users=[project_member, another_project_member])
  349. main_project_label = mommy.make('server.Label', project=main_project)
  350. main_project_doc = mommy.make('server.Document', project=main_project)
  351. mommy.make('server.SequenceAnnotation', document=main_project_doc, user=project_member)
  352. mommy.make('server.SequenceAnnotation', document=main_project_doc, user=another_project_member)
  353. sub_project = mommy.make('server.SequenceLabelingProject', users=[non_project_member])
  354. sub_project_doc = mommy.make('server.Document', project=sub_project)
  355. mommy.make('server.SequenceAnnotation', document=sub_project_doc)
  356. cls.url = reverse(viewname='annotation_list', args=[main_project.id, main_project_doc.id])
  357. cls.post_data = {'start_offset': 0, 'end_offset': 1, 'label': main_project_label.id}
  358. cls.num_entity_of_project_member = SequenceAnnotation.objects.filter(document=main_project_doc,
  359. user=project_member).count()
  360. def test_returns_annotations_to_project_member(self):
  361. self.client.login(username=self.project_member_name,
  362. password=self.project_member_pass)
  363. response = self.client.get(self.url, format='json')
  364. self.assertEqual(response.status_code, status.HTTP_200_OK)
  365. def test_do_not_return_annotations_to_non_project_member(self):
  366. self.client.login(username=self.non_project_member_name,
  367. password=self.non_project_member_pass)
  368. response = self.client.get(self.url, format='json')
  369. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  370. def test_do_not_return_annotations_of_another_project_member(self):
  371. self.client.login(username=self.project_member_name,
  372. password=self.project_member_pass)
  373. response = self.client.get(self.url, format='json')
  374. self.assertEqual(len(response.data), self.num_entity_of_project_member)
  375. def test_allows_project_member_to_create_annotation(self):
  376. self.client.login(username=self.project_member_name,
  377. password=self.project_member_pass)
  378. response = self.client.post(self.url, format='json', data=self.post_data)
  379. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  380. def test_disallows_non_project_member_to_create_annotation(self):
  381. self.client.login(username=self.non_project_member_name,
  382. password=self.non_project_member_pass)
  383. response = self.client.post(self.url, format='json', data=self.post_data)
  384. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  385. class TestAnnotationDetailAPI(APITestCase):
  386. @classmethod
  387. def setUpTestData(cls):
  388. cls.project_member_name = 'project_member_name'
  389. cls.project_member_pass = 'project_member_pass'
  390. cls.another_project_member_name = 'another_project_member_name'
  391. cls.another_project_member_pass = 'another_project_member_pass'
  392. cls.non_project_member_name = 'non_project_member_name'
  393. cls.non_project_member_pass = 'non_project_member_pass'
  394. project_member = User.objects.create_user(username=cls.project_member_name,
  395. password=cls.project_member_pass)
  396. another_project_member = User.objects.create_user(username=cls.another_project_member_name,
  397. password=cls.another_project_member_pass)
  398. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  399. password=cls.non_project_member_pass)
  400. main_project = mommy.make('server.SequenceLabelingProject',
  401. users=[project_member, another_project_member])
  402. main_project_doc = mommy.make('server.Document', project=main_project)
  403. main_project_entity = mommy.make('server.SequenceAnnotation',
  404. document=main_project_doc, user=project_member)
  405. another_entity = mommy.make('server.SequenceAnnotation',
  406. document=main_project_doc, user=another_project_member)
  407. sub_project = mommy.make('server.SequenceLabelingProject', users=[non_project_member])
  408. sub_project_doc = mommy.make('server.Document', project=sub_project)
  409. mommy.make('server.SequenceAnnotation', document=sub_project_doc)
  410. cls.url = reverse(viewname='annotation_detail', args=[main_project.id,
  411. main_project_doc.id,
  412. main_project_entity.id])
  413. cls.another_url = reverse(viewname='annotation_detail', args=[main_project.id,
  414. main_project_doc.id,
  415. another_entity.id])
  416. cls.post_data = {'start_offset': 0, 'end_offset': 10}
  417. def test_returns_annotation_to_project_member(self):
  418. self.client.login(username=self.project_member_name,
  419. password=self.project_member_pass)
  420. response = self.client.get(self.url, format='json')
  421. self.assertEqual(response.status_code, status.HTTP_200_OK)
  422. def test_do_not_return_annotation_to_non_project_member(self):
  423. self.client.login(username=self.non_project_member_name,
  424. password=self.non_project_member_pass)
  425. response = self.client.get(self.url, format='json')
  426. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  427. def test_do_not_return_annotation_by_another_project_member(self):
  428. self.client.login(username=self.project_member_name,
  429. password=self.project_member_pass)
  430. response = self.client.get(self.another_url, format='json')
  431. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  432. def test_allows_project_member_to_update_annotation(self):
  433. self.client.login(username=self.project_member_name,
  434. password=self.project_member_pass)
  435. response = self.client.patch(self.url, format='json', data=self.post_data)
  436. self.assertEqual(response.status_code, status.HTTP_200_OK)
  437. def test_disallows_non_project_member_to_update_annotation(self):
  438. self.client.login(username=self.non_project_member_name,
  439. password=self.non_project_member_pass)
  440. response = self.client.patch(self.url, format='json', data=self.post_data)
  441. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  442. def test_disallows_project_member_to_update_annotation_of_another_member(self):
  443. self.client.login(username=self.project_member_name,
  444. password=self.project_member_pass)
  445. response = self.client.patch(self.another_url, format='json', data=self.post_data)
  446. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  447. def test_allows_project_member_to_delete_annotation(self):
  448. self.client.login(username=self.project_member_name,
  449. password=self.project_member_pass)
  450. response = self.client.delete(self.url, format='json')
  451. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  452. def test_disallows_project_member_to_delete_annotation(self):
  453. self.client.login(username=self.non_project_member_name,
  454. password=self.non_project_member_pass)
  455. response = self.client.delete(self.url, format='json')
  456. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  457. def test_disallows_project_member_to_delete_annotation_of_another_member(self):
  458. self.client.login(username=self.project_member_name,
  459. password=self.project_member_pass)
  460. response = self.client.delete(self.another_url, format='json', data=self.post_data)
  461. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  462. class TestSearch(APITestCase):
  463. @classmethod
  464. def setUpTestData(cls):
  465. cls.project_member_name = 'project_member_name'
  466. cls.project_member_pass = 'project_member_pass'
  467. cls.non_project_member_name = 'non_project_member_name'
  468. cls.non_project_member_pass = 'non_project_member_pass'
  469. project_member = User.objects.create_user(username=cls.project_member_name,
  470. password=cls.project_member_pass)
  471. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  472. password=cls.non_project_member_pass)
  473. cls.main_project = mommy.make('server.TextClassificationProject', users=[project_member])
  474. cls.search_term = 'example'
  475. doc1 = mommy.make('server.Document', text=cls.search_term, project=cls.main_project)
  476. doc2 = mommy.make('server.Document', text='Lorem', project=cls.main_project)
  477. label1 = mommy.make('server.Label', project=cls.main_project)
  478. label2 = mommy.make('server.Label', project=cls.main_project)
  479. mommy.make('server.SequenceAnnotation', document=doc1, user=project_member, label=label1)
  480. mommy.make('server.SequenceAnnotation', document=doc2, user=project_member, label=label2)
  481. sub_project = mommy.make('server.TextClassificationProject', users=[non_project_member])
  482. mommy.make('server.Document', text=cls.search_term, project=sub_project)
  483. cls.url = reverse(viewname='doc_list', args=[cls.main_project.id])
  484. cls.data = {'q': cls.search_term}
  485. def test_can_filter_doc_by_term(self):
  486. self.client.login(username=self.project_member_name,
  487. password=self.project_member_pass)
  488. response = self.client.get(self.url, format='json', data=self.data)
  489. count = Document.objects.filter(text__contains=self.search_term,
  490. project=self.main_project).count()
  491. self.assertEqual(response.data['count'], count)
  492. def test_can_order_doc_by_created_at_ascending(self):
  493. params = {'ordering': 'created_at'}
  494. self.client.login(username=self.project_member_name,
  495. password=self.project_member_pass)
  496. response = self.client.get(self.url, format='json', data=params)
  497. docs = Document.objects.filter(project=self.main_project).order_by('created_at').values()
  498. for d1, d2 in zip(response.data['results'], docs):
  499. self.assertEqual(d1['id'], d2['id'])
  500. def test_can_order_doc_by_created_at_descending(self):
  501. params = {'ordering': '-created_at'}
  502. self.client.login(username=self.project_member_name,
  503. password=self.project_member_pass)
  504. response = self.client.get(self.url, format='json', data=params)
  505. docs = Document.objects.filter(project=self.main_project).order_by('-created_at').values()
  506. for d1, d2 in zip(response.data['results'], docs):
  507. self.assertEqual(d1['id'], d2['id'])
  508. def test_can_order_doc_by_annotation_updated_at_ascending(self):
  509. params = {'ordering': 'seq_annotations__updated_at'}
  510. self.client.login(username=self.project_member_name,
  511. password=self.project_member_pass)
  512. response = self.client.get(self.url, format='json', data=params)
  513. docs = Document.objects.filter(project=self.main_project).order_by('seq_annotations__updated_at').values()
  514. for d1, d2 in zip(response.data['results'], docs):
  515. self.assertEqual(d1['id'], d2['id'])
  516. def test_can_order_doc_by_annotation_updated_at_descending(self):
  517. params = {'ordering': '-seq_annotations__updated_at'}
  518. self.client.login(username=self.project_member_name,
  519. password=self.project_member_pass)
  520. response = self.client.get(self.url, format='json', data=params)
  521. docs = Document.objects.filter(project=self.main_project).order_by('-seq_annotations__updated_at').values()
  522. for d1, d2 in zip(response.data['results'], docs):
  523. self.assertEqual(d1['id'], d2['id'])
  524. class TestFilter(APITestCase):
  525. @classmethod
  526. def setUpTestData(cls):
  527. cls.project_member_name = 'project_member_name'
  528. cls.project_member_pass = 'project_member_pass'
  529. project_member = User.objects.create_user(username=cls.project_member_name,
  530. password=cls.project_member_pass)
  531. cls.main_project = mommy.make('server.SequenceLabelingProject', users=[project_member])
  532. cls.label1 = mommy.make('server.Label', project=cls.main_project)
  533. cls.label2 = mommy.make('server.Label', project=cls.main_project)
  534. doc1 = mommy.make('server.Document', project=cls.main_project)
  535. doc2 = mommy.make('server.Document', project=cls.main_project)
  536. doc3 = mommy.make('server.Document', project=cls.main_project)
  537. mommy.make('server.SequenceAnnotation', document=doc1, user=project_member, label=cls.label1)
  538. mommy.make('server.SequenceAnnotation', document=doc2, user=project_member, label=cls.label2)
  539. cls.url = reverse(viewname='doc_list', args=[cls.main_project.id])
  540. cls.params = {'seq_annotations__label__id': cls.label1.id}
  541. def test_can_filter_by_label(self):
  542. self.client.login(username=self.project_member_name,
  543. password=self.project_member_pass)
  544. response = self.client.get(self.url, format='json', data=self.params)
  545. docs = Document.objects.filter(project=self.main_project,
  546. seq_annotations__label__id=self.label1.id).values()
  547. for d1, d2 in zip(response.data['results'], docs):
  548. self.assertEqual(d1['id'], d2['id'])
  549. def test_can_filter_doc_with_annotation(self):
  550. params = {'seq_annotations__isnull': False}
  551. self.client.login(username=self.project_member_name,
  552. password=self.project_member_pass)
  553. response = self.client.get(self.url, format='json', data=params)
  554. docs = Document.objects.filter(project=self.main_project, seq_annotations__isnull=False).values()
  555. self.assertEqual(response.data['count'], docs.count())
  556. for d1, d2 in zip(response.data['results'], docs):
  557. self.assertEqual(d1['id'], d2['id'])
  558. def test_can_filter_doc_without_anotation(self):
  559. params = {'seq_annotations__isnull': True}
  560. self.client.login(username=self.project_member_name,
  561. password=self.project_member_pass)
  562. response = self.client.get(self.url, format='json', data=params)
  563. docs = Document.objects.filter(project=self.main_project, seq_annotations__isnull=True).values()
  564. self.assertEqual(response.data['count'], docs.count())
  565. for d1, d2 in zip(response.data['results'], docs):
  566. self.assertEqual(d1['id'], d2['id'])
  567. class TestUploader(APITestCase):
  568. @classmethod
  569. def setUpTestData(cls):
  570. cls.super_user_name = 'super_user_name'
  571. cls.super_user_pass = 'super_user_pass'
  572. # Todo: change super_user to project_admin.
  573. super_user = User.objects.create_superuser(username=cls.super_user_name,
  574. password=cls.super_user_pass,
  575. email='fizz@buzz.com')
  576. cls.classification_project = mommy.make('server.TextClassificationProject',
  577. users=[super_user], project_type=DOCUMENT_CLASSIFICATION)
  578. cls.labeling_project = mommy.make('server.SequenceLabelingProject',
  579. users=[super_user], project_type=SEQUENCE_LABELING)
  580. cls.seq2seq_project = mommy.make('server.Seq2seqProject', users=[super_user], project_type=SEQ2SEQ)
  581. cls.classification_url = reverse(viewname='doc_uploader', args=[cls.classification_project.id])
  582. cls.classification_labels_url = reverse(viewname='label_list', args=[cls.classification_project.id])
  583. cls.labeling_url = reverse(viewname='doc_uploader', args=[cls.labeling_project.id])
  584. cls.labeling_labels_url = reverse(viewname='label_list', args=[cls.labeling_project.id])
  585. cls.seq2seq_url = reverse(viewname='doc_uploader', args=[cls.seq2seq_project.id])
  586. def setUp(self):
  587. self.client.login(username=self.super_user_name,
  588. password=self.super_user_pass)
  589. def upload_test_helper(self, url, filename, format, expected_status):
  590. with open(os.path.join(DATA_DIR, filename)) as f:
  591. response = self.client.post(url, data={'file': f, 'format': format})
  592. self.assertEqual(response.status_code, expected_status)
  593. def label_test_helper(self, url, expected_labels, expected_label_keys):
  594. expected_keys = {key for label in expected_labels for key in label}
  595. response = self.client.get(url).json()
  596. actual_labels = [{key: value for (key, value) in label.items() if key in expected_keys}
  597. for label in response]
  598. self.assertCountEqual(actual_labels, expected_labels)
  599. for label in response:
  600. for expected_label_key in expected_label_keys:
  601. self.assertIsNotNone(label.get(expected_label_key))
  602. def test_can_upload_conll_format_file(self):
  603. self.upload_test_helper(url=self.labeling_url,
  604. filename='labeling.conll',
  605. format='conll',
  606. expected_status=status.HTTP_201_CREATED)
  607. def test_cannot_upload_wrong_conll_format_file(self):
  608. self.upload_test_helper(url=self.labeling_url,
  609. filename='labeling.invalid.conll',
  610. format='conll',
  611. expected_status=status.HTTP_400_BAD_REQUEST)
  612. def test_can_upload_classification_csv(self):
  613. self.upload_test_helper(url=self.classification_url,
  614. filename='example.csv',
  615. format='csv',
  616. expected_status=status.HTTP_201_CREATED)
  617. def test_can_upload_seq2seq_csv(self):
  618. self.upload_test_helper(url=self.seq2seq_url,
  619. filename='example.csv',
  620. format='csv',
  621. expected_status=status.HTTP_201_CREATED)
  622. def test_cannot_upload_csv_file_does_not_match_column_and_row(self):
  623. self.upload_test_helper(url=self.classification_url,
  624. filename='example.invalid.1.csv',
  625. format='csv',
  626. expected_status=status.HTTP_400_BAD_REQUEST)
  627. def test_cannot_upload_csv_file_has_too_many_columns(self):
  628. self.upload_test_helper(url=self.classification_url,
  629. filename='example.invalid.2.csv',
  630. format='csv',
  631. expected_status=status.HTTP_400_BAD_REQUEST)
  632. def test_can_upload_classification_jsonl(self):
  633. self.upload_test_helper(url=self.classification_url,
  634. filename='classification.jsonl',
  635. format='json',
  636. expected_status=status.HTTP_201_CREATED)
  637. self.label_test_helper(
  638. url=self.classification_labels_url,
  639. expected_labels=[
  640. {'text': 'positive', 'suffix_key': 'p', 'prefix_key': None},
  641. {'text': 'negative', 'suffix_key': 'n', 'prefix_key': None},
  642. {'text': 'neutral', 'suffix_key': 'n', 'prefix_key': 'ctrl'},
  643. ],
  644. expected_label_keys=[
  645. 'background_color',
  646. 'text_color',
  647. ])
  648. def test_can_upload_labeling_jsonl(self):
  649. self.upload_test_helper(url=self.labeling_url,
  650. filename='labeling.jsonl',
  651. format='json',
  652. expected_status=status.HTTP_201_CREATED)
  653. self.label_test_helper(
  654. url=self.labeling_labels_url,
  655. expected_labels=[
  656. {'text': 'LOC', 'suffix_key': 'l', 'prefix_key': None},
  657. {'text': 'ORG', 'suffix_key': 'o', 'prefix_key': None},
  658. {'text': 'PER', 'suffix_key': 'p', 'prefix_key': None},
  659. ],
  660. expected_label_keys=[
  661. 'background_color',
  662. 'text_color',
  663. ])
  664. def test_can_upload_seq2seq_jsonl(self):
  665. self.upload_test_helper(url=self.seq2seq_url,
  666. filename='seq2seq.jsonl',
  667. format='json',
  668. expected_status=status.HTTP_201_CREATED)
  669. def test_can_upload_plain_text(self):
  670. self.upload_test_helper(url=self.classification_url,
  671. filename='example.txt',
  672. format='plain',
  673. expected_status=status.HTTP_201_CREATED)
  674. def test_can_upload_data_without_label(self):
  675. self.upload_test_helper(url=self.classification_url,
  676. filename='example.jsonl',
  677. format='json',
  678. expected_status=status.HTTP_201_CREATED)
  679. class TestParser(APITestCase):
  680. def parser_helper(self, filename, parser, include_label=True):
  681. with open(os.path.join(DATA_DIR, filename), mode='rb') as f:
  682. result = parser.parse(f)
  683. for data in result:
  684. for r in data:
  685. self.assertIn('text', r)
  686. if include_label:
  687. self.assertIn('labels', r)
  688. def test_give_valid_data_to_conll_parser(self):
  689. self.parser_helper(filename='labeling.conll', parser=CoNLLParser())
  690. def test_plain_parser(self):
  691. self.parser_helper(filename='example.txt', parser=PlainTextParser(), include_label=False)
  692. def test_give_invalid_data_to_conll_parser(self):
  693. with self.assertRaises(FileParseException):
  694. self.parser_helper(filename='labeling.invalid.conll',
  695. parser=CoNLLParser())
  696. def test_give_classification_data_to_csv_parser(self):
  697. self.parser_helper(filename='example.csv', parser=CSVParser())
  698. def test_give_seq2seq_data_to_csv_parser(self):
  699. self.parser_helper(filename='example.csv', parser=CSVParser())
  700. def test_give_classification_data_to_json_parser(self):
  701. self.parser_helper(filename='classification.jsonl', parser=JSONParser())
  702. def test_give_labeling_data_to_json_parser(self):
  703. self.parser_helper(filename='labeling.jsonl', parser=JSONParser())
  704. def test_give_seq2seq_data_to_json_parser(self):
  705. self.parser_helper(filename='seq2seq.jsonl', parser=JSONParser())
  706. def test_give_data_without_label_to_json_parser(self):
  707. self.parser_helper(filename='example.jsonl', parser=JSONParser(), include_label=False)
  708. class TestDownloader(APITestCase):
  709. @classmethod
  710. def setUpTestData(cls):
  711. cls.super_user_name = 'super_user_name'
  712. cls.super_user_pass = 'super_user_pass'
  713. # Todo: change super_user to project_admin.
  714. super_user = User.objects.create_superuser(username=cls.super_user_name,
  715. password=cls.super_user_pass,
  716. email='fizz@buzz.com')
  717. cls.classification_project = mommy.make('server.TextClassificationProject',
  718. users=[super_user], project_type=DOCUMENT_CLASSIFICATION)
  719. cls.labeling_project = mommy.make('server.SequenceLabelingProject',
  720. users=[super_user], project_type=SEQUENCE_LABELING)
  721. cls.seq2seq_project = mommy.make('server.Seq2seqProject', users=[super_user], project_type=SEQ2SEQ)
  722. cls.classification_url = reverse(viewname='doc_downloader', args=[cls.classification_project.id])
  723. cls.labeling_url = reverse(viewname='doc_downloader', args=[cls.labeling_project.id])
  724. cls.seq2seq_url = reverse(viewname='doc_downloader', args=[cls.seq2seq_project.id])
  725. def setUp(self):
  726. self.client.login(username=self.super_user_name,
  727. password=self.super_user_pass)
  728. def download_test_helper(self, url, format, expected_status):
  729. response = self.client.get(url, data={'q': format})
  730. self.assertEqual(response.status_code, expected_status)
  731. def test_cannot_download_conll_format_file(self):
  732. self.download_test_helper(url=self.labeling_url,
  733. format='conll',
  734. expected_status=status.HTTP_400_BAD_REQUEST)
  735. def test_can_download_classification_csv(self):
  736. self.download_test_helper(url=self.classification_url,
  737. format='csv',
  738. expected_status=status.HTTP_200_OK)
  739. def test_can_download_labeling_csv(self):
  740. self.download_test_helper(url=self.labeling_url,
  741. format='csv',
  742. expected_status=status.HTTP_200_OK)
  743. def test_can_download_seq2seq_csv(self):
  744. self.download_test_helper(url=self.seq2seq_url,
  745. format='csv',
  746. expected_status=status.HTTP_200_OK)
  747. def test_can_download_classification_jsonl(self):
  748. self.download_test_helper(url=self.classification_url,
  749. format='json',
  750. expected_status=status.HTTP_200_OK)
  751. def test_can_download_labeling_jsonl(self):
  752. self.download_test_helper(url=self.labeling_url,
  753. format='json',
  754. expected_status=status.HTTP_200_OK)
  755. def test_can_download_seq2seq_jsonl(self):
  756. self.download_test_helper(url=self.seq2seq_url,
  757. format='json',
  758. expected_status=status.HTTP_200_OK)
  759. def test_can_download_plain_text(self):
  760. self.download_test_helper(url=self.classification_url,
  761. format='plain',
  762. expected_status=status.HTTP_400_BAD_REQUEST)
  763. class TestStatisticsAPI(APITestCase):
  764. @classmethod
  765. def setUpTestData(cls):
  766. cls.super_user_name = 'super_user_name'
  767. cls.super_user_pass = 'super_user_pass'
  768. # Todo: change super_user to project_admin.
  769. super_user = User.objects.create_superuser(username=cls.super_user_name,
  770. password=cls.super_user_pass,
  771. email='fizz@buzz.com')
  772. main_project = mommy.make('server.TextClassificationProject', users=[super_user])
  773. doc1 = mommy.make('server.Document', project=main_project)
  774. doc2 = mommy.make('server.Document', project=main_project)
  775. mommy.make('DocumentAnnotation', document=doc1)
  776. cls.url = reverse(viewname='statistics', args=[main_project.id])
  777. cls.doc = Document.objects.filter(project=main_project)
  778. def test_returns_exact_progress(self):
  779. self.client.login(username=self.super_user_name,
  780. password=self.super_user_pass)
  781. response = self.client.get(self.url, format='json')
  782. total = self.doc.count()
  783. remaining = self.doc.filter(doc_annotations__isnull=True).count()
  784. self.assertEqual(response.data['total'], total)
  785. self.assertEqual(response.data['remaining'], remaining)
  786. def test_returns_user_count(self):
  787. self.client.login(username=self.super_user_name,
  788. password=self.super_user_pass)
  789. response = self.client.get(self.url, format='json')
  790. self.assertIn('label', response.data)
  791. self.assertIsInstance(response.data['label'], dict)
  792. def test_returns_label_count(self):
  793. self.client.login(username=self.super_user_name,
  794. password=self.super_user_pass)
  795. response = self.client.get(self.url, format='json')
  796. self.assertIn('user', response.data)
  797. self.assertIsInstance(response.data['user'], dict)