can_csv_import_text_module_examples.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'csv'
  3. RSpec.shared_examples 'CanCsvImport - TextModule specific tests', :aggregate_failures do
  4. describe '.csv_example' do
  5. context 'when no data avaiable' do
  6. let(:headers) do
  7. CSV.parse(TextModule.csv_example).shift
  8. end
  9. it 'returns expected headers' do
  10. expect(headers).to start_with('id', 'name', 'keywords', 'content', 'note', 'active')
  11. expect(headers).not_to include('organization', 'state', 'owner', 'priority', 'customer')
  12. end
  13. end
  14. end
  15. describe '.csv_import' do
  16. let(:try) { true }
  17. let(:delete) { false }
  18. let(:params) { { string: csv_string, parse_params: { col_sep: ';' }, try: try, delete: delete } }
  19. let(:result) { TextModule.csv_import(**params) }
  20. shared_examples 'fails with error' do |errors|
  21. shared_examples 'checks error handling' do
  22. it 'returns error(s)' do
  23. expect(result).to include({ try: try, result: 'failed', errors: errors })
  24. end
  25. it 'does not import organizations' do
  26. # Any single failure will cause the entire import to be aborted.
  27. expect { result }.not_to change(Organization, :count)
  28. end
  29. end
  30. context 'with :try' do
  31. include_examples 'checks error handling'
  32. end
  33. context 'without :try' do
  34. let(:try) { false }
  35. include_examples 'checks error handling'
  36. end
  37. end
  38. context 'with empty string' do
  39. let(:csv_string) { '' }
  40. include_examples 'fails with error', ['Unable to parse empty file/string for TextModule.']
  41. end
  42. context 'with just CSV header line' do
  43. let(:csv_string) { 'name;keywords;content;note;active;' }
  44. include_examples 'fails with error', ['No records found in file/string for TextModule.']
  45. end
  46. context 'without required lookup header' do
  47. let(:csv_string) { "firstname;lastname;active;\nfirstname-simple-import1;lastname-simple-import1;;true\nfirstname-simple-import2;lastname-simple-import2;false\n" }
  48. include_examples 'fails with error', ['No lookup column like id,name for TextModule found.']
  49. end
  50. context 'with valid import data' do
  51. let(:csv_string) { "name;keywords;content;note;active;\nsome name1;keyword1;\"some\ncontent1\";-;\nsome name2;keyword2;some content<br>test123\n" }
  52. before do
  53. create(
  54. :text_module,
  55. name: 'some name1',
  56. content: 'some name1',
  57. keywords: 'keyword1',
  58. active: true,
  59. )
  60. create(
  61. :text_module,
  62. name: 'name should be deleted',
  63. content: 'content should be deleted',
  64. keywords: 'keyword should be deleted',
  65. active: true,
  66. )
  67. end
  68. context 'without :delete' do
  69. context 'with :try' do
  70. it 'returns success' do
  71. expect(result).to include({ try: try, result: 'success' })
  72. expect(result[:records].count).to be(2)
  73. end
  74. it 'does not import text modules' do
  75. expect { result }.not_to change(TextModule, :count)
  76. end
  77. end
  78. context 'without :try' do
  79. let(:try) { false }
  80. let(:first_mod) { TextModule.last(2).first }
  81. let(:second_mod) { TextModule.last }
  82. it 'returns success' do
  83. expect(result).to include({ try: try, result: 'success' })
  84. expect(result[:records].count).to be(2)
  85. end
  86. it 'does import organizations' do
  87. expect { result }.to change(TextModule, :count).by(1)
  88. expect(first_mod).to have_attributes(
  89. name: 'name should be deleted',
  90. keywords: 'keyword should be deleted',
  91. content: 'content should be deleted',
  92. active: true,
  93. )
  94. expect(second_mod).to have_attributes(
  95. name: 'some name2',
  96. keywords: 'keyword2',
  97. content: 'some content<br>test123',
  98. active: true,
  99. )
  100. end
  101. end
  102. end
  103. context 'with :delete' do
  104. let(:delete) { true }
  105. context 'with :try' do
  106. it 'returns success' do
  107. expect(result).to include({ try: try, result: 'success' })
  108. expect(result[:records].count).to be(2)
  109. end
  110. it 'does not import text modules' do
  111. expect { result }.not_to change(TextModule, :count)
  112. end
  113. end
  114. context 'without :try' do
  115. let(:try) { false }
  116. let(:first_mod) { TextModule.last(2).first }
  117. let(:second_mod) { TextModule.last }
  118. it 'returns success' do
  119. expect(result).to include({ try: try, result: 'success' })
  120. expect(result[:records].count).to be(2)
  121. end
  122. it 'does import organizations' do
  123. expect { result }.not_to change(TextModule, :count)
  124. expect(first_mod).to have_attributes(
  125. name: 'some name1',
  126. keywords: 'keyword1',
  127. content: 'some<br>content1',
  128. active: true,
  129. )
  130. expect(second_mod).to have_attributes(
  131. name: 'some name2',
  132. keywords: 'keyword2',
  133. content: 'some content<br>test123',
  134. active: true,
  135. )
  136. end
  137. end
  138. end
  139. end
  140. end
  141. end