object_manager_test.rb 17 KB

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