Skip to content

Commit

Permalink
Added 'validate_query' helper for unit testing with SQLiss. Fixed tes…
Browse files Browse the repository at this point in the history
…ting issues with custom queries.
  • Loading branch information
gaspard committed Apr 6, 2011
1 parent a064e71 commit f085739
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/tasks/zena.rake
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ namespace :zena do
if RAILS_ENV == 'production'
puts "You cannot reset database in production !"
else
ENV['WORKER'] = 'false'
# FIXME: it seems the ENV is not propagated to the tasks below...
%w{db:drop db:create zena:migrate zena:build_fixtures db:test:clone}.each do |task|
puts "******************************* #{task}"
Rake::Task[task].invoke
Expand Down Expand Up @@ -340,13 +342,14 @@ namespace :zena do
end

%w{db:fixtures:load zena:rebuild_index}.each do |task|
ENV['WORKER'] = 'false'
puts "******************************* #{task}"
Rake::Task[task].invoke
end

index_tables = Node.connection.tables.select {|t| t =~ /^idx_/ }
Zena::FoxyParser.dump_fixtures(index_tables)
# Currently, inline indexes are not serialized in the fixtures and need to be
# explicitely set along with the property value.
end
end

Expand All @@ -359,7 +362,7 @@ namespace :zena do
sites = Site.all
end
sites.each do |site|
if ENV['WORKER'] == 'false'
if ENV['WORKER'] == 'false' || RAILS_ENV == 'test'
# We avoid SiteWorker by passing nodes.
Thread.current[:visitor] = site.any_admin
nodes = Node.find(:all,
Expand Down
2 changes: 1 addition & 1 deletion lib/zena/foxy_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def set_defaults
records = [
{ 'id' => Zena::FoxyParser::id(site, "#{name}_#{node['v_lang'] || node['ref_lang']}"),
'publish_from' => node['v_publish_from'],
'status' => Zena::Status[node['v_status'].to_sym],
'status' => Zena::Status[(node['v_status'] || 'pub').to_sym],
'lang' => node['v_lang'] || node['ref_lang']
}
]
Expand Down
64 changes: 64 additions & 0 deletions lib/zena/unit/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,70 @@ def self.helper_attr(*args)
# Ignore since we include helpers in the TestCase itself
end

# Specific helpers to validate model relations and queries with SQLiss
def main_date
raise "The test uses 'validate_query' without defining @main_date" unless @main_date
return @main_date.strftime("'%Y-%m-%d'")
end

# Test a query (useful with complex custom queries). Usage:
#
# validate_query "emp_comp_dates where log_at is not null", [
# { :title => 'Creativity',
# :priority => '5',
# :log_at => '2010-06-01',
# :event_at => '2011-06-01',
# },
# { :title => 'Leadership',
# :priority => '5',
# :log_at => '2003-01-01',
# :event_at => nil, # forever
# },
# ]
#
def validate_query(query, expected_list)
list = subject.find(:all, query, :errors => true)
if expected_list.nil? || expected_list.empty?
assert_equal nil, list
elsif expected_list.first.kind_of?(String)
assert_equal expected_list, list.map(&:title)
else
proto = expected_list.first.keys
sz = [expected_list.size, list.size].max

list.each_with_index do |r, i|
record = list[i]
expected = expected_list[i]
if not r
assert_equal expected[:title], nil
elsif not expected
assert_equal nil, map_to_proto(proto, record)
else
if expected[:title] != record.title
assert_equal expected[:title], map_to_proto(proto, record)
else
proto.each do |key|
value = format_date(r[key] || record.send(key))
assert_equal expected[key], value, "(#{record.title} #{key} expected to be #{expected[key].inspect} but was #{value.inspect}"
end
end
end
end
end
end

private
def format_date(date)
if date.respond_to?(:strftime)
date.strftime('%Y-%m-%d')
else
date
end
end

def map_to_proto(proto, record)
Hash[*proto.map{|k| [k, format_date(record[k] || record.send(k))]}.flatten]
end
end
end
end

0 comments on commit f085739

Please sign in to comment.