project_tests.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import uuid
  2. from flask import url_for
  3. from pycs.database.Collection import Collection
  4. from pycs.database.File import File
  5. from pycs.database.Label import Label
  6. from pycs.database.Model import Model
  7. from pycs.database.Project import Project
  8. from tests.base import BaseTestCase
  9. class _BaseProjectTests(BaseTestCase):
  10. def setupModels(self):
  11. model = Model.new(
  12. commit=False,
  13. name="TestModel",
  14. description="Model for a test case",
  15. root_folder="models/fixed_model",
  16. )
  17. model.supports = ["labeled-image"]
  18. model.flush()
  19. self.model = model
  20. class ProjectCreationTests(_BaseProjectTests):
  21. def setUp(self):
  22. super().setUp()
  23. self.url = url_for("create_project")
  24. def test_project_creation_without_name(self):
  25. self.assertEqual(0, Project.query.count())
  26. self.post(self.url, json=dict(
  27. # name="Some Project",
  28. description="Some description",
  29. model=self.model.id,
  30. label=None,
  31. external=None,
  32. ),
  33. status_code=400,
  34. )
  35. self.assertEqual(0, Project.query.count())
  36. def test_project_creation_without_description(self):
  37. self.assertEqual(0, Project.query.count())
  38. self.post(self.url, json=dict(
  39. name="Some Project",
  40. # description="Some description",
  41. model=self.model.id,
  42. label=None,
  43. external=None,
  44. ),
  45. status_code=400,
  46. )
  47. self.assertEqual(0, Project.query.count())
  48. def test_project_creation(self):
  49. self.assertEqual(0, Project.query.count())
  50. self.post(self.url, json=dict(
  51. name="Some Project",
  52. description="Some description",
  53. model=self.model.id,
  54. label=None,
  55. external=None,
  56. )
  57. )
  58. self.assertEqual(1, Project.query.count())
  59. project = Project.query.first()
  60. self.assertIsNotNone(project)
  61. self.assertIsNotNone(project.model)
  62. self.assertIsNone(project.label_provider)
  63. class ProjectDeletionTests(_BaseProjectTests):
  64. def setupModels(self):
  65. super().setupModels()
  66. self.project = Project.new(
  67. name="test_project",
  68. description="Project for a test case",
  69. model=self.model,
  70. root_folder="project_folder",
  71. external_data=False,
  72. data_folder="project_folder/data",)
  73. @property
  74. def url(self):
  75. return url_for("remove_project",
  76. project_id=self.project.id)
  77. def test_project_deletion(self):
  78. self.assertEqual(1, Project.query.count())
  79. self.post(self.url, json=dict(remove=True))
  80. self.assertEqual(0, Project.query.count())
  81. def test_project_deletion_without_flag(self):
  82. self.assertEqual(1, Project.query.count())
  83. self.post(self.url, json=dict(), status_code=400)
  84. self.assertEqual(1, Project.query.count())
  85. self.post(self.url, json=dict(remove=False), status_code=400)
  86. self.assertEqual(1, Project.query.count())
  87. def test_project_deletion_with_labels(self):
  88. self.assertEqual(1, Project.query.count())
  89. self.assertEqual(0, Label.query.count())
  90. for i in range(1, 11):
  91. self.project.create_label(name=f"Label_{i}")
  92. self.assertEqual(10, Label.query.count())
  93. self.post(self.url, json=dict(remove=True))
  94. self.assertEqual(0, Project.query.count())
  95. self.assertEqual(0, Label.query.count())
  96. class ProjectListTests(_BaseProjectTests):
  97. def test_list_projects(self):
  98. self.assertEqual(0, Project.query.count())
  99. for i in range(1, 11):
  100. Project.new(
  101. name=f"TestProject{i}",
  102. description=f"Project for a test case #{i}",
  103. model=self.model,
  104. root_folder=f"project_folder{i}",
  105. external_data=False,
  106. data_folder=f"project_folder{i}/data",
  107. )
  108. self.assertEqual(10, Project.query.count())
  109. response = self.get(url_for("list_projects"))
  110. self.assertTrue(response.is_json)
  111. content = response.json
  112. self.assertEqual(10, len(content))
  113. for entry in content:
  114. project = Project.query.get(entry["id"])
  115. self.assertIsNotNone(project)
  116. self.assertDictEqual(entry, project.serialize())
  117. def test_list_project_collections(self):
  118. project = Project.new(
  119. name="TestProject",
  120. description="Project for a test case",
  121. model=self.model,
  122. root_folder="project_folder",
  123. external_data=False,
  124. data_folder="project_folder/data",
  125. )
  126. self.assertEqual(0, Collection.query.count())
  127. for i in range(1, 11):
  128. project.create_collection(
  129. reference=f"collection_{i}",
  130. name=f"Some collection {i}",
  131. description=f"A description {i}",
  132. position=i,
  133. autoselect=i == 1
  134. )
  135. self.assertEqual(10, Collection.query.count())
  136. response = self.get(url_for("list_collections",
  137. project_id=project.id))
  138. self.assertTrue(response.is_json)
  139. content = response.json
  140. self.assertEqual(10, len(content))
  141. for entry in content:
  142. collection = Collection.query.get(entry["id"])
  143. self.assertIsNotNone(collection)
  144. self.assertDictEqual(entry, collection.serialize())
  145. def test_list_all_files(self):
  146. project = Project.new(
  147. name="TestProject",
  148. description="Project for a test case",
  149. model=self.model,
  150. root_folder="project_folder",
  151. external_data=False,
  152. data_folder="project_folder/data",
  153. )
  154. self.assertEqual(0, File.query.count())
  155. files = []
  156. for i in range(1, 11):
  157. file_uuid = str(uuid.uuid1())
  158. file, is_new = project.add_file(
  159. uuid=file_uuid,
  160. file_type="image",
  161. name=f"name{i}",
  162. filename=f"image_{i:03d}",
  163. extension=".jpg",
  164. size=32*1024,
  165. )
  166. self.assertTrue(is_new)
  167. files.append(file)
  168. self.assertEqual(10, File.query.count())
  169. response = self.get(url_for("list_all_files",
  170. project_id=project.id))
  171. self.assertTrue(response.is_json)
  172. _content = response.json
  173. count = _content["count"]
  174. content = _content["files"]
  175. self.assertEqual(10, count)
  176. self.assertEqual(10, len(content))
  177. for file, entry in zip(files, content):
  178. self.assertDictEqual(entry, file.serialize())
  179. def test_list_some_files(self):
  180. project = Project.new(
  181. name="TestProject",
  182. description="Project for a test case",
  183. model=self.model,
  184. root_folder="project_folder",
  185. external_data=False,
  186. data_folder="project_folder/data",
  187. )
  188. self.assertEqual(0, File.query.count())
  189. files = []
  190. for i in range(1, 11):
  191. file_uuid = str(uuid.uuid1())
  192. file, is_new = project.add_file(
  193. uuid=file_uuid,
  194. file_type="image",
  195. name=f"name{i}",
  196. filename=f"image_{i:03d}",
  197. extension=".jpg",
  198. size=32*1024,
  199. )
  200. self.assertTrue(is_new)
  201. files.append(file)
  202. self.assertEqual(10, File.query.count())
  203. for start, length in [(0, 5), (0, 15), (5, 3), (5, 8)]:
  204. response = self.get(url_for("list_files",
  205. project_id=project.id,
  206. start=start, length=length))
  207. self.assertTrue(response.is_json)
  208. _content = response.json
  209. count = _content["count"]
  210. content = _content["files"]
  211. self.assertEqual(len(files), count)
  212. self.assertEqual(min(len(files), start+length) - start, len(content))
  213. for file, entry in zip(files[start:start+length], content):
  214. self.assertDictEqual(entry, file.serialize())
  215. def test_list_collection_files_of_non_existing_collection(self):
  216. project = Project.new(
  217. name="TestProject",
  218. description="Project for a test case",
  219. model=self.model,
  220. root_folder="project_folder",
  221. external_data=False,
  222. data_folder="project_folder/data",
  223. )
  224. url = url_for("list_collection_files",
  225. project_id=project.id, collection_id=42,
  226. start=0, length=30)
  227. self.get(url, status_code=404)
  228. def test_list_collection_files(self):
  229. project = Project.new(
  230. name="TestProject",
  231. description="Project for a test case",
  232. model=self.model,
  233. root_folder="project_folder",
  234. external_data=False,
  235. data_folder="project_folder/data",
  236. )
  237. self.assertEqual(1, Project.query.count())
  238. collections = {}
  239. for i in range(1, 3):
  240. collection, is_new = project.create_collection(
  241. reference=f"collection_{i}",
  242. name=f"Some collection {i}",
  243. description=f"A description {i}",
  244. position=i,
  245. autoselect=i == 1
  246. )
  247. self.assertTrue(is_new)
  248. collection_files = []
  249. for j in range(1, 4):
  250. file_uuid = str(uuid.uuid1())
  251. file, is_new = collection.add_file(
  252. uuid=file_uuid,
  253. file_type="image",
  254. name=f"col_{i}_name{j}",
  255. filename=f"col_{i}_image_{j:03d}",
  256. extension=".jpg",
  257. size=32*1024,
  258. )
  259. self.assertTrue(is_new)
  260. collection_files.append(file)
  261. collections[collection.id] = collection_files
  262. files = []
  263. for j in range(1, 4):
  264. file_uuid = str(uuid.uuid1())
  265. file, is_new = project.add_file(
  266. uuid=file_uuid,
  267. file_type="image",
  268. name=f"name{j}",
  269. filename=f"image_{j:03d}",
  270. extension=".jpg",
  271. size=32*1024,
  272. )
  273. self.assertTrue(is_new)
  274. files.append(file)
  275. collections[0] = files
  276. self.assertEqual(2, Collection.query.filter(Collection.project_id==project.id).count())
  277. self.assertEqual(6, File.query.filter(
  278. File.project_id == project.id,
  279. File.collection_id != None,
  280. ).count())
  281. self.assertEqual(3, File.query.filter(
  282. File.project_id == project.id,
  283. File.collection_id == None,
  284. ).count())
  285. for collection_id, files in collections.items():
  286. for start, length in [(0, 5), (0, 15), (1, 3), (1, 8)]:
  287. response = self.get(url_for("list_collection_files",
  288. project_id=project.id, collection_id=collection_id,
  289. start=start, length=length))
  290. self.assertTrue(response.is_json)
  291. _content = response.json
  292. count = _content["count"]
  293. content = _content["files"]
  294. self.assertEqual(len(files), count)
  295. self.assertEqual(min(len(files), start+length) - start, len(content))
  296. for file, entry in zip(files[start:start+length], content):
  297. self.assertDictEqual(entry, file.serialize())