/spec/lib/migration/contents_spec.rb

https://gitlab.com/minuto/media_choice · Ruby · 118 lines · 100 code · 12 blank · 6 comment · 0 complexity · 78a3f88ffac3262d803ec9a47eaa20e4 MD5 · raw file

  1. require 'unit_helper'
  2. require 'migration/migration'
  3. describe Migration::Content do
  4. describe 'class methods' do
  5. describe 'migrate' do
  6. let(:content_to_be_migrated) { build(:migration_content) }
  7. let(:prepared_query_result) do
  8. hash = content_to_be_migrated.to_h
  9. hash.stringify_keys
  10. end
  11. let(:columns) { prepared_query_result.keys }
  12. let(:relation_mock) { double('ActiveRecord::Relation') }
  13. let(:values) { [prepared_query_result.values] }
  14. before do
  15. # There's no way to retrieve the column names from the database, as the connection is not allowed on test environment
  16. # Therefore, we use an OpenStruct with the attributes method (or mock) to retrieve the attributes
  17. content_to_be_migrated.class.send(:define_method, 'attributes') do
  18. to_h.stringify_keys
  19. end
  20. expect(Migration::Content.connection).to receive(:exec_query).and_return(relation_mock)
  21. expect(relation_mock).to receive(:columns).and_return columns
  22. expect(relation_mock).to receive(:map).and_return values
  23. end
  24. context 'without errors' do
  25. it 'correctly saves the contents on the database' do
  26. expect(described_class).to receive(:save_records).with(columns, values, ::Content, false).and_return true
  27. Migration::Content.migrate
  28. end
  29. end
  30. context 'with errors' do
  31. it 'doesn\'t save the contents on the database' do
  32. expect(described_class).to receive(:save_records).with(columns, values, ::Content, false).and_return false
  33. Migration::Content.migrate
  34. end
  35. end
  36. end
  37. describe 'migrate_original_medias' do
  38. let(:content_to_be_migrated) { build(:migration_content) }
  39. let(:prepared_query_result) do
  40. hash = content_to_be_migrated.to_h
  41. hash.stringify_keys
  42. result = {}
  43. result['content_id'] = hash['id']
  44. result['created_at'] = hash['created_at']
  45. result['updated_at'] = hash['updated_at']
  46. result['media_file_name'] = hash['input_file_name']
  47. result['media_content_type'] = hash['input_content_type']
  48. result['media_file_size'] = hash['input_file_size']
  49. result['media_updated_at'] = hash['input_updated_at']
  50. result
  51. end
  52. let(:columns) { %w(content_id created_at updated_at media_file_name media_content_type media_file_size media_updated_at) }
  53. let(:values) { [prepared_query_result.values] }
  54. before do
  55. # There's no way to retrieve the column names from the database, as the connection is not allowed on test environment
  56. # Therefore, we use an OpenStruct with the attributes method (or mock) to retrieve the attributes
  57. content_to_be_migrated.class.send(:define_method, 'attributes') do
  58. to_h.stringify_keys
  59. end
  60. expect(Migration::Content).to receive(:pluck).and_return(values)
  61. end
  62. context 'without errors' do
  63. it 'correctly saves the original medias on the database' do
  64. expect(described_class).to receive(:save_records).with(columns, values, OriginalMedia, false).and_return true
  65. Migration::Content.migrate_original_medias
  66. end
  67. end
  68. context 'with errors' do
  69. it 'doesn\'t save the original medias on the database' do
  70. expect(described_class).to receive(:save_records).with(columns, values, OriginalMedia, false).and_return false
  71. Migration::Content.migrate_original_medias
  72. end
  73. end
  74. end
  75. describe 'migrate_judge_evaluations' do
  76. let(:content_to_be_migrated) { build(:migration_content) }
  77. let(:prepared_query_result) do
  78. hash = content_to_be_migrated.to_h
  79. hash.stringify_keys
  80. end
  81. let(:relation_mock) { double('ActiveRecord::Relation') }
  82. let(:columns) { %w(content_id judge_rating created_at updated_at evaluation_state) }
  83. let(:values) { [prepared_query_result.values] }
  84. before do
  85. # There's no way to retrieve the column names from the database, as the connection is not allowed on test environment
  86. # Therefore, we use an OpenStruct with the attributes method (or mock) to retrieve the attributes
  87. content_to_be_migrated.class.send(:define_method, 'attributes') do
  88. to_h.stringify_keys
  89. end
  90. expect(Migration::Content).to receive(:pluck).and_return(values)
  91. end
  92. context 'without errors' do
  93. it 'correctly saves the judge evaluations on the database' do
  94. expect(described_class).to receive(:save_records).with(columns, values, ::JudgeEvaluation).and_return true
  95. Migration::Content.migrate_judge_evaluations
  96. end
  97. end
  98. context 'with errors' do
  99. it 'doesn\'t save the judge evaluations on the database' do
  100. expect(described_class).to receive(:save_records).with(columns, values, ::JudgeEvaluation).and_return false
  101. Migration::Content.migrate_judge_evaluations
  102. end
  103. end
  104. end
  105. end
  106. end