rsim / ruby-plsql-spec

Oracle PL/SQL unit testing with Ruby
MIT License
68 stars 25 forks source link

Calling member methods that change object state and maintaining in local variable #11

Open jlincoln opened 11 years ago

jlincoln commented 11 years ago

Is it possible to call a method on a object that changes the state of the object in the Oracle database and have that state reflected in the local ruby variable?

Ex. my_obj = plsql.my_obj_type(:attr1=>'test',:attr2=>null) my_obj.is_attr2_null.should == true my_obj.set_attr2(:attr2=>'test1') my_obj.is_attr2_null.should == false

jlincoln commented 11 years ago

I ended up doing the following.

describe "Create New Instance" do it "should have correct attribute values" do key_values = [] key_values << plsql.xxmcl_key_value(:key=>'ID',:value=>'1') key_values << plsql.xxmcl_key_value(:key=>'TEXT1',:value=>'text1') dao = plsql.xxmcl_dao(:table_name=>'xxmcl_dao_test',:primary_key=>'id',:key_values=>key_values) dao[:table_name].should == 'XXMCL_DAO_TEST' dao[:primary_key].should == 'ID' dao[:database_state].should == NULL dao.key_at_index(:key=>'TEXT1')[0].should > 0 dao.get_key_value(:key=>'TEXT1')[0].should == 'text1' dao.insertable[0].should == true dao.insert_record[0] == true plsql.commit plsql.selectone("select count() from xxmcl_dao_test where text1 = 'text1'").should == 1 dao.set_key_value(:key=>'TEXT1',:value=>'TESTUPDATE')[0].should == true dao.set_key_value(:key=>'TEXT1',:value=>'TESTUPDATE')[1][:self][:database_state].should == 'C' dao.set_key_value(:key=>'TEXT1',:value=>'TESTUPDATE')[0].should == true dao2 = dao.set_key_value(:key=>'TEXT1',:value=>'TESTUPDATE')[1][:self]

create a new PLSQL::ObjectInstance using the hash returned from the method call to continue

  dao = plsql.xxmcl_dao(:table_name=>dao2[:table_name],:primary_key=>dao2[:primary_key],:key_values=>dao2[:key_values])
  dao.updateable[0].should == true
  dao.update_record[0].should == true
  plsql.select_one("select count(_) from xxmcl_dao_test where text1 = 'TESTUPDATE'").should == 1 # does not work because local dao var is unchanged by set_key_value
end

end

jgebal commented 8 years ago

That is not possible at the moment as Ruby object is not directly linked to underlying Oracle object.

Oracle object state "lives" through the duration of database code execution. If you look at object type unit tests for ruby-plsql framework you will see that the solution is to reference the Oracle object type explicitly each time.

When referencing the Ruby representation of Oracle Object Type Instance, it's best to thing of it as of values of Oracle Object stored in Ruby code, not the object itself.

jgebal commented 8 years ago

This issue is more related to the ruby-plsql or ruby-oci8