test_job_manager.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. from time import sleep
  2. import pytest
  3. from facefusion.jobs.job_helper import get_step_output_path
  4. from facefusion.jobs.job_manager import add_step, clear_jobs, count_step_total, create_job, delete_job, delete_jobs, find_job_ids, get_steps, init_jobs, insert_step, move_job_file, remix_step, remove_step, set_step_status, set_steps_status, submit_job, submit_jobs
  5. from .helper import get_test_jobs_directory
  6. @pytest.fixture(scope = 'function', autouse = True)
  7. def before_each() -> None:
  8. clear_jobs(get_test_jobs_directory())
  9. init_jobs(get_test_jobs_directory())
  10. def test_create_job() -> None:
  11. args_1 =\
  12. {
  13. 'source_path': 'source-1.jpg',
  14. 'target_path': 'target-1.jpg',
  15. 'output_path': 'output-1.jpg'
  16. }
  17. assert create_job('job-test-create-job') is True
  18. assert create_job('job-test-create-job') is False
  19. add_step('job-test-submit-job', args_1)
  20. submit_job('job-test-create-job')
  21. assert create_job('job-test-create-job') is False
  22. def test_submit_job() -> None:
  23. args_1 =\
  24. {
  25. 'source_path': 'source-1.jpg',
  26. 'target_path': 'target-1.jpg',
  27. 'output_path': 'output-1.jpg'
  28. }
  29. assert submit_job('job-invalid') is False
  30. create_job('job-test-submit-job')
  31. assert submit_job('job-test-submit-job') is False
  32. add_step('job-test-submit-job', args_1)
  33. assert submit_job('job-test-submit-job') is True
  34. assert submit_job('job-test-submit-job') is False
  35. def test_submit_jobs() -> None:
  36. args_1 =\
  37. {
  38. 'source_path': 'source-1.jpg',
  39. 'target_path': 'target-1.jpg',
  40. 'output_path': 'output-1.jpg'
  41. }
  42. args_2 =\
  43. {
  44. 'source_path': 'source-2.jpg',
  45. 'target_path': 'target-2.jpg',
  46. 'output_path': 'output-2.jpg'
  47. }
  48. assert submit_jobs() is False
  49. create_job('job-test-submit-jobs-1')
  50. create_job('job-test-submit-jobs-2')
  51. assert submit_jobs() is False
  52. add_step('job-test-submit-jobs-1', args_1)
  53. add_step('job-test-submit-jobs-2', args_2)
  54. assert submit_jobs() is True
  55. assert submit_jobs() is False
  56. def test_delete_job() -> None:
  57. assert delete_job('job-invalid') is False
  58. create_job('job-test-delete-job')
  59. assert delete_job('job-test-delete-job') is True
  60. assert delete_job('job-test-delete-job') is False
  61. def test_delete_jobs() -> None:
  62. assert delete_jobs() is False
  63. create_job('job-test-delete-jobs-1')
  64. create_job('job-test-delete-jobs-2')
  65. assert delete_jobs() is True
  66. @pytest.mark.skip()
  67. def test_find_jobs() -> None:
  68. pass
  69. def test_find_job_ids() -> None:
  70. create_job('job-test-find-job-ids-1')
  71. sleep(0.5)
  72. create_job('job-test-find-job-ids-2')
  73. sleep(0.5)
  74. create_job('job-test-find-job-ids-3')
  75. assert find_job_ids('drafted') == [ 'job-test-find-job-ids-1', 'job-test-find-job-ids-2', 'job-test-find-job-ids-3' ]
  76. assert find_job_ids('queued') == []
  77. assert find_job_ids('completed') == []
  78. assert find_job_ids('failed') == []
  79. move_job_file('job-test-find-job-ids-1', 'queued')
  80. move_job_file('job-test-find-job-ids-2', 'queued')
  81. move_job_file('job-test-find-job-ids-3', 'queued')
  82. assert find_job_ids('drafted') == []
  83. assert find_job_ids('queued') == [ 'job-test-find-job-ids-1', 'job-test-find-job-ids-2', 'job-test-find-job-ids-3' ]
  84. assert find_job_ids('completed') == []
  85. assert find_job_ids('failed') == []
  86. move_job_file('job-test-find-job-ids-1', 'completed')
  87. assert find_job_ids('drafted') == []
  88. assert find_job_ids('queued') == [ 'job-test-find-job-ids-2', 'job-test-find-job-ids-3' ]
  89. assert find_job_ids('completed') == [ 'job-test-find-job-ids-1' ]
  90. assert find_job_ids('failed') == []
  91. move_job_file('job-test-find-job-ids-2', 'failed')
  92. assert find_job_ids('drafted') == []
  93. assert find_job_ids('queued') == [ 'job-test-find-job-ids-3' ]
  94. assert find_job_ids('completed') == [ 'job-test-find-job-ids-1' ]
  95. assert find_job_ids('failed') == [ 'job-test-find-job-ids-2' ]
  96. move_job_file('job-test-find-job-ids-3', 'completed')
  97. assert find_job_ids('drafted') == []
  98. assert find_job_ids('queued') == []
  99. assert find_job_ids('completed') == [ 'job-test-find-job-ids-1', 'job-test-find-job-ids-3' ]
  100. assert find_job_ids('failed') == [ 'job-test-find-job-ids-2' ]
  101. def test_add_step() -> None:
  102. args_1 =\
  103. {
  104. 'source_path': 'source-1.jpg',
  105. 'target_path': 'target-1.jpg',
  106. 'output_path': 'output-1.jpg'
  107. }
  108. args_2 =\
  109. {
  110. 'source_path': 'source-2.jpg',
  111. 'target_path': 'target-2.jpg',
  112. 'output_path': 'output-2.jpg'
  113. }
  114. assert add_step('job-invalid', args_1) is False
  115. create_job('job-test-add-step')
  116. assert add_step('job-test-add-step', args_1) is True
  117. assert add_step('job-test-add-step', args_2) is True
  118. steps = get_steps('job-test-add-step')
  119. assert steps[0].get('args') == args_1
  120. assert steps[1].get('args') == args_2
  121. assert count_step_total('job-test-add-step') == 2
  122. def test_remix_step() -> None:
  123. args_1 =\
  124. {
  125. 'source_path': 'source-1.jpg',
  126. 'target_path': 'target-1.jpg',
  127. 'output_path': 'output-1.jpg'
  128. }
  129. args_2 =\
  130. {
  131. 'source_path': 'source-2.jpg',
  132. 'target_path': 'target-2.jpg',
  133. 'output_path': 'output-2.jpg'
  134. }
  135. assert remix_step('job-invalid', 0, args_1) is False
  136. create_job('job-test-remix-step')
  137. add_step('job-test-remix-step', args_1)
  138. add_step('job-test-remix-step', args_2)
  139. assert remix_step('job-test-remix-step', 99, args_1) is False
  140. assert remix_step('job-test-remix-step', 0, args_2) is True
  141. assert remix_step('job-test-remix-step', -1, args_2) is True
  142. steps = get_steps('job-test-remix-step')
  143. assert steps[0].get('args') == args_1
  144. assert steps[1].get('args') == args_2
  145. assert steps[2].get('args').get('source_path') == args_2.get('source_path')
  146. assert steps[2].get('args').get('target_path') == get_step_output_path('job-test-remix-step', 0, args_1.get('output_path'))
  147. assert steps[2].get('args').get('output_path') == args_2.get('output_path')
  148. assert steps[3].get('args').get('source_path') == args_2.get('source_path')
  149. assert steps[3].get('args').get('target_path') == get_step_output_path('job-test-remix-step', 2, args_2.get('output_path'))
  150. assert steps[3].get('args').get('output_path') == args_2.get('output_path')
  151. assert count_step_total('job-test-remix-step') == 4
  152. def test_insert_step() -> None:
  153. args_1 =\
  154. {
  155. 'source_path': 'source-1.jpg',
  156. 'target_path': 'target-1.jpg',
  157. 'output_path': 'output-1.jpg'
  158. }
  159. args_2 =\
  160. {
  161. 'source_path': 'source-2.jpg',
  162. 'target_path': 'target-2.jpg',
  163. 'output_path': 'output-2.jpg'
  164. }
  165. args_3 =\
  166. {
  167. 'source_path': 'source-3.jpg',
  168. 'target_path': 'target-3.jpg',
  169. 'output_path': 'output-3.jpg'
  170. }
  171. assert insert_step('job-invalid', 0, args_1) is False
  172. create_job('job-test-insert-step')
  173. add_step('job-test-insert-step', args_1)
  174. add_step('job-test-insert-step', args_1)
  175. assert insert_step('job-test-insert-step', 99, args_1) is False
  176. assert insert_step('job-test-insert-step', 0, args_2) is True
  177. assert insert_step('job-test-insert-step', -1, args_3) is True
  178. steps = get_steps('job-test-insert-step')
  179. assert steps[0].get('args') == args_2
  180. assert steps[1].get('args') == args_1
  181. assert steps[2].get('args') == args_3
  182. assert steps[3].get('args') == args_1
  183. assert count_step_total('job-test-insert-step') == 4
  184. def test_remove_step() -> None:
  185. args_1 =\
  186. {
  187. 'source_path': 'source-1.jpg',
  188. 'target_path': 'target-1.jpg',
  189. 'output_path': 'output-1.jpg'
  190. }
  191. args_2 =\
  192. {
  193. 'source_path': 'source-2.jpg',
  194. 'target_path': 'target-2.jpg',
  195. 'output_path': 'output-2.jpg'
  196. }
  197. args_3 =\
  198. {
  199. 'source_path': 'source-3.jpg',
  200. 'target_path': 'target-3.jpg',
  201. 'output_path': 'output-3.jpg'
  202. }
  203. assert remove_step('job-invalid', 0) is False
  204. create_job('job-test-remove-step')
  205. add_step('job-test-remove-step', args_1)
  206. add_step('job-test-remove-step', args_2)
  207. add_step('job-test-remove-step', args_1)
  208. add_step('job-test-remove-step', args_3)
  209. assert remove_step('job-test-remove-step', 99) is False
  210. assert remove_step('job-test-remove-step', 0) is True
  211. assert remove_step('job-test-remove-step', -1) is True
  212. steps = get_steps('job-test-remove-step')
  213. assert steps[0].get('args') == args_2
  214. assert steps[1].get('args') == args_1
  215. assert count_step_total('job-test-remove-step') == 2
  216. def test_get_steps() -> None:
  217. args_1 =\
  218. {
  219. 'source_path': 'source-1.jpg',
  220. 'target_path': 'target-1.jpg',
  221. 'output_path': 'output-1.jpg'
  222. }
  223. args_2 =\
  224. {
  225. 'source_path': 'source-2.jpg',
  226. 'target_path': 'target-2.jpg',
  227. 'output_path': 'output-2.jpg'
  228. }
  229. assert get_steps('job-invalid') == []
  230. create_job('job-test-get-steps')
  231. add_step('job-test-get-steps', args_1)
  232. add_step('job-test-get-steps', args_2)
  233. steps = get_steps('job-test-get-steps')
  234. assert steps[0].get('args') == args_1
  235. assert steps[1].get('args') == args_2
  236. assert count_step_total('job-test-get-steps') == 2
  237. def test_set_step_status() -> None:
  238. args_1 =\
  239. {
  240. 'source_path': 'source-1.jpg',
  241. 'target_path': 'target-1.jpg',
  242. 'output_path': 'output-1.jpg'
  243. }
  244. args_2 =\
  245. {
  246. 'source_path': 'source-2.jpg',
  247. 'target_path': 'target-2.jpg',
  248. 'output_path': 'output-2.jpg'
  249. }
  250. assert set_step_status('job-invalid', 0, 'completed') is False
  251. create_job('job-test-set-step-status')
  252. add_step('job-test-set-step-status', args_1)
  253. add_step('job-test-set-step-status', args_2)
  254. assert set_step_status('job-test-set-step-status', 99, 'completed') is False
  255. assert set_step_status('job-test-set-step-status', 0, 'completed') is True
  256. assert set_step_status('job-test-set-step-status', 1, 'failed') is True
  257. steps = get_steps('job-test-set-step-status')
  258. assert steps[0].get('status') == 'completed'
  259. assert steps[1].get('status') == 'failed'
  260. assert count_step_total('job-test-set-step-status') == 2
  261. def test_set_steps_status() -> None:
  262. args_1 =\
  263. {
  264. 'source_path': 'source-1.jpg',
  265. 'target_path': 'target-1.jpg',
  266. 'output_path': 'output-1.jpg'
  267. }
  268. args_2 =\
  269. {
  270. 'source_path': 'source-2.jpg',
  271. 'target_path': 'target-2.jpg',
  272. 'output_path': 'output-2.jpg'
  273. }
  274. assert set_steps_status('job-invalid', 'queued') is False
  275. create_job('job-test-set-steps-status')
  276. add_step('job-test-set-steps-status', args_1)
  277. add_step('job-test-set-steps-status', args_2)
  278. assert set_steps_status('job-test-set-steps-status', 'queued') is True
  279. steps = get_steps('job-test-set-steps-status')
  280. assert steps[0].get('status') == 'queued'
  281. assert steps[1].get('status') == 'queued'
  282. assert count_step_total('job-test-set-steps-status') == 2