Skip to content

Commit a192dab

Browse files
committedNov 12, 2018
Unit tests for extract binary algorithm
1 parent 62bdc28 commit a192dab

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed
 

‎src/analysis/processing/qgsalgorithmextractbinary.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ QString QgsExtractBinaryFieldAlgorithm::displayName() const
3232

3333
QString QgsExtractBinaryFieldAlgorithm::shortHelpString() const
3434
{
35-
return QObject::tr( "This algorithm extracts contents from a binary field, saving them to individual files." );
35+
return QObject::tr( "This algorithm extracts contents from a binary field, saving them to individual files.\n\n"
36+
"Filenames can be generated using data defined values, allowing them to be taken from"
37+
"an attribute in the source table or based on a more complex expression." );
3638
}
3739

3840
QString QgsExtractBinaryFieldAlgorithm::shortDescription() const

‎tests/src/analysis/testqgsprocessingalgs.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class TestQgsProcessingAlgs: public QObject
4747
void transformAlg();
4848
void kmeansCluster();
4949
void categorizeByStyle();
50+
void extractBinary();
5051

5152
private:
5253

@@ -628,6 +629,45 @@ void TestQgsProcessingAlgs::categorizeByStyle()
628629
QCOMPARE( catRenderer->categories().at( catRenderer->categoryIndexForValue( QStringLiteral( "c " ) ) ).symbol()->color().name(), QStringLiteral( "#0000ff" ) );
629630
}
630631

632+
void TestQgsProcessingAlgs::extractBinary()
633+
{
634+
std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:extractbinary" ) ) );
635+
QVERIFY( alg != nullptr );
636+
637+
std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
638+
QgsProject p;
639+
context->setProject( &p );
640+
641+
QString dataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
642+
const QString source = dataDir + QStringLiteral( "/attachments.gdb|layername=points__ATTACH" );
643+
644+
645+
QVariantMap parameters;
646+
parameters.insert( QStringLiteral( "INPUT" ), source );
647+
parameters.insert( QStringLiteral( "FIELD" ), QStringLiteral( "DATA" ) );
648+
parameters.insert( QStringLiteral( "FILENAME" ), QgsProperty::fromExpression( QStringLiteral( "'test' || \"ATTACHMENTID\" || '.jpg'" ) ) );
649+
const QString folder = QDir::tempPath();
650+
parameters.insert( QStringLiteral( "FOLDER" ), folder );
651+
652+
bool ok = false;
653+
QgsProcessingFeedback feedback;
654+
QVariantMap results = alg->run( parameters, *context, &feedback, &ok );
655+
QVERIFY( ok );
656+
657+
QCOMPARE( results.count(), 1 );
658+
QCOMPARE( results.value( QStringLiteral( "FOLDER" ) ), folder );
659+
660+
QFile file( folder + "/test1.jpg" );
661+
QVERIFY( file.open( QIODevice::ReadOnly ) );
662+
QByteArray d = file.readAll();
663+
QCOMPARE( QCryptographicHash::hash( d, QCryptographicHash::Md5 ).toHex(), QStringLiteral( "ef3dbc530cc39a545832a6c82aac57b6" ) );
664+
665+
QFile file2( folder + "/test2.jpg" );
666+
QVERIFY( file2.open( QIODevice::ReadOnly ) );
667+
d = file2.readAll();
668+
QCOMPARE( QCryptographicHash::hash( d, QCryptographicHash::Md5 ).toHex(), QStringLiteral( "4b952b80e4288ca5111be2f6dd5d6809" ) );
669+
}
670+
631671

632672
QGSTEST_MAIN( TestQgsProcessingAlgs )
633673
#include "testqgsprocessingalgs.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.