-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SRCH-1282 - raketask populate first last name (#546)
* SRCH-1282 - raketask populate first last name * fix code climate feedback * remove extra space
- Loading branch information
Showing
5 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :usasearch do | ||
namespace :populate_user_fields do | ||
desc 'populate first and last name in the user table' | ||
task :update_first_last_name, [:path] => [:environment] do |_t, args| | ||
CSV.foreach(args.path, headers: true) do |row| | ||
record = row.to_h | ||
@user = User.find_by(email: record['Email']) | ||
@user&.update!(first_name: record['First Name'], last_name: record['Last Name']) | ||
puts "updating #{record['Email']} with " \ | ||
"#{record['First Name']} #{record['Last Name']}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Email,First Name,Last Name | ||
no_first_last_name_1@email.gov,Homer,Simpson | ||
no_first_last_name_2@email.gov,Seymour,Skinner | ||
no_first_last_name_3@email.gov,Ned,Flanders | ||
no_first_last_name_4@email.gov,Nelson,Muntz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Populate user rake tasks' do | ||
|
||
before(:all) do | ||
@rake = Rake::Application.new | ||
Rake.application = @rake | ||
Rake.application.rake_require('tasks/populate_user') | ||
Rake::Task.define_task(:environment) | ||
end | ||
|
||
let(:user1) { users(:no_first_last_name_1) } | ||
let(:user2) { users(:no_first_last_name_2) } | ||
let(:user3) { users(:no_first_last_name_3) } | ||
let(:user4) { users(:no_first_last_name_4) } | ||
|
||
let(:csv_file_path) do | ||
Rails.root.join(Rails.root.to_s, | ||
'spec', | ||
'fixtures', | ||
'csv', 'users.csv') | ||
end | ||
|
||
describe 'usasearch:populate_user_fields:update_first_last_name' do | ||
let(:task_name) { 'usasearch:populate_user_fields:update_first_last_name' } | ||
|
||
before do | ||
@rake[task_name].reenable | ||
end | ||
|
||
it "has 'environment' as a prereq" do | ||
expect(@rake[task_name].prerequisites).to include('environment') | ||
end | ||
|
||
it 'populates the first name' do | ||
@rake[task_name].invoke(csv_file_path) | ||
|
||
expect(user1.first_name).to eq 'Homer' | ||
expect(user2.first_name).to eq 'Seymour' | ||
expect(user3.first_name).to eq 'Ned' | ||
expect(user4.first_name).to eq 'Nelson' | ||
end | ||
|
||
it 'populates the last name' do | ||
@rake[task_name].invoke(csv_file_path) | ||
|
||
expect(user1.last_name).to eq 'Simpson' | ||
expect(user2.last_name).to eq 'Skinner' | ||
expect(user3.last_name).to eq 'Flanders' | ||
expect(user4.last_name).to eq 'Muntz' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters