"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "test/table_test.rb" between
ruport-1.7.1.tar.gz and ruport-1.8.0.tar.gz

About: Ruport (Ruby Reports) is a simple, extensible reporting system built for Rubyists. It provides tools for data acquisition, database interaction, formatting, and parsing/munging.

table_test.rb  (ruport-1.7.1):table_test.rb  (ruport-1.8.0)
skipping to change at line 28 skipping to change at line 28
table2 = Ruport::Data::Table.new :column_names => %w[a b c] table2 = Ruport::Data::Table.new :column_names => %w[a b c]
table3 = Ruport::Data::Table.new :data => [[1,2,3]] table3 = Ruport::Data::Table.new :data => [[1,2,3]]
table4 = Ruport::Data::Table.new :column_names => %w[col1 col2 col3], table4 = Ruport::Data::Table.new :column_names => %w[col1 col2 col3],
:data => [[1,2,3]] :data => [[1,2,3]]
tables = [table,table2,table3,table4] tables = [table,table2,table3,table4]
tables.zip([[],%w[a b c], [], %w[col1 col2 col3]]).each do |t,n| tables.zip([[],%w[a b c], [], %w[col1 col2 col3]]).each do |t,n|
assert_equal n, t.column_names assert_equal n, t.column_names
t = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]]) t = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
table_from_records = Ruport.Table(t.column_names, :data => t.data) Ruport.Table(t.column_names, :data => t.data)
end end
a = Ruport::Data::Record.new [1,2,3] a = Ruport::Data::Record.new [1,2,3]
b = Ruport::Data::Record.new [1,2,3], :attributes => %w[col1 col2 col3] b = Ruport::Data::Record.new [1,2,3], :attributes => %w[col1 col2 col3]
tables.zip([[],[],[a],[b]]).each do |t,n| tables.zip([[],[],[a],[b]]).each do |t,n|
assert_equal n, t.data assert_equal n, t.data
end end
end end
context "when filtering data" do context "when filtering data" do
def setup def setup
@data = [[1,2,3],[4,5,6],[7,8,9]] @data = [[1,2,3],[4,5,6],[7,8,9]]
end end
def specify_filters_should_discard_unmatched_rows should "specify_filters_should_discard_unmatched_rows" do
table = Ruport::Data::Table.new(:column_names => %w[a b c], table = Ruport::Data::Table.new(:column_names => %w[a b c],
:data => [[1,2,3],[4,5,6],[7,8,9]], :data => [[1,2,3],[4,5,6],[7,8,9]],
:filters => [ lambda { |r| r.a % 2 == 1 } ] ) :filters => [ lambda { |r| r.a % 2 == 1 } ] )
assert_equal Ruport.Table(%w[a b c]) << [1,2,3] << [7,8,9], table assert_equal Ruport.Table(%w[a b c]) << [1,2,3] << [7,8,9], table
end end
def specify_filters_should_work_on_csvs should "specify_filters_should_work_on_csvs" do
only_ids_less_than_3 = lambda { |r| r["id"].to_i < 3 } only_ids_less_than_3 = lambda { |r| r["id"].to_i < 3 }
table = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), table = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"),
:filters => [only_ids_less_than_3]) :filters => [only_ids_less_than_3])
assert_equal ["1","2"], table.map { |r| r["id"] } assert_equal ["1","2"], table.map { |r| r["id"] }
end end
end end
context "when transforming data" do context "when transforming data" do
def setup setup do
@data = [[1,2,3],[4,5,6],[7,8,9]] @data = [[1,2,3],[4,5,6],[7,8,9]]
end end
def specify_transforms_should_modify_table_data should "specify_transforms_should_modify_table_data" do
stringify_c = lambda { |r| r.c = r.c.to_s } stringify_c = lambda { |r| r.c = r.c.to_s }
add_two_to_all_int_cols = lambda { |r| add_two_to_all_int_cols = lambda { |r|
r.each_with_index do |c,i| r.each_with_index do |c,i|
if Fixnum === c if Integer === c
r[i] += 2 r[i] += 2
end end
end end
} }
table = Ruport::Data::Table.new(:column_names => %w[a b c], table = Ruport::Data::Table.new(:column_names => %w[a b c],
:data => @data, :data => @data,
:transforms => [stringify_c, :transforms => [stringify_c,
add_two_to_all_int_cols]) add_two_to_all_int_cols])
assert_equal Ruport.Table(%w[a b c]) << [3,4,"3"] << [6,7,"6"] << [9,10,"9" ], assert_equal Ruport.Table(%w[a b c]) << [3,4,"3"] << [6,7,"6"] << [9,10,"9" ],
table table
end end
def specify_transforms_should_work_on_csvs should "specify_transforms_should_work_on_csvs" do
ids_to_i = lambda { |r| r["id"] = r["id"].to_i } ids_to_i = lambda { |r| r["id"] = r["id"].to_i }
table = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), table = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"),
:filters => [ids_to_i]) :filters => [ids_to_i])
assert_equal [1,2,3,4,5], table.map { |r| r["id"] } assert_equal [1,2,3,4,5], table.map { |r| r["id"] }
end end
end end
def test_to_group def test_to_group
a = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]]).to_group("Packrats") a = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]]).to_group("Packrats")
b = Ruport::Data::Group.new( :data => [[1,2,3],[4,5,6]], b = Ruport::Data::Group.new( :data => [[1,2,3],[4,5,6]],
skipping to change at line 189 skipping to change at line 189
b.each { |r| b.each { |r|
assert_equal rows.shift, r.to_a assert_equal rows.shift, r.to_a
assert_equal %w[a c], r.attributes assert_equal %w[a c], r.attributes
assert_equal b.column_names.object_id, assert_equal b.column_names.object_id,
r.instance_eval{@attributes}.object_id r.instance_eval{@attributes}.object_id
} }
end end
context "when sorting rows" do context "when sorting rows" do
def setup setup do
@table = Ruport.Table(%w[a b c]) << [1,2,3] << [6,1,8] << [9,1,4] @table = Ruport.Table(%w[a b c]) << [1,2,3] << [6,1,8] << [9,1,4]
@table_with_nils = Ruport.Table(%w[a b c]) << [1,nil,3] << [9,3,4] << [6,1 ,8] @table_with_nils = Ruport.Table(%w[a b c]) << [1,nil,3] << [9,3,4] << [6,1 ,8]
end end
def specify_should_sort_in_reverse_order_on_descending should "specify_should_sort_in_reverse_order_on_descending" do
t = @table.sort_rows_by("a", :order => :descending ) t = @table.sort_rows_by("a", :order => :descending )
assert_equal Ruport.Table(%w[a b c]) << [9,1,4] << [6,1,8] << [1,2,3], t assert_equal Ruport.Table(%w[a b c]) << [9,1,4] << [6,1,8] << [1,2,3], t
t = @table.sort_rows_by("c", :order => :descending ) t = @table.sort_rows_by("c", :order => :descending )
assert_equal Ruport.Table(%w[a b c]) << [6,1,8] << [9,1,4] << [1,2,3], t assert_equal Ruport.Table(%w[a b c]) << [6,1,8] << [9,1,4] << [1,2,3], t
end end
def specify_show_put_rows_with_nil_columns_after_sorted_rows should "specify_show_put_rows_with_nil_columns_after_sorted_rows" do
# should not effect when using columns that are all populated # should not effect when using columns that are all populated
t = @table_with_nils.sort_rows_by("a") t = @table_with_nils.sort_rows_by("a")
assert_equal Ruport.Table(%w[a b c]) << [1,nil,3] << [6,1,8] << [9,3,4], t assert_equal Ruport.Table(%w[a b c]) << [1,nil,3] << [6,1,8] << [9,3,4], t
t = @table_with_nils.sort_rows_by("b") t = @table_with_nils.sort_rows_by("b")
assert_equal Ruport.Table(%w[a b c]) << [6,1,8] << [9,3,4] << [1,nil,3], t assert_equal Ruport.Table(%w[a b c]) << [6,1,8] << [9,3,4] << [1,nil,3], t
t = @table_with_nils.sort_rows_by("b", :order => :descending) t = @table_with_nils.sort_rows_by("b", :order => :descending)
assert_equal Ruport.Table(%w[a b c]) << [1,nil,3] << [9,3,4] << [6,1,8], t assert_equal Ruport.Table(%w[a b c]) << [1,nil,3] << [9,3,4] << [6,1,8], t
end end
def specify_in_place_sort_should_allow_order_by should "specify_in_place_sort_should_allow_order_by" do
@table.sort_rows_by!("a", :order => :descending ) @table.sort_rows_by!("a", :order => :descending )
assert_equal Ruport.Table(%w[a b c]) << [9,1,4] << [6,1,8] << [1,2,3], @t able assert_equal Ruport.Table(%w[a b c]) << [9,1,4] << [6,1,8] << [1,2,3], @t able
end end
def specify_sort_rows_by should "specify_sort_rows_by" do
table = Ruport::Data::Table.new :column_names => %w[a b c] table = Ruport::Data::Table.new :column_names => %w[a b c]
table << [1,2,3] << [6,1,8] << [9,1,4] table << [1,2,3] << [6,1,8] << [9,1,4]
table2 = Ruport::Data::Table.new :column_names => [:a, :b, :c] table2 = Ruport::Data::Table.new :column_names => [:a, :b, :c]
table2 << [1,2,3] << [6,1,8] << [9,1,4] table2 << [1,2,3] << [6,1,8] << [9,1,4]
sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c] sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c]
sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4] sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4]
sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c] sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
skipping to change at line 244 skipping to change at line 244
sorted_table_bs = Ruport::Data::Table.new :column_names => [:a, :b, :c] sorted_table_bs = Ruport::Data::Table.new :column_names => [:a, :b, :c]
sorted_table_bs << [6,1,8] << [9,1,4] << [1,2,3] sorted_table_bs << [6,1,8] << [9,1,4] << [1,2,3]
assert_equal sorted_table_a, table.sort_rows_by {|r| r['a']} assert_equal sorted_table_a, table.sort_rows_by {|r| r['a']}
assert_equal sorted_table_b, table.sort_rows_by(['b']) assert_equal sorted_table_b, table.sort_rows_by(['b'])
assert_equal sorted_table_bc, table.sort_rows_by(['b', 'c']) assert_equal sorted_table_bc, table.sort_rows_by(['b', 'c'])
assert_equal sorted_table_bs, table2.sort_rows_by(:b) assert_equal sorted_table_bs, table2.sort_rows_by(:b)
end end
def specify_sort_rows_by! should "specify_sort_rows_by!" do
table = Ruport::Data::Table.new :column_names => %w[a b c] table = Ruport::Data::Table.new :column_names => %w[a b c]
table << [1,2,3] << [6,1,8] << [9,1,4] table << [1,2,3] << [6,1,8] << [9,1,4]
sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c] sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c]
sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4] sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4]
sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c] sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3] sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3]
sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c] sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c]
skipping to change at line 274 skipping to change at line 274
table_bc.sort_rows_by!(['b', 'c']) table_bc.sort_rows_by!(['b', 'c'])
assert_equal sorted_table_a, table_a assert_equal sorted_table_a, table_a
assert_equal sorted_table_b, table_b assert_equal sorted_table_b, table_b
assert_equal sorted_table_bc, table_bc assert_equal sorted_table_bc, table_bc
end end
end end
context "when adding rows" do context "when adding rows" do
def setup setup do
@columns = %w[a b c] @columns = %w[a b c]
@data = [[1,2,3],[4,5,6],[7,8,9]] @data = [[1,2,3],[4,5,6],[7,8,9]]
@table = Ruport::Data::Table.new(:column_names => @columns, :data => @data ) @table = Ruport::Data::Table.new(:column_names => @columns, :data => @data )
@new_row = [-1,-2,-3] @new_row = [-1,-2,-3]
end end
def specify_insert_at_the_beginning should "specify_insert_at_the_beginning" do
@table.add_row(@new_row, :position => 0) @table.add_row(@new_row, :position => 0)
assert_equal Ruport::Data::Table.new(:column_names => @columns, assert_equal Ruport::Data::Table.new(:column_names => @columns,
:data => [@new_row] + @data), @table :data => [@new_row] + @data), @table
end end
def specify_insert_in_the_middle should "specify_insert_in_the_middle" do
@table.add_row(@new_row, :position => 2) @table.add_row(@new_row, :position => 2)
assert_equal Ruport::Data::Table.new(:column_names => @columns, assert_equal Ruport::Data::Table.new(:column_names => @columns,
:data => [[1,2,3],[4,5,6],[-1,-2,-3],[7,8,9]]), @table :data => [[1,2,3],[4,5,6],[-1,-2,-3],[7,8,9]]), @table
end end
def specify_insert_at_the_end should "specify_insert_at_the_end" do
@table.add_row(@new_row) @table.add_row(@new_row)
assert_equal Ruport::Data::Table.new(:column_names => @columns, assert_equal Ruport::Data::Table.new(:column_names => @columns,
:data => @data + [@new_row]), @table :data => @data + [@new_row]), @table
end end
def should_return_itself should "return_itself" do
assert_equal @table.object_id, @table.add_row(@new_row).object_id assert_equal @table.object_id, @table.add_row(@new_row).object_id
end end
end end
def test_record_class def test_record_class
a = Ruport::Data::Table.new( :column_names => %w[first_name last_name c], a = Ruport::Data::Table.new( :column_names => %w[first_name last_name c],
:data =>[['joe','loop',3],['jim','blue',6]], :data =>[['joe','loop',3],['jim','blue',6]],
:record_class => Person ) :record_class => Person )
assert_equal a, Ruport.Table(%w[first_name last_name c], assert_equal a, Ruport.Table(%w[first_name last_name c],
:data => [ ['joe','loop',3],['jim','blue',6] ]) :data => [ ['joe','loop',3],['jim','blue',6] ])
skipping to change at line 721 skipping to change at line 721
# ticket:94 # ticket:94
table = Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.tsv"), table = Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.tsv"),
:csv_options => { :col_sep => "\t" } ) :csv_options => { :col_sep => "\t" } )
assert_equal expected, table assert_equal expected, table
expected = ['c','e'] expected = ['c','e']
table = Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.csv"), table = Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.csv"),
:csv_options => { :headers => true, :header_converters => :symbol } :csv_options => { :headers => true, :header_converters => :symbol }
) do |s,r| ) do |_s,r|
assert_equal expected.shift, r[:col3] assert_equal expected.shift, r[:col3]
end end
assert_equal [:col1,:col2,:col3], table.column_names assert_equal [:col1,:col2,:col3], table.column_names
expected = ['c','e'] expected = ['c','e']
Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.csv"), Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.csv"),
:records => true ) do |s,r| :records => true ) do |_s,r|
assert_equal expected.shift, r.col3 assert_equal expected.shift, r.col3
assert_kind_of Ruport::Data::Record, r assert_kind_of Ruport::Data::Record, r
end end
table = Ruport::Data::Table.load( File.join(TEST_SAMPLES, "data.csv"), table = Ruport::Data::Table.load( File.join(TEST_SAMPLES, "data.csv"),
:has_names => false ) :has_names => false )
assert_equal([],table.column_names) assert_equal([],table.column_names)
assert_equal(Ruport.Table([], assert_equal(Ruport.Table([],
:data => [%w[col1 col2 col3],%w[a b c],["d",nil,"e"]]), table) :data => [%w[col1 col2 col3],%w[a b c],["d",nil,"e"]]), table)
end end
skipping to change at line 792 skipping to change at line 792
x = Ruport::Data::Table.load(File.join(TEST_SAMPLES,"data.csv")) { |s,r| x = Ruport::Data::Table.load(File.join(TEST_SAMPLES,"data.csv")) { |s,r|
assert_kind_of Ruport::Data::Feeder, s assert_kind_of Ruport::Data::Feeder, s
assert_kind_of Array, r assert_kind_of Array, r
s << r s << r
s << r s << r
} }
assert_equal 4, x.length assert_equal 4, x.length
end end
def test_ensure_csv_loading_accepts_table_options def test_ensure_csv_loading_accepts_table_options
a = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), a = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), :record_class =>
:record_class => DuckRecord) DuckRecord)
a.each { |r| assert_kind_of(DuckRecord,r) } a.each { |r| assert_kind_of(DuckRecord,r) }
end end
def test_ensure_table_from_csv_accepts_record_class_in_block_usage def test_ensure_table_from_csv_accepts_record_class_in_block_usage
a = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), :record_class => Duc
:record_class => DuckRecord, :records => true) do |s,r| kRecord, :records => true) do |_s,r|
assert_kind_of(DuckRecord,r) assert_kind_of(DuckRecord,r)
end end
end end
end end
class TestTableKernelHack < Minitest::Test class TestTableKernelHack < Minitest::Test
def test_simple def test_simple
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]), assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]), Ruport.Tab
Ruport.Table(%w[a b c]) le(%w[a b c])
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]), assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]), Ruport.Tab
Ruport.Table("a","b","c") le("a","b","c")
assert_equal Ruport::Data::Table.load( assert_equal Ruport::Data::Table.load(
File.join(TEST_SAMPLES,"addressbook.csv")), File.join(TEST_SAMPLES,"addressbook.csv")),
Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv")) Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"))
assert_equal Ruport::Data::Table.load( assert_equal Ruport::Data::Table.load(
File.join(TEST_SAMPLES,"addressbook.csv"), :has_names => fals e), File.join(TEST_SAMPLES,"addressbook.csv"), :has_names => fals e),
Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), :has_na mes => false) Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), :has_na mes => false)
Ruport.Table("a","b","c") do |t| Ruport.Table("a","b","c") do |t|
t << [1,2,3] t << [1,2,3]
assert_equal( assert_equal(
Ruport::Data::Table.new(:column_names => %w[a b c], :data => [[1,2,3]]), Ruport::Data::Table.new(:column_names => %w[a b c], :data => [[1,2,3]]),
skipping to change at line 864 skipping to change at line 858
csv_string = "id,name\n1,Inky\n2,Blinky\n3,Clyde" csv_string = "id,name\n1,Inky\n2,Blinky\n3,Clyde"
assert_equal Ruport::Data::Table.parse(csv_string), assert_equal Ruport::Data::Table.parse(csv_string),
Ruport.Table(:string => csv_string) Ruport.Table(:string => csv_string)
end end
def test_ensure_table_hack_accepts_normal_constructor_args def test_ensure_table_hack_accepts_normal_constructor_args
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]), assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]),
Ruport.Table(:column_names => %w[a b c]) Ruport.Table(:column_names => %w[a b c])
end end
end end
 End of changes. 26 change blocks. 
33 lines changed or deleted 30 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)