object_manager_test.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. end
  386. test 'b object manager attribute' do
  387. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  388. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  389. assert_equal(0, ObjectManager::Attribute.migrations.count)
  390. attribute1 = ObjectManager::Attribute.add(
  391. object: 'Ticket',
  392. name: 'attribute1',
  393. display: 'Attribute 1',
  394. data_type: 'input',
  395. data_option: {
  396. maxlength: 200,
  397. type: 'text',
  398. null: true,
  399. },
  400. active: true,
  401. screens: {},
  402. position: 20,
  403. created_by_id: 1,
  404. updated_by_id: 1,
  405. )
  406. assert(attribute1)
  407. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  408. assert_equal(1, ObjectManager::Attribute.where(to_migrate: true).count)
  409. assert_equal(1, ObjectManager::Attribute.migrations.count)
  410. # execute migrations
  411. assert(ObjectManager::Attribute.migration_execute)
  412. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  413. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  414. assert_equal(0, ObjectManager::Attribute.migrations.count)
  415. # create example ticket
  416. ticket1 = Ticket.create(
  417. title: 'some attribute test1',
  418. group: Group.lookup(name: 'Users'),
  419. customer_id: 2,
  420. state: Ticket::State.lookup(name: 'new'),
  421. priority: Ticket::Priority.lookup(name: '2 normal'),
  422. attribute1: 'some attribute text',
  423. updated_by_id: 1,
  424. created_by_id: 1,
  425. )
  426. assert('ticket1 created', ticket1)
  427. assert_equal('some attribute test1', ticket1.title)
  428. assert_equal('Users', ticket1.group.name)
  429. assert_equal('new', ticket1.state.name)
  430. assert_equal('some attribute text', ticket1.attribute1)
  431. # add additional attributes
  432. attribute2 = ObjectManager::Attribute.add(
  433. object: 'Ticket',
  434. name: 'attribute2',
  435. display: 'Attribute 2',
  436. data_type: 'select',
  437. data_option: {
  438. default: '2',
  439. options: {
  440. '1' => 'aa',
  441. '2' => 'bb',
  442. },
  443. null: true,
  444. },
  445. active: true,
  446. screens: {},
  447. position: 20,
  448. created_by_id: 1,
  449. updated_by_id: 1,
  450. )
  451. attribute3 = ObjectManager::Attribute.add(
  452. object: 'Ticket',
  453. name: 'attribute3',
  454. display: 'Attribute 3',
  455. data_type: 'datetime',
  456. data_option: {
  457. future: true,
  458. past: false,
  459. diff: 24,
  460. null: true,
  461. },
  462. active: true,
  463. screens: {},
  464. position: 20,
  465. created_by_id: 1,
  466. updated_by_id: 1,
  467. )
  468. attribute4 = ObjectManager::Attribute.add(
  469. object: 'Ticket',
  470. name: 'attribute4',
  471. display: 'Attribute 4',
  472. data_type: 'datetime',
  473. data_option: {
  474. future: true,
  475. past: false,
  476. diff: 24,
  477. null: true,
  478. },
  479. active: true,
  480. screens: {},
  481. position: 20,
  482. created_by_id: 1,
  483. updated_by_id: 1,
  484. )
  485. # execute migrations
  486. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  487. assert(ObjectManager::Attribute.migration_execute)
  488. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  489. # create example ticket
  490. ticket2 = Ticket.create(
  491. title: 'some attribute test2',
  492. group: Group.lookup(name: 'Users'),
  493. customer_id: 2,
  494. state: Ticket::State.lookup(name: 'new'),
  495. priority: Ticket::Priority.lookup(name: '2 normal'),
  496. attribute1: 'some attribute text',
  497. attribute2: '1',
  498. attribute3: Time.zone.parse('2016-05-12 00:59:59 UTC'),
  499. attribute4: Date.parse('2016-05-11'),
  500. updated_by_id: 1,
  501. created_by_id: 1,
  502. )
  503. assert('ticket2 created', ticket2)
  504. assert_equal('some attribute test2', ticket2.title)
  505. assert_equal('Users', ticket2.group.name)
  506. assert_equal('new', ticket2.state.name)
  507. assert_equal('some attribute text', ticket2.attribute1)
  508. assert_equal('1', ticket2.attribute2)
  509. assert_equal(Time.zone.parse('2016-05-12 00:59:59 UTC'), ticket2.attribute3)
  510. assert_equal(Date.parse('2016-05-11'), ticket2.attribute4)
  511. # update data_option null -> to_config
  512. attribute1 = ObjectManager::Attribute.add(
  513. object: 'Ticket',
  514. name: 'attribute1',
  515. display: 'Attribute 1',
  516. data_type: 'input',
  517. data_option: {
  518. maxlength: 200,
  519. type: 'text',
  520. null: false,
  521. },
  522. active: true,
  523. screens: {},
  524. position: 20,
  525. created_by_id: 1,
  526. updated_by_id: 1,
  527. )
  528. assert(attribute1)
  529. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  530. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  531. assert_equal(1, ObjectManager::Attribute.where(to_config: true).count)
  532. assert_equal(1, ObjectManager::Attribute.migrations.count)
  533. # execute migrations
  534. assert(ObjectManager::Attribute.migration_execute)
  535. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  536. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  537. assert_equal(0, ObjectManager::Attribute.where(to_config: true).count)
  538. assert_equal(0, ObjectManager::Attribute.migrations.count)
  539. # update data_option maxlength -> to_config && to_migrate
  540. attribute1 = ObjectManager::Attribute.add(
  541. object: 'Ticket',
  542. name: 'attribute1',
  543. display: 'Attribute 1',
  544. data_type: 'input',
  545. data_option: {
  546. maxlength: 250,
  547. type: 'text',
  548. null: false,
  549. },
  550. active: true,
  551. screens: {},
  552. position: 20,
  553. created_by_id: 1,
  554. updated_by_id: 1,
  555. )
  556. assert(attribute1)
  557. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  558. assert_equal(1, ObjectManager::Attribute.where(to_migrate: true).count)
  559. assert_equal(1, ObjectManager::Attribute.where(to_config: true).count)
  560. assert_equal(1, ObjectManager::Attribute.migrations.count)
  561. # execute migrations
  562. assert(ObjectManager::Attribute.migration_execute)
  563. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  564. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  565. assert_equal(0, ObjectManager::Attribute.where(to_config: true).count)
  566. assert_equal(0, ObjectManager::Attribute.migrations.count)
  567. # remove attribute
  568. ObjectManager::Attribute.remove(
  569. object: 'Ticket',
  570. name: 'attribute1',
  571. )
  572. ObjectManager::Attribute.remove(
  573. object: 'Ticket',
  574. name: 'attribute2',
  575. )
  576. ObjectManager::Attribute.remove(
  577. object: 'Ticket',
  578. name: 'attribute3',
  579. )
  580. ObjectManager::Attribute.remove(
  581. object: 'Ticket',
  582. name: 'attribute4',
  583. )
  584. assert(ObjectManager::Attribute.migration_execute)
  585. ticket2 = Ticket.find(ticket2.id)
  586. assert('ticket2 created', ticket2)
  587. assert_equal('some attribute test2', ticket2.title)
  588. assert_equal('Users', ticket2.group.name)
  589. assert_equal('new', ticket2.state.name)
  590. assert_equal(nil, ticket2[:attribute1])
  591. assert_equal(nil, ticket2[:attribute2])
  592. assert_equal(nil, ticket2[:attribute3])
  593. assert_equal(nil, ticket2[:attribute4])
  594. end
  595. end