Skip to content

Instantly share code, notes, and snippets.

@pond
Created September 30, 2013 22:17
Show Gist options
  • Save pond/6771131 to your computer and use it in GitHub Desktop.
Save pond/6771131 to your computer and use it in GitHub Desktop.
require File.dirname(__FILE__) + '/../test_helper'
class SavedReportTest < ActiveSupport::TestCase
def not_nil( a, msg )
@errors << [ msg, a ] if a.nil?
end
def check( a, b, msg )
@errors << [ msg, a, b ] unless a == b
end
def compare_reports( id, ref, com )
@errors = []
# Start with the large scale stuff - overall report data, main
# counts and so-on.
check ref.column_count(), com.column_count(), "Report #{ id }: Differing column counts"
check ref.user_count(), com.user_count(), "Report #{ id }: Differing user counts"
check ref.committed(), com.committed(), "Report #{ id }: Report committed total differs"
check ref.not_committed(), com.not_committed(), "Report #{ id }: Report not committed total differs"
check ref.total(), com.total(), "Report #{ id }: Report overall total differs"
# Per-column data.
ref_crngs = []
com_crngs = []
ref.each_column_range { | r | ref_crngs << r }
com.each_column_range { | r | com_crngs << r }
ref_crngs.each_index do | index |
ref_crng = ref_crngs[ index ]
com_crng = com_crngs[ index ]
check ref_crng, com_crng, "Report #{ id }, linear column index #{ index }: Differing column ranges"
end
ref_cttls = []
com_cttls = []
ref.each_column_total { | t | ref_cttls << t }
com.each_column_total { | t | com_cttls << t }
ref_cttls.each_index do | index |
ref_cttl = ref_cttls[ index ]
com_cttl = com_cttls[ index ]
next if ref_cttl.nil?
check ref_cttl.committed(), com_cttl.committed(), "Report #{ id }, linear column index #{ index }: Committed total differs"
check ref_cttl.not_committed(), com_cttl.not_committed(), "Report #{ id }, linear column index #{ index }: Not committed total differs"
check ref_cttl.total(), com_cttl.total(), "Report #{ id }, linear column index #{ index }: Overall total differs"
end
# Per-user data for the report and columns.
ref_users = []
com_users = []
ref.each_user { | u | ref_users << u }
com.each_user { | u | com_users << u }
ref_users.each_index do | uindex |
ref_user = ref_users[ uindex ]
com_user = com_users[ uindex ]
check ref_user.id.to_s, com_user.id.to_s, "Report #{ id }: Differing users in main user list at linear user index #{ uindex }"
check ref.user_total( ref_user.id.to_s ).committed(), com.user_total( com_user.id.to_s ).committed(), "Report #{ id }, user #{ com_user.id }, linear user index #{ uindex }: User report committed total differs"
check ref.user_total( ref_user.id.to_s ).not_committed(), com.user_total( com_user.id.to_s ).not_committed(), "Report #{ id }, user #{ com_user.id }, linear user index #{ uindex }: User report not committed total differs"
check ref.user_total( ref_user.id.to_s ).total(), com.user_total( com_user.id.to_s ).total(), "Report #{ id }, user #{ com_user.id }, linear user index #{ uindex }: User report overall total differs"
ref_cttls.each_index do | cindex |
ref_ucttl = ref_cttls[ cindex ].try( :user_total, ref_user.id.to_s )
com_ucttl = com_cttls[ cindex ].try( :user_total, com_user.id.to_s )
next if ref_ucttl.nil?
check ref_ucttl.committed(), com_ucttl.committed(), "Report #{ id }, linear column index #{ cindex }, user #{ com_user.id }, linear user index #{ uindex }: User column committed total differs"
check ref_ucttl.not_committed(), com_ucttl.not_committed(), "Report #{ id }, linear column index #{ cindex }, user #{ com_user.id }, linear user index #{ uindex }: User column not committed total differs"
check ref_ucttl.total(), com_ucttl.total(), "Report #{ id }, linear column index #{ cindex }, user #{ com_user.id }, linear user index #{ uindex }: User column overall total differs"
end
end
# Finally, check all of the rows, cells and per-user data therein.
ref.each_row do | ref_row, ref_task |
com_row = com.row( ref_task.id.to_s )
next if ( ref_row.nil? && com_row.nil? )
not_nil com_row, "Report #{ id }: Row for task #{ ref_task.id } missing"
check com_row.nil?, ref_row.nil?, "Report #{ id }: Row for task #{ ref_task.id } unexpectedly present"
ref_section, ref_is_new_section, ref_group, ref_is_new_group = ref.retrieve( ref_task.id.to_s )
com_section, com_is_new_section, com_group, com_is_new_group = com.retrieve( ref_task.id.to_s )
check ref_section.title( nil, true ), com_section.title( nil, true ), "Report #{ id }: Section title differs"
check ref_group.title(), com_group.title(), "Report #{ id }: Group title differs"
check ref_is_new_section, com_is_new_section, "Report #{ id }: Section flag differs"
check ref_is_new_group, com_is_new_group, "Report #{ id }: Group flag differs"
check ref_row.committed(), com_row.committed(), "Report #{ id }, task #{ ref_task.id }: Row committed total differs"
check ref_row.not_committed(), com_row.not_committed(), "Report #{ id }, task #{ ref_task.id }: Row not committed total differs"
check ref_row.total(), com_row.total(), "Report #{ id }, task #{ ref_task.id }: Row overall total differs"
ref_users = []
ref_utfrs = []
com_users = []
com_utfrs = []
ref.each_user_on_row( ref_row ) { | u, ut | ref_users << u; ref_utfrs << ut }
com.each_user_on_row( com_row ) { | u, ut | com_users << u; com_utfrs << ut }
ref_users.each_index do | uindex |
ref_user = ref_users[ uindex ];
ref_utfr = ref_utfrs[ uindex ];
com_user = com_users[ uindex ];
com_utfr = com_utfrs[ uindex ];
next if ref_user.nil?
check ref_user.id.to_s, com_user.id.to_s, "Report #{ id }, task #{ ref_task.id }, linear user index #{ uindex }: Differing user IDs"
next if ref_utfr.nil?
check ref_utfr.committed(), com_utfr.committed(), "Report #{ id }, task #{ ref_task.id }, user #{ com_user.id }, linear user index #{ uindex }: User row committed total differs"
check ref_utfr.not_committed(), com_utfr.not_committed(), "Report #{ id }, task #{ ref_task.id }, user #{ com_user.id }, linear user index #{ uindex }: User row not committed total differs"
check ref_utfr.total(), com_utfr.total(), "Report #{ id }, task #{ ref_task.id }, user #{ com_user.id }, linear user index #{ uindex }: User row overall total differs"
end
ref_cells = []
com_cells = []
ref.each_cell_for( ref_row ) { | c | ref_cells << c }
com.each_cell_for( com_row ) { | c | com_cells << c }
ref_cells.each_index do | cindex |
ref_cell = ref_cells[ cindex ]
com_cell = com_cells[ cindex ]
next if ref_cell.nil?
check ref_cell.committed(), com_cell.committed(), "Report #{ id }, task #{ ref_task.id }, linear cell index #{ cindex }: Cell committed value differs"
check ref_cell.not_committed(), com_cell.not_committed(), "Report #{ id }, task #{ ref_task.id }, linear cell index #{ cindex }: Cell not committed value differs"
check ref_cell.total(), com_cell.total(), "Report #{ id }, task #{ ref_task.id }, linear cell index #{ cindex }: Cell overall total differs"
ref_ucells = []
com_ucells = []
ref_users.each_index do | uindex |
ref_user = ref_users[ uindex ];
com_user = com_users[ uindex ];
ref_ucells[ uindex ] = []
com_ucells[ uindex ] = []
ref.each_cell_for_user_on_row( ref_user, ref_row ) { | c | ref_ucells[ uindex ] << c }
com.each_cell_for_user_on_row( com_user, com_row ) { | c | com_ucells[ uindex ] << c }
end
ref_users.each_index do | uindex |
ref_user = ref_users[ uindex ];
com_user = com_users[ uindex ];
ref_uucells = ref_ucells[ uindex ]
com_uucells = com_ucells[ uindex ]
ref_uucells.each_index do | cindex |
ref_uucell = ref_uucells[ cindex ]
com_uucell = com_uucells[ cindex ]
next if ref_uucell.nil?
check ref_uucell.committed(), com_uucell.committed(), "Report #{ id }, task #{ ref_task.id }, linear cell index #{ cindex }, user #{ com_user.id }, linear user index #{ uindex }: User cell committed value differs"
check ref_uucell.not_committed(), com_uucell.not_committed(), "Report #{ id }, task #{ ref_task.id }, linear cell index #{ cindex }, user #{ com_user.id }, linear user index #{ uindex }: User cell not committed value differs"
check ref_uucell.total(), com_uucell.total(), "Report #{ id }, task #{ ref_task.id }, linear cell index #{ cindex }, user #{ com_user.id }, linear user index #{ uindex }: User cell overall total differs"
end
end
end
end
# Make sure there were no errors, else report the details
assert @errors.empty?, @errors.map() { | e | e.join( "\n" ) }.join( "\n\n" )
end
reps = YAML::load_file( File.join( Rails.root, "test", "fixtures", "saved_reports.yml" ) )
ids = reps.values.map { | h | h[ "id" ] }
ids.each do | id |
class_eval %|
test "99 compare report ID #{ id } with reference" do
# Sigh:
#
# https://github.com/rails/rails/issues/9263
# https://github.com/rails/arel/issues/149
#
# ...via:
#
# http://stackoverflow.com/questions/17667768/delayed-job-inside-model-wrong-number-of-arguments
module Arel
module Nodes
class SqlLiteral < String
def encode_with(coder)
coder['string'] = to_s
end
def init_with(coder)
clear << coder['string']
end
end
end
end
base = File.join( Rails.root, "test", "comparison_data", "saved_reports" )
sr = SavedReport.find( id )
comparison = sr.generate_report
comparison.compile()
# See "lib/tasks/db_dump_reports_for_tests.rake" for rationale.
path = File.join( base, "#{ id }.yaml.gz" )
reference = Zlib::GzipReader.open( path ) { \| r_gz \| r_gz.read }
reference = YAML::load( reference )
compare_reports( id, reference, comparison )
end
|
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment