object_manager_test.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. # rubocop:disable Lint/BooleanSymbol
  2. require 'test_helper'
  3. class ObjectManagerTest < ActiveSupport::TestCase
  4. self.use_transactional_tests = false
  5. test 'a object manager' do
  6. list_objects = ObjectManager.list_objects
  7. assert_equal(%w[Ticket TicketArticle User Organization Group], list_objects)
  8. list_objects = ObjectManager.list_frontend_objects
  9. assert_equal(%w[Ticket User Organization Group], list_objects)
  10. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  11. # create simple attribute
  12. attribute1 = ObjectManager::Attribute.add(
  13. object: 'Ticket',
  14. name: 'test1',
  15. display: 'Test 1',
  16. data_type: 'input',
  17. data_option: {
  18. maxlength: 200,
  19. type: 'text',
  20. null: false,
  21. },
  22. active: true,
  23. screens: {},
  24. position: 20,
  25. created_by_id: 1,
  26. updated_by_id: 1,
  27. editable: false,
  28. to_migrate: false,
  29. )
  30. assert(attribute1)
  31. assert_equal('test1', attribute1.name)
  32. assert_equal(true, attribute1.editable)
  33. assert_equal(true, attribute1.to_create)
  34. assert_equal(true, attribute1.to_migrate)
  35. assert_equal(false, attribute1.to_delete)
  36. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  37. attribute1 = ObjectManager::Attribute.get(
  38. object: 'Ticket',
  39. name: 'test1',
  40. )
  41. assert(attribute1)
  42. assert_equal('test1', attribute1.name)
  43. assert_equal(true, attribute1.editable)
  44. assert_equal(true, attribute1.to_create)
  45. assert_equal(true, attribute1.to_migrate)
  46. assert_equal(false, attribute1.to_delete)
  47. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  48. # delete attribute without execute migrations
  49. ObjectManager::Attribute.remove(
  50. object: 'Ticket',
  51. name: 'test1',
  52. )
  53. attribute1 = ObjectManager::Attribute.get(
  54. object: 'Ticket',
  55. name: 'test1',
  56. )
  57. assert_not(attribute1)
  58. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  59. assert(ObjectManager::Attribute.migration_execute)
  60. attribute1 = ObjectManager::Attribute.get(
  61. object: 'Ticket',
  62. name: 'test1',
  63. )
  64. assert_not(attribute1)
  65. # create invalid attributes
  66. assert_raises(RuntimeError) do
  67. attribute2 = ObjectManager::Attribute.add(
  68. object: 'Ticket',
  69. name: 'test2_id',
  70. display: 'Test 2 with id',
  71. data_type: 'input',
  72. data_option: {
  73. maxlength: 200,
  74. type: 'text',
  75. null: false,
  76. },
  77. active: true,
  78. screens: {},
  79. position: 20,
  80. created_by_id: 1,
  81. updated_by_id: 1,
  82. )
  83. end
  84. assert_raises(RuntimeError) do
  85. attribute3 = ObjectManager::Attribute.add(
  86. object: 'Ticket',
  87. name: 'test3_ids',
  88. display: 'Test 3 with id',
  89. data_type: 'input',
  90. data_option: {
  91. maxlength: 200,
  92. type: 'text',
  93. null: false,
  94. },
  95. active: true,
  96. screens: {},
  97. position: 20,
  98. created_by_id: 1,
  99. updated_by_id: 1,
  100. )
  101. end
  102. assert_raises(RuntimeError) do
  103. attribute4 = ObjectManager::Attribute.add(
  104. object: 'Ticket',
  105. name: 'test4',
  106. display: 'Test 4 with missing data_option[:type]',
  107. data_type: 'input',
  108. data_option: {
  109. maxlength: 200,
  110. null: false,
  111. },
  112. active: true,
  113. screens: {},
  114. position: 20,
  115. created_by_id: 1,
  116. updated_by_id: 1,
  117. )
  118. end
  119. attribute5 = ObjectManager::Attribute.add(
  120. object: 'Ticket',
  121. name: 'test5',
  122. display: 'Test 5',
  123. data_type: 'boolean',
  124. data_option: {
  125. default: true,
  126. options: {
  127. true: 'Yes',
  128. false: 'No',
  129. },
  130. null: false,
  131. },
  132. active: true,
  133. screens: {},
  134. position: 20,
  135. created_by_id: 1,
  136. updated_by_id: 1,
  137. )
  138. assert(attribute5)
  139. assert_equal('test5', attribute5.name)
  140. ObjectManager::Attribute.remove(
  141. object: 'Ticket',
  142. name: 'test5',
  143. )
  144. assert_raises(RuntimeError) do
  145. attribute6 = ObjectManager::Attribute.add(
  146. object: 'Ticket',
  147. name: 'test6',
  148. display: 'Test 6',
  149. data_type: 'boolean',
  150. data_option: {
  151. options: {
  152. true: 'Yes',
  153. false: 'No',
  154. },
  155. null: false,
  156. },
  157. active: true,
  158. screens: {},
  159. position: 20,
  160. created_by_id: 1,
  161. updated_by_id: 1,
  162. )
  163. end
  164. attribute7 = ObjectManager::Attribute.add(
  165. object: 'Ticket',
  166. name: 'test7',
  167. display: 'Test 7',
  168. data_type: 'select',
  169. data_option: {
  170. default: 1,
  171. options: {
  172. '1' => 'aa',
  173. '2' => 'bb',
  174. },
  175. null: false,
  176. },
  177. active: true,
  178. screens: {},
  179. position: 20,
  180. created_by_id: 1,
  181. updated_by_id: 1,
  182. )
  183. assert(attribute7)
  184. assert_equal('test7', attribute7.name)
  185. ObjectManager::Attribute.remove(
  186. object: 'Ticket',
  187. name: 'test7',
  188. )
  189. assert_raises(RuntimeError) do
  190. attribute8 = ObjectManager::Attribute.add(
  191. object: 'Ticket',
  192. name: 'test8',
  193. display: 'Test 8',
  194. data_type: 'select',
  195. data_option: {
  196. default: 1,
  197. null: false,
  198. },
  199. active: true,
  200. screens: {},
  201. position: 20,
  202. created_by_id: 1,
  203. updated_by_id: 1,
  204. )
  205. end
  206. attribute9 = ObjectManager::Attribute.add(
  207. object: 'Ticket',
  208. name: 'test9',
  209. display: 'Test 9',
  210. data_type: 'datetime',
  211. data_option: {
  212. future: true,
  213. past: false,
  214. diff: 24,
  215. null: true,
  216. },
  217. active: true,
  218. screens: {},
  219. position: 20,
  220. created_by_id: 1,
  221. updated_by_id: 1,
  222. )
  223. assert(attribute9)
  224. assert_equal('test9', attribute9.name)
  225. ObjectManager::Attribute.remove(
  226. object: 'Ticket',
  227. name: 'test9',
  228. )
  229. assert_raises(RuntimeError) do
  230. attribute10 = ObjectManager::Attribute.add(
  231. object: 'Ticket',
  232. name: 'test10',
  233. display: 'Test 10',
  234. data_type: 'datetime',
  235. data_option: {
  236. past: false,
  237. diff: 24,
  238. null: true,
  239. },
  240. active: true,
  241. screens: {},
  242. position: 20,
  243. created_by_id: 1,
  244. updated_by_id: 1,
  245. )
  246. end
  247. attribute11 = ObjectManager::Attribute.add(
  248. object: 'Ticket',
  249. name: 'test11',
  250. display: 'Test 11',
  251. data_type: 'date',
  252. data_option: {
  253. future: true,
  254. past: false,
  255. diff: 24,
  256. null: true,
  257. },
  258. active: true,
  259. screens: {},
  260. position: 20,
  261. created_by_id: 1,
  262. updated_by_id: 1,
  263. )
  264. assert(attribute11)
  265. assert_equal('test11', attribute11.name)
  266. ObjectManager::Attribute.remove(
  267. object: 'Ticket',
  268. name: 'test11',
  269. )
  270. assert_raises(RuntimeError) do
  271. attribute12 = ObjectManager::Attribute.add(
  272. object: 'Ticket',
  273. name: 'test12',
  274. display: 'Test 12',
  275. data_type: 'date',
  276. data_option: {
  277. past: false,
  278. diff: 24,
  279. null: true,
  280. },
  281. active: true,
  282. screens: {},
  283. position: 20,
  284. created_by_id: 1,
  285. updated_by_id: 1,
  286. )
  287. end
  288. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  289. assert_raises(RuntimeError) do
  290. attribute13 = ObjectManager::Attribute.add(
  291. object: 'Ticket',
  292. name: 'test13|',
  293. display: 'Test 13',
  294. data_type: 'date',
  295. data_option: {
  296. future: true,
  297. past: false,
  298. diff: 24,
  299. null: true,
  300. },
  301. active: true,
  302. screens: {},
  303. position: 20,
  304. created_by_id: 1,
  305. updated_by_id: 1,
  306. )
  307. end
  308. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  309. assert_raises(RuntimeError) do
  310. attribute14 = ObjectManager::Attribute.add(
  311. object: 'Ticket',
  312. name: 'test14!',
  313. display: 'Test 14',
  314. data_type: 'date',
  315. data_option: {
  316. future: true,
  317. past: false,
  318. diff: 24,
  319. null: true,
  320. },
  321. active: true,
  322. screens: {},
  323. position: 20,
  324. created_by_id: 1,
  325. updated_by_id: 1,
  326. )
  327. end
  328. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  329. assert_raises(RuntimeError) do
  330. attribute15 = ObjectManager::Attribute.add(
  331. object: 'Ticket',
  332. name: 'test15ä',
  333. display: 'Test 15',
  334. data_type: 'date',
  335. data_option: {
  336. future: true,
  337. past: false,
  338. diff: 24,
  339. null: true,
  340. },
  341. active: true,
  342. screens: {},
  343. position: 20,
  344. created_by_id: 1,
  345. updated_by_id: 1,
  346. )
  347. end
  348. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  349. assert_raises(RuntimeError) do
  350. attribute16 = ObjectManager::Attribute.add(
  351. object: 'Ticket',
  352. name: 'test16',
  353. display: 'Test 16',
  354. data_type: 'integer',
  355. data_option: {
  356. default: 2,
  357. min: 1,
  358. max: 999,
  359. },
  360. active: true,
  361. screens: {},
  362. position: 20,
  363. created_by_id: 1,
  364. updated_by_id: 1,
  365. )
  366. end
  367. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  368. assert_raises(RuntimeError) do
  369. attribute17 = ObjectManager::Attribute.add(
  370. object: 'Ticket',
  371. name: 'test17',
  372. display: 'Test 17',
  373. data_type: 'integer',
  374. data_option: {
  375. default: 2,
  376. min: 1,
  377. },
  378. active: true,
  379. screens: {},
  380. position: 20,
  381. created_by_id: 1,
  382. updated_by_id: 1,
  383. )
  384. end
  385. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  386. assert_raises(RuntimeError) do
  387. attribute18 = ObjectManager::Attribute.add(
  388. object: 'Ticket',
  389. name: 'delete',
  390. display: 'Test 18',
  391. data_type: 'input',
  392. data_option: {
  393. maxlength: 200,
  394. type: 'text',
  395. null: false,
  396. },
  397. active: true,
  398. screens: {},
  399. position: 20,
  400. created_by_id: 1,
  401. updated_by_id: 1,
  402. )
  403. end
  404. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  405. end
  406. test 'b object manager attribute' do
  407. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  408. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  409. assert_equal(0, ObjectManager::Attribute.migrations.count)
  410. attribute1 = ObjectManager::Attribute.add(
  411. object: 'Ticket',
  412. name: 'attribute1',
  413. display: 'Attribute 1',
  414. data_type: 'input',
  415. data_option: {
  416. maxlength: 200,
  417. type: 'text',
  418. null: true,
  419. },
  420. active: true,
  421. screens: {},
  422. position: 20,
  423. created_by_id: 1,
  424. updated_by_id: 1,
  425. )
  426. assert(attribute1)
  427. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  428. assert_equal(1, ObjectManager::Attribute.where(to_migrate: true).count)
  429. assert_equal(1, ObjectManager::Attribute.migrations.count)
  430. # execute migrations
  431. assert(ObjectManager::Attribute.migration_execute)
  432. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  433. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  434. assert_equal(0, ObjectManager::Attribute.migrations.count)
  435. # create example ticket
  436. ticket1 = Ticket.create(
  437. title: 'some attribute test1',
  438. group: Group.lookup(name: 'Users'),
  439. customer_id: 2,
  440. state: Ticket::State.lookup(name: 'new'),
  441. priority: Ticket::Priority.lookup(name: '2 normal'),
  442. attribute1: 'some attribute text',
  443. updated_by_id: 1,
  444. created_by_id: 1,
  445. )
  446. assert('ticket1 created', ticket1)
  447. assert_equal('some attribute test1', ticket1.title)
  448. assert_equal('Users', ticket1.group.name)
  449. assert_equal('new', ticket1.state.name)
  450. assert_equal('some attribute text', ticket1.attribute1)
  451. # add additional attributes
  452. attribute2 = ObjectManager::Attribute.add(
  453. object: 'Ticket',
  454. name: 'attribute2',
  455. display: 'Attribute 2',
  456. data_type: 'select',
  457. data_option: {
  458. default: '2',
  459. options: {
  460. '1' => 'aa',
  461. '2' => 'bb',
  462. },
  463. null: true,
  464. },
  465. active: true,
  466. screens: {},
  467. position: 20,
  468. created_by_id: 1,
  469. updated_by_id: 1,
  470. )
  471. attribute3 = ObjectManager::Attribute.add(
  472. object: 'Ticket',
  473. name: 'attribute3',
  474. display: 'Attribute 3',
  475. data_type: 'datetime',
  476. data_option: {
  477. future: true,
  478. past: false,
  479. diff: 24,
  480. null: true,
  481. },
  482. active: true,
  483. screens: {},
  484. position: 20,
  485. created_by_id: 1,
  486. updated_by_id: 1,
  487. )
  488. attribute4 = ObjectManager::Attribute.add(
  489. object: 'Ticket',
  490. name: 'attribute4',
  491. display: 'Attribute 4',
  492. data_type: 'datetime',
  493. data_option: {
  494. future: true,
  495. past: false,
  496. diff: 24,
  497. null: true,
  498. },
  499. active: true,
  500. screens: {},
  501. position: 20,
  502. created_by_id: 1,
  503. updated_by_id: 1,
  504. )
  505. # execute migrations
  506. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  507. assert(ObjectManager::Attribute.migration_execute)
  508. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  509. # create example ticket
  510. ticket2 = Ticket.create(
  511. title: 'some attribute test2',
  512. group: Group.lookup(name: 'Users'),
  513. customer_id: 2,
  514. state: Ticket::State.lookup(name: 'new'),
  515. priority: Ticket::Priority.lookup(name: '2 normal'),
  516. attribute1: 'some attribute text',
  517. attribute2: '1',
  518. attribute3: Time.zone.parse('2016-05-12 00:59:59 UTC'),
  519. attribute4: Date.parse('2016-05-11'),
  520. updated_by_id: 1,
  521. created_by_id: 1,
  522. )
  523. assert('ticket2 created', ticket2)
  524. assert_equal('some attribute test2', ticket2.title)
  525. assert_equal('Users', ticket2.group.name)
  526. assert_equal('new', ticket2.state.name)
  527. assert_equal('some attribute text', ticket2.attribute1)
  528. assert_equal('1', ticket2.attribute2)
  529. assert_equal(Time.zone.parse('2016-05-12 00:59:59 UTC'), ticket2.attribute3)
  530. assert_equal(Date.parse('2016-05-11'), ticket2.attribute4)
  531. # update data_option null -> to_config
  532. attribute1 = ObjectManager::Attribute.add(
  533. object: 'Ticket',
  534. name: 'attribute1',
  535. display: 'Attribute 1',
  536. data_type: 'input',
  537. data_option: {
  538. maxlength: 200,
  539. type: 'text',
  540. null: false,
  541. },
  542. active: true,
  543. screens: {},
  544. position: 20,
  545. created_by_id: 1,
  546. updated_by_id: 1,
  547. )
  548. assert(attribute1)
  549. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  550. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  551. assert_equal(1, ObjectManager::Attribute.where(to_config: true).count)
  552. assert_equal(1, ObjectManager::Attribute.migrations.count)
  553. # execute migrations
  554. assert(ObjectManager::Attribute.migration_execute)
  555. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  556. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  557. assert_equal(0, ObjectManager::Attribute.where(to_config: true).count)
  558. assert_equal(0, ObjectManager::Attribute.migrations.count)
  559. # update data_option maxlength -> to_config && to_migrate
  560. attribute1 = ObjectManager::Attribute.add(
  561. object: 'Ticket',
  562. name: 'attribute1',
  563. display: 'Attribute 1',
  564. data_type: 'input',
  565. data_option: {
  566. maxlength: 250,
  567. type: 'text',
  568. null: false,
  569. },
  570. active: true,
  571. screens: {},
  572. position: 20,
  573. created_by_id: 1,
  574. updated_by_id: 1,
  575. )
  576. assert(attribute1)
  577. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  578. assert_equal(1, ObjectManager::Attribute.where(to_migrate: true).count)
  579. assert_equal(1, ObjectManager::Attribute.where(to_config: true).count)
  580. assert_equal(1, ObjectManager::Attribute.migrations.count)
  581. # execute migrations
  582. assert(ObjectManager::Attribute.migration_execute)
  583. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  584. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  585. assert_equal(0, ObjectManager::Attribute.where(to_config: true).count)
  586. assert_equal(0, ObjectManager::Attribute.migrations.count)
  587. # remove attribute
  588. ObjectManager::Attribute.remove(
  589. object: 'Ticket',
  590. name: 'attribute1',
  591. )
  592. ObjectManager::Attribute.remove(
  593. object: 'Ticket',
  594. name: 'attribute2',
  595. )
  596. ObjectManager::Attribute.remove(
  597. object: 'Ticket',
  598. name: 'attribute3',
  599. )
  600. ObjectManager::Attribute.remove(
  601. object: 'Ticket',
  602. name: 'attribute4',
  603. )
  604. assert(ObjectManager::Attribute.migration_execute)
  605. ticket2 = Ticket.find(ticket2.id)
  606. assert('ticket2 created', ticket2)
  607. assert_equal('some attribute test2', ticket2.title)
  608. assert_equal('Users', ticket2.group.name)
  609. assert_equal('new', ticket2.state.name)
  610. assert_nil(ticket2[:attribute1])
  611. assert_nil(ticket2[:attribute2])
  612. assert_nil(ticket2[:attribute3])
  613. assert_nil(ticket2[:attribute4])
  614. end
  615. end