From 867bc3b48e2e34196101d5a629fb21a1d13e8d7d Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Fri, 1 May 2020 11:34:55 +1000 Subject: [PATCH] FIX: Change base importer to create new Bookmark records (#9603) Also add a spec and fixture with a mock importer that we can use to test the create_X methods of the base importer --- script/import_scripts/base.rb | 7 +-- spec/fixtures/json/base-import-data.json | 57 ++++++++++++++++++++++++ spec/script/import_scripts/base_spec.rb | 57 ++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/json/base-import-data.json create mode 100644 spec/script/import_scripts/base_spec.rb diff --git a/script/import_scripts/base.rb b/script/import_scripts/base.rb index ea43db27300..48bd67e516f 100644 --- a/script/import_scripts/base.rb +++ b/script/import_scripts/base.rb @@ -606,9 +606,10 @@ class ImportScripts::Base skipped += 1 puts "Skipping bookmark for user id #{params[:user_id]} and post id #{params[:post_id]}" else - result = PostActionCreator.create(user, post, :bookmark) - created += 1 if result.success? - skipped += 1 if result.failed? + result = BookmarkManager.new(user).create(post_id: post.id) + + created += 1 if result.errors.none? + skipped += 1 if result.errors.any? end end diff --git a/spec/fixtures/json/base-import-data.json b/spec/fixtures/json/base-import-data.json new file mode 100644 index 00000000000..08f3f400c7e --- /dev/null +++ b/spec/fixtures/json/base-import-data.json @@ -0,0 +1,57 @@ +{ + "bookmarks":[ + { + "post_id":10, + "user_id":20 + }, + { + "post_id":11, + "user_id":20 + }, + { + "post_id":12, + "user_id":20 + }, + { + "post_id":13, + "user_id":20 + }, + { + "post_id":14, + "user_id":20 + } + ], + "posts":[ + { + "user_id":20, + "id":10, + "raw":"This is test post 1" + }, + { + "user_id":20, + "id":11, + "raw":"This is test post 2" + }, + { + "user_id":20, + "id":12, + "raw":"This is test post 3" + }, + { + "user_id":20, + "id":13, + "raw":"This is test post 4" + }, + { + "user_id":20, + "id":14, + "raw":"This is test post 5" + } + ], + "users":[ + { + "id":20, + "email":"testimportuser@test.com" + } + ] +} diff --git a/spec/script/import_scripts/base_spec.rb b/spec/script/import_scripts/base_spec.rb new file mode 100644 index 00000000000..e7594d2fa56 --- /dev/null +++ b/spec/script/import_scripts/base_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'rails_helper' +require_relative '../../../script/import_scripts/base' + +describe ImportScripts::Base do + before do + STDOUT.stubs(:write) + end + + class MockSpecImporter < ImportScripts::Base + def initialize(data) + super() + @import_data = data + end + + def execute + import_users + import_posts + import_bookmarks + end + + def import_users + users = @import_data[:users] + create_users(users) do |row| + { email: row[:email], id: row[:id] } + end + end + + def import_posts + posts = @import_data[:posts] + create_posts(posts) do |row| + user_id = @lookup.user_id_from_imported_user_id(row[:user_id]) || -1 + { user_id: user_id, raw: row[:raw], id: row[:id], title: "Test topic for post #{row[:id]}" } + end + end + + def import_bookmarks + bookmarks = @import_data[:bookmarks] + create_bookmarks(bookmarks) do |row| + { post_id: row[:post_id], user_id: row[:user_id] } + end + end + end + + let(:import_data) do + import_file = Rack::Test::UploadedFile.new(file_from_fixtures("base-import-data.json", "json")) + ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(import_file.read)) + end + + it "creates bookmarks, posts, and users" do + MockSpecImporter.new(import_data).perform + expect(Bookmark.count).to eq(5) + expect(Post.count).to eq(5) + expect(User.where('id > 0').count).to eq(1) + end +end