tag_test.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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(!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 = if test[:tag_add]
  145. test[:tag_add]
  146. else
  147. test[:tag_remove]
  148. end
  149. success = Tag.tag_remove(tags)
  150. assert(success, 'Tag.tag_remove successful')
  151. list = Tag.tag_list(tags)
  152. assert(!list.include?(tags[:item]), 'Tag entry destroyed')
  153. end
  154. end
  155. test 'tags - real live' do
  156. ticket1 = Ticket.create(
  157. title: 'some title tag1',
  158. group: Group.lookup(name: 'Users'),
  159. customer_id: 2,
  160. state: Ticket::State.lookup(name: 'new'),
  161. priority: Ticket::Priority.lookup(name: '2 normal'),
  162. updated_by_id: 1,
  163. created_by_id: 1,
  164. )
  165. ticket2 = Ticket.create(
  166. title: 'some title tag2',
  167. group: Group.lookup(name: 'Users'),
  168. customer_id: 2,
  169. state: Ticket::State.lookup(name: 'new'),
  170. priority: Ticket::Priority.lookup(name: '2 normal'),
  171. updated_by_id: 1,
  172. created_by_id: 1,
  173. )
  174. travel 2.seconds
  175. ticket1.tag_add('some tag1', 1)
  176. ticket1.tag_add('some tag2 ', 1)
  177. ticket1.tag_add(' some tag3', 1)
  178. ticket1.tag_add('some TAG4', 1)
  179. ticket1.tag_add(' some tag4', 1)
  180. ticket2.tag_add('some tag3', 1)
  181. ticket2.tag_add('some TAG4', 1)
  182. ticket2.tag_add('some tag4 ', 1)
  183. ticket1.tag_remove('some tag1', 1)
  184. ticket1_lookup1 = Ticket.lookup(id: ticket1.id)
  185. assert_not_equal(ticket1.updated_at.to_s, ticket1_lookup1.updated_at.to_s)
  186. ticket2_lookup1 = Ticket.lookup(id: ticket2.id)
  187. assert_not_equal(ticket2.updated_at.to_s, ticket2_lookup1.updated_at.to_s)
  188. tags_ticket1 = ticket1.tag_list
  189. assert_equal(4, tags_ticket1.count)
  190. assert(tags_ticket1.include?('some tag2'))
  191. assert(tags_ticket1.include?('some tag3'))
  192. assert(tags_ticket1.include?('some TAG4'))
  193. assert(tags_ticket1.include?('some tag4'))
  194. tags_ticket2 = ticket2.tag_list
  195. assert_equal(3, tags_ticket2.count)
  196. assert(tags_ticket2.include?('some tag3'))
  197. assert(tags_ticket2.include?('some TAG4'))
  198. assert(tags_ticket2.include?('some tag4'))
  199. # rename tag
  200. travel 2.seconds
  201. tag_item3 = Tag::Item.find_by(name: 'some tag3')
  202. Tag::Item.rename(
  203. id: tag_item3.id,
  204. name: ' some tag33',
  205. created_by_id: 1,
  206. )
  207. ticket1_lookup2 = Ticket.lookup(id: ticket1.id)
  208. assert_not_equal(ticket1_lookup2.updated_at.to_s, ticket1_lookup1.updated_at.to_s)
  209. ticket2_lookup2 = Ticket.lookup(id: ticket2.id)
  210. assert_not_equal(ticket2_lookup2.updated_at.to_s, ticket2_lookup1.updated_at.to_s)
  211. tags_ticket1 = ticket1.tag_list
  212. assert_equal(4, tags_ticket1.count)
  213. assert(tags_ticket1.include?('some tag2'))
  214. assert(tags_ticket1.include?('some tag33'))
  215. assert(tags_ticket1.include?('some TAG4'))
  216. assert(tags_ticket1.include?('some tag4'))
  217. tags_ticket2 = ticket2.tag_list
  218. assert_equal(3, tags_ticket2.count)
  219. assert(tags_ticket2.include?('some tag33'))
  220. assert(tags_ticket2.include?('some TAG4'))
  221. assert(tags_ticket2.include?('some tag4'))
  222. # merge tags
  223. travel 2.seconds
  224. Tag::Item.rename(
  225. id: tag_item3.id,
  226. name: 'some tag2',
  227. created_by_id: 1,
  228. )
  229. ticket1_lookup3 = Ticket.lookup(id: ticket1.id)
  230. assert_not_equal(ticket1_lookup3.updated_at.to_s, ticket1_lookup2.updated_at.to_s)
  231. ticket2_lookup3 = Ticket.lookup(id: ticket2.id)
  232. assert_not_equal(ticket2_lookup3.updated_at.to_s, ticket2_lookup2.updated_at.to_s)
  233. tags_ticket1 = ticket1.tag_list
  234. assert_equal(3, tags_ticket1.count)
  235. assert(tags_ticket1.include?('some tag2'))
  236. assert(tags_ticket1.include?('some TAG4'))
  237. assert(tags_ticket1.include?('some tag4'))
  238. tags_ticket2 = ticket2.tag_list
  239. assert_equal(3, tags_ticket2.count)
  240. assert(tags_ticket2.include?('some tag2'))
  241. assert(tags_ticket2.include?('some TAG4'))
  242. assert(tags_ticket2.include?('some tag4'))
  243. assert_not(Tag::Item.find_by(id: tag_item3.id))
  244. # remove tag item
  245. travel 2.seconds
  246. tag_item4 = Tag::Item.find_by(name: 'some TAG4')
  247. Tag::Item.remove(tag_item4.id)
  248. tags_ticket1 = ticket1.tag_list
  249. assert_equal(2, tags_ticket1.count)
  250. assert(tags_ticket1.include?('some tag2'))
  251. assert(tags_ticket1.include?('some tag4'))
  252. tags_ticket2 = ticket2.tag_list
  253. assert_equal(2, tags_ticket2.count)
  254. assert(tags_ticket2.include?('some tag2'))
  255. assert(tags_ticket2.include?('some tag4'))
  256. assert_not(Tag::Item.find_by(id: tag_item4.id))
  257. ticket1_lookup4 = Ticket.lookup(id: ticket1.id)
  258. assert_not_equal(ticket1_lookup4.updated_at.to_s, ticket1_lookup3.updated_at.to_s)
  259. ticket2_lookup4 = Ticket.lookup(id: ticket2.id)
  260. assert_not_equal(ticket2_lookup4.updated_at.to_s, ticket2_lookup3.updated_at.to_s)
  261. travel_back
  262. end
  263. test 'tags - rename tag with same name' do
  264. ticket1 = Ticket.create(
  265. title: 'rename tag1',
  266. group: Group.lookup(name: 'Users'),
  267. customer_id: 2,
  268. updated_by_id: 1,
  269. created_by_id: 1,
  270. )
  271. ticket2 = Ticket.create(
  272. title: 'rename tag2',
  273. group: Group.lookup(name: 'Users'),
  274. customer_id: 2,
  275. updated_by_id: 1,
  276. created_by_id: 1,
  277. )
  278. ticket1.tag_add('some rename tag1', 1)
  279. ticket1.tag_add('some rename tag2 ', 1)
  280. ticket2.tag_add('some rename tag2', 1)
  281. tags_ticket1 = ticket1.tag_list
  282. assert_equal(2, tags_ticket1.count)
  283. assert(tags_ticket1.include?('some rename tag1'))
  284. assert(tags_ticket1.include?('some rename tag2'))
  285. tags_ticket2 = ticket2.tag_list
  286. assert_equal(1, tags_ticket2.count)
  287. assert(tags_ticket2.include?('some rename tag2'))
  288. tag_item1 = Tag::Item.find_by(name: 'some rename tag1')
  289. Tag::Item.rename(
  290. id: tag_item1.id,
  291. name: ' some rename tag1',
  292. created_by_id: 1,
  293. )
  294. tags_ticket1 = ticket1.tag_list
  295. assert_equal(2, tags_ticket1.count)
  296. assert(tags_ticket1.include?('some rename tag1'))
  297. assert(tags_ticket1.include?('some rename tag2'))
  298. tags_ticket2 = ticket2.tag_list
  299. assert_equal(1, tags_ticket2.count)
  300. assert(tags_ticket2.include?('some rename tag2'))
  301. end
  302. test 'tags - rename and merge tag with existing tag' do
  303. ticket1 = Ticket.create(
  304. title: 'rename tag1',
  305. group: Group.lookup(name: 'Users'),
  306. customer_id: 2,
  307. updated_by_id: 1,
  308. created_by_id: 1,
  309. )
  310. ticket2 = Ticket.create(
  311. title: 'rename tag2',
  312. group: Group.lookup(name: 'Users'),
  313. customer_id: 2,
  314. updated_by_id: 1,
  315. created_by_id: 1,
  316. )
  317. ticket1.tag_add('tagname1', 1)
  318. ticket1.tag_add('tagname2', 1)
  319. ticket2.tag_add('Tagname2', 1)
  320. tags_ticket1 = ticket1.tag_list
  321. assert_equal(2, tags_ticket1.count)
  322. assert(tags_ticket1.include?('tagname1'))
  323. assert(tags_ticket1.include?('tagname2'))
  324. tags_ticket2 = ticket2.tag_list
  325. assert_equal(1, tags_ticket2.count)
  326. assert(tags_ticket2.include?('Tagname2'))
  327. tag_item1 = Tag::Item.lookup(name: 'Tagname2')
  328. Tag::Item.rename(
  329. id: tag_item1.id,
  330. name: 'tagname2',
  331. created_by_id: 1,
  332. )
  333. tags_ticket1 = ticket1.tag_list
  334. assert_equal(2, tags_ticket1.count)
  335. assert(tags_ticket1.include?('tagname1'))
  336. assert(tags_ticket1.include?('tagname2'))
  337. tags_ticket2 = ticket2.tag_list
  338. assert_equal(1, tags_ticket2.count)
  339. assert(tags_ticket2.include?('tagname2'))
  340. end
  341. end