Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/plsql_unit_test/test_unit_patch.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.set_database_interface(interface) ⇒ Object

Sets the class instance variable @@db_interface to the object that is passed in



5
6
7
# File 'lib/plsql_unit_test/test_unit_patch.rb', line 5

def self.set_database_interface(interface)
  @@db_interface = interface
end

Instance Method Details

#assert_table_has_many_rows(table, row_count, where_clause = nil, message = nil) ⇒ Object

Used to assert / test a given table has a given number of rows. If a where clause is passed, it should start with ‘where’. The table name and where clause are used to form a select statement in the form:

"select count(*) from #{table} #{where_clause}"

It is possible to pass an array for the where clause, where the first element is the where string with bind placeholders, and the remaining elements are the bind variables:

Examples:

Assert the users table has zero rows for user foouser

assert_table_has_many_rows('users',
                            10
                           "where status = 'locked'",
                           "Ensure the users table has 10 locked rows")

Assert the users table has a single row for user foouser

assert_table_has_many_rows('users',
                         ["where status = ?", 'locked'],
                         "Ensure the users table has 10 locked rows")


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/plsql_unit_test/test_unit_patch.rb', line 78

def assert_table_has_many_rows(table, row_count, where_clause=nil, message=nil)
  where = where_clause
  binds = Array.new
  if where_clause and where_clause.is_a? Array
    where = where_clause.shift
    binds = where_clause
  end
  results = @@db_interface.execute_sql("select count(*) from #{table} #{where}", *binds).all_array
  message = build_message(message, "A count of <?> was expected but was <?> for <?> <?>", row_count.to_s, results[0][0].to_i, table, where_clause)
  assert_block(message) do
    row_count == results[0][0].to_i
  end
end

#assert_table_has_one_row(table, where_clause = nil, message = nil) ⇒ Object

Used to assert / test a given table has only a single row. If a where clause is passed, it should start with ‘where’. The table name and where clause are used to form a select statement in the form:

"select count(*) from #{table} #{where_clause}"

It is possible to pass an array for the where clause, where the first element is the where string with bind placeholders, and the remaining elements are the bind variables:

Examples:

Assert the users table has a single row for user foouser

assert_table_has_one_row('users',
                         "where username = 'foouser'",
                         "Ensure the users table has a single row")

Assert the users table has a single row for user foouser

assert_table_has_one_row('users',
                         ["where username = ?", 'foouser'],
                         "Ensure the users table has a single row")


29
30
31
# File 'lib/plsql_unit_test/test_unit_patch.rb', line 29

def assert_table_has_one_row(table, where_clause=nil, message=nil)
  assert_table_has_many_rows(table, 1, where_clause, message)
end

#assert_table_has_zero_rows(table, where_clause = nil, message = nil) ⇒ Object

Used to assert / test a given table has zero rows. If a where clause is passed, it should start with ‘where’. The table name and where clause are used to form a select statement in the form:

"select count(*) from #{table} #{where_clause}"

It is possible to pass an array for the where clause, where the first element is the where string with bind placeholders, and the remaining elements are the bind variables:

Examples:

Assert the users table has zero rows for user foouser

assert_table_has_zero_rows('users',
                           "where username = 'foouser'",
                           "Ensure the users table has zero rows")

Assert the users table has a single row for user foouser

assert_table_has_zero_row('users',
                         ["where username = ?", 'foouser'],
                         "Ensure the users table has zero rows")


53
54
55
# File 'lib/plsql_unit_test/test_unit_patch.rb', line 53

def assert_table_has_zero_rows(table, where_clause=nil, message=nil)
  assert_table_has_many_rows(table, 0, where_clause, message)
end

#time_as_oracle_dt(time) ⇒ Object

Returns a string representing an Ruby Time object in Oracle to_date format, accurate to a day.



100
101
102
# File 'lib/plsql_unit_test/test_unit_patch.rb', line 100

def time_as_oracle_dt(time)
  "to_date('#{time.strftime('%Y%m%d')}', 'YYYYMMDD')"
end

#time_as_oracle_dtm(time) ⇒ Object

Returns a string representing an Ruby Time object in Oracle to_date format, accurate to a second.



94
95
96
# File 'lib/plsql_unit_test/test_unit_patch.rb', line 94

def time_as_oracle_dtm(time)
  "to_date('#{time.strftime('%Y%m%d %H:%M:%S')}', 'YYYYMMDD HH24:MI:SS')"
end