Module: RWebSpec::WebDriver::DatabaseChecker
- Defined in:
- lib/rwebspec-webdriver/database_checker.rb
Instance Method Summary collapse
- #clear_database_connection ⇒ Object
-
#connect_to_database(db_settings, force = false) ⇒ Object
Connect to databse, example mysql_db(:host => “localhost”, :database => “lavabuild_local”, :user => “root”, :password => “”).
- #load_table(table_name) ⇒ Object
-
#mysql_db(settings) ⇒ Object
Example connect_to_database mysql_db(:host => “localhost”, :database => “lavabuild_local”, :user => “root”, :password => “”), true.
-
#sqlite3_db(settings) ⇒ Object
connect_to_database sqlite3_db(:database => File.join(File.dirname(__FILE__), “testdata”, “sample.sqlite3”)), true.
- #sqlserver_db(settings) ⇒ Object
- #sqlserver_db_dbi(options) ⇒ Object
Instance Method Details
#clear_database_connection ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/rwebspec-webdriver/database_checker.rb', line 33 def clear_database_connection begin ActiveRecord::Base.remove_connection rescue => e puts "failed o clear database connection: #{e}" end end |
#connect_to_database(db_settings, force = false) ⇒ Object
Connect to databse, example
mysql_db(:host => "localhost", :database => "lavabuild_local", :user => "root", :password => "")
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rwebspec-webdriver/database_checker.rb', line 43 def connect_to_database(db_settings, force = false) # only setup database connection once if force ActiveRecord::Base.establish_connection(db_settings) else begin ActiveRecord::Base.connection rescue => e require 'pp' pp db_settings puts "failed to connect: #{e}" ActiveRecord::Base.establish_connection(db_settings) end end end |
#load_table(table_name) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rwebspec-webdriver/database_checker.rb', line 59 def load_table(table_name) begin ActiveRecord::Base.connection rescue =>e raise "No database connection setup yet, use connect_to_database() method" end class_name = table_name.classify # define the class, so can use ActiveRecord in # such as # Perosn.count.should == 2 def_class = "class ::#{class_name} < ActiveRecord::Base; end" eval def_class return def_class end |
#mysql_db(settings) ⇒ Object
Example
connect_to_database mysql_db(:host => "localhost", :database => "lavabuild_local", :user => "root", :password => ""), true
9 10 11 12 |
# File 'lib/rwebspec-webdriver/database_checker.rb', line 9 def mysql_db(settings) = {:adapter => "mysql"} .merge!(settings) end |
#sqlite3_db(settings) ⇒ Object
connect_to_database sqlite3_db(:database => File.join(File.dirname(__FILE__), “testdata”, “sample.sqlite3”)), true
15 16 17 18 |
# File 'lib/rwebspec-webdriver/database_checker.rb', line 15 def sqlite3_db(settings) = {:adapter => "sqlite3"} .merge!(settings) end |
#sqlserver_db(settings) ⇒ Object
20 21 22 23 24 |
# File 'lib/rwebspec-webdriver/database_checker.rb', line 20 def sqlserver_db(settings) = {:adapter => "sqlserver"} [:username] ||= settings[:user] .merge!(settings) end |
#sqlserver_db_dbi(options) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/rwebspec-webdriver/database_checker.rb', line 26 def sqlserver_db_dbi() [:user] ||= [:username] [:username] ||= [:user] conn_str = "DBI:ADO:Provider=SQLOLEDB;Data Source=#{[:host]};Initial Catalog=#{[:database]};User ID=\"#{[:user]}\";password=\"#{[:password]}\" " dbh = DBI.connect(conn_str) end |