job_test.rb 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. require 'test_helper'
  2. class JobTest < ActiveSupport::TestCase
  3. test 'case 1' do
  4. # create ticket
  5. group1 = Group.lookup(name: 'Users')
  6. group2 = Group.create_or_update(
  7. name: 'JobTest2',
  8. updated_by_id: 1,
  9. created_by_id: 1,
  10. )
  11. ticket1 = Ticket.create!(
  12. title: 'job test 1',
  13. group: group1,
  14. customer_id: 2,
  15. state: Ticket::State.lookup(name: 'new'),
  16. priority: Ticket::Priority.lookup(name: '2 normal'),
  17. created_at: Time.zone.now - 3.days,
  18. updated_at: Time.zone.now - 3.days,
  19. created_by_id: 1,
  20. updated_by_id: 1,
  21. )
  22. ticket2 = Ticket.create!(
  23. title: 'job test 2',
  24. group: group1,
  25. customer_id: 2,
  26. state: Ticket::State.lookup(name: 'new'),
  27. priority: Ticket::Priority.lookup(name: '2 normal'),
  28. created_at: Time.zone.now - 1.day,
  29. created_by_id: 1,
  30. updated_at: Time.zone.now - 1.day,
  31. updated_by_id: 1,
  32. )
  33. ticket3 = Ticket.create!(
  34. title: 'job test 3',
  35. group: group2,
  36. customer_id: 2,
  37. state: Ticket::State.lookup(name: 'open'),
  38. priority: Ticket::Priority.lookup(name: '3 high'),
  39. created_at: Time.zone.now - 1.day,
  40. created_by_id: 1,
  41. updated_at: Time.zone.now - 1.day,
  42. updated_by_id: 1,
  43. )
  44. ticket4 = Ticket.create!(
  45. title: 'job test 4',
  46. group: group2,
  47. customer_id: 2,
  48. state: Ticket::State.lookup(name: 'closed'),
  49. priority: Ticket::Priority.lookup(name: '2 normal'),
  50. created_at: Time.zone.now - 3.days,
  51. created_by_id: 1,
  52. updated_at: Time.zone.now - 3.days,
  53. updated_by_id: 1,
  54. )
  55. ticket5 = Ticket.create!(
  56. title: 'job test 5',
  57. group: group2,
  58. customer_id: 2,
  59. state: Ticket::State.lookup(name: 'open'),
  60. priority: Ticket::Priority.lookup(name: '2 normal'),
  61. created_at: Time.zone.now - 3.days,
  62. created_by_id: 1,
  63. updated_by_id: 1,
  64. updated_at: Time.zone.now - 3.days,
  65. )
  66. # create jobs
  67. job1 = Job.create_or_update(
  68. name: 'Test Job1',
  69. timeplan: {
  70. days: {
  71. Mon: false,
  72. Tue: false,
  73. Wed: false,
  74. Thu: false,
  75. Fri: false,
  76. Sat: false,
  77. Sun: false,
  78. },
  79. hours: {
  80. 0 => false,
  81. 1 => false,
  82. 2 => false,
  83. 3 => false,
  84. 4 => false,
  85. 5 => false,
  86. 6 => false,
  87. 7 => false,
  88. 8 => false,
  89. 9 => false,
  90. 10 => false,
  91. 11 => false,
  92. 12 => false,
  93. 13 => false,
  94. 14 => false,
  95. 15 => false,
  96. 16 => false,
  97. 17 => false,
  98. 18 => false,
  99. 19 => false,
  100. 20 => false,
  101. 21 => false,
  102. 22 => false,
  103. 23 => false,
  104. },
  105. minutes: {
  106. 0 => false,
  107. 10 => false,
  108. 20 => false,
  109. 30 => false,
  110. 40 => false,
  111. 50 => false,
  112. },
  113. },
  114. condition: {
  115. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  116. 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
  117. },
  118. perform: {
  119. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
  120. },
  121. disable_notification: true,
  122. last_run_at: nil,
  123. active: true,
  124. created_by_id: 1,
  125. created_at: Time.zone.now,
  126. updated_by_id: 1,
  127. updated_at: Time.zone.now,
  128. )
  129. assert_not(job1.next_run_at)
  130. assert_not(job1.executable?)
  131. job1.last_run_at = Time.zone.now - 15.minutes
  132. job1.save!
  133. assert_not(job1.executable?)
  134. job1.updated_at = Time.zone.now - 15.minutes
  135. job1.save!
  136. assert(job1.executable?)
  137. job1.active = false
  138. job1.save!
  139. assert_not(job1.executable?)
  140. job1.active = true
  141. job1.save!
  142. assert_not(job1.executable?)
  143. assert_not(job1.in_timeplan?)
  144. time = Time.zone.now
  145. day_map = {
  146. 0 => 'Sun',
  147. 1 => 'Mon',
  148. 2 => 'Tue',
  149. 3 => 'Wed',
  150. 4 => 'Thu',
  151. 5 => 'Fri',
  152. 6 => 'Sat',
  153. }
  154. job1.timeplan['days'][day_map[time.wday]] = true
  155. job1.save!
  156. assert_not(job1.in_timeplan?(time))
  157. job1.timeplan['hours'][time.hour.to_s] = true
  158. job1.save!
  159. assert_not(job1.in_timeplan?(time))
  160. min = time.min
  161. if min < 9
  162. min = 0
  163. elsif min < 20
  164. min = 10
  165. elsif min < 30
  166. min = 20
  167. elsif min < 40
  168. min = 30
  169. elsif min < 50
  170. min = 40
  171. elsif min < 60
  172. min = 50
  173. end
  174. job1.timeplan['minutes'][min.to_s] = true
  175. job1.save!
  176. assert(job1.in_timeplan?(time))
  177. job1.timeplan['hours'][time.hour] = true
  178. job1.save!
  179. job1.timeplan['minutes'][min] = true
  180. job1.save!
  181. assert(job1.in_timeplan?(time))
  182. # execute jobs
  183. job1.updated_at = Time.zone.now - 15.minutes
  184. job1.save!
  185. Job.run
  186. assert(job1.next_run_at)
  187. assert(job1.executable?)
  188. assert(job1.in_timeplan?)
  189. # verify changes on tickets
  190. ticket1_later = Ticket.find(ticket1.id)
  191. assert_equal('closed', ticket1_later.state.name)
  192. assert_not_equal(ticket1.updated_at.to_s, ticket1_later.updated_at.to_s)
  193. ticket2_later = Ticket.find(ticket2.id)
  194. assert_equal('new', ticket2_later.state.name)
  195. assert_equal(ticket2.updated_at.to_s, ticket2_later.updated_at.to_s)
  196. ticket3_later = Ticket.find(ticket3.id)
  197. assert_equal('open', ticket3_later.state.name)
  198. assert_equal(ticket3.updated_at.to_s, ticket3_later.updated_at.to_s)
  199. ticket4_later = Ticket.find(ticket4.id)
  200. assert_equal('closed', ticket4_later.state.name)
  201. assert_equal(ticket4.updated_at.to_s, ticket4_later.updated_at.to_s)
  202. ticket5_later = Ticket.find(ticket5.id)
  203. assert_equal('closed', ticket5_later.state.name)
  204. assert_not_equal(ticket5.updated_at.to_s, ticket5_later.updated_at.to_s)
  205. # execute jobs again
  206. job1.updated_at = Time.zone.now - 15.minutes
  207. job1.save!
  208. Job.run
  209. # verify changes on tickets
  210. ticket1_later_next = Ticket.find(ticket1.id)
  211. assert_equal('closed', ticket1_later_next.state.name)
  212. assert_equal(ticket1_later.updated_at.to_s, ticket1_later_next.updated_at.to_s)
  213. ticket2_later_next = Ticket.find(ticket2.id)
  214. assert_equal('new', ticket2_later_next.state.name)
  215. assert_equal(ticket2_later.updated_at.to_s, ticket2_later_next.updated_at.to_s)
  216. ticket3_later_next = Ticket.find(ticket3.id)
  217. assert_equal('open', ticket3_later_next.state.name)
  218. assert_equal(ticket3_later.updated_at.to_s, ticket3_later_next.updated_at.to_s)
  219. ticket4_later_next = Ticket.find(ticket4.id)
  220. assert_equal('closed', ticket4_later_next.state.name)
  221. assert_equal(ticket4_later.updated_at.to_s, ticket4_later_next.updated_at.to_s)
  222. ticket5_later_next = Ticket.find(ticket5.id)
  223. assert_equal('closed', ticket5_later_next.state.name)
  224. assert_equal(ticket5_later.updated_at.to_s, ticket5_later_next.updated_at.to_s)
  225. end
  226. test 'with invalid state_id' do
  227. # create ticket
  228. group1 = Group.lookup(name: 'Users')
  229. group2 = Group.create_or_update(
  230. name: 'JobTest2',
  231. updated_by_id: 1,
  232. created_by_id: 1,
  233. )
  234. ticket1 = Ticket.create!(
  235. title: 'job test 1',
  236. group: group1,
  237. customer_id: 2,
  238. state: Ticket::State.lookup(name: 'new'),
  239. priority: Ticket::Priority.lookup(name: '2 normal'),
  240. created_at: Time.zone.now - 3.days,
  241. updated_at: Time.zone.now - 3.days,
  242. created_by_id: 1,
  243. updated_by_id: 1,
  244. )
  245. ticket2 = Ticket.create!(
  246. title: 'job test 2',
  247. group: group1,
  248. customer_id: 2,
  249. state: Ticket::State.lookup(name: 'new'),
  250. priority: Ticket::Priority.lookup(name: '2 normal'),
  251. created_at: Time.zone.now - 1.day,
  252. created_by_id: 1,
  253. updated_at: Time.zone.now - 1.day,
  254. updated_by_id: 1,
  255. )
  256. # create jobs
  257. job1 = Job.create_or_update(
  258. name: 'Test Job1',
  259. timeplan: {
  260. days: {
  261. Mon: true,
  262. Tue: true,
  263. Wed: true,
  264. Thu: true,
  265. Fri: true,
  266. Sat: true,
  267. Sun: true,
  268. },
  269. hours: {
  270. 0 => true,
  271. 1 => true,
  272. 2 => true,
  273. 3 => true,
  274. 4 => true,
  275. 5 => true,
  276. 6 => true,
  277. 7 => true,
  278. 8 => true,
  279. 9 => true,
  280. 10 => true,
  281. 11 => true,
  282. 12 => true,
  283. 13 => true,
  284. 14 => true,
  285. 15 => true,
  286. 16 => true,
  287. 17 => true,
  288. 18 => true,
  289. 19 => true,
  290. 20 => true,
  291. 21 => true,
  292. 22 => true,
  293. 23 => true,
  294. },
  295. minutes: {
  296. 0 => true,
  297. 10 => true,
  298. 20 => true,
  299. 30 => true,
  300. 40 => true,
  301. 50 => true,
  302. },
  303. },
  304. condition: {
  305. 'ticket.state_id' => { 'operator' => 'is', 'value' => '9999' },
  306. 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
  307. },
  308. perform: {
  309. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
  310. },
  311. disable_notification: true,
  312. last_run_at: nil,
  313. updated_at: Time.zone.now - 15.minutes,
  314. active: true,
  315. updated_by_id: 1,
  316. created_by_id: 1,
  317. )
  318. assert(job1.executable?)
  319. assert(job1.in_timeplan?)
  320. Job.run
  321. # verify changes on tickets
  322. ticket1_later = Ticket.find(ticket1.id)
  323. assert_equal('new', ticket1_later.state.name)
  324. assert_equal(ticket1.updated_at.to_s, ticket1_later.updated_at.to_s)
  325. ticket2_later = Ticket.find(ticket2.id)
  326. assert_equal('new', ticket2_later.state.name)
  327. assert_equal(ticket2.updated_at.to_s, ticket2_later.updated_at.to_s)
  328. end
  329. test 'case 3' do
  330. # create jobs
  331. job1 = Job.create_or_update(
  332. name: 'Test Job1',
  333. timeplan: {
  334. days: {
  335. Mon: true,
  336. Tue: false,
  337. Wed: false,
  338. Thu: false,
  339. Fri: true,
  340. Sat: false,
  341. Sun: false,
  342. },
  343. hours: {
  344. 0 => false,
  345. 1 => true,
  346. 2 => false,
  347. 3 => false,
  348. 4 => false,
  349. 5 => false,
  350. 6 => false,
  351. 7 => false,
  352. 8 => false,
  353. 9 => false,
  354. 10 => true,
  355. 11 => false,
  356. 12 => false,
  357. 13 => false,
  358. 14 => false,
  359. 15 => false,
  360. 16 => false,
  361. 17 => false,
  362. 18 => false,
  363. 19 => false,
  364. 20 => false,
  365. 21 => false,
  366. 22 => false,
  367. 23 => false,
  368. },
  369. minutes: {
  370. 0 => true,
  371. 10 => false,
  372. 20 => false,
  373. 30 => false,
  374. 40 => true,
  375. 50 => false,
  376. },
  377. },
  378. condition: {
  379. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  380. 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
  381. },
  382. perform: {
  383. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
  384. },
  385. disable_notification: true,
  386. last_run_at: nil,
  387. active: true,
  388. created_by_id: 1,
  389. created_at: Time.zone.now,
  390. updated_by_id: 1,
  391. updated_at: Time.zone.now,
  392. )
  393. time_now = Time.zone.parse('2016-03-18 09:17:13 UTC')
  394. next_run_at = job1.next_run_at_calculate(time_now)
  395. assert_equal('2016-03-18 10:00:00 UTC', next_run_at.to_s)
  396. time_now = Time.zone.parse('2016-03-18 10:37:13 UTC')
  397. next_run_at = job1.next_run_at_calculate(time_now)
  398. assert_equal('2016-03-18 10:40:00 UTC', next_run_at.to_s)
  399. time_now = Time.zone.parse('2016-03-17 09:17:13 UTC')
  400. next_run_at = job1.next_run_at_calculate(time_now)
  401. assert_equal('2016-03-18 01:00:00 UTC', next_run_at.to_s)
  402. time_now = Time.zone.parse('2016-03-17 11:17:13 UTC')
  403. next_run_at = job1.next_run_at_calculate(time_now)
  404. assert_equal('2016-03-18 01:00:00 UTC', next_run_at.to_s)
  405. time_now = Time.zone.parse('2016-03-19 11:17:13 UTC')
  406. next_run_at = job1.next_run_at_calculate(time_now)
  407. assert_equal('2016-03-21 01:00:00 UTC', next_run_at.to_s)
  408. time_now = Time.zone.parse('2016-03-22 00:59:59 UTC')
  409. next_run_at = job1.next_run_at_calculate(time_now)
  410. assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
  411. time_now = Time.zone.parse('2016-03-25 00:59:59 UTC')
  412. next_run_at = job1.next_run_at_calculate(time_now)
  413. assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
  414. time_now = Time.zone.parse('2016-03-24 00:59:59 UTC')
  415. next_run_at = job1.next_run_at_calculate(time_now)
  416. assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
  417. time_now = Time.zone.parse('2016-03-24 23:59:59 UTC')
  418. next_run_at = job1.next_run_at_calculate(time_now)
  419. assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
  420. time_now = Time.zone.parse('2016-03-25 01:00:01 UTC')
  421. next_run_at = job1.next_run_at_calculate(time_now)
  422. assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
  423. time_now = Time.zone.parse('2016-03-25 01:09:01 UTC')
  424. next_run_at = job1.next_run_at_calculate(time_now)
  425. assert_equal('2016-03-25 01:40:00 UTC', next_run_at.to_s)
  426. time_now = Time.zone.parse('2016-03-25 01:09:59 UTC')
  427. next_run_at = job1.next_run_at_calculate(time_now)
  428. assert_equal('2016-03-25 01:40:00 UTC', next_run_at.to_s)
  429. job1.last_run_at = Time.zone.parse('2016-03-18 10:00:01 UTC')
  430. job1.save!
  431. time_now = Time.zone.parse('2016-03-18 10:00:02 UTC')
  432. next_run_at = job1.next_run_at_calculate(time_now)
  433. assert_equal('2016-03-18 10:40:00 UTC', next_run_at.to_s)
  434. job1.last_run_at = Time.zone.parse('2016-03-18 10:40:01 UTC')
  435. job1.save!
  436. time_now = Time.zone.parse('2016-03-18 10:40:02 UTC')
  437. next_run_at = job1.next_run_at_calculate(time_now)
  438. assert_equal('2016-03-21 01:00:00 UTC', next_run_at.to_s)
  439. end
  440. test 'case 4' do
  441. # create jobs
  442. job1 = Job.create_or_update(
  443. name: 'Test Job1',
  444. timeplan: {
  445. days: {
  446. Mon: true,
  447. Tue: false,
  448. Wed: false,
  449. Thu: false,
  450. Fri: true,
  451. Sat: false,
  452. Sun: false,
  453. },
  454. hours: {
  455. 0 => true,
  456. 1 => false,
  457. 2 => false,
  458. 3 => false,
  459. 4 => false,
  460. 5 => false,
  461. 6 => false,
  462. 7 => false,
  463. 8 => false,
  464. 9 => false,
  465. 10 => true,
  466. 11 => false,
  467. 12 => false,
  468. 13 => false,
  469. 14 => false,
  470. 15 => false,
  471. 16 => false,
  472. 17 => false,
  473. 18 => false,
  474. 19 => false,
  475. 20 => false,
  476. 21 => false,
  477. 22 => false,
  478. 23 => false,
  479. },
  480. minutes: {
  481. 0 => true,
  482. 10 => false,
  483. 20 => false,
  484. 30 => false,
  485. 40 => true,
  486. 50 => false,
  487. },
  488. },
  489. condition: {
  490. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  491. 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
  492. },
  493. perform: {
  494. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
  495. },
  496. disable_notification: true,
  497. last_run_at: nil,
  498. active: true,
  499. created_by_id: 1,
  500. created_at: Time.zone.now,
  501. updated_by_id: 1,
  502. updated_at: Time.zone.now,
  503. )
  504. time_now = Time.zone.parse('2016-03-17 23:51:23 UTC')
  505. next_run_at = job1.next_run_at_calculate(time_now)
  506. assert_equal('2016-03-18 00:00:00 UTC', next_run_at.to_s)
  507. job1.last_run_at = Time.zone.parse('2016-03-17 23:45:01 UTC')
  508. job1.save!
  509. time_now = Time.zone.parse('2016-03-17 23:51:23 UTC')
  510. next_run_at = job1.next_run_at_calculate(time_now)
  511. assert_equal('2016-03-18 00:00:00 UTC', next_run_at.to_s)
  512. job1.last_run_at = Time.zone.parse('2016-03-17 23:59:01 UTC')
  513. job1.save!
  514. time_now = Time.zone.parse('2016-03-17 23:59:23 UTC')
  515. next_run_at = job1.next_run_at_calculate(time_now)
  516. assert_equal('2016-03-18 00:40:00 UTC', next_run_at.to_s)
  517. time_now = Time.zone.parse('2016-03-17 23:59:23 UTC')
  518. assert_not(job1.in_timeplan?(time_now))
  519. time_now = Time.zone.parse('2016-03-18 00:01:23 UTC')
  520. assert(job1.in_timeplan?(time_now))
  521. end
  522. test 'check next_run_at' do
  523. job1 = Job.create_or_update(
  524. name: 'Test Job1',
  525. timeplan: {
  526. days: {
  527. Mon: true,
  528. Tue: false,
  529. Wed: false,
  530. Thu: false,
  531. Fri: false,
  532. Sat: false,
  533. Sun: false,
  534. },
  535. hours: {
  536. '0' => true,
  537. '1' => false,
  538. '2' => false,
  539. '3' => false,
  540. '4' => false,
  541. '5' => false,
  542. '6' => false,
  543. '7' => false,
  544. '8' => false,
  545. '9' => false,
  546. '10' => false,
  547. '11' => false,
  548. '12' => false,
  549. '13' => false,
  550. '14' => false,
  551. '15' => false,
  552. '16' => false,
  553. '17' => false,
  554. '18' => false,
  555. '19' => false,
  556. '20' => false,
  557. '21' => false,
  558. '22' => false,
  559. '23' => false,
  560. },
  561. minutes: {
  562. '0' => true,
  563. '10' => false,
  564. '20' => false,
  565. '30' => false,
  566. '40' => false,
  567. '50' => false,
  568. },
  569. },
  570. condition: {
  571. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  572. 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
  573. },
  574. perform: {
  575. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
  576. },
  577. disable_notification: true,
  578. last_run_at: nil,
  579. active: true,
  580. created_by_id: 1,
  581. created_at: Time.zone.now,
  582. updated_by_id: 1,
  583. updated_at: Time.zone.now,
  584. )
  585. time_now = Time.zone.parse('2016-03-17 23:51:23 UTC')
  586. next_run_at = job1.next_run_at_calculate(time_now)
  587. assert_equal('2016-03-21 00:00:00 UTC', next_run_at.to_s)
  588. end
  589. test 'update next run at' do
  590. travel_to Time.zone.local(2017, 11, 10, 22, 0o4, 44)
  591. job1 = Job.create_or_update(
  592. name: 'Test Job1',
  593. timeplan: {
  594. days: {
  595. Mon: false,
  596. Tue: false,
  597. Wed: false,
  598. Thu: false,
  599. Fri: false,
  600. Sat: true,
  601. Sun: false,
  602. },
  603. hours: {
  604. '0' => false,
  605. '1' => false,
  606. '2' => false,
  607. '3' => false,
  608. '4' => false,
  609. '5' => false,
  610. '6' => false,
  611. '7' => false,
  612. '8' => false,
  613. '9' => false,
  614. '10' => false,
  615. '11' => false,
  616. '12' => false,
  617. '13' => false,
  618. '14' => false,
  619. '15' => false,
  620. '16' => false,
  621. '17' => false,
  622. '18' => false,
  623. '19' => false,
  624. '20' => false,
  625. '21' => false,
  626. '22' => false,
  627. '23' => true,
  628. },
  629. minutes: {
  630. '0' => true,
  631. '10' => false,
  632. '20' => false,
  633. '30' => false,
  634. '40' => false,
  635. '50' => false,
  636. },
  637. },
  638. condition: {
  639. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  640. },
  641. perform: {
  642. 'ticket.action' => { 'value' => 'delete' },
  643. },
  644. disable_notification: true,
  645. last_run_at: nil,
  646. active: true,
  647. created_by_id: 1,
  648. created_at: Time.zone.now,
  649. updated_by_id: 1,
  650. updated_at: Time.zone.now,
  651. )
  652. assert_equal('2017-11-11 23:00:00 UTC', job1.next_run_at.to_s)
  653. assert_not(job1.last_run_at)
  654. travel_to Time.zone.local(2017, 11, 16, 22, 0o4, 44)
  655. Job.run
  656. job1.reload
  657. assert_equal('2017-11-18 23:00:00 UTC', job1.next_run_at.to_s)
  658. assert_not(job1.last_run_at)
  659. travel_back
  660. end
  661. test 'execute on certain time' do
  662. travel_to Time.zone.local(2017, 11, 16, 22, 0o4, 44)
  663. group1 = Group.lookup(name: 'Users')
  664. ticket1 = Ticket.create!(
  665. title: 'job test 1',
  666. group: group1,
  667. customer_id: 2,
  668. state: Ticket::State.lookup(name: 'new'),
  669. priority: Ticket::Priority.lookup(name: '2 normal'),
  670. created_by_id: 1,
  671. updated_by_id: 1,
  672. )
  673. ticket2 = Ticket.create!(
  674. title: 'job test 2',
  675. group: group1,
  676. customer_id: 2,
  677. state: Ticket::State.lookup(name: 'new'),
  678. priority: Ticket::Priority.lookup(name: '2 normal'),
  679. created_by_id: 1,
  680. updated_by_id: 1,
  681. )
  682. job1 = Job.create_or_update(
  683. name: 'Test Job1',
  684. timeplan: {
  685. days: {
  686. Mon: false,
  687. Tue: false,
  688. Wed: false,
  689. Thu: true,
  690. Fri: false,
  691. Sat: false,
  692. Sun: false,
  693. },
  694. hours: {
  695. '0' => false,
  696. '1' => false,
  697. '2' => false,
  698. '3' => false,
  699. '4' => false,
  700. '5' => false,
  701. '6' => false,
  702. '7' => false,
  703. '8' => false,
  704. '9' => false,
  705. '10' => false,
  706. '11' => false,
  707. '12' => false,
  708. '13' => false,
  709. '14' => false,
  710. '15' => false,
  711. '16' => false,
  712. '17' => false,
  713. '18' => false,
  714. '19' => false,
  715. '20' => false,
  716. '21' => false,
  717. '22' => false,
  718. '23' => true,
  719. },
  720. minutes: {
  721. '0' => true,
  722. '10' => false,
  723. '20' => false,
  724. '30' => false,
  725. '40' => false,
  726. '50' => false,
  727. },
  728. },
  729. condition: {
  730. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  731. },
  732. perform: {
  733. 'ticket.action' => { 'value' => 'delete' },
  734. },
  735. disable_notification: true,
  736. last_run_at: nil,
  737. active: true,
  738. created_by_id: 1,
  739. created_at: Time.zone.now,
  740. updated_by_id: 1,
  741. updated_at: Time.zone.now,
  742. )
  743. Job.run
  744. assert(Ticket.find_by(id: ticket1.id))
  745. assert(Ticket.find_by(id: ticket2.id))
  746. travel_to Time.zone.local(2017, 11, 16, 23, 0o4, 44)
  747. Job.run
  748. assert_not(Ticket.find_by(id: ticket1.id))
  749. assert_not(Ticket.find_by(id: ticket2.id))
  750. travel_back
  751. end
  752. test 'delete based on tag' do
  753. # create ticket
  754. group1 = Group.lookup(name: 'Users')
  755. group2 = Group.create_or_update(
  756. name: 'JobTest2',
  757. updated_by_id: 1,
  758. created_by_id: 1,
  759. )
  760. ticket1 = Ticket.create!(
  761. title: 'job test 1',
  762. group: group1,
  763. customer_id: 2,
  764. state: Ticket::State.lookup(name: 'new'),
  765. priority: Ticket::Priority.lookup(name: '2 normal'),
  766. created_at: Time.zone.now - 3.days,
  767. updated_at: Time.zone.now - 3.days,
  768. created_by_id: 1,
  769. updated_by_id: 1,
  770. )
  771. ticket1.tag_add('spam', 1)
  772. ticket1.tag_add('test1 ', 1)
  773. ticket2 = Ticket.create!(
  774. title: 'job test 2',
  775. group: group1,
  776. customer_id: 2,
  777. state: Ticket::State.lookup(name: 'new'),
  778. priority: Ticket::Priority.lookup(name: '2 normal'),
  779. created_at: Time.zone.now - 1.day,
  780. created_by_id: 1,
  781. updated_at: Time.zone.now - 1.day,
  782. updated_by_id: 1,
  783. )
  784. job1 = Job.create_or_update(
  785. name: 'Test Job1',
  786. timeplan: {
  787. days: {
  788. Mon: true,
  789. Tue: true,
  790. Wed: true,
  791. Thu: true,
  792. Fri: true,
  793. Sat: true,
  794. Sun: true,
  795. },
  796. hours: {
  797. 0 => true,
  798. 1 => true,
  799. 2 => true,
  800. 3 => true,
  801. 4 => true,
  802. 5 => true,
  803. 6 => true,
  804. 7 => true,
  805. 8 => true,
  806. 9 => true,
  807. 10 => true,
  808. 11 => true,
  809. 12 => true,
  810. 13 => true,
  811. 14 => true,
  812. 15 => true,
  813. 16 => true,
  814. 17 => true,
  815. 18 => true,
  816. 19 => true,
  817. 20 => true,
  818. 21 => true,
  819. 22 => true,
  820. 23 => true,
  821. },
  822. minutes: {
  823. 0 => true,
  824. 10 => true,
  825. 20 => true,
  826. 30 => true,
  827. 40 => true,
  828. 50 => true,
  829. },
  830. },
  831. condition: {
  832. 'ticket.tags' => { 'operator' => 'contains one', 'value' => 'spam' },
  833. },
  834. perform: {
  835. 'ticket.action' => { 'value' => 'delete' },
  836. },
  837. disable_notification: true,
  838. last_run_at: nil,
  839. updated_at: Time.zone.now - 15.minutes,
  840. active: true,
  841. updated_by_id: 1,
  842. created_by_id: 1,
  843. )
  844. assert(job1.executable?)
  845. assert(job1.in_timeplan?)
  846. Job.run
  847. assert_not(Ticket.find_by(id: ticket1.id))
  848. assert(Ticket.find_by(id: ticket2.id))
  849. end
  850. end