text_module_csv_import_test.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. require 'test_helper'
  2. class TextModuleCsvImportTest < ActiveSupport::TestCase
  3. test 'import example verify' do
  4. TextModule.load('en-en')
  5. csv_string = TextModule.csv_example
  6. rows = CSV.parse(csv_string)
  7. header = rows.shift
  8. assert_equal('id', header[0])
  9. assert_equal('name', header[1])
  10. assert_equal('keywords', header[2])
  11. assert_equal('content', header[3])
  12. assert_equal('note', header[4])
  13. assert_equal('active', header[5])
  14. assert_not(header.include?('organization'))
  15. assert_not(header.include?('priority'))
  16. assert_not(header.include?('state'))
  17. assert_not(header.include?('owner'))
  18. assert_not(header.include?('customer'))
  19. end
  20. test 'empty payload' do
  21. csv_string = ''
  22. result = TextModule.csv_import(
  23. string: csv_string,
  24. parse_params: {
  25. col_sep: ';',
  26. },
  27. try: true,
  28. )
  29. assert_equal(true, result[:try])
  30. assert_nil(result[:records])
  31. assert_equal('failed', result[:result])
  32. assert_equal('Unable to parse empty file/string for TextModule.', result[:errors][0])
  33. csv_string = 'name;keywords;content;note;active;'
  34. result = TextModule.csv_import(
  35. string: csv_string,
  36. parse_params: {
  37. col_sep: ';',
  38. },
  39. try: true,
  40. )
  41. assert_equal(true, result[:try])
  42. assert(result[:records].blank?)
  43. assert_equal('failed', result[:result])
  44. assert_equal('No records found in file/string for TextModule.', result[:errors][0])
  45. end
  46. test 'verify required lookup headers' do
  47. csv_string = "firstname;lastname;active;\nfirstname-simple-import1;lastname-simple-import1;;true\nfirstname-simple-import2;lastname-simple-import2;false\n"
  48. result = TextModule.csv_import(
  49. string: csv_string,
  50. parse_params: {
  51. col_sep: ';',
  52. },
  53. try: true,
  54. )
  55. assert_equal(true, result[:try])
  56. assert_equal('failed', result[:result])
  57. assert_equal('No lookup column like id,name for TextModule found.', result[:errors][0])
  58. end
  59. test 'simple import' do
  60. TextModule.create!(
  61. name: 'nsome name1',
  62. content: 'nsome name1',
  63. active: true,
  64. updated_by_id: 1,
  65. created_by_id: 1,
  66. )
  67. csv_string = "name;keywords;content;note;active;\nsome name1;keyword1;\"some\ncontent1\";-;\nsome name2;keyword2;some content<br>test123\n"
  68. result = TextModule.csv_import(
  69. string: csv_string,
  70. parse_params: {
  71. col_sep: ';',
  72. },
  73. try: true,
  74. )
  75. assert_equal(true, result[:try])
  76. assert_equal(2, result[:records].count)
  77. assert_equal('success', result[:result])
  78. assert_nil(TextModule.find_by(name: 'some name1'))
  79. assert_nil(TextModule.find_by(name: 'some name2'))
  80. result = TextModule.csv_import(
  81. string: csv_string,
  82. parse_params: {
  83. col_sep: ';',
  84. },
  85. try: false,
  86. )
  87. assert_equal(false, result[:try])
  88. assert_equal(2, result[:records].count)
  89. assert_equal('success', result[:result])
  90. text_module1 = TextModule.find_by(name: 'some name1')
  91. assert(text_module1)
  92. assert_equal(text_module1.name, 'some name1')
  93. assert_equal(text_module1.keywords, 'keyword1')
  94. assert_equal(text_module1.content, 'some<br>content1')
  95. assert_equal(text_module1.active, true)
  96. text_module2 = TextModule.find_by(name: 'some name2')
  97. assert(text_module2)
  98. assert_equal(text_module2.name, 'some name2')
  99. assert_equal(text_module2.keywords, 'keyword2')
  100. assert_equal(text_module2.content, 'some content<br>test123')
  101. assert_equal(text_module2.active, true)
  102. text_module1.destroy!
  103. text_module2.destroy!
  104. end
  105. test 'simple import with delete' do
  106. assert_equal(0, TextModule.count)
  107. TextModule.create!(
  108. name: 'some name1',
  109. content: 'some name1',
  110. active: true,
  111. updated_by_id: 1,
  112. created_by_id: 1,
  113. )
  114. TextModule.create!(
  115. name: 'name should be deleted 2',
  116. content: 'name should be deleted 1',
  117. active: true,
  118. updated_by_id: 1,
  119. created_by_id: 1,
  120. )
  121. csv_string = "name;keywords;content;note;active;\nsome name1;keyword1;\"some\ncontent1\";-;\nsome name2;keyword2;some content<br>test123\n"
  122. result = TextModule.csv_import(
  123. string: csv_string,
  124. parse_params: {
  125. col_sep: ';',
  126. },
  127. try: true,
  128. delete: true,
  129. )
  130. assert_equal(true, result[:try])
  131. assert(result[:stats])
  132. assert_equal(2, result[:stats][:created])
  133. assert_equal(0, result[:stats][:updated])
  134. assert_equal(2, result[:stats][:deleted])
  135. assert_equal(2, result[:records].count)
  136. assert_equal('success', result[:result])
  137. assert(TextModule.find_by(name: 'some name1'))
  138. assert_nil(TextModule.find_by(name: 'some name2'))
  139. result = TextModule.csv_import(
  140. string: csv_string,
  141. parse_params: {
  142. col_sep: ';',
  143. },
  144. try: false,
  145. delete: true,
  146. )
  147. assert_equal(false, result[:try])
  148. assert(result[:stats])
  149. assert_equal(2, result[:stats][:created])
  150. assert_equal(0, result[:stats][:updated])
  151. assert_equal(2, result[:stats][:deleted])
  152. assert_equal(2, result[:records].count)
  153. assert_equal('success', result[:result])
  154. assert_equal(2, TextModule.count)
  155. text_module1 = TextModule.find_by(name: 'some name1')
  156. assert(text_module1)
  157. assert_equal(text_module1.name, 'some name1')
  158. assert_equal(text_module1.keywords, 'keyword1')
  159. assert_equal(text_module1.content, 'some<br>content1')
  160. assert_equal(text_module1.active, true)
  161. text_module2 = TextModule.find_by(name: 'some name2')
  162. assert(text_module2)
  163. assert_equal(text_module2.name, 'some name2')
  164. assert_equal(text_module2.keywords, 'keyword2')
  165. assert_equal(text_module2.content, 'some content<br>test123')
  166. assert_equal(text_module2.active, true)
  167. assert_nil(TextModule.find_by(name: 'name should be deleted 2'))
  168. text_module1.destroy!
  169. text_module2.destroy!
  170. end
  171. end