object_manager_test.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. end
  349. test 'b object manager attribute' do
  350. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  351. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  352. assert_equal(0, ObjectManager::Attribute.migrations.count)
  353. attribute1 = ObjectManager::Attribute.add(
  354. object: 'Ticket',
  355. name: 'attribute1',
  356. display: 'Attribute 1',
  357. data_type: 'input',
  358. data_option: {
  359. maxlength: 200,
  360. type: 'text',
  361. null: true,
  362. },
  363. active: true,
  364. screens: {},
  365. position: 20,
  366. created_by_id: 1,
  367. updated_by_id: 1,
  368. )
  369. assert(attribute1)
  370. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  371. assert_equal(1, ObjectManager::Attribute.where(to_migrate: true).count)
  372. assert_equal(1, ObjectManager::Attribute.migrations.count)
  373. # execute migrations
  374. assert(ObjectManager::Attribute.migration_execute)
  375. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  376. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  377. assert_equal(0, ObjectManager::Attribute.migrations.count)
  378. # create example ticket
  379. ticket1 = Ticket.create(
  380. title: 'some attribute test1',
  381. group: Group.lookup(name: 'Users'),
  382. customer_id: 2,
  383. state: Ticket::State.lookup(name: 'new'),
  384. priority: Ticket::Priority.lookup(name: '2 normal'),
  385. attribute1: 'some attribute text',
  386. updated_by_id: 1,
  387. created_by_id: 1,
  388. )
  389. assert('ticket1 created', ticket1)
  390. assert_equal('some attribute test1', ticket1.title)
  391. assert_equal('Users', ticket1.group.name)
  392. assert_equal('new', ticket1.state.name)
  393. assert_equal('some attribute text', ticket1.attribute1)
  394. # add additional attributes
  395. attribute2 = ObjectManager::Attribute.add(
  396. object: 'Ticket',
  397. name: 'attribute2',
  398. display: 'Attribute 2',
  399. data_type: 'select',
  400. data_option: {
  401. default: '2',
  402. options: {
  403. '1' => 'aa',
  404. '2' => 'bb',
  405. },
  406. null: true,
  407. },
  408. active: true,
  409. screens: {},
  410. position: 20,
  411. created_by_id: 1,
  412. updated_by_id: 1,
  413. )
  414. attribute3 = ObjectManager::Attribute.add(
  415. object: 'Ticket',
  416. name: 'attribute3',
  417. display: 'Attribute 3',
  418. data_type: 'datetime',
  419. data_option: {
  420. future: true,
  421. past: false,
  422. diff: 24,
  423. null: true,
  424. },
  425. active: true,
  426. screens: {},
  427. position: 20,
  428. created_by_id: 1,
  429. updated_by_id: 1,
  430. )
  431. attribute4 = ObjectManager::Attribute.add(
  432. object: 'Ticket',
  433. name: 'attribute4',
  434. display: 'Attribute 4',
  435. data_type: 'datetime',
  436. data_option: {
  437. future: true,
  438. past: false,
  439. diff: 24,
  440. null: true,
  441. },
  442. active: true,
  443. screens: {},
  444. position: 20,
  445. created_by_id: 1,
  446. updated_by_id: 1,
  447. )
  448. # execute migrations
  449. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  450. assert(ObjectManager::Attribute.migration_execute)
  451. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  452. # create example ticket
  453. ticket2 = Ticket.create(
  454. title: 'some attribute test2',
  455. group: Group.lookup(name: 'Users'),
  456. customer_id: 2,
  457. state: Ticket::State.lookup(name: 'new'),
  458. priority: Ticket::Priority.lookup(name: '2 normal'),
  459. attribute1: 'some attribute text',
  460. attribute2: '1',
  461. attribute3: Time.zone.parse('2016-05-12 00:59:59 UTC'),
  462. attribute4: Date.parse('2016-05-11'),
  463. updated_by_id: 1,
  464. created_by_id: 1,
  465. )
  466. assert('ticket2 created', ticket2)
  467. assert_equal('some attribute test2', ticket2.title)
  468. assert_equal('Users', ticket2.group.name)
  469. assert_equal('new', ticket2.state.name)
  470. assert_equal('some attribute text', ticket2.attribute1)
  471. assert_equal('1', ticket2.attribute2)
  472. assert_equal(Time.zone.parse('2016-05-12 00:59:59 UTC'), ticket2.attribute3)
  473. assert_equal(Date.parse('2016-05-11'), ticket2.attribute4)
  474. # remove attribute
  475. ObjectManager::Attribute.remove(
  476. object: 'Ticket',
  477. name: 'attribute1',
  478. )
  479. ObjectManager::Attribute.remove(
  480. object: 'Ticket',
  481. name: 'attribute2',
  482. )
  483. ObjectManager::Attribute.remove(
  484. object: 'Ticket',
  485. name: 'attribute3',
  486. )
  487. ObjectManager::Attribute.remove(
  488. object: 'Ticket',
  489. name: 'attribute4',
  490. )
  491. assert(ObjectManager::Attribute.migration_execute)
  492. ticket2 = Ticket.find(ticket2.id)
  493. assert('ticket2 created', ticket2)
  494. assert_equal('some attribute test2', ticket2.title)
  495. assert_equal('Users', ticket2.group.name)
  496. assert_equal('new', ticket2.state.name)
  497. assert_equal(nil, ticket2[:attribute1])
  498. assert_equal(nil, ticket2[:attribute2])
  499. assert_equal(nil, ticket2[:attribute3])
  500. assert_equal(nil, ticket2[:attribute4])
  501. end
  502. end