Repository navigation
Expand file tree
/
Copy pathTestImport.cpp
More file actions
128 lines (114 loc) · 4.4 KB
/
Copy pathTestImport.cpp
File metadata and controls
128 lines (114 loc) · 4.4 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <QTemporaryFile>
#include <QtTest/QTest>
#include <QCoreApplication>
#include <QTextStream>
#include "csvparser.h"
#include "TestImport.h"
#include "../sqlitedb.h"
Q_DECLARE_METATYPE(CSVParser::TCSVResult)
TestImport::TestImport()
{
// Init basic application
// The app needs to be initialized for the utf8 test
// to work
argcount = 1;
args[0] = "sqlb-unittests";
app = new QCoreApplication(argcount, args);
}
TestImport::~TestImport()
{
delete app;
}
void TestImport::csvImport()
{
// Fetch data
QFETCH(QString, csv);
QFETCH(char, separator);
QFETCH(char, quote);
QFETCH(QString, encoding);
QFETCH(int, numfields);
QFETCH(QVector<QStringList>, result);
// Create temporary CSV file
QTemporaryFile file;
QVERIFY(file.open());
file.write(csv.toUtf8());
file.flush();
// Call decodeCSV function
DBBrowserDB db;
CSVParser csvparser(true, separator, quote);
file.seek(0);
QTextStream tstream(&file);
csvparser.parse(tstream);
// Check return values
QCOMPARE(csvparser.csv(), result);
QCOMPARE((int)csvparser.columns(), numfields);
}
void TestImport::csvImport_data()
{
QTest::addColumn<QString>("csv");
QTest::addColumn<char>("separator");
QTest::addColumn<char>("quote");
QTest::addColumn<QString>("encoding");
QTest::addColumn<int>("numfields");
QTest::addColumn<CSVParser::TCSVResult>("result");
CSVParser::TCSVResult result;
result.append(QStringList() << "a" << "b" << "c");
result.append(QStringList() << "d" << "e" << "f");
result.append(QStringList() << "g" << "h" << "i");
QTest::newRow("commas_noquotes") << "a,b,c\nd,e,f\ng,h,i\n"
<< ','
<< (char)0
<< "UTF-8"
<< 3
<< result;
QTest::newRow("semicolons_noquotes") << "a;b;c\nd;e;f\ng;h;i\n"
<< ';'
<< (char)0
<< "UTF-8"
<< 3
<< result;
QTest::newRow("commas_doublequotes") << "\"a\",\"b\",\"c\"\n\"d\",\"e\",\"f\"\n\"g\",\"h\",\"i\"\n"
<< ','
<< '"'
<< "UTF-8"
<< 3
<< result;
QTest::newRow("noquotes_butquotesset") << "a,b,c\nd,e,f\ng,h,i\n"
<< ','
<< '"'
<< "UTF-8"
<< 3
<< result;
QTest::newRow("windowslinebreaks") << "a,b,c\r\nd,e,f\r\ng,h,i\r\n"
<< ','
<< (char)0
<< "UTF-8"
<< 3
<< result;
result.clear();
result.append(QStringList() << "a" << "b" << "c");
QTest::newRow("oneline") << "a,b,c"
<< ','
<< (char)0
<< "UTF-8"
<< 3
<< result;
result.clear();
result.append(QStringList() << "a,a\"" << "b" << "c");
result.append(QStringList() << "d" << "e" << "\"\"f,f");
QTest::newRow("manyquotes") << "\"a,a\"\"\",\"b\",\"c\"\n\"d\",\"e\",\"\"\"\"\"f,f\"\n"
<< ','
<< '"'
<< "UTF-8"
<< 3
<< result;
result.clear();
result.append(QStringList() << 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
<< "UTF-8"
<< 3
<< result;
}