Newbie: Control flow of tests using 'define_method'

I am very new to development and even more in the dark when it comes to
Ruby, but I am still having fun. :wink: I also apologise for the long
snippets of code in this post.

I am trying to test a database migration using Ruby and the built in
Test framework.

I am stuck however on a particular problem and it relates to control
flow and the dynamic use of ‘define_method’ to create multiple Tests if
an assert_equal ‘fails’ thus jumping out of a loop and stopping all
future tests.

I found what I thought was the solution from this post:

http://www.ruby-forum.com/topic/204730

One I notice however is it will not execute the define_method block
until it has executed all of the exterior loops.

My code is as follows:

class TestSuite < Test::Unit::TestCase

#Build a hash of Source Database columns and target Database columns

companyRRAccessToBeacon = Hash.new
companyRRAccessToBeacon['company_id'] = 'ID'
cmpanyRRAccessToBeacon['ultimate_parent_id'] = 'GlobalUltimateID'
companyRRAccessToBeacon['fullname'] = 'Name'
companyRRAccessToBeacon['turnover_curr'] = 'TurnoverCurrencyTypeID'
companyRRAccessToBeacon['turnover'] = 'TurnoverAmount'
companyRRAccessToBeacon['u_size_code'] = 'CorporateSizeTypeID'
companyRRAccessToBeacon['corp_status_id'] =

‘CorporateOwnershipTypeID’
companyRRAccessToBeacon[‘created_user’] = ‘RowCreatedBy’
companyRRAccessToBeacon[‘created_datetime’] = ‘RowCreatedDate’
companyRRAccessToBeacon[‘last_modification_by’] = ‘RowModifiedBy’
companyRRAccessToBeacon[‘last_modification_date’] =
‘RowModifiedDate’
companyRRAccessToBeacon[‘off_limits_id’] = ‘OffLimitsID’

#Connect to the RRAccess and Beacon databases
rraccesshandle = connectrraccessdb
beaconhandle = connectbeacondb


#Companyrowdata stores the Data Driven tests used for the test

below.
companyrowData =
getXlsRowData(“C:\Users\ecucropc\Documents\Visual Studio
2008\Projects\DataSynchronisation\DataSynchronisation\Company
Data.xls”, “Company”, “A1:L3”)

#Loop through each record returned in the Data Driven Test set.
companyrowData.each do | companydata |
  puts "In #{companydata} loop"
  #If no Company records are returned then run the test.
  companydata["company_id"] != ""
  recordtofetch = companydata["company_id"]

  #Get the common RRAccess Company columns
  sqlRRaccessValues = (getDBValue(rraccesshandle, "select

company_id, ultimate_parent_id, fullname, turnover_curr, turnover,
U_Size_Code, corp_status_id, created_user, created_datetime,
last_modification_by, last_modification_date, off_limits_id from
RRAccessLive.Company WHERE Company_id = ?", recordtofetch))

  #Get the common Beacon Company columns
  sqlBeaconValues = (getDBValue(beaconhandle, "select ID,

GlobalUltimateID, Name, TurnoverCurrencyTypeID, TurnoverAmount,
CorporateSizeTypeID, CorporateOwnershipTypeID, RowCreatedBy,
RowCreatedDate, RowModifiedBy, RowModifiedDate, OffLimitsID from
BeaconLive.Company WHERE ID = ?", recordtofetch))

  #For each HashMap pair get the values from the RRAccess and Beacon

databases and then
#loop through and perform the comparison test.

  companyRRAccessToBeacon.each {|key, value|
    puts "Inside each loop #{companyRRAccessToBeacon[key]}"
      rrAccessColumnValue = sqlRRaccessValues[key]
      rrBeaconColumnValue = sqlBeaconValues[value]

    #This dynamic define_method is used to create many tests.

    define_method ("test_company_" + companyRRAccessToBeacon[key])

do
puts “Inside assert_equal loop for
test_company_#{companyRRAccessToBeacon[key]}”
assert_equal sqlRRaccessValues[key], sqlBeaconValues[value]
end
}
end
end

The output of this is as follows:

In off_limits_idFturnover_currUSDlast_modification_date2008/06/25
11:10:00corp_status_idQUOcreated_user2000734ultimate_parent_id1fullnameAAR
CorporationU_Size_Code1last_modification_by40000347turnover1061169created_datetime1988/10/17
00:00:00company_id1 loop
Inside each loop OffLimitsID
Inside each loop TurnoverCurrencyTypeID
Inside each loop RowModifiedDate
Inside each loop CorporateOwnershipTypeID
Inside each loop RowCreatedBy
Inside each loop GlobalUltimateID
Inside each loop Name
Inside each loop CorporateSizeTypeID
Inside each loop RowModifiedBy
Inside each loop TurnoverAmount
Inside each loop RowCreatedDate
Inside each loop ID
In off_limits_idCturnover_currUSDlast_modification_date2009/12/01
14:51:00corp_status_idQUOcreated_user9999999ultimate_parent_id2fullnameAbbott
LaboratoriesU_Size_Code1last_modification_by40001040turnover22476322created_datetime1978/01/01
00:00:00company_id2 loop
Inside each loop OffLimitsID
Inside each loop TurnoverCurrencyTypeID
Inside each loop RowModifiedDate
Inside each loop CorporateOwnershipTypeID
Inside each loop RowCreatedBy
Inside each loop GlobalUltimateID
Inside each loop Name
Inside each loop CorporateSizeTypeID
Inside each loop RowModifiedBy
Inside each loop TurnoverAmount
Inside each loop RowCreatedDate
Inside each loop ID
In off_limits_idFturnover_currUSDlast_modification_date2008/06/25
11:10:00corp_status_idQUOcreated_user2000734ultimate_parent_id1fullnameAAR
CorporationU_Size_Code1last_modification_by40000347turnover1061169created_datetime1988/10/17
00:00:00company_id1 loop
Inside each loop OffLimitsID
Inside each loop TurnoverCurrencyTypeID
Inside each loop RowModifiedDate
Inside each loop CorporateOwnershipTypeID
Inside each loop RowCreatedBy
Inside each loop GlobalUltimateID
Inside each loop Name
Inside each loop CorporateSizeTypeID
Inside each loop RowModifiedBy
Inside each loop TurnoverAmount
Inside each loop RowCreatedDate
Inside each loop ID
In off_limits_idCturnover_currUSDlast_modification_date2009/12/01
14:51:00corp_status_idQUOcreated_user9999999ultimate_parent_id2fullnameAbbott
LaboratoriesU_Size_Code1last_modification_by40001040turnover22476322created_datetime1978/01/01
00:00:00company_id2 loop
Inside each loop OffLimitsID
Inside each loop TurnoverCurrencyTypeID
Inside each loop RowModifiedDate
Inside each loop CorporateOwnershipTypeID
Inside each loop RowCreatedBy
Inside each loop GlobalUltimateID
Inside each loop Name
Inside each loop CorporateSizeTypeID
Inside each loop RowModifiedBy
Inside each loop TurnoverAmount
Inside each loop RowCreatedDate
Inside each loop ID
Loaded suite C:/Users/ecucropc/Documents/Visual Studio
2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest
Started
Inside assert_equal loop for test_company_CorporateOwnershipTypeID
FInside assert_equal loop for test_company_CorporateSizeTypeID
FInside assert_equal loop for test_company_GlobalUltimateID
.Inside assert_equal loop for test_company_ID
.Inside assert_equal loop for test_company_Name
.Inside assert_equal loop for test_company_OffLimitsID
FInside assert_equal loop for test_company_RowCreatedBy
.Inside assert_equal loop for test_company_RowCreatedDate
.Inside assert_equal loop for test_company_RowModifiedBy
.Inside assert_equal loop for test_company_RowModifiedDate
.Inside assert_equal loop for test_company_TurnoverAmount
.Inside assert_equal loop for test_company_TurnoverCurrencyTypeID
F
Finished in 0.029 seconds.

  1. Failure:
    test_company_CorporateOwnershipTypeID(TestSuite)
    [C:/Users/ecucropc/Documents/Visual Studio
    2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:
    <"QUO "> expected but was
    <1>.

  2. Failure:
    test_company_CorporateSizeTypeID(TestSuite)
    [C:/Users/ecucropc/Documents/Visual Studio
    2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:
    expected but was
    <1>.

  3. Failure:
    test_company_OffLimitsID(TestSuite) [C:/Users/ecucropc/Documents/Visual
    Studio
    2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:
    <“C”> expected but was
    .

  4. Failure:
    test_company_TurnoverCurrencyTypeID(TestSuite)
    [C:/Users/ecucropc/Documents/Visual Studio
    2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:
    <“USD”> expected but was
    <166>.

12 tests, 12 assertions, 4 failures, 0 errors

If I comment out the ‘define_method’ block of code, it will continue
where I was expecting it to go.

Any help would be greatly appreciated.

Chris Cropleu wrote:

class TestSuite < Test::Unit::TestCase

#Build a hash of Source Database columns and target Database columns

companyRRAccessToBeacon = Hash.new
companyRRAccessToBeacon['company_id'] = 'ID'
cmpanyRRAccessToBeacon['ultimate_parent_id'] = 'GlobalUltimateID'
companyRRAccessToBeacon['fullname'] = 'Name'
companyRRAccessToBeacon['turnover_curr'] = 'TurnoverCurrencyTypeID'
companyRRAccessToBeacon['turnover'] = 'TurnoverAmount'
companyRRAccessToBeacon['u_size_code'] = 'CorporateSizeTypeID'
companyRRAccessToBeacon['corp_status_id'] =

‘CorporateOwnershipTypeID’
companyRRAccessToBeacon[‘created_user’] = ‘RowCreatedBy’
companyRRAccessToBeacon[‘created_datetime’] = ‘RowCreatedDate’
companyRRAccessToBeacon[‘last_modification_by’] = ‘RowModifiedBy’
companyRRAccessToBeacon[‘last_modification_date’] =
‘RowModifiedDate’
companyRRAccessToBeacon[‘off_limits_id’] = ‘OffLimitsID’
Wowzer. How about this?
companyRRAccessToBeacon = {‘company_id’ => ‘ID’, ‘ultimate_parent_id’ =>
‘GlobalUltimateID’ }
And so on and so forth ? A little cleaner, a little easier to read,
fewer repetitions.

#Connect to the RRAccess and Beacon databases
rraccesshandle = connectrraccessdb
beaconhandle = connectbeacondb

[…]

#Loop through each record returned in the Data Driven Test set.
companyrowData.each do | companydata |
  puts "In #{companydata} loop"
  #If no Company records are returned then run the test.
  companydata["company_id"] != ""
  recordtofetch = companydata["company_id"]

Where is your if statement? Maybe you mean this?
recordtofetch = companydata[‘company_id’] unless
companydata[‘company_id’].empty?

[…]

The output of this is as follows:

[…]

Inside assert_equal loop for test_company_CorporateOwnershipTypeID
FInside assert_equal loop for test_company_CorporateSizeTypeID
FInside assert_equal loop for test_company_GlobalUltimateID
.Inside assert_equal loop for test_company_ID
.Inside assert_equal loop for test_company_Name
.Inside assert_equal loop for test_company_OffLimitsID
FInside assert_equal loop for test_company_RowCreatedBy
.Inside assert_equal loop for test_company_RowCreatedDate
.Inside assert_equal loop for test_company_RowModifiedBy
.Inside assert_equal loop for test_company_RowModifiedDate
.Inside assert_equal loop for test_company_TurnoverAmount
.Inside assert_equal loop for test_company_TurnoverCurrencyTypeID
F
Finished in 0.029 seconds.

  1. Failure:
    test_company_CorporateOwnershipTypeID(TestSuite)
    [C:/Users/ecucropc/Documents/Visual Studio
    2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:
    <"QUO "> expected but was
    <1>.

Well, in your first test, you get a ‘1’ back and you apparently expect a
"QUO ".
Anything else the errors can tell you that they can’t tell us?
Look into this and get back to us a little later… The tests work,
they’re telling you that they fail :slight_smile:

Aldric G. wrote:

Chris Cropleu wrote:

class TestSuite < Test::Unit::TestCase

#Build a hash of Source Database columns and target Database columns

companyRRAccessToBeacon = Hash.new
companyRRAccessToBeacon['company_id'] = 'ID'
cmpanyRRAccessToBeacon['ultimate_parent_id'] = 'GlobalUltimateID'
companyRRAccessToBeacon['fullname'] = 'Name'
companyRRAccessToBeacon['turnover_curr'] = 'TurnoverCurrencyTypeID'
companyRRAccessToBeacon['turnover'] = 'TurnoverAmount'
companyRRAccessToBeacon['u_size_code'] = 'CorporateSizeTypeID'
companyRRAccessToBeacon['corp_status_id'] =

‘CorporateOwnershipTypeID’
companyRRAccessToBeacon[‘created_user’] = ‘RowCreatedBy’
companyRRAccessToBeacon[‘created_datetime’] = ‘RowCreatedDate’
companyRRAccessToBeacon[‘last_modification_by’] = ‘RowModifiedBy’
companyRRAccessToBeacon[‘last_modification_date’] =
‘RowModifiedDate’
companyRRAccessToBeacon[‘off_limits_id’] = ‘OffLimitsID’
Wowzer. How about this?
companyRRAccessToBeacon = {‘company_id’ => ‘ID’, ‘ultimate_parent_id’ =>
‘GlobalUltimateID’ }
And so on and so forth ? A little cleaner, a little easier to read,
fewer repetitions.

#Connect to the RRAccess and Beacon databases
rraccesshandle = connectrraccessdb
beaconhandle = connectbeacondb

[…]

#Loop through each record returned in the Data Driven Test set.
companyrowData.each do | companydata |
  puts "In #{companydata} loop"
  #If no Company records are returned then run the test.
  companydata["company_id"] != ""
  recordtofetch = companydata["company_id"]

Where is your if statement? Maybe you mean this?
recordtofetch = companydata[‘company_id’] unless
companydata[‘company_id’].empty?

[…]

The output of this is as follows:

[…]

Inside assert_equal loop for test_company_CorporateOwnershipTypeID
FInside assert_equal loop for test_company_CorporateSizeTypeID
FInside assert_equal loop for test_company_GlobalUltimateID
.Inside assert_equal loop for test_company_ID
.Inside assert_equal loop for test_company_Name
.Inside assert_equal loop for test_company_OffLimitsID
FInside assert_equal loop for test_company_RowCreatedBy
.Inside assert_equal loop for test_company_RowCreatedDate
.Inside assert_equal loop for test_company_RowModifiedBy
.Inside assert_equal loop for test_company_RowModifiedDate
.Inside assert_equal loop for test_company_TurnoverAmount
.Inside assert_equal loop for test_company_TurnoverCurrencyTypeID
F
Finished in 0.029 seconds.

  1. Failure:
    test_company_CorporateOwnershipTypeID(TestSuite)
    [C:/Users/ecucropc/Documents/Visual Studio
    2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:
    <"QUO "> expected but was
    <1>.

Well, in your first test, you get a ‘1’ back and you apparently expect a
"QUO ".
Anything else the errors can tell you that they can’t tell us?
Look into this and get back to us a little later… The tests work,
they’re telling you that they fail :slight_smile:

Thanks for your feedback. You are quite right, the assertion is giving
me the failures I was expecting, however my rather large code snippet
probably did not highlight my particular problem in the loop control I
was trying to describe.

I have since spoken with some wiser heads (namely the developers on the
team) and they suggested a good method for me to move forward on this.

I have now captured the column differences (if there are any) in a hash
and the put that hash into an assert_nil test.

Much cleaner and I only need to run one test as opposed to the potential
hundreds in my original solution.

Regards,

Chris

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs