55describe CSVImporter do
66 # Mimics an active record model
77 class User
8- include Virtus . model
9- include ActiveModel ::Model
8+ include ActiveModel ::Attributes
9+ include ActiveModel ::Validations
10+ include ActiveModel ::Dirty
1011
1112 attribute :id
1213 attribute :email
1314 attribute :f_name
1415 attribute :l_name
1516 attribute :confirmed_at
1617 attribute :created_by_user_id
17- attribute :custom_fields , Hash
18+ attribute :custom_fields , default : -> { Hash . new }
1819
1920 validates_presence_of :email
2021 validates_format_of :email , with : /[^@]+@[^@]/ # contains one @ symbol
@@ -25,30 +26,34 @@ def self.transaction
2526 end
2627
2728 def persisted?
28- !!id
29+ !!self . id
2930 end
3031
3132 def save
3233 return false unless valid?
3334
3435 unless persisted?
35- @ id = rand ( 100 )
36+ self . id = rand ( 100 )
3637 self . class . store << self
3738 end
3839
40+ changes_applied
41+
3942 true
4043 end
4144
4245 def self . find_by ( attributes )
43- store . find { |u | attributes . all? { |k , v | u . attributes [ k ] == v } }
46+ store . find { |u | attributes . all? { |k , v | u . public_send ( k ) == v } }
4447 end
4548
4649 def self . reset_store!
4750 @store = Set . new
4851
49- User . new (
50- email : "mark@example.com" , f_name : "mark" , l_name : "lee" , confirmed_at : Time . new ( 2012 )
51- ) . save
52+ user = User . new
53+ { email : "mark@example.com" , f_name : "mark" , l_name : "lee" , confirmed_at : Time . new ( 2012 ) } . each do |k , v |
54+ user . public_send ( "#{ k } =" , v )
55+ end
56+ user . save
5257
5358 @store
5459 end
@@ -633,8 +638,20 @@ class ImportUserCSVByFirstName
633638 end
634639
635640 import . run!
641+ expect ( import . report . valid_rows . size ) . to eq ( 1 )
636642 expect ( import . report . message ) . to eq "Import completed: 1 created, 1 update skipped"
637643 end
644+
645+ it "if no changes detected" do
646+ csv_content = "email,confirmed,first_name,last_name
647+ mark@example.com,true,mark,lee"
648+ import = ImportUserCSV . new ( content : csv_content )
649+
650+ import . run!
651+
652+ expect ( import . report . valid_rows . size ) . to eq ( 0 )
653+ expect ( import . report . message ) . to eq "Import completed: 1 update skipped"
654+ end
638655
639656 it "doesn't call skip! twice" do
640657 csv_content = "email,confirmed,first_name,last_name
0 commit comments