Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / sc / qa / unit / dataproviders_test.cxx
blob01c118d4a73afc6a61718ffd6ee3cb471f9fd680
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 <vcl/scheduler.hxx>
17 #include <orcusxml.hxx>
19 #include <memory>
21 using namespace ::com::sun::star;
23 class ScDataProvidersTest : public ScModelTestBase
25 public:
26 ScDataProvidersTest();
28 void testCSVImport();
29 void testDataLargerThanDB();
30 void testHTMLImport();
31 void testXMLImport();
32 // void testBaseImport();
34 CPPUNIT_TEST_SUITE(ScDataProvidersTest);
35 CPPUNIT_TEST(testCSVImport);
36 CPPUNIT_TEST(testDataLargerThanDB);
37 CPPUNIT_TEST(testHTMLImport);
38 CPPUNIT_TEST(testXMLImport);
39 // CPPUNIT_TEST(testBaseImport);
40 CPPUNIT_TEST_SUITE_END();
43 void ScDataProvidersTest::testCSVImport()
45 createScDoc();
47 ScDBData* pDBData = new ScDBData(u"testDB"_ustr, 0, 0, 0, 10, 10);
48 ScDocument* pDoc = getScDoc();
49 bool bInserted
50 = pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
51 CPPUNIT_ASSERT(bInserted);
53 OUString aFileURL = createFileURL(u"csv/test1.csv");
54 sc::ExternalDataSource aDataSource(aFileURL, u"org.libreoffice.calc.csv"_ustr, pDoc);
55 aDataSource.setDBData(pDBData->GetName());
57 pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
58 auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
59 CPPUNIT_ASSERT(!rDataSources.empty());
61 rDataSources[0].refresh(pDoc, true);
62 Scheduler::ProcessEventsToIdle();
64 CPPUNIT_ASSERT_EQUAL(1.0, pDoc->GetValue(0, 0, 0));
65 CPPUNIT_ASSERT_EQUAL(2.0, pDoc->GetValue(1, 0, 0));
66 CPPUNIT_ASSERT_EQUAL(3.0, pDoc->GetValue(2, 0, 0));
67 CPPUNIT_ASSERT_EQUAL(4.0, pDoc->GetValue(3, 0, 0));
68 CPPUNIT_ASSERT_EQUAL(u"test1"_ustr, pDoc->GetString(0, 1, 0));
69 CPPUNIT_ASSERT_EQUAL(u"test2"_ustr, pDoc->GetString(1, 1, 0));
70 CPPUNIT_ASSERT_EQUAL(u"test3"_ustr, pDoc->GetString(2, 1, 0));
73 void ScDataProvidersTest::testDataLargerThanDB()
75 createScDoc();
77 ScDBData* pDBData = new ScDBData(u"testDB"_ustr, 0, 0, 0, 1, 1);
78 ScDocument* pDoc = getScDoc();
79 bool bInserted
80 = pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
81 CPPUNIT_ASSERT(bInserted);
83 OUString aFileURL = createFileURL(u"csv/test1.csv");
84 sc::ExternalDataSource aDataSource(aFileURL, u"org.libreoffice.calc.csv"_ustr, pDoc);
85 aDataSource.setDBData(pDBData->GetName());
87 pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
88 auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
89 CPPUNIT_ASSERT(!rDataSources.empty());
91 rDataSources[0].refresh(pDoc, true);
92 Scheduler::ProcessEventsToIdle();
94 CPPUNIT_ASSERT_EQUAL(1.0, pDoc->GetValue(0, 0, 0));
95 CPPUNIT_ASSERT_EQUAL(2.0, pDoc->GetValue(1, 0, 0));
96 CPPUNIT_ASSERT_EQUAL(0.0, pDoc->GetValue(2, 0, 0));
97 CPPUNIT_ASSERT_EQUAL(0.0, pDoc->GetValue(3, 0, 0));
98 CPPUNIT_ASSERT_EQUAL(u"test1"_ustr, pDoc->GetString(0, 1, 0));
99 CPPUNIT_ASSERT_EQUAL(u"test2"_ustr, pDoc->GetString(1, 1, 0));
100 CPPUNIT_ASSERT_EQUAL(OUString(), pDoc->GetString(2, 1, 0));
103 void ScDataProvidersTest::testHTMLImport()
105 createScDoc();
107 ScDBData* pDBData = new ScDBData(u"testDB"_ustr, 0, 0, 0, 10, 10);
108 ScDocument* pDoc = getScDoc();
109 bool bInserted
110 = pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
111 CPPUNIT_ASSERT(bInserted);
113 OUString aFileURL = createFileURL(u"html/test1.html");
114 sc::ExternalDataSource aDataSource(aFileURL, u"org.libreoffice.calc.html"_ustr, pDoc);
115 aDataSource.setID(u"//table"_ustr);
116 aDataSource.setDBData(pDBData->GetName());
118 pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
119 auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
120 CPPUNIT_ASSERT(!rDataSources.empty());
122 rDataSources[0].refresh(pDoc, true);
123 Scheduler::ProcessEventsToIdle();
125 std::vector<OUString> aCarManufacturers
126 = { u"Audi"_ustr, u"GM"_ustr, u"Nissan"_ustr, u"Ferrari"_ustr, u"Peugeot"_ustr };
127 std::vector<OUString> aCities
128 = { u"Berlin"_ustr, u"San Francisco"_ustr, u"Tokyo"_ustr, u"Rome"_ustr, u"Paris"_ustr };
129 std::vector<double> aFirstCol = { 1, 10, -100, -0.11111, 1 };
130 std::vector<double> aSecondCol = {
131 2, 2.1, 40179, 2, 2,
132 }; // 40179 is equal to 2010-1-1
134 CPPUNIT_ASSERT_EQUAL(u"Col1"_ustr, pDoc->GetString(0, 0, 0));
135 CPPUNIT_ASSERT_EQUAL(u"Col2"_ustr, pDoc->GetString(1, 0, 0));
136 CPPUNIT_ASSERT_EQUAL(u"Col3"_ustr, pDoc->GetString(2, 0, 0));
137 CPPUNIT_ASSERT_EQUAL(u"Col4"_ustr, pDoc->GetString(3, 0, 0));
139 for (SCROW nRow = 0; nRow <= 4; ++nRow)
141 ASSERT_DOUBLES_EQUAL(aFirstCol[nRow], pDoc->GetValue(0, nRow + 1, 0));
142 ASSERT_DOUBLES_EQUAL(aSecondCol[nRow], pDoc->GetValue(1, nRow + 1, 0));
143 CPPUNIT_ASSERT_EQUAL(aCarManufacturers[nRow], pDoc->GetString(2, nRow + 1, 0));
144 CPPUNIT_ASSERT_EQUAL(aCities[nRow], pDoc->GetString(3, nRow + 1, 0));
148 void ScDataProvidersTest::testXMLImport()
150 createScDoc();
152 ScDBData* pDBData = new ScDBData(u"testDB"_ustr, 0, 0, 0, 10, 10);
153 ScDocument* pDoc = getScDoc();
154 bool bInserted
155 = pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
156 CPPUNIT_ASSERT(bInserted);
158 ScOrcusImportXMLParam aParam;
160 ScOrcusImportXMLParam::RangeLink aRangeLink;
161 aRangeLink.maPos = ScAddress(0, 0, 0);
162 aRangeLink.maFieldPaths.push_back("/bookstore/book/title"_ostr);
163 aRangeLink.maFieldPaths.push_back("/bookstore/book/author"_ostr);
164 aRangeLink.maRowGroups.push_back("/bookstore/book"_ostr);
165 aParam.maRangeLinks.push_back(aRangeLink);
167 OUString aFileURL = createFileURL(u"xml/test1.xml");
168 sc::ExternalDataSource aDataSource(aFileURL, u"org.libreoffice.calc.xml"_ustr, pDoc);
169 aDataSource.setDBData(u"testDB"_ustr);
170 aDataSource.setXMLImportParam(aParam);
172 pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
173 auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
174 CPPUNIT_ASSERT(!rDataSources.empty());
176 rDataSources[0].refresh(pDoc, true);
177 Scheduler::ProcessEventsToIdle();
179 CPPUNIT_ASSERT_EQUAL(u"title"_ustr, pDoc->GetString(0, 0, 0));
180 CPPUNIT_ASSERT_EQUAL(u"author"_ustr, pDoc->GetString(1, 0, 0));
181 CPPUNIT_ASSERT_EQUAL(1.0, pDoc->GetValue(0, 1, 0));
182 CPPUNIT_ASSERT_EQUAL(u"test1"_ustr, pDoc->GetString(1, 1, 0));
183 CPPUNIT_ASSERT_EQUAL(2.0, pDoc->GetValue(0, 2, 0));
184 CPPUNIT_ASSERT_EQUAL(u"test2"_ustr, pDoc->GetString(1, 2, 0));
185 CPPUNIT_ASSERT_EQUAL(3.0, pDoc->GetValue(0, 3, 0));
186 CPPUNIT_ASSERT_EQUAL(u"test3"_ustr, pDoc->GetString(1, 3, 0));
187 CPPUNIT_ASSERT_EQUAL(4.0, pDoc->GetValue(0, 4, 0));
188 CPPUNIT_ASSERT_EQUAL(u"test4"_ustr, pDoc->GetString(1, 4, 0));
192 void ScDataProvidersTest::testBaseImport()
194 createScDoc();
196 ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 10, 10);
197 ScDocument* pDoc = getScDoc();
198 bool bInserted = pDoc->GetDBCollection()->getNamedDBs().insert(pDBData);
199 CPPUNIT_ASSERT(bInserted);
201 sc::ExternalDataSource aDataSource("~/dummy.file", "org.libreoffice.calc.sql", pDoc);
202 aDataSource.setDBData("testDB");
203 aDataSource.setID("biblio@Bibliography");
206 pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
207 auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
208 CPPUNIT_ASSERT(!rDataSources.empty());
210 rDataSources[0].refresh(pDoc, true);
211 Scheduler::ProcessEventsToIdle();
213 CPPUNIT_ASSERT_EQUAL(OUString("ARJ00"), pDoc->GetString(0, 0, 0));
214 CPPUNIT_ASSERT_EQUAL(OUString("AVV00"), pDoc->GetString(1, 1, 0));
218 ScDataProvidersTest::ScDataProvidersTest()
219 : ScModelTestBase(u"sc/qa/unit/data/dataprovider"_ustr)
223 CPPUNIT_TEST_SUITE_REGISTRATION(ScDataProvidersTest);
225 CPPUNIT_PLUGIN_IMPLEMENT();
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */