Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / qa / unit / dataproviders_test.cxx
bloba9b6b1142c7fb99bce83df73d974905a2c76991f
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
9 #include <rtl/ustring.hxx>
11 #include "helper/qahelper.hxx"
12 #include <document.hxx>
13 #include <address.hxx>
14 #include <dbdata.hxx>
15 #include <datamapper.hxx>
16 #include <scdll.hxx>
17 #include <vcl/scheduler.hxx>
18 #include <orcusxml.hxx>
20 #include <memory>
22 class ScDataProvidersTest : public ScBootstrapFixture
24 public:
26 ScDataProvidersTest();
28 virtual void setUp() override;
29 virtual void tearDown() override;
31 void testCSVImport();
32 void testDataLargerThanDB();
33 void testHTMLImport();
34 void testXMLImport();
35 // void testBaseImport();
37 CPPUNIT_TEST_SUITE(ScDataProvidersTest);
38 CPPUNIT_TEST(testCSVImport);
39 CPPUNIT_TEST(testDataLargerThanDB);
40 CPPUNIT_TEST(testHTMLImport);
41 CPPUNIT_TEST(testXMLImport);
42 // CPPUNIT_TEST(testBaseImport);
43 CPPUNIT_TEST_SUITE_END();
45 private:
46 ScDocShellRef m_xDocShell;
47 ScDocument *m_pDoc;
50 void ScDataProvidersTest::testCSVImport()
52 ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 10, 10);
53 bool bInserted = m_pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
54 CPPUNIT_ASSERT(bInserted);
56 OUString aFileURL;
57 createFileURL("test1.", "csv", aFileURL);
58 sc::ExternalDataSource aDataSource(aFileURL, "org.libreoffice.calc.csv", m_pDoc);
59 aDataSource.setDBData(pDBData->GetName());
62 m_pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
63 auto& rDataSources = m_pDoc->GetExternalDataMapper().getDataSources();
64 CPPUNIT_ASSERT(!rDataSources.empty());
66 rDataSources[0].refresh(m_pDoc, true);
67 Scheduler::ProcessEventsToIdle();
69 CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(0, 0, 0));
70 CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(1, 0, 0));
71 CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(2, 0, 0));
72 CPPUNIT_ASSERT_EQUAL(4.0, m_pDoc->GetValue(3, 0, 0));
73 CPPUNIT_ASSERT_EQUAL(OUString("test1"), m_pDoc->GetString(0, 1, 0));
74 CPPUNIT_ASSERT_EQUAL(OUString("test2"), m_pDoc->GetString(1, 1, 0));
75 CPPUNIT_ASSERT_EQUAL(OUString("test3"), m_pDoc->GetString(2, 1, 0));
78 void ScDataProvidersTest::testDataLargerThanDB()
80 ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 1, 1);
81 bool bInserted = m_pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
82 CPPUNIT_ASSERT(bInserted);
84 OUString aFileURL;
85 createFileURL("test1.", "csv", aFileURL);
86 sc::ExternalDataSource aDataSource(aFileURL, "org.libreoffice.calc.csv", m_pDoc);
87 aDataSource.setDBData(pDBData->GetName());
90 m_pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
91 auto& rDataSources = m_pDoc->GetExternalDataMapper().getDataSources();
92 CPPUNIT_ASSERT(!rDataSources.empty());
94 rDataSources[0].refresh(m_pDoc, true);
95 Scheduler::ProcessEventsToIdle();
97 CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(0, 0, 0));
98 CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(1, 0, 0));
99 CPPUNIT_ASSERT_EQUAL(0.0, m_pDoc->GetValue(2, 0, 0));
100 CPPUNIT_ASSERT_EQUAL(0.0, m_pDoc->GetValue(3, 0, 0));
101 CPPUNIT_ASSERT_EQUAL(OUString("test1"), m_pDoc->GetString(0, 1, 0));
102 CPPUNIT_ASSERT_EQUAL(OUString("test2"), m_pDoc->GetString(1, 1, 0));
103 CPPUNIT_ASSERT_EQUAL(OUString(), m_pDoc->GetString(2, 1, 0));
106 void ScDataProvidersTest::testHTMLImport()
108 ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 10, 10);
109 bool bInserted = m_pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
110 CPPUNIT_ASSERT(bInserted);
112 OUString aFileURL;
113 createFileURL("test1.", "html", aFileURL);
114 sc::ExternalDataSource aDataSource(aFileURL, "org.libreoffice.calc.html", m_pDoc);
115 aDataSource.setID("//table");
116 aDataSource.setDBData(pDBData->GetName());
119 m_pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
120 auto& rDataSources = m_pDoc->GetExternalDataMapper().getDataSources();
121 CPPUNIT_ASSERT(!rDataSources.empty());
123 rDataSources[0].refresh(m_pDoc, true);
124 Scheduler::ProcessEventsToIdle();
127 std::vector<OUString> aCarManufacturers = {"Audi", "GM", "Nissan", "Ferrari", "Peugeot"};
128 std::vector<OUString> aCities = {"Berlin", "San Francisco", "Tokyo", "Rome", "Paris"};
129 std::vector<double> aFirstCol = {1, 10, -100, -0.11111, 1};
130 std::vector<double> aSecondCol = {2, 2.1, 40179, 2, 2,}; // 40179 is equal to 2010-1-1
132 CPPUNIT_ASSERT_EQUAL(OUString("Col1"), m_pDoc->GetString(0, 0, 0));
133 CPPUNIT_ASSERT_EQUAL(OUString("Col2"), m_pDoc->GetString(1, 0, 0));
134 CPPUNIT_ASSERT_EQUAL(OUString("Col3"), m_pDoc->GetString(2, 0, 0));
135 CPPUNIT_ASSERT_EQUAL(OUString("Col4"), m_pDoc->GetString(3, 0, 0));
137 for (SCROW nRow = 0; nRow <= 4; ++nRow)
139 ASSERT_DOUBLES_EQUAL(aFirstCol[nRow], m_pDoc->GetValue(0, nRow + 1, 0));
140 ASSERT_DOUBLES_EQUAL(aSecondCol[nRow], m_pDoc->GetValue(1, nRow + 1, 0));
141 CPPUNIT_ASSERT_EQUAL(aCarManufacturers[nRow], m_pDoc->GetString(2, nRow + 1, 0));
142 CPPUNIT_ASSERT_EQUAL(aCities[nRow], m_pDoc->GetString(3, nRow + 1, 0));
146 void ScDataProvidersTest::testXMLImport()
148 ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 10, 10);
149 bool bInserted = m_pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
150 CPPUNIT_ASSERT(bInserted);
152 OUString aFileURL;
153 ScOrcusImportXMLParam aParam;
155 ScOrcusImportXMLParam::RangeLink aRangeLink;
156 aRangeLink.maPos = ScAddress(0,0,0);
157 aRangeLink.maFieldPaths.push_back("/bookstore/book/title");
158 aRangeLink.maFieldPaths.push_back("/bookstore/book/author");
159 aRangeLink.maRowGroups.push_back("/bookstore/book");
160 aParam.maRangeLinks.push_back(aRangeLink);
162 createFileURL("test1.", "xml", aFileURL);
163 sc::ExternalDataSource aDataSource(aFileURL, "org.libreoffice.calc.xml", m_pDoc);
164 aDataSource.setDBData("testDB");
165 aDataSource.setXMLImportParam(aParam);
168 m_pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
169 auto& rDataSources = m_pDoc->GetExternalDataMapper().getDataSources();
170 CPPUNIT_ASSERT(!rDataSources.empty());
172 rDataSources[0].refresh(m_pDoc, true);
173 Scheduler::ProcessEventsToIdle();
175 CPPUNIT_ASSERT_EQUAL(OUString("title"), m_pDoc->GetString(0, 0, 0));
176 CPPUNIT_ASSERT_EQUAL(OUString("author"), m_pDoc->GetString(1, 0, 0));
177 CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(0, 1, 0));
178 CPPUNIT_ASSERT_EQUAL(OUString("test1"), m_pDoc->GetString(1, 1, 0));
179 CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(0, 2, 0));
180 CPPUNIT_ASSERT_EQUAL(OUString("test2"), m_pDoc->GetString(1, 2, 0));
181 CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(0, 3, 0));
182 CPPUNIT_ASSERT_EQUAL(OUString("test3"), m_pDoc->GetString(1, 3, 0));
183 CPPUNIT_ASSERT_EQUAL(4.0, m_pDoc->GetValue(0, 4, 0));
184 CPPUNIT_ASSERT_EQUAL(OUString("test4"), m_pDoc->GetString(1, 4, 0));
188 void ScDataProvidersTest::testBaseImport()
190 ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 10, 10);
191 bool bInserted = m_pDoc->GetDBCollection()->getNamedDBs().insert(pDBData);
192 CPPUNIT_ASSERT(bInserted);
194 sc::ExternalDataSource aDataSource("~/dummy.file", "org.libreoffice.calc.sql", m_pDoc);
195 aDataSource.setDBData("testDB");
196 aDataSource.setID("biblio@Bibliography");
199 m_pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
200 auto& rDataSources = m_pDoc->GetExternalDataMapper().getDataSources();
201 CPPUNIT_ASSERT(!rDataSources.empty());
203 rDataSources[0].refresh(m_pDoc, true);
204 Scheduler::ProcessEventsToIdle();
206 CPPUNIT_ASSERT_EQUAL(OUString("ARJ00"), m_pDoc->GetString(0, 0, 0));
207 CPPUNIT_ASSERT_EQUAL(OUString("AVV00"), m_pDoc->GetString(1, 1, 0));
211 ScDataProvidersTest::ScDataProvidersTest() :
212 ScBootstrapFixture( "sc/qa/unit/data/dataprovider" ),
213 m_pDoc(nullptr)
217 void ScDataProvidersTest::setUp()
219 ScBootstrapFixture::setUp();
221 ScDLL::Init();
222 m_xDocShell = new ScDocShell(
223 SfxModelFlags::EMBEDDED_OBJECT |
224 SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS |
225 SfxModelFlags::DISABLE_DOCUMENT_RECOVERY);
227 m_xDocShell->SetIsInUcalc();
228 m_xDocShell->DoInitUnitTest();
229 m_pDoc = &m_xDocShell->GetDocument();
230 m_pDoc->InsertTab(0, "Tab");
233 void ScDataProvidersTest::tearDown()
235 m_xDocShell->DoClose();
236 m_xDocShell.clear();
237 ScBootstrapFixture::tearDown();
240 CPPUNIT_TEST_SUITE_REGISTRATION(ScDataProvidersTest);
242 CPPUNIT_PLUGIN_IMPLEMENT();
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */