Module: Rhosync::TestMethods

Defined in:
lib/rhosync/test_methods.rb

Instance Method Summary collapse

Instance Method Details

#cdObject

Returns the client document (:cd) for the source adapter + client under test. The master document (:md) and client document (:cd) should be equal



146
147
148
# File 'lib/rhosync/test_methods.rb', line 146

def cd
  @c.get_data(:cd)
end

#create_errorsObject

Returns any errors stored in redis from the previous source adapter create (same structure as query errors)



82
83
84
# File 'lib/rhosync/test_methods.rb', line 82

def create_errors
  @c.get_data(:create_errors)
end

#delete_errorsObject

Returns any errors stored in redis from the previous source adapter delete (same structure as query errors)



134
135
136
# File 'lib/rhosync/test_methods.rb', line 134

def delete_errors
  @c.get_data(:delete_errors)
end

#mdObject

Returns the master document (:md) for the source adapter stored in redis. This is equivalent to the @result hash of hashes structure.



140
141
142
# File 'lib/rhosync/test_methods.rb', line 140

def md
  @s.get_data(:md)
end

#query_errorsObject

Returns any errors stored in redis for the previous source adapter query For example: connecting to web service!”}



50
51
52
# File 'lib/rhosync/test_methods.rb', line 50

def query_errors
  @s.get_data(:errors)
end

#setup_test_for(adapter, user_id) ⇒ Object

Initializes the source adapter under test for a given user, typically in a before(:each) block setup_test_for(Product,‘testuser’) #=> ‘testuser’ will be used by rest of the specs



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rhosync/test_methods.rb', line 9

def setup_test_for(adapter,user_id)
  app_id = 'application'
  s_fields = {
    :user_id => user_id,
    :app_id => app_id
  }
  c_fields = {
    :device_type => 'Apple',
    :device_pin => 'abcd',
    :device_port => '3333',
    :user_id => user_id,
    :app_id => app_id 
  }
  @u = User.create(:login => user_id)
  @s = Source.load(adapter.to_s,s_fields)
  @c = Client.create(c_fields,{:source_name => adapter.to_s})
  @ss = SourceSync.new(@s)
end

#test_create(record) ⇒ Object

Execute’s the adapter’s create method with a provided record and returns the object string from the create method. If the create method returns a string, then a link will be saved for the device next time it synchronizes. This link can be tested here.

For example, in your spec: } new_product_id = test_create(@product) create_errors.should == {} md.should == @product

This will return the result of the adapter’s create method. The master document (:md) should also contain the new record.



73
74
75
76
77
78
# File 'lib/rhosync/test_methods.rb', line 73

def test_create(record)
  @c.put_data(:create,{'temp-id' => record})
  @ss.create(@c.id)
  links = @c.get_data(:create_links)['temp-id']
  links ? links['l'] : nil
end

#test_delete(record) ⇒ Object

Execute the source adapter’s delete method. Takes a record as hash of hashes (object_id => object)

For example: } test_delete(‘4’ => @product) delete_errors.should == {} md.should == {}

This will call the adapter’s delete method for product ‘4’ NOTE: The master document (:md) will be updated and can be verified as shown above.



127
128
129
130
# File 'lib/rhosync/test_methods.rb', line 127

def test_delete(record)
  @c.put_data(:delete,record)
  @ss.delete(@c.id)
end

#test_queryObject

Executes the adapter’s query method and returns the master document (:md) stored in redis For example, if your source adapter query method was: def query(params=nil)

@result = { 
  "1"=>{"name"=>"Acme", "industry"=>"Electronics"},
  "2"=>{"name"=>"Best", "industry"=>"Software"}
}

end

test_query would return:

"1"=>{"name"=>"Acme", "industry"=>"Electronics",
"2"=>"industry"=>"Software"

}



43
44
45
46
# File 'lib/rhosync/test_methods.rb', line 43

def test_query
  @ss.process_query
  return md
end

#test_update(record) ⇒ Object

Execute the source adapter’s update method. Takes a record as hash of hashes (object_id => object)

For example: test_update(=> {‘price’ => ‘$199.99’}) update_errors.should == {} test_query md[‘price’].should == ‘$199.99’

This will call the adapter’s update method for object_id ‘4’ NOTE: To test the master document, you will need to run def test_query as shown above



98
99
100
101
# File 'lib/rhosync/test_methods.rb', line 98

def test_update(record)
  @c.put_data(:update,record)
  @ss.update(@c.id)
end

#update_errorsObject

Returns any errors stored in redis from the previous source adapter update (same structure as query errors)



105
106
107
# File 'lib/rhosync/test_methods.rb', line 105

def update_errors
  @c.get_data(:update_errors)
end