Skip to content

Commit 16497ee

Browse files
committedMay 9, 2018
fix edit form test with local HTTP server
1 parent 8e20996 commit 16497ee

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed
 

‎tests/src/python/test_qgseditformconfig.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@
1616
import os
1717
import filecmp
1818

19-
from qgis.core import (QgsVectorLayer,
19+
from qgis.core import (QgsApplication,
20+
QgsVectorLayer,
2021
QgsReadWriteContext,
21-
QgsEditFormConfig)
22+
QgsEditFormConfig,
23+
QgsFetchedContent)
2224
from qgis.gui import QgsGui
2325

2426
from qgis.testing import start_app, unittest
2527
from qgis.PyQt.QtXml import QDomDocument, QDomElement
2628
from utilities import unitTestDataPath
29+
import socketserver
30+
import threading
31+
import http.server
2732

28-
TEST_DATA_DIR = unitTestDataPath()
29-
start_app()
33+
app = start_app()
3034

3135

3236
class TestQgsEditFormConfig(unittest.TestCase):
@@ -35,6 +39,17 @@ class TestQgsEditFormConfig(unittest.TestCase):
3539
def setUpClass(cls):
3640
QgsGui.editorWidgetRegistry().initEditors()
3741

42+
# Bring up a simple HTTP server
43+
os.chdir(unitTestDataPath() + '')
44+
handler = http.server.SimpleHTTPRequestHandler
45+
46+
cls.httpd = socketserver.TCPServer(('localhost', 0), handler)
47+
cls.port = cls.httpd.server_address[1]
48+
49+
cls.httpd_thread = threading.Thread(target=cls.httpd.serve_forever)
50+
cls.httpd_thread.setDaemon(True)
51+
cls.httpd_thread.start()
52+
3853
def createLayer(self):
3954
self.layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
4055
"addfeat", "memory")
@@ -66,28 +81,26 @@ def testFormUi(self):
6681
layer = self.createLayer()
6782
config = layer.editFormConfig()
6883

69-
uiLocal = os.path.join(TEST_DATA_DIR, 'layer_attribute_form.ui')
70-
(ok, _) = config.setUiForm(uiLocal)
71-
self.assertTrue(ok)
72-
self.assertEqual(config.layout(), QgsEditFormConfig.UiFileLayout)
73-
self.assertEqual(config.uiForm(QgsEditFormConfig.Original), uiLocal)
74-
self.assertEqual(config.uiForm(QgsEditFormConfig.LocalCopy), uiLocal)
84+
config.setLayout(QgsEditFormConfig.GeneratedLayout)
85+
self.assertEqual(config.layout(), QgsEditFormConfig.GeneratedLayout)
7586

76-
uiUrl = 'https://raw.githubusercontent.com/3nids/QGIS/7ac09b1f9b1921e7f95ab077d97dcf0dfd57be4a/tests/testdata/layer_attribute_form.ui'
77-
(ok, _) = config.setUiForm(uiUrl)
78-
self.assertTrue(ok)
87+
uiLocal = os.path.join(unitTestDataPath(), '/qgis_local_server/layer_attribute_form.ui')
88+
config.setUiForm(uiLocal)
7989
self.assertEqual(config.layout(), QgsEditFormConfig.UiFileLayout)
80-
self.assertEqual(config.uiForm(QgsEditFormConfig.Original), uiUrl)
81-
localCopy = config.uiForm(QgsEditFormConfig.LocalCopy)
82-
self.assertNotEqual(localCopy, uiUrl)
83-
filecmp.cmp(localCopy, uiLocal)
84-
85-
uiBadUrl = 'http://www.qwertzuiopasdfghjklyxcvbnm.qwer/xxx.ui'
86-
(ok, _) = config.setUiForm(uiBadUrl)
87-
self.assertFalse(ok)
90+
91+
config.setLayout(QgsEditFormConfig.GeneratedLayout)
8892
self.assertEqual(config.layout(), QgsEditFormConfig.GeneratedLayout)
89-
self.assertEqual(config.uiForm(QgsEditFormConfig.Original), uiBadUrl)
90-
self.assertEqual(config.uiForm(QgsEditFormConfig.LocalCopy), '')
93+
94+
uiUrl = 'http://localhost:' + str(self.port) + '/qgis_local_server/layer_attribute_form.ui'
95+
config.setUiForm(uiUrl)
96+
self.assertEqual(config.layout(), QgsEditFormConfig.UiFileLayout)
97+
content = QgsApplication.networkContentFetcherRegistry().fetch(uiUrl)
98+
self.assertTrue(content is not None)
99+
while True:
100+
if content.status() in (QgsFetchedContent.Finished, QgsFetchedContent.Failed):
101+
break
102+
app.processEvents()
103+
self.assertEqual(content.status(), QgsFetchedContent.Finished)
91104

92105
def testReadOnly(self):
93106
layer = self.createLayer()

0 commit comments

Comments
 (0)
Please sign in to comment.