tag_test.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. require 'test_helper'
  2. class TagTest < ActiveSupport::TestCase
  3. test 'tags' do
  4. tests = [
  5. # test 1
  6. {
  7. tag_add: {
  8. item: 'tag1',
  9. object: 'Object1',
  10. o_id: 123,
  11. created_by_id: 1
  12. },
  13. verify: {
  14. object: 'Object1',
  15. items: {
  16. 'tag1' => true,
  17. 'tag2' => false,
  18. },
  19. },
  20. },
  21. # test 2
  22. {
  23. tag_add: {
  24. item: 'tag2',
  25. object: 'Object1',
  26. o_id: 123,
  27. created_by_id: 1
  28. },
  29. verify: {
  30. object: 'Object1',
  31. items: {
  32. 'tag1' => true,
  33. 'tag2' => true,
  34. },
  35. },
  36. },
  37. {
  38. tag_add: {
  39. item: 'TAG2',
  40. object: 'Object1',
  41. o_id: 123,
  42. created_by_id: 1
  43. },
  44. verify: {
  45. object: 'Object1',
  46. items: {
  47. 'tag1' => true,
  48. 'tag2' => true,
  49. 'TAG2' => true,
  50. },
  51. },
  52. },
  53. # test 2
  54. {
  55. tag_add: {
  56. item: 'tagöäüß1',
  57. object: 'Object2',
  58. o_id: 123,
  59. created_by_id: 1
  60. },
  61. verify: {
  62. object: 'Object2',
  63. items: {
  64. 'tagöäüß1' => true,
  65. 'tag2' => false,
  66. },
  67. },
  68. },
  69. # test 4
  70. {
  71. tag_add: {
  72. item: 'Tagöäüß2',
  73. object: 'Object2',
  74. o_id: 123,
  75. created_by_id: 1
  76. },
  77. verify: {
  78. object: 'Object2',
  79. items: {
  80. 'tagöäüß1' => true,
  81. 'Tagöäüß2' => true,
  82. 'tagöäüß3' => false,
  83. },
  84. },
  85. },
  86. # test 5
  87. {
  88. tag_remove: {
  89. item: 'tag1',
  90. object: 'Object1',
  91. o_id: 123,
  92. created_by_id: 1
  93. },
  94. verify: {
  95. object: 'Object1',
  96. items: {
  97. 'tag1' => false,
  98. 'tag2' => true,
  99. 'TAG2' => true,
  100. },
  101. },
  102. },
  103. # test 5
  104. {
  105. tag_remove: {
  106. item: 'TAG2',
  107. object: 'Object1',
  108. o_id: 123,
  109. created_by_id: 1
  110. },
  111. verify: {
  112. object: 'Object1',
  113. items: {
  114. 'tag1' => false,
  115. 'tag2' => true,
  116. 'TAG2' => false,
  117. },
  118. },
  119. },
  120. ]
  121. tests.each do |test|
  122. tags = nil
  123. if test[:tag_add]
  124. tags = test[:tag_add]
  125. success = Tag.tag_add(tags)
  126. assert(success, 'Tag.tag_add successful')
  127. else
  128. tags = test[:tag_remove]
  129. success = Tag.tag_remove(tags)
  130. assert(success, 'Tag.tag_remove successful')
  131. end
  132. list = Tag.tag_list(tags)
  133. test[:verify][:items].each do |key, value|
  134. if value == true
  135. assert(list.include?(key), "Tag verify - should exists but exists #{key}")
  136. else
  137. assert_not(list.include?(key), "Tag verify - exists but should not #{key}")
  138. end
  139. end
  140. end
  141. # delete tags
  142. tests.each do |test|
  143. tags = nil
  144. tags = test[:tag_add] || test[:tag_remove]
  145. success = Tag.tag_remove(tags)
  146. assert(success, 'Tag.tag_remove successful')
  147. list = Tag.tag_list(tags)
  148. assert_not(list.include?(tags[:item]), 'Tag entry destroyed')
  149. end
  150. end
  151. test 'tags - real live' do
  152. ticket1 = Ticket.create(
  153. title: 'some title tag1',
  154. group: Group.lookup(name: 'Users'),
  155. customer_id: 2,
  156. state: Ticket::State.lookup(name: 'new'),
  157. priority: Ticket::Priority.lookup(name: '2 normal'),
  158. updated_by_id: 1,
  159. created_by_id: 1,
  160. )
  161. ticket2 = Ticket.create(
  162. title: 'some title tag2',
  163. group: Group.lookup(name: 'Users'),
  164. customer_id: 2,
  165. state: Ticket::State.lookup(name: 'new'),
  166. priority: Ticket::Priority.lookup(name: '2 normal'),
  167. updated_by_id: 1,
  168. created_by_id: 1,
  169. )
  170. travel 2.seconds
  171. ticket1.tag_add('some tag1', 1)
  172. ticket1.tag_add('some tag2 ', 1)
  173. ticket1.tag_add(' some tag3', 1)
  174. ticket1.tag_add('some TAG4', 1)
  175. ticket1.tag_add(' some tag4', 1)
  176. ticket2.tag_add('some tag3', 1)
  177. ticket2.tag_add('some TAG4', 1)
  178. ticket2.tag_add('some tag4 ', 1)
  179. ticket1.tag_remove('some tag1', 1)
  180. ticket1_lookup1 = Ticket.lookup(id: ticket1.id)
  181. assert_not_equal(ticket1.updated_at.to_s, ticket1_lookup1.updated_at.to_s)
  182. ticket2_lookup1 = Ticket.lookup(id: ticket2.id)
  183. assert_not_equal(ticket2.updated_at.to_s, ticket2_lookup1.updated_at.to_s)
  184. tags_ticket1 = ticket1.tag_list
  185. assert_equal(4, tags_ticket1.count)
  186. assert(tags_ticket1.include?('some tag2'))
  187. assert(tags_ticket1.include?('some tag3'))
  188. assert(tags_ticket1.include?('some TAG4'))
  189. assert(tags_ticket1.include?('some tag4'))
  190. tags_ticket2 = ticket2.tag_list
  191. assert_equal(3, tags_ticket2.count)
  192. assert(tags_ticket2.include?('some tag3'))
  193. assert(tags_ticket2.include?('some TAG4'))
  194. assert(tags_ticket2.include?('some tag4'))
  195. # rename tag
  196. travel 2.seconds
  197. tag_item3 = Tag::Item.find_by(name: 'some tag3')
  198. Tag::Item.rename(
  199. id: tag_item3.id,
  200. name: ' some tag33',
  201. created_by_id: 1,
  202. )
  203. ticket1_lookup2 = Ticket.lookup(id: ticket1.id)
  204. assert_not_equal(ticket1_lookup2.updated_at.to_s, ticket1_lookup1.updated_at.to_s)
  205. ticket2_lookup2 = Ticket.lookup(id: ticket2.id)
  206. assert_not_equal(ticket2_lookup2.updated_at.to_s, ticket2_lookup1.updated_at.to_s)
  207. tags_ticket1 = ticket1.tag_list
  208. assert_equal(4, tags_ticket1.count)
  209. assert(tags_ticket1.include?('some tag2'))
  210. assert(tags_ticket1.include?('some tag33'))
  211. assert(tags_ticket1.include?('some TAG4'))
  212. assert(tags_ticket1.include?('some tag4'))
  213. tags_ticket2 = ticket2.tag_list
  214. assert_equal(3, tags_ticket2.count)
  215. assert(tags_ticket2.include?('some tag33'))
  216. assert(tags_ticket2.include?('some TAG4'))
  217. assert(tags_ticket2.include?('some tag4'))
  218. # merge tags
  219. travel 2.seconds
  220. Tag::Item.rename(
  221. id: tag_item3.id,
  222. name: 'some tag2',
  223. created_by_id: 1,
  224. )
  225. ticket1_lookup3 = Ticket.lookup(id: ticket1.id)
  226. assert_not_equal(ticket1_lookup3.updated_at.to_s, ticket1_lookup2.updated_at.to_s)
  227. ticket2_lookup3 = Ticket.lookup(id: ticket2.id)
  228. assert_not_equal(ticket2_lookup3.updated_at.to_s, ticket2_lookup2.updated_at.to_s)
  229. tags_ticket1 = ticket1.tag_list
  230. assert_equal(3, tags_ticket1.count)
  231. assert(tags_ticket1.include?('some tag2'))
  232. assert(tags_ticket1.include?('some TAG4'))
  233. assert(tags_ticket1.include?('some tag4'))
  234. tags_ticket2 = ticket2.tag_list
  235. assert_equal(3, tags_ticket2.count)
  236. assert(tags_ticket2.include?('some tag2'))
  237. assert(tags_ticket2.include?('some TAG4'))
  238. assert(tags_ticket2.include?('some tag4'))
  239. assert_not(Tag::Item.find_by(id: tag_item3.id))
  240. # remove tag item
  241. travel 2.seconds
  242. tag_item4 = Tag::Item.find_by(name: 'some TAG4')
  243. Tag::Item.remove(tag_item4.id)
  244. tags_ticket1 = ticket1.tag_list
  245. assert_equal(2, tags_ticket1.count)
  246. assert(tags_ticket1.include?('some tag2'))
  247. assert(tags_ticket1.include?('some tag4'))
  248. tags_ticket2 = ticket2.tag_list
  249. assert_equal(2, tags_ticket2.count)
  250. assert(tags_ticket2.include?('some tag2'))
  251. assert(tags_ticket2.include?('some tag4'))
  252. assert_not(Tag::Item.find_by(id: tag_item4.id))
  253. ticket1_lookup4 = Ticket.lookup(id: ticket1.id)
  254. assert_not_equal(ticket1_lookup4.updated_at.to_s, ticket1_lookup3.updated_at.to_s)
  255. ticket2_lookup4 = Ticket.lookup(id: ticket2.id)
  256. assert_not_equal(ticket2_lookup4.updated_at.to_s, ticket2_lookup3.updated_at.to_s)
  257. travel_back
  258. end
  259. test 'tags - rename tag with same name' do
  260. ticket1 = Ticket.create(
  261. title: 'rename tag1',
  262. group: Group.lookup(name: 'Users'),
  263. customer_id: 2,
  264. updated_by_id: 1,
  265. created_by_id: 1,
  266. )
  267. ticket2 = Ticket.create(
  268. title: 'rename tag2',
  269. group: Group.lookup(name: 'Users'),
  270. customer_id: 2,
  271. updated_by_id: 1,
  272. created_by_id: 1,
  273. )
  274. ticket1.tag_add('some rename tag1', 1)
  275. ticket1.tag_add('some rename tag2 ', 1)
  276. ticket2.tag_add('some rename tag2', 1)
  277. tags_ticket1 = ticket1.tag_list
  278. assert_equal(2, tags_ticket1.count)
  279. assert(tags_ticket1.include?('some rename tag1'))
  280. assert(tags_ticket1.include?('some rename tag2'))
  281. tags_ticket2 = ticket2.tag_list
  282. assert_equal(1, tags_ticket2.count)
  283. assert(tags_ticket2.include?('some rename tag2'))
  284. tag_item1 = Tag::Item.find_by(name: 'some rename tag1')
  285. Tag::Item.rename(
  286. id: tag_item1.id,
  287. name: ' some rename tag1',
  288. created_by_id: 1,
  289. )
  290. tags_ticket1 = ticket1.tag_list
  291. assert_equal(2, tags_ticket1.count)
  292. assert(tags_ticket1.include?('some rename tag1'))
  293. assert(tags_ticket1.include?('some rename tag2'))
  294. tags_ticket2 = ticket2.tag_list
  295. assert_equal(1, tags_ticket2.count)
  296. assert(tags_ticket2.include?('some rename tag2'))
  297. end
  298. test 'tags - rename and merge tag with existing tag' do
  299. ticket1 = Ticket.create(
  300. title: 'rename tag1',
  301. group: Group.lookup(name: 'Users'),
  302. customer_id: 2,
  303. updated_by_id: 1,
  304. created_by_id: 1,
  305. )
  306. ticket2 = Ticket.create(
  307. title: 'rename tag2',
  308. group: Group.lookup(name: 'Users'),
  309. customer_id: 2,
  310. updated_by_id: 1,
  311. created_by_id: 1,
  312. )
  313. ticket1.tag_add('tagname1', 1)
  314. ticket1.tag_add('tagname2', 1)
  315. ticket2.tag_add('Tagname2', 1)
  316. tags_ticket1 = ticket1.tag_list
  317. assert_equal(2, tags_ticket1.count)
  318. assert(tags_ticket1.include?('tagname1'))
  319. assert(tags_ticket1.include?('tagname2'))
  320. tags_ticket2 = ticket2.tag_list
  321. assert_equal(1, tags_ticket2.count)
  322. assert(tags_ticket2.include?('Tagname2'))
  323. tag_item1 = Tag::Item.lookup(name: 'Tagname2')
  324. Tag::Item.rename(
  325. id: tag_item1.id,
  326. name: 'tagname2',
  327. created_by_id: 1,
  328. )
  329. tags_ticket1 = ticket1.tag_list
  330. assert_equal(2, tags_ticket1.count)
  331. assert(tags_ticket1.include?('tagname1'))
  332. assert(tags_ticket1.include?('tagname2'))
  333. tags_ticket2 = ticket2.tag_list
  334. assert_equal(1, tags_ticket2.count)
  335. assert(tags_ticket2.include?('tagname2'))
  336. end
  337. end