diff options
author | Kohei Yoshida <kohei.yoshida@gmail.com> | 2012-03-22 21:26:26 -0400 |
---|---|---|
committer | Kohei Yoshida <kohei.yoshida@gmail.com> | 2012-03-22 21:27:07 -0400 |
commit | a3d71e97eaf1958a4d5ab41a6b8788c95d85d2bb (patch) | |
tree | 64edb5c4aa1b694376b573a3ea47edce62510745 /sc/qa | |
parent | 914e797844435a730a20d4269cd7093d725e37b6 (diff) |
New unit test for pivot table with date grouping.
Diffstat (limited to 'sc/qa')
-rw-r--r-- | sc/qa/unit/ucalc.cxx | 122 |
1 files changed, 120 insertions, 2 deletions
diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx index 89315afb224b..cb6ab44c5521 100644 --- a/sc/qa/unit/ucalc.cxx +++ b/sc/qa/unit/ucalc.cxx @@ -75,6 +75,7 @@ #include <sfx2/docfile.hxx> #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp> +#include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp> #include <com/sun/star/sheet/GeneralFunction.hpp> #include <ucbhelper/contentbroker.hxx> @@ -161,8 +162,8 @@ public: void testPivotTableDuplicateDataFields(); void testPivotTableNormalGrouping(); - void testPivotTableNumberGrouping(); + void testPivotTableDateGrouping(); void testSheetCopy(); void testSheetMove(); @@ -222,6 +223,7 @@ public: CPPUNIT_TEST(testPivotTableDuplicateDataFields); CPPUNIT_TEST(testPivotTableNormalGrouping); CPPUNIT_TEST(testPivotTableNumberGrouping); + CPPUNIT_TEST(testPivotTableDateGrouping); CPPUNIT_TEST(testSheetCopy); CPPUNIT_TEST(testSheetMove); CPPUNIT_TEST(testExternalRef); @@ -1245,7 +1247,6 @@ ScDPObject* createDPFromSourceDesc( ScDPSaveDimension* pDim = aSaveData.GetNewDimensionByName(aDimName); pDim->SetOrientation(static_cast<sal_uInt16>(aFields[i].eOrient)); pDim->SetUsedHierarchy(0); - pDim->SetShowEmpty(true); if (aFields[i].eOrient == sheet::DataPilotFieldOrientation_DATA) { @@ -2424,6 +2425,123 @@ void Test::testPivotTableNumberGrouping() m_pDoc->DeleteTab(0); } +void Test::testPivotTableDateGrouping() +{ + m_pDoc->InsertTab(0, OUString(RTL_CONSTASCII_USTRINGPARAM("Data"))); + m_pDoc->InsertTab(1, OUString(RTL_CONSTASCII_USTRINGPARAM("Table"))); + + // Raw data + const char* aData[][2] = { + { "Date", "Value" }, + { "2011-01-01", "1" }, + { "2011-03-02", "2" }, + { "2012-01-04", "3" }, + { "2012-02-23", "4" }, + { "2012-02-24", "5" }, + { "2012-03-15", "6" }, + { "2011-09-03", "7" }, + { "2012-12-25", "8" } + }; + + // Dimension definition + DPFieldDef aFields[] = { + { "Date", sheet::DataPilotFieldOrientation_ROW, 0 }, + { "Value", sheet::DataPilotFieldOrientation_DATA, sheet::GeneralFunction_SUM }, + }; + + ScAddress aPos(1,1,0); + ScRange aDataRange = insertRangeData(m_pDoc, aPos, aData, SAL_N_ELEMENTS(aData)); + CPPUNIT_ASSERT_MESSAGE("failed to insert range data at correct position", aDataRange.aStart == aPos); + + ScDPObject* pDPObj = createDPFromRange( + m_pDoc, aDataRange, aFields, SAL_N_ELEMENTS(aFields), false); + + ScDPCollection* pDPs = m_pDoc->GetDPCollection(); + bool bSuccess = pDPs->InsertNewTable(pDPObj); + + CPPUNIT_ASSERT_MESSAGE("failed to insert a new pivot table object into document.", bSuccess); + CPPUNIT_ASSERT_MESSAGE("there should be only one data pilot table.", + pDPs->GetCount() == 1); + pDPObj->SetName(pDPs->CreateNewName()); + + ScDPSaveData* pSaveData = pDPObj->GetSaveData(); + CPPUNIT_ASSERT_MESSAGE("No save data !?", pSaveData); + ScDPDimensionSaveData* pDimData = pSaveData->GetDimensionData(); + CPPUNIT_ASSERT_MESSAGE("No dimension data !?", pDimData); + + rtl::OUString aBaseDimName(RTL_CONSTASCII_USTRINGPARAM("Date")); + + ScDPNumGroupInfo aInfo; + aInfo.mbEnable = true; + aInfo.mbAutoStart = true; + aInfo.mbAutoEnd = true; + { + // Turn the Date dimension into months. The first of the date + // dimensions is always a number-group dimension which replaces the + // original dimension. + ScDPSaveNumGroupDimension aGroup(aBaseDimName, aInfo, sheet::DataPilotFieldGroupBy::MONTHS); + pDimData->AddNumGroupDimension(aGroup); + } + + { + // Add quarter dimension. This will be an additional dimension. + rtl::OUString aGroupDimName = + pDimData->CreateDateGroupDimName( + sheet::DataPilotFieldGroupBy::QUARTERS, *pDPObj, true, NULL); + ScDPSaveGroupDimension aGroupDim(aBaseDimName, aGroupDimName); + aGroupDim.SetDateInfo(aInfo, sheet::DataPilotFieldGroupBy::QUARTERS); + pDimData->AddGroupDimension(aGroupDim); + + // Set orientation. + ScDPSaveDimension* pDim = pSaveData->GetDimensionByName(aGroupDimName); + pDim->SetOrientation(sheet::DataPilotFieldOrientation_ROW); + pSaveData->SetPosition(pDim, 0); // set it to the left end. + } + + { + // Add year dimension. This is a new dimension also. + rtl::OUString aGroupDimName = + pDimData->CreateDateGroupDimName( + sheet::DataPilotFieldGroupBy::YEARS, *pDPObj, true, NULL); + ScDPSaveGroupDimension aGroupDim(aBaseDimName, aGroupDimName); + aGroupDim.SetDateInfo(aInfo, sheet::DataPilotFieldGroupBy::YEARS); + pDimData->AddGroupDimension(aGroupDim); + + // Set orientation. + ScDPSaveDimension* pDim = pSaveData->GetDimensionByName(aGroupDimName); + pDim->SetOrientation(sheet::DataPilotFieldOrientation_ROW); + pSaveData->SetPosition(pDim, 0); // set it to the left end. + } + + pDPObj->SetSaveData(*pSaveData); + ScRange aOutRange = refreshGroups(pDPs, pDPObj); + { + // Expected output table content. 0 = empty cell + const char* aOutputCheck[][4] = { + { "Years", "Quarters", "Date", 0 }, + { "2011", "Q1", "Jan", "1" }, + { 0, 0, "Mar", "2" }, + { 0, "Q3", "Sep", "7" }, + { "2012", "Q1", "Jan", "3" }, + { 0, 0, "Feb", "9" }, + { 0, 0, "Mar", "6" }, + { 0, "Q4", "Dec", "8" }, + { "Total Result", 0, 0, "36" }, + }; + + bSuccess = checkDPTableOutput<4>(m_pDoc, aOutRange, aOutputCheck, "Years, quarters and months date groups."); + CPPUNIT_ASSERT_MESSAGE("Table output check failed", bSuccess); + } + + pDPs->FreeTable(pDPObj); + CPPUNIT_ASSERT_MESSAGE("There should be no more tables.", pDPs->GetCount() == 0); + CPPUNIT_ASSERT_MESSAGE("There shouldn't be any more cache stored.", + pDPs->GetSheetCaches().size() == 0); + + m_pDoc->DeleteTab(1); + m_pDoc->DeleteTab(0); +} + void Test::testSheetCopy() { OUString aTabName(RTL_CONSTASCII_USTRINGPARAM("TestTab")); |