summaryrefslogtreecommitdiffhomepage
path: root/test/workbook
diff options
context:
space:
mode:
authorRandy Morgan (@morgan_randy) <[email protected]>2012-11-27 15:36:21 -0800
committerRandy Morgan (@morgan_randy) <[email protected]>2012-11-27 15:36:21 -0800
commitc3a36737a56a4f0334b97a897a0d3aa17eded82d (patch)
tree87de5615c5c43d49e9c31f583be14168056f0681 /test/workbook
parent7d3d588a1a34777119fae41749711a734da22974 (diff)
parent036f5883939a91fbc3eb377d968d85500dc3098a (diff)
downloadcaxlsx-c3a36737a56a4f0334b97a897a0d3aa17eded82d.tar.gz
caxlsx-c3a36737a56a4f0334b97a897a0d3aa17eded82d.zip
Merge pull request #148 from alexrothenberg/pivot_table
Create a simple Pivot Table
Diffstat (limited to 'test/workbook')
-rw-r--r--test/workbook/worksheet/tc_pivot_table.rb101
-rw-r--r--test/workbook/worksheet/tc_pivot_table_cache_definition.rb46
-rw-r--r--test/workbook/worksheet/tc_worksheet.rb9
3 files changed, 156 insertions, 0 deletions
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