2013-02-23 21:43:17 +01:00
# -*- coding: utf-8 -*-
"""
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
TestTools . py
- - - - - - - - - - - - - - - - - - - - -
Date : February 2013
Copyright : ( C ) 2013 by Victor Olaya
Email : volayaf at gmail dot com
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This program is free software ; you can redistribute it and / or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation ; either version 2 of the License , or *
* ( at your option ) any later version . *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
"""
2013-10-01 20:52:22 +03:00
2013-02-23 21:43:17 +01:00
__author__ = ' Victor Olaya '
__date__ = ' February 2013 '
__copyright__ = ' (C) 2013, Victor Olaya '
2013-10-01 20:52:22 +03:00
2013-02-23 21:43:17 +01:00
# This will get replaced with a git SHA1 when you do a git archive
2013-10-01 20:52:22 +03:00
2013-02-23 21:43:17 +01:00
__revision__ = ' $Format: % H$ '
2013-03-10 21:10:01 +01:00
import os
2016-02-03 16:42:18 +01:00
import yaml
import hashlib
2013-10-01 20:52:22 +03:00
from osgeo import gdal
from osgeo . gdalconst import GA_ReadOnly
2016-03-21 04:58:12 +01:00
from PyQt . QtCore import QCoreApplication , QMetaObject
from PyQt . QtWidgets import QDialog , QVBoxLayout , QTextEdit
2013-10-01 20:52:22 +03:00
2013-08-12 20:44:27 +02:00
from processing . core . Processing import Processing
2016-02-03 16:42:18 +01:00
from processing . core . outputs import (
OutputNumber ,
OutputString ,
OutputRaster ,
2016-02-21 13:15:44 +01:00
OutputVector ,
OutputHTML
2016-02-03 16:42:18 +01:00
)
from processing . core . parameters import (
ParameterRaster ,
ParameterVector ,
ParameterMultipleInput
)
def extractSchemaPath ( filepath ) :
"""
Trys to find where the file is relative to the QGIS source code directory .
If it is already placed in the processing or QGIS testdata directory it will
return an appropriate schema and relative filepath
Args :
filepath : The path of the file to examine
Returns :
A tuple ( schema , relative_file_path ) where the schema is ' qgs ' or ' proc '
if we can assume that the file is in this testdata directory .
"""
parts = [ ]
schema = None
localpath = ' '
path = filepath
part = True
while part :
( path , part ) = os . path . split ( path )
if part == ' testdata ' and not localpath :
localparts = parts
localparts . reverse ( )
localpath = os . path . join ( * localparts )
parts . append ( part )
parts . reverse ( )
try :
testsindex = parts . index ( ' tests ' )
except ValueError :
return ' ' , filepath
if parts [ testsindex - 1 ] == ' processing ' :
schema = ' proc '
return schema , localpath
2013-10-01 20:52:22 +03:00
2013-02-17 23:14:12 +01:00
2013-03-23 21:45:52 +01:00
def createTest ( text ) :
2016-02-03 16:42:18 +01:00
definition = { }
2013-10-01 20:52:22 +03:00
tokens = text [ len ( ' processing.runalg( ' ) : - 1 ] . split ( ' , ' )
cmdname = ( tokens [ 0 ] ) [ 1 : - 1 ]
2013-08-12 20:44:27 +02:00
alg = Processing . getAlgorithm ( cmdname )
2016-02-03 16:42:18 +01:00
definition [ ' name ' ] = ' Test ( {} ) ' . format ( cmdname )
definition [ ' algorithm ' ] = cmdname
2016-02-05 10:58:21 +01:00
params = { }
2016-02-03 16:42:18 +01:00
results = { }
2013-02-17 23:14:12 +01:00
i = 0
2016-02-03 16:42:18 +01:00
for param in alg . parameters :
if param . hidden :
continue
2013-10-01 20:52:22 +03:00
i + = 1
2016-02-03 16:42:18 +01:00
token = tokens [ i ]
if isinstance ( param , ParameterVector ) :
filename = token [ 1 : - 1 ]
schema , filepath = extractSchemaPath ( filename )
p = {
' type ' : ' vector ' ,
' name ' : filepath
}
if not schema :
p [ ' location ' ] = ' [The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.] '
2016-02-05 10:58:21 +01:00
params [ param . name ] = p
2016-02-03 16:42:18 +01:00
elif isinstance ( param , ParameterRaster ) :
filename = token [ 1 : - 1 ]
schema , filepath = extractSchemaPath ( filename )
p = {
' type ' : ' raster ' ,
' name ' : filepath
}
if not schema :
p [ ' location ' ] = ' [The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.] '
2016-02-05 10:58:21 +01:00
params [ param . name ] = p
2016-02-03 16:42:18 +01:00
elif isinstance ( param , ParameterMultipleInput ) :
multiparams = token [ 1 : - 1 ] . split ( ' ; ' )
newparam = [ ]
for mp in multiparams :
schema , filepath = extractSchemaPath ( mp )
newparam . append ( {
' type ' : ' vector ' ,
' name ' : filepath
} )
p = {
' type ' : ' multi ' ,
' params ' : newparam
}
if not schema :
p [ ' location ' ] = ' [The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.] '
2016-02-05 10:58:21 +01:00
params [ param . name ] = p
2016-02-03 16:42:18 +01:00
else :
2016-03-15 10:57:38 +01:00
try :
params [ param . name ] = int ( token )
except ValueError :
try :
params [ param . name ] = float ( token )
except ValueError :
if token [ 0 ] == ' " ' :
token = token [ 1 : ]
if token [ - 1 ] == ' " ' :
token = token [ : - 1 ]
params [ param . name ] = token
2016-02-03 16:42:18 +01:00
definition [ ' params ' ] = params
2016-02-21 13:15:44 +01:00
for i , out in enumerate ( [ out for out in alg . outputs if not out . hidden ] ) :
token = tokens [ i - alg . getVisibleOutputsCount ( ) ]
2016-02-03 16:42:18 +01:00
2013-02-17 23:14:12 +01:00
if isinstance ( out , ( OutputNumber , OutputString ) ) :
2016-02-03 16:42:18 +01:00
results [ out . name ] = unicode ( out )
elif isinstance ( out , OutputRaster ) :
filename = token [ 1 : - 1 ]
2013-02-17 23:14:12 +01:00
dataset = gdal . Open ( filename , GA_ReadOnly )
2016-02-03 16:42:18 +01:00
strhash = hashlib . sha224 ( dataset . ReadAsArray ( 0 ) . data ) . hexdigest ( )
results [ out . name ] = {
' type ' : ' rasterhash ' ,
' hash ' : strhash
}
elif isinstance ( out , OutputVector ) :
filename = token [ 1 : - 1 ]
schema , filepath = extractSchemaPath ( filename )
results [ out . name ] = {
' type ' : ' vector ' ,
' name ' : filepath
}
if not schema :
results [ out . name ] [ ' location ' ] = ' [The expected result data is not in the testdata directory. Please write it to processing/tests/testdata/expected. Prefer gml files.] '
2016-02-21 13:15:44 +01:00
elif isinstance ( out , OutputHTML ) :
filename = token [ 1 : - 1 ]
schema , filepath = extractSchemaPath ( filename )
results [ out . name ] = {
' type ' : ' file ' ,
' name ' : filepath
}
if not schema :
results [ out . name ] [ ' location ' ] = ' [The expected result file is not in the testdata directory. Please redirect the output to processing/tests/testdata/expected.] '
2016-02-03 16:42:18 +01:00
definition [ ' results ' ] = results
dlg = ShowTestDialog ( yaml . dump ( [ definition ] , default_flow_style = False ) )
2013-02-17 23:14:12 +01:00
dlg . exec_ ( )
2013-02-28 22:08:32 +01:00
2015-08-22 14:29:41 +02:00
2014-10-03 21:56:24 +03:00
def tr ( string ) :
return QCoreApplication . translate ( ' TestTools ' , string )
2013-02-17 23:14:12 +01:00
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
class ShowTestDialog ( QDialog ) :
2013-10-01 20:52:22 +03:00
2013-02-28 22:08:32 +01:00
def __init__ ( self , s ) :
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
QDialog . __init__ ( self )
2013-02-17 23:14:12 +01:00
self . setModal ( True )
2013-10-01 20:52:22 +03:00
self . resize ( 600 , 400 )
2014-10-03 21:56:24 +03:00
self . setWindowTitle ( self . tr ( ' Unit test ' ) )
2013-02-17 23:14:12 +01:00
layout = QVBoxLayout ( )
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
self . text = QTextEdit ( )
2016-02-03 16:42:18 +01:00
self . text . setFontFamily ( " monospace " )
2013-02-28 22:08:32 +01:00
self . text . setEnabled ( True )
self . text . setText ( s )
layout . addWidget ( self . text )
2013-02-17 23:14:12 +01:00
self . setLayout ( layout )
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
QMetaObject . connectSlotsByName ( self )