Replace ServiceUser with fakes in ServicesController

This commit is contained in:
Raphael Sofaer 2011-05-16 18:48:03 -07:00
parent a357cf9217
commit 164226a3d7
4 changed files with 72 additions and 7 deletions

View file

@ -12,7 +12,7 @@ class ServiceUser < ActiveRecord::Base
def attach_local_models
service_for_uid = Services::Facebook.where(:type => service.type.to_s, :uid => self.uid).first
if !service_for_uid.blank? && (service_for_uid.user.person.profile.searchable)
self.person = service_for_uid.user.person
self.person = service_for_uid.user.person
else
self.person = nil
end
@ -28,3 +28,24 @@ class ServiceUser < ActiveRecord::Base
:invitation_identifier => self.uid}).first
end
end
class FakeServiceUser < HashWithIndifferentAccess
def initialize(row)
columns = ServiceUser.column_names
self.replace Hash[columns.zip(row)]
end
ServiceUser.column_names.each do |column|
symbol = column.to_sym
define_method symbol do
self[symbol]
end
end
ServiceUser.reflect_on_all_associations.each do |assoc|
define_method assoc.name do
assoc.klass.find(self[assoc.primary_key_name])
end
end
end

View file

@ -27,14 +27,16 @@ class Services::Facebook < Service
Resque.enqueue(Job::UpdateServiceUsers, self.id)
end
person = Person.arel_table
service_user = ServiceUser.arel_table
query = self.service_users.scoped
if opts[:local]
ServiceUser.joins(:person).where(:service_id => self.id).where(person[:owner_id].not_eq(nil)).all
query = query.joins(:person).where(person[:owner_id].not_eq(nil))
elsif opts[:remote]
ServiceUser.joins(:person).where(:service_id => self.id).where(person[:owner_id].eq(nil)).all
else
self.service_users
query = query.joins(:person).where(person[:owner_id].eq(nil))
end
result = ServiceUser.connection.execute(query.to_sql).to_a
fakes = result.map{|r| FakeServiceUser.new(r) }
end
def save_friends

View file

@ -106,3 +106,36 @@ JSON
end
end
end
describe FakeServiceUser do
describe '.initialize' do
before do
@data = [182, "820651", "Maxwell Salzberg", "http://cdn.fn.com/pic1.jpg", 299, 1610, nil, nil, nil, DateTime.parse("Tue May 17 00:31:44 UTC 2011"), DateTime.parse("Tue May 17 00:31:44 UTC 2011")]
@fake = FakeServiceUser.new(@data)
end
it 'takes a mysql row and sets the attr names to their values' do
@fake[:id].should == @data[0]
@fake[:uid].should == @data[1]
@fake[:name].should == @data[2]
@fake[:photo_url].should == @data[3]
@fake[:service_id].should == @data[4]
@fake[:person_id].should == @data[5]
@fake[:contact_id].should == @data[6]
@fake[:request_id].should == @data[7]
@fake[:invitation_id].should == @data[8]
@fake[:created_at].should == @data[9]
@fake[:updated_at].should == @data[10]
end
it 'has reader methods' do
@fake.photo_url.should == @data[3]
@fake.person_id.should == @data[5]
end
it 'has association methods' do
person = mock
Person.should_receive(:find).with(@data[5]).and_return person
@fake.person.should == person
end
end
end

View file

@ -28,7 +28,7 @@ describe Services::Facebook do
end
context 'finder' do
before do
before do
@user2 = Factory.create(:user_with_aspect)
@user2_fb_id = '820651'
@user2_fb_name = 'Maxwell Salzberg'
@ -70,16 +70,25 @@ JSON
end
describe '#finder' do
it 'returns an array of non-activerecord objects' do
@service.save_friends
result = @service.finder
result.should be_an(Array)
result.first.should_not be_an ActiveRecord::Base
end
it 'does a syncronous call if it has not been called before' do
@service.should_receive(:save_friends)
@service.finder
end
it 'dispatches a resque job' do
Resque.should_receive(:enqueue).with(Job::UpdateServiceUsers, @service.id)
su2 = ServiceUser.create(:service => @user2_service, :uid => @user2_fb_id, :name => @user2_fb_name, :photo_url => @user2_fb_photo_url)
@service.service_users = [su2]
@service.finder
end
context 'opts' do
it 'only local does not return people who are remote' do
@service.save_friends