# Author:: Vincent Cuissard # Copyright:: Copyright (c) 2004, 2005 Uttk team. All rights reserved. # License:: LGPL # $Id: /w/fey/uttk/trunk/lib/uttk/strategies/SqlQuery.rb 8789 2005-09-27T14:49:49.088376Z ertai $ module Uttk module Strategies # # This strategy manage _SQL_ base tests. # It use unit testing (EXCEPT ALL query) for checking results. # To use it, you need to have _dbi_ installed on your test system # and have a database with these things: # * a test schema (MY) # * a reference schema (REF) # # After that you have just to write all queries that you want to test. # Queries will be called on each schema by applying a delta operation: # (REF ` MY) U (MY ` REF). # # In queries, %s will be replaced by given schemas. # class SqlQuery < Strategy include Concrete @@db = nil # # Methods # def prologue super if @@db.nil? require 'dbi' # Check if driver is available if DBI.available_drivers.to_s !~ /#@driver/ raise_error("Unknown driver: #@driver") else # Start DB connection @@db = DBI.connect("DBI:#@driver:host=#@host;database=#@database", @user, @passwd) end end end protected :prologue def run_impl @delta = nil begin @delta = perform_delta rescue DBI::DatabaseError => ex fail(ex) end end protected :run_impl def assertion unless @delta.nil? @log.diff_output = @delta fail('Delta not empty') end super end protected :assertion def perform_delta output_a = [] output_b = [] # Make queries ref_query = @query.gsub(/%s/, @ref_schema) my_query = @query.gsub(/%s/, @my_schema) # Perform REF ` MY @@db.execute("#{ref_query} EXCEPT ALL #{my_query}").fetch do |row| output_a << row.join(' | ') end # Perform MY ` REF @@db.execute("#{my_query} EXCEPT ALL #{ref_query}").fetch do |row| output_b << row.join(' | ') end # REF DELTA MY => (REF ` MY) U (MY ` REF) = {} # Check this return nil if output_a.empty? and output_b.empty? # Make report { :'REF ` MY' => output_a.join("\n"), :'MY ` REF' => output_b.join("\n") } end private :perform_delta # # Attributes # attribute :my_schema, 'Test schema', 'my' attribute :ref_schema, 'Reference schema', 'ref' attribute :query, 'SQL query (%s for the schema)', :mandatory attribute :driver, 'SQL server driver', :mandatory attribute :database, 'SQL database name', :mandatory attribute :host, 'SQL server host', :mandatory attribute :user, 'SQL user', :mandatory attribute :passwd, 'SQL pass', :mandatory end # class SqlQuery end # module Strategies end # module Uttk