diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/tc_axlsx.rb | 6 | ||||
| -rw-r--r-- | test/tc_package.rb | 19 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_pivot_table.rb | 101 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_pivot_table_cache_definition.rb | 46 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_worksheet.rb | 9 |
5 files changed, 175 insertions, 6 deletions
diff --git a/test/tc_axlsx.rb b/test/tc_axlsx.rb index 4901ede2..0f94b3e5 100644 --- a/test/tc_axlsx.rb +++ b/test/tc_axlsx.rb @@ -54,4 +54,10 @@ class TestAxlsx < Test::Unit::TestCase # todo end + def test_range_to_a + assert_equal([['A1', 'B1', 'C1']], Axlsx::range_to_a('A1:C1')) + assert_equal([['A1', 'B1', 'C1'], ['A2', 'B2', 'C2']], Axlsx::range_to_a('A1:C2')) + assert_equal([['Z5', 'AA5', 'AB5'], ['Z6', 'AA6', 'AB6']], Axlsx::range_to_a('Z5:AB6')) + end + end diff --git a/test/tc_package.rb b/test/tc_package.rb index d4333993..f3802105 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -8,7 +8,8 @@ class TestPackage < Test::Unit::TestCase ws.add_row ['Can', 'we', 'build it?'] ws.add_row ['Yes!', 'We', 'can!'] ws.add_hyperlink :ref => ws.rows.first.cells.last, :location => 'https://github.com/randym' - ws.workbook.add_defined_name("#{ws.name}!A1:C2", :name => '_xlnm.Print_Titles', :hidden => true) + # Not sure what this does but no specs break without it and `definedNames` is not in sml.xsd + # ws.workbook.add_defined_name("#{ws.name}!A1:C2", :name => '_xlnm.Print_Titles', :hidden => true) ws.protect_range('A1:C1') ws.protect_range(ws.rows.last.cells) ws.add_comment :author => 'alice', :text => 'Hi Bob', :ref => 'A12' @@ -38,15 +39,15 @@ class TestPackage < Test::Unit::TestCase ws.add_chart(Axlsx::Line3DChart, :title => "axis labels") do |chart| chart.valAxis.title = 'bob' chart.d_lbls.show_val = true - end - + end + ws.add_chart(Axlsx::Bar3DChart, :title => 'bar chart') do |chart| chart.add_series :data => [1,4,5], :labels => %w(A B C) chart.d_lbls.show_percent = true end ws.add_chart(Axlsx::ScatterChart, :title => 'scat man') do |chart| - chart.add_series :xData => [1,2,3,4], :yData => [4,3,2,1] + chart.add_series :xData => [1,2,3,4], :yData => [4,3,2,1] chart.d_lbls.show_val = true end @@ -61,7 +62,7 @@ class TestPackage < Test::Unit::TestCase ws.add_image :image_src => File.expand_path('../../examples/image1.gif', __FILE__) do |image| image.start_at 0, 20 image.width=360 - image.height=333 + image.height=333 end ws.add_image :image_src => File.expand_path('../../examples/image1.png', __FILE__) do |image| image.start_at 9, 20 @@ -69,6 +70,9 @@ class TestPackage < Test::Unit::TestCase image.height = 167 end ws.add_table 'A1:C1' + + ws.add_pivot_table 'G5:G6', 'A1:B3' + end def test_use_autowidth @@ -135,10 +139,13 @@ class TestPackage < Test::Unit::TestCase assert_equal(p.select{ |part| part[:entry] =~ /xl\/worksheets\/sheet\d\.xml/ }.size, @package.workbook.worksheets.size, "one or more sheet missing") assert_equal(p.select{ |part| part[:entry] =~ /xl\/worksheets\/_rels\/sheet\d\.xml\.rels/ }.size, @package.workbook.worksheets.size, "one or more sheet rels missing") assert_equal(p.select{ |part| part[:entry] =~ /xl\/comments\d\.xml/ }.size, @package.workbook.worksheets.size, "one or more sheet rels missing") + assert_equal(p.select{ |part| part[:entry] =~ /xl\/pivotTables\/pivotTable\d\.xml/ }.size, @package.workbook.worksheets.first.pivot_tables.size, "one or more pivot tables missing") + assert_equal(p.select{ |part| part[:entry] =~ /xl\/pivotTables\/_rels\/pivotTable\d\.xml.rels/ }.size, @package.workbook.worksheets.first.pivot_tables.size, "one or more pivot tables rels missing") + assert_equal(p.select{ |part| part[:entry] =~ /xl\/pivotCache\/pivotCacheDefinition\d\.xml/ }.size, @package.workbook.worksheets.first.pivot_tables.size, "one or more pivot tables missing") #no mystery parts - assert_equal(p.size, 21) + assert_equal(p.size, 24) end diff --git a/test/workbook/worksheet/tc_pivot_table.rb b/test/workbook/worksheet/tc_pivot_table.rb new file mode 100644 index 00000000..3a94cfae --- /dev/null +++ b/test/workbook/worksheet/tc_pivot_table.rb @@ -0,0 +1,101 @@ +require 'tc_helper.rb' + +class TestPivotTable < Test::Unit::TestCase + def setup + p = Axlsx::Package.new + @ws = p.workbook.add_worksheet + + @ws << ["Year","Month","Region", "Type", "Sales"] + @ws << [2012, "Nov", "East", "Soda", "12345"] + end + + def test_initialization + assert(@ws.workbook.pivot_tables.empty?) + assert(@ws.pivot_tables.empty?) + end + + def test_add_pivot_table + pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5') + assert_equal('G5:G6', pivot_table.ref, 'ref assigned from first parameter') + assert_equal('A1:D5', pivot_table.range, 'range assigned from second parameter') + assert_equal('PivotTable1', pivot_table.name, 'name automatically generated') + assert(pivot_table.is_a?(Axlsx::PivotTable), "must create a pivot table") + assert_equal(@ws.workbook.pivot_tables.last, pivot_table, "must be added to workbook pivot tables collection") + assert_equal(@ws.pivot_tables.last, pivot_table, "must be added to worksheet pivot tables collection") + end + + def test_add_pivot_table_with_config + pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5') do |pt| + pt.rows = ['Year', 'Month'] + pt.columns = ['Type'] + pt.data = ['Sales'] + pt.pages = ['Region'] + end + assert_equal(['Year', 'Month'], pivot_table.rows) + assert_equal(['Type'], pivot_table.columns) + assert_equal(['Sales'], pivot_table.data) + assert_equal(['Region'], pivot_table.pages) + end + + def test_header_indices + pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5') + assert_equal(0, pivot_table.header_index_of('Year' )) + assert_equal(1, pivot_table.header_index_of('Month' )) + assert_equal(2, pivot_table.header_index_of('Region' )) + assert_equal(3, pivot_table.header_index_of('Type' )) + assert_equal(4, pivot_table.header_index_of('Sales' )) + assert_equal(nil, pivot_table.header_index_of('Missing')) + end + + def test_pn + @ws.add_pivot_table('G5:G6', 'A1:D5') + assert_equal(@ws.pivot_tables.first.pn, "pivotTables/pivotTable1.xml") + end + + def test_rId + @ws.add_pivot_table('G5:G6', 'A1:D5') + assert_equal(@ws.pivot_tables.first.rId, "rId1") + end + + def test_index + @ws.add_pivot_table('G5:G6', 'A1:D5') + assert_equal(@ws.pivot_tables.first.index, @ws.workbook.pivot_tables.index(@ws.pivot_tables.first)) + end + + def test_relationships + assert(@ws.relationships.empty?) + @ws.add_pivot_table('G5:G6', 'A1:D5') + assert_equal(@ws.relationships.size, 1, "adding a pivot table adds a relationship") + @ws.add_pivot_table('G10:G11', 'A1:D5') + assert_equal(@ws.relationships.size, 2, "adding a pivot table adds a relationship") + end + + def test_to_xml_string + pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5') + schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) + doc = Nokogiri::XML(pivot_table.to_xml_string) + errors = [] + schema.validate(doc).each do |error| + errors.push error + puts error.message + end + assert(errors.empty?, "error free validation") + end + + def test_to_xml_string_with_configuration + pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5') do |pt| + pt.rows = ['Year', 'Month'] + pt.columns = ['Type'] + pt.data = ['Sales'] + pt.pages = ['Region'] + end + schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) + doc = Nokogiri::XML(pivot_table.to_xml_string) + errors = [] + schema.validate(doc).each do |error| + errors.push error + puts error.message + end + assert(errors.empty?, "error free validation") + end +end diff --git a/test/workbook/worksheet/tc_pivot_table_cache_definition.rb b/test/workbook/worksheet/tc_pivot_table_cache_definition.rb new file mode 100644 index 00000000..2b4389b7 --- /dev/null +++ b/test/workbook/worksheet/tc_pivot_table_cache_definition.rb @@ -0,0 +1,46 @@ +require 'tc_helper.rb' + +class TestPivotTableCacheDefinition < Test::Unit::TestCase + def setup + p = Axlsx::Package.new + @ws = p.workbook.add_worksheet + 5.times do + @ws << ["aa","aa","aa","aa"] + end + @pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5') + @cache_definition = @pivot_table.cache_definition + end + + def test_initialization + assert(@cache_definition.is_a?(Axlsx::PivotTableCacheDefinition), "must create a pivot table cache definition") + assert_equal(@pivot_table, @cache_definition.pivot_table, 'refers back to its pivot table') + end + + def test_pn + assert_equal('pivotCache/pivotCacheDefinition1.xml', @cache_definition.pn) + end + + def test_rId + assert_equal('rId1', @cache_definition.rId) + end + + def test_index + assert_equal(0, @cache_definition.index) + end + + def test_cache_id + assert_equal(1, @cache_definition.cache_id) + end + + def test_to_xml_string + schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) + doc = Nokogiri::XML(@cache_definition.to_xml_string) + errors = [] + schema.validate(doc).each do |error| + errors.push error + puts error.message + end + assert(errors.empty?, "error free validation") + end + +end diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index da90ffa0..5d5e14a4 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -168,6 +168,12 @@ class TestWorksheet < Test::Unit::TestCase assert @ws.drawing.is_a?(Axlsx::Drawing) end + def test_add_pivot_table + assert(@ws.workbook.pivot_tables.empty?, "the sheet's workbook should not have any pivot tables by default") + @ws.add_pivot_table 'G5:G6', 'A1:D:10' + assert_equal(@ws.workbook.pivot_tables.size, 1, "add_pivot_tables adds a pivot_table to the workbook") + end + def test_col_style @ws.add_row [1,2,3,4] @ws.add_row [1,2,3,4] @@ -366,6 +372,7 @@ class TestWorksheet < Test::Unit::TestCase @ws.merge_cells "A4:A5" @ws.add_chart Axlsx::Pie3DChart @ws.add_table "E1:F3" + @ws.add_pivot_table 'G5:G6', 'A1:D10' schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) doc = Nokogiri::XML(@ws.to_xml_string) assert(schema.validate(doc).map { |e| puts e.message; e }.empty?, schema.validate(doc).map { |e| e.message }.join('\n')) @@ -382,6 +389,8 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(@ws.relationships.size, 4, "adding a comment adds 3 relationships") c = @ws.add_comment :text => 'not that is a comment!', :author => 'travis', :ref => "A1" assert_equal(@ws.relationships.size, 4, "adding multiple comments in the same worksheet should not add any additional comment relationships") + c = @ws.add_pivot_table 'G5:G6', 'A1:D10' + assert_equal(@ws.relationships.size, 5, "adding a pivot table adds 1 relationship") end |
