99 lines
4.1 KiB
Python
Raw Normal View History

2013-04-13 11:04:22 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
ScriptTest.py
---------------------
Date : March 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. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'March 2013'
__copyright__ = '(C) 2013, Victor Olaya'
2013-04-13 11:04:22 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2013-04-13 11:04:22 +02:00
__revision__ = '$Format:%H$'
2013-03-27 00:55:38 +01:00
import unittest
import processing
from processing.tools import dataobjects
2013-03-27 00:55:38 +01:00
from processing.tests.TestData import polygons, union
2013-03-27 00:55:38 +01:00
class ScriptTest(unittest.TestCase):
"""Tests that use scripts."""
2013-03-31 21:18:27 +02:00
2013-03-27 00:55:38 +01:00
def test_scriptcreatetilingfromvectorlayer(self):
outputs = processing.runalg('script:createtilingfromvectorlayer',
union(), 10, None)
output = outputs['polygons']
layer = dataobjects.getObjectFromUri(output, True)
fields = layer.pendingFields()
expectednames = ['longitude', 'latitude']
expectedtypes = ['Real', 'Real']
names = [unicode(f.name()) for f in fields]
types = [unicode(f.typeName()) for f in fields]
2013-03-27 00:55:38 +01:00
self.assertEqual(expectednames, names)
self.assertEqual(expectedtypes, types)
features = processing.features(layer)
2013-03-27 00:55:38 +01:00
self.assertEqual(10, len(features))
feature = features.next()
attrs = feature.attributes()
expectedvalues = ['270761.415396242', '4458948.29588823']
values = [unicode(attr) for attr in attrs]
2013-03-27 00:55:38 +01:00
self.assertEqual(expectedvalues, values)
wkt = 'POLYGON((270755.54427424 4458901.23378639,270755.54427424 4458995.35799007,270767.28651824 4458995.35799007,270767.28651824 4458901.23378639,270755.54427424 4458901.23378639))'
self.assertEqual(wkt, unicode(feature.geometry().exportToWkt()))
2013-03-31 21:18:27 +02:00
2013-03-27 00:55:38 +01:00
def test_scripthexgridfromlayerbounds(self):
outputs = processing.runalg('script:hexgridfromlayerbounds',
polygons(), 10, None)
output = outputs['grid']
layer = dataobjects.getObjectFromUri(output, True)
fields = layer.pendingFields()
expectednames = ['longitude', 'latitude']
expectedtypes = ['Real', 'Real']
names = [unicode(f.name()) for f in fields]
types = [unicode(f.typeName()) for f in fields]
2013-03-27 00:55:38 +01:00
self.assertEqual(expectednames, names)
self.assertEqual(expectedtypes, types)
features = processing.features(layer)
2013-03-27 00:55:38 +01:00
self.assertEqual(117, len(features))
feature = features.next()
attrs = feature.attributes()
expectedvalues = ['270765.621834001', '4458907.27146471']
values = [unicode(attr) for attr in attrs]
2013-03-27 00:55:38 +01:00
self.assertEqual(expectedvalues, values)
wkt = 'POLYGON((270771.39533669 4458907.27146471,270768.50858535 4458902.27146471,270762.73508265 4458902.27146471,270759.84833131 4458907.27146471,270762.73508265 4458912.27146471,270768.50858535 4458912.27146471,270771.39533669 4458907.27146471))'
self.assertEqual(wkt, unicode(feature.geometry().exportToWkt()))
2013-03-31 21:18:27 +02:00
def test_scriptascriptthatreturnsanumber(self):
outputs = processing.runalg('script:ascriptthatreturnsanumber')
output = outputs['number']
self.assertTrue(10, output)
2013-04-15 07:16:20 +02:00
2013-03-27 00:55:38 +01:00
def suite():
2013-03-31 21:18:27 +02:00
suite = unittest.makeSuite(ScriptTest, 'test')
2013-03-27 00:55:38 +01:00
return suite
2013-03-27 00:55:38 +01:00
def runtests():
2013-03-31 21:18:27 +02:00
result = unittest.TestResult()
2013-03-27 00:55:38 +01:00
testsuite = suite()
testsuite.run(result)
return result