1 /***************************************************************************
2 testqgspostgresstringutils.cpp
3 --------------------------------------
4 Date : July 2019
5 Copyright : (C) 2019 David Signer
6 email : david at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16 #include <QString>
17 #include "qgstest.h"
18 #include "qgspostgresstringutils.h"
19
20 class TestQgsPostgresStringUtils : public QObject
21 {
22 Q_OBJECT
23 private slots:
24
25 void testPgArrayStringToListAndBack();
26 void testUnquotedPgArrayStringToListAndBack();
27 void testNumberArrayStringToListAndBack();
28 void testMultidimensionalPgArrayStringToListAndBack();
29 };
30
31
testPgArrayStringToListAndBack()32 void TestQgsPostgresStringUtils::testPgArrayStringToListAndBack()
33 {
34
35 QVariantList vl;
36 vl.push_back( QStringLiteral( "one" ) );
37 vl.push_back( QStringLiteral( "}two{" ) );
38 vl.push_back( QStringLiteral( "thr\"ee" ) );
39 vl.push_back( QStringLiteral( "fo,ur" ) );
40 vl.push_back( QStringLiteral( "fiv'e" ) );
41 vl.push_back( 6 );
42 vl.push_back( QStringLiteral( "and 7garcìa][" ) );
43 vl.push_back( QStringLiteral( "...all the etceteras" ) );
44
45 QString string = QStringLiteral( "{\"one\",\"}two{\",\"thr\\\"ee\",\"fo,ur\",\"fiv'e\",6,\"and 7garcìa][\",\"...all the etceteras\"}" );
46 QCOMPARE( QgsPostgresStringUtils::parseArray( string ), vl );
47
48 // and back
49 QCOMPARE( QgsPostgresStringUtils::buildArray( vl ), string );
50 }
51
testUnquotedPgArrayStringToListAndBack()52 void TestQgsPostgresStringUtils::testUnquotedPgArrayStringToListAndBack()
53 {
54 //there might have been used
55 QVariantList vl;
56 vl.push_back( QStringLiteral( "one" ) );
57 vl.push_back( QStringLiteral( "two" ) );
58 vl.push_back( QStringLiteral( "three" ) );
59 vl.push_back( QStringLiteral( "four" ) );
60 vl.push_back( QStringLiteral( "five" ) );
61 vl.push_back( QStringLiteral( "that genius" ) );
62
63 QString fallback_string = QStringLiteral( "{one,two,three,four,five,that genius}" );
64 QCOMPARE( QgsPostgresStringUtils::parseArray( fallback_string ), vl );
65
66 // and back including quotes
67 QString new_string = QStringLiteral( "{\"one\",\"two\",\"three\",\"four\",\"five\",\"that genius\"}" );
68 QCOMPARE( QgsPostgresStringUtils::buildArray( vl ), new_string );
69 }
70
testNumberArrayStringToListAndBack()71 void TestQgsPostgresStringUtils::testNumberArrayStringToListAndBack()
72 {
73 //there might have been used
74 QVariantList vl;
75 vl.push_back( 1 );
76 vl.push_back( 2 );
77 vl.push_back( 3 );
78 vl.push_back( 4 );
79
80 QString number_string = QStringLiteral( "{1,2,3,4}" );
81 QCOMPARE( QgsPostgresStringUtils::parseArray( number_string ), vl );
82
83 // and back without quotes
84 QCOMPARE( QgsPostgresStringUtils::buildArray( vl ), number_string );
85 }
86
testMultidimensionalPgArrayStringToListAndBack()87 void TestQgsPostgresStringUtils::testMultidimensionalPgArrayStringToListAndBack()
88 {
89 //there might have been used
90 QVariantList vl;
91 vl.push_back( QStringLiteral( "{one one third,one two third,one three third}" ) );
92 vl.push_back( QStringLiteral( "{\"two one third\",\"two two third\",\"two three third\"}" ) );
93 vl.push_back( QStringLiteral( "{three one third,three two third,three three third}" ) );
94
95 QString string = QStringLiteral( "{{one one third,one two third,one three third},{\"two one third\",\"two two third\",\"two three third\"},{three one third,three two third,three three third}}" );
96 QCOMPARE( QgsPostgresStringUtils::parseArray( string ), vl );
97
98 // and back without quotes
99 QCOMPARE( QgsPostgresStringUtils::buildArray( vl ), string );
100 }
101
102 QGSTEST_MAIN( TestQgsPostgresStringUtils )
103 #include "testqgspostgresstringutils.moc"
104