forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestImport.cpp
98 lines (87 loc) · 3.51 KB
/
TestImport.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <QTemporaryFile>
#include <QtTest/QTest>
#include <QApplication>
#include "TestImport.h"
#include "../sqlitedb.h"
void TestImport::csvImport()
{
// Fetch data
QFETCH(QString, csv);
QFETCH(char, separator);
QFETCH(char, quote);
QFETCH(int, numfields);
QFETCH(QStringList, result);
// Init basic application
int argcount = 1;
const char* appname = "sqlb-unittests";
QApplication app(argcount, const_cast<char**>(&appname));
// Create temporary CSV file
QTemporaryFile file;
QVERIFY(file.open());
file.write(csv.toUtf8());
file.flush();
// Call decodeCSV function
DBBrowserDB db;
int numfields_read;
QStringList retval = db.decodeCSV(file.fileName(), separator, quote, -1, &numfields_read);
// Check return values
QCOMPARE(retval, result);
QCOMPARE(numfields_read, numfields);
}
void TestImport::csvImport_data()
{
QTest::addColumn<QString>("csv");
QTest::addColumn<char>("separator");
QTest::addColumn<char>("quote");
QTest::addColumn<int>("numfields");
QTest::addColumn<QStringList>("result");
QStringList result;
result << "a" << "b" << "c" << "d" << "e" << "f" << "g" << "h" << "i";
QTest::newRow("commas_noquotes") << "a,b,c\nd,e,f\ng,h,i\n"
<< ','
<< (char)0
<< 3
<< result;
QTest::newRow("semicolons_noquotes") << "a;b;c\nd;e;f\ng;h;i\n"
<< ';'
<< (char)0
<< 3
<< result;
QTest::newRow("commas_doublequotes") << "\"a\",\"b\",\"c\"\n\"d\",\"e\",\"f\"\n\"g\",\"h\",\"i\"\n"
<< ','
<< '"'
<< 3
<< result;
QTest::newRow("noquotes_butquotesset") << "a,b,c\nd,e,f\ng,h,i\n"
<< ','
<< '"'
<< 3
<< result;
QTest::newRow("windowslinebreaks") << "a,b,c\r\nd,e,f\r\ng,h,i\r\n"
<< ','
<< (char)0
<< 3
<< result;
result.clear();
result << "a" << "b" << "c";
QTest::newRow("oneline") << "a,b,c"
<< ','
<< (char)0
<< 3
<< result;
result.clear();
result << "a,a\"" << "b" << "c" << "d" << "e" << "\"\"f,f";
QTest::newRow("manyquotes") << "\"a,a\"\"\",\"b\",\"c\"\n\"d\",\"e\",\"\"\"\"\"f,f\"\n"
<< ','
<< '"'
<< 3
<< result;
result.clear();
result << QString::fromUtf8("\u4E18") << QString::fromUtf8("\u4E26") << QString::fromUtf8("\u4E4B");
QString csv = QString::fromUtf8("\u4E18") + "," + QString::fromUtf8("\u4E26") + "," + QString::fromUtf8("\u4E4B") + "\n";
QTest::newRow("utf8chars") << csv
<< ','
<< (char)0
<< 3
<< result;
}