summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorAlex Rothenberg <[email protected]>2012-11-27 09:53:30 -0500
committerAlex Rothenberg <[email protected]>2012-11-27 09:54:23 -0500
commit4560bd0a1b8b46bf4d8c0783f9fa12e8ceee714f (patch)
treef5e6d539c7b4962442509ab6a09b2b4c42e8eaa5 /test
parent2feca4f74f21e6a3e63bd3badd02267be4062047 (diff)
downloadcaxlsx-4560bd0a1b8b46bf4d8c0783f9fa12e8ceee714f.tar.gz
caxlsx-4560bd0a1b8b46bf4d8c0783f9fa12e8ceee714f.zip
Create a simple Pivot Table
* an example can be run with `ruby examples/pivot_table.rb` * right now you cannot set options on the pivot table to make it useful (coming soon...)
Diffstat (limited to 'test')
-rw-r--r--test/tc_package.rb19
-rw-r--r--test/workbook/worksheet/tc_pivot_table.rb62
-rw-r--r--test/workbook/worksheet/tc_pivot_table_cache_definition.rb46
-rw-r--r--test/workbook/worksheet/tc_worksheet.rb19
4 files changed, 135 insertions, 11 deletions
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..6f1f3f7a
--- /dev/null
+++ b/test/workbook/worksheet/tc_pivot_table.rb
@@ -0,0 +1,62 @@
+require 'tc_helper.rb'
+
+class TestPivotTable < Test::Unit::TestCase
+ def setup
+ p = Axlsx::Package.new
+ @ws = p.workbook.add_worksheet
+ 40.times do
+ @ws << ["aa","aa","aa","aa","aa","aa"]
+ end
+ 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_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
+
+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..78563f7d
--- /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(0, @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 4c8f2822..c488e83d 100644
--- a/test/workbook/worksheet/tc_worksheet.rb
+++ b/test/workbook/worksheet/tc_worksheet.rb
@@ -153,6 +153,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]
@@ -317,7 +323,7 @@ class TestWorksheet < Test::Unit::TestCase
def test_styles
assert(@ws.styles.is_a?(Axlsx::Styles), 'worksheet provides access to styles')
end
-
+
def test_to_xml_string_with_illegal_chars
nasties = "\v\u2028\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u001f"
@ws.add_row [nasties]
@@ -341,6 +347,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'))
@@ -357,6 +364,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
@@ -400,7 +409,7 @@ class TestWorksheet < Test::Unit::TestCase
@ws.add_row [1, 2, 3]
assert_nothing_raised {@ws.protect_range(@ws.rows.first.cells) }
assert_equal('A1:C1', @ws.send(:protected_ranges).last.sqref)
-
+
end
def test_merge_cells
@ws.add_row [1,2,3]
@@ -412,7 +421,7 @@ class TestWorksheet < Test::Unit::TestCase
assert_equal(@ws.send(:merged_cells).size, 3)
assert_equal(@ws.send(:merged_cells).last, "A3:B3")
end
-
+
def test_merge_cells_sorts_correctly_by_row_when_given_array
10.times do |i|
@ws.add_row [i]
@@ -420,7 +429,7 @@ class TestWorksheet < Test::Unit::TestCase
@ws.merge_cells [@ws.rows[8].cells.first, @ws.rows[9].cells.first]
assert_equal "A9:A10", @ws.send(:merged_cells).first
end
-
+
def test_auto_filter
assert(@ws.auto_filter.range.nil?)
assert_raise(ArgumentError) { @ws.auto_filter = 123 }
@@ -432,6 +441,6 @@ class TestWorksheet < Test::Unit::TestCase
@ws.auto_filter.range = 'A1:D9'
@ws.auto_filter.add_column 0, :filters, :filter_items => [1]
doc = Nokogiri::XML(@ws.to_xml_string)
- assert(doc.xpath('//sheetPr[@filterMode="true"]'))
+ assert(doc.xpath('//sheetPr[@filterMode="true"]'))
end
end